利益/損失の計算

API

利益および損失(P&L)の計算は、ほとんどすべての想定可能なcTrader Open APIアプリケーション/サービスで実装する必要がある重要なユースケースです。

手動でP&Lを計算することは困難なプロセスです。計算にはポジションサイズと購入/売却シンボルの引用通貨と口座のデポジット通貨の変換を考慮する必要があります。

幸いなことに、cTraderバックエンドにP&Lの計算を依頼することができます。そのためには、ProtoOAGetPositionUnrealizedPnLReq メッセージを送信します。このメッセージには、ProtoOAGetPositionUnrealizedPnLRes メッセージが含まれ、その中にProtoOAPositionUnrealizedPnL モデルメッセージが1つのフィールドに含まれます。

注意

ProtoOAGetPositionUnrealizedPnLResには、指定されたctidTraderAccountIdのアカウントのすべての現在のオープンポジションのP&Lに関する情報が、繰り返しのpositionUnrealizedPnLフィールドに含まれています。

レート制限

P&Lをリクエストすることは非履歴的なリクエストであり、1秒あたり最大50件のリクエストしか実行できません。リクエストレート制限を超えるリスクがないように、P&Lを2〜3秒ごとに更新することをお勧めします。

メッセージの送受信について詳しく学びたい場合は、こちらをクリックしてProtobufのチュートリアルをご覧ください。またはこちらをクリックしてJSONのチュートリアルをご覧ください

Protobufを使用する場合は、P&Lレートを取得するためのメッセージのシリアル化/デシリアル化にコンパイラによって生成されたクラスを使用できます。JSONを操作する場合は、自分でそのようなクラスを作成する必要があります。以下は、それらがどのように見えるかの例です。

				
					public abstract class OpenAPIModelMessageBase { }

public class PositionUnrealizedPnL : OpenAPIModelMessageBase
{
    public PositionUnrealizedPnL() { }
    public PositionUnrealizedPnL(int positionId, int grossUnrealizedPnL, int netUnrealizedPnL) 
    {
        this.PositionId = positionId;
        this.GrossUnrealizedPnL = grossUnrealizedPnL;
        this.NetUnrealizedPnL = netUnrealizedPnL;
    }

    public int PositionId { get; set; }
    public int GrossUnrealizedPnL { get; set; }
    public int NetUnrealizedPnL { get; set; }
}

public class GetPositionUnrealizedPnLReq : OpenAPIMessageBase
{
    public GetPositionUnrealizedPnLReq() { }
    public GetPositionUnrealizedPnLReq(int ctidTraderAccountId)
    {
        this.Payload = new GetPositionUnrealizedPnLReqPayload(ctidTraderAccountId);
        this.ClientMsgId = Guid.NewGuid().ToString();
    }

    public override int PayloadType => 2187;
    public GetPositionUnrealizedPnLReqPayload? Payload { get; set; }
}

public class GetPositionUnrealizedPnLReqPayload : OpenAPIMessagePayloadBase
{
    public GetPositionUnrealizedPnLReqPayload() { }
    public GetPositionUnrealizedPnLReqPayload(int ctidTraderAccountId)
    {
        this.CtidTraderAccountId = ctidTraderAccountId;
    }

    public int CtidTraderAccountId { get; set; } = 0;

}

public class GetPositionUnrealizedPnLRes : OpenAPIMessageBase 
{
    public GetPositionUnrealizedPnLRes() { }
    public GetPositionUnrealizedPnLRes(int ctidTraderAccountId, int moneyDigits, List<PositionUnrealizedPnL> positionUnrealizedPnL)
    {
        this.Payload = new GetPositionUnrealizedPnLResPayload(ctidTraderAccountId, moneyDigits, positionUnrealizedPnL);
        this.ClientMsgId = Guid.NewGuid().ToString();
    }

    public override int PayloadType => 2188;
    public GetPositionUnrealizedPnLResPayload? Payload { get; set; }

}

public class GetPositionUnrealizedPnLResPayload : OpenAPIMessagePayloadBase
{
    public GetPositionUnrealizedPnLResPayload() { }
    public GetPositionUnrealizedPnLResPayload(int ctidTraderAccountId, int moneyDigits, List<PositionUnrealizedPnL> positionUnrealizedPnL)
    {
        this.CtidTraderAccountId = ctidTraderAccountId;
        this.MoneyDigits = moneyDigits;
        this.PositionUnrealizedPnL = positionUnrealizedPnL;
    }

    public int CtidTraderAccountId { get; set; } = 0;
    public int MoneyDigits { get; set; } = 0;
    public List<PositionUnrealizedPnL> PositionUnrealizedPnL { get; set; } = new List<PositionUnrealizedPnL>();
}
				
			
				
					class OpenAPIModelMessage:
    def __init__(self):
        pass

class PositionUnrealizedPnL(OpenAPIModelMessage):
    def __init__(self, position_id, gross_unrealized_pnl, net_unrealized_pnl):
        self.position_id = position_id
        self.gross_unrealized_pnl = gross_unrealized_pnl
        self.net_unrealized_pnl = net_unrealized_pnl

class GetPositionUnrealizedPnLReq(OpenAPIMessage):
    def __init__(self, ctid_trader_account_id, client_msg_id = str(uuid.uuid4())):
        self.ctid_trader_account_id = ctid_trader_account_id
        self.payload_type = 2187
        self.client_msg_id = client_msg_id
        self.payload = {"ctidTraderAccountId": self.ctid_trader_account_id}

    def as_json_string(self):
        return json.dumps({"clientMsgId": self.client_msg_id, "payloadType": self.payload_type, "payload": self.payload})

    @staticmethod
    def from_json(json_dct):
        return GetPositionUnrealizedPnLReq(client_id=json_dct['payload']['clientId'], client_secret=json_dct['payload']['clientSecret'], client_msg_id=json_dct['clientMsgId'])

class GetPositionUnrealizedPnLRes(OpenAPIMessage):
    def __init__(self, ctid_trader_account_id, position_unrealized_pnl, money_digits, client_msg_id):
        self.ctid_trader_account_id = ctid_trader_account_id
        self.position_unrealized_pnl = position_unrealized_pnl
        self.money_digits = money_digits
        self.client_msg_id = client_msg_id

    def as_json_string(self):
        return json.dumps({"clientMsgId": self.client_msg_id, "payloadType": self.payload_type, "payload": self.payload})

    @staticmethod
    def from_json(json_dct):
        return GetPositionUnrealizedPnLRes(ctid_trader_account_id=json_dct['payload']['clientSecret'], position_unrealized_pnl=json_dct['payload']['positionUnrealizedPnL'], money_digits=json_dct['payload']['moneyDigits'], client_msg_id=['clientMsgId'])
				
			

このページについて