Facebook

OPENING AND CLOSING TRADE TUTORIAL

In this tutorial, you will learn the details of the transaction process. How to open, close the trade and get transaction status of this transactions. The main purpose of this tutorial is to explain the meaning of all order numbers that are connected to this process. For more info, please refer to the documentation: developers.xstore.pro/api/documentation

Commands used in this tutorial:

  • addOrder,
  • getOrderStatus,
  • getSymbol,
  • getTrades.

Before you start...

  • To complete this tutorial, you need C# or Java wrappers (available here: developers.xstore.pro/api/wrappers) or be able to send all commands listed above by yourself.
  • You also need a test trading account (you can get it here: developers.xstore.pro/panel).
  • We assume that you have at least basic knowledge of xAPI and you are able to establish a connection and login to the server.

Step 1. Get symbol data

  1. Pick a symbol you wish to trade on, in our case it will be 'EURUSD'.
  2. Execute the GetSymbol command to get the price of the symbol.
GetSymbolResponse symbolResponse = apiClient.GetSymbol("EURUSD");

Step 2. Open the trade

  1. Execute the AddOrder command with appropriate parameters.
double price = symbolResponse.Symbol.Ask;
double sl = 0.0;
double tp = 0.0;
string symbol = symbolResponse.Symbol.Symbol;
double volume = 0.1;
Side side = Side.BUY;
TradeType tradeType = TradeType.MARKET;
string customComment = "my comment";
long expiration = 0;
AddOrderResponse addOrderResponse = apiClient.AddOrder(customComment, expiration, price, side, sl, symbol, tp, tradeType, volume);

Here you can see the JSON from addOrderResponse

{
    "returnData": {
        "order": 111511
    },
    "status": true
}

Step 3. Check transaction status

  1. Execute GetOrderStatus command with the data from addOrderResponse
GetOrderStatusResponse getOrderStatusResponse = apiClient.GetOrderStatus(addOrderResponse.Order);

Here you can see the JSON from GetOrderStatusResponse

{
    "returnData": {
        "price": 1.40000,
        "customComment": "my comment",
		"message": null,
        "order": 111511,
        "requestStatus": 3
    },
    "status": true
}

Step 4. Get opened trade data

  1. Execute GetTrades command
  2. Find the order opened above by iterating through all opened trades, by comparing the order2  from tradeRecord and order  from TradeTransactionResponse:
GetTradesResponse tradesResponse = apiClient.GetTrades();
TradeRecord tradeRecordToClose = null;
foreach (TradeRecord tradeRecord in tradesResponse.TradeRecords)
{
    if (tradeRecord.Order2 == getOrderStatusResponse.OrderStatus.Order)
    {
        tradeRecordToClose = tradeRecord;
    }
}

Here you can see the JSON from GetTradesResponse

{
    "returnData": [
        {
            ...
            "open_timeString": "Thu Jun 27 08:24:39 EDT 2013",
            "order": 111512,
            "order2": 111511,
            "position": 111512,
            "profit": -968.300000,
            ...
        }
    ],
    "status": true
}

As we can see, the order  number from GetOrderStatus Command is equal to the order2 field in the response of GetTrades command. This because the GetOrderStatus returns the number of the order that opened the position, and the order2  in the response of GetTrades command also points to this data. What is more, for opened trades, the order  and position  numbers are the same and they refer to the number of the position.

Now we have all data needed to close the transaction in the tradeRecordToClose object of the TradeRecord class.

Step 5. Close the transaction

  1. Execute the ClosePosition command using data from tradeRecordToClose record from the previous step:
    • tradeRecordToClose.Close_price
    • tradeRecordToClose.Position
    • tradeRecordToClose.Volume
ClosePositionResponse closePositionResponse = apiClient.ClosePosition(tradeRecordToClose.Position, tradeRecordToClose.ClosePrice, tradeRecordToClose.Volume);

Here you can see the JSON from TradeTransactionResponse

{
    "returnData": {
        "order": 111514
    },
    "status": true
}

Step 6. Check status of the closed transaction

  1. Execute GetOrderStatus with the data from closePositionResponse from the previous step.
GetOrderStatusResponse getOrderStatusCloseResponse = apiClient.GetOrderStatus(closePositionResponse.Order);

Here you can see the JSON from GetOrderStatusResponse

{
    "returnData": {
        "ask": 1.30317,
        "bid": 1.30317,
        "customComment": "my comment 2",
		"message": null,
        "order": 111514,
        "requestStatus": 3
    },
    "status": true
}

In the GetOrderStatusResponse we can see new order number, which is the number of the order that closed the position.

Summary

Opening and closing the transaction is not so complicated as it may seem. Now you know what all these numbers mean. There are three key elements you should be aware of:

  • number of the order which opens the position,
  • number of the order which closes the position,
  • number of the position, which is the link between the two numbers above.