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:

  • tradeTransaction,
  • tradeTransactionStatus,
  • 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.
SymbolResponse symbolResponse = APICommandFactory.ExecuteSymbolCommand(connector, "EURUSD");

Step 2. Open the trade

  1. Create an TradeTransInfoRecord with trade data, using data from symbolResponse
  2. Execute the TradeTransaction command
double price = symbolResponse.Symbol.Ask.GetValueOrDefault();
double sl = 0.0;
double tp = 0.0;
string symbol = symbolResponse.Symbol.Symbol;
double volume = 0.1;
long order = 0;
string customComment = "my comment";
long expiration = 0;
TradeTransInfoRecord ttOpenInfoRecord = new TradeTransInfoRecord(
	TRADE_OPERATION_CODE.BUY, 
	TRADE_TRANSACTION_TYPE.ORDER_OPEN, 
	price, sl, tp, symbol, volume, order, customComment, expiration);

TradeTransactionResponse tradeTransactionResponse = APICommandFactory.ExecuteTradeTransactionCommand(connector, ttOpenInfoRecord);

Here you can see the JSON from tradeTransactionResponse

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

Step 3. Check transaction status

  1. Execute TradeTransactionStatus with the data from tradeTransactionResponse
TradeTransactionStatusResponse ttsResponse = APICommandFactory.ExecuteTradeTransactionStatusCommand(connector, tradeTransactionResponse.Order);

Here you can see the JSON from TradeTransactionStatusResponse

{
    "returnData": {
        "ask": 1.40000,
        "bid": 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:
TradesResponse tradesResponse = APICommandFactory.ExecuteTradesCommand(connector, true);
TradeRecord tradeRecordToClose = null;
foreach(TradeRecord tradeRecord in tradesResponse.TradeRecords) 
{
	if (tradeRecord.Order2 == ttsResponse.Order)
	{
		tradeRecordToClose = tradeRecord;
	}
}

Here you can see the JSON from TradesResponse

{
    "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 TradeTransactionStatus Command is equal to the order2 field in the response of GetTrades command. This because the TradeTransactionStatus 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. Create an TradeTransInfoRecord with trade data, using data from tradeRecordToClose record from the previous step:
    • tradeRecordToClose.Close_price
    • tradeRecordToClose.Symbol
    • tradeRecordToClose.Order
  2. Execute the TradeTransaction command using created TradeTransInfoRecord
price = tradeRecordToClose.Close_price.GetValueOrDefault();
sl = 0.0;
tp = 0.0;
symbol = tradeRecordToClose.Symbol;
volume = 0.1;
order = tradeRecordToClose.Order.GetValueOrDefault();
customComment = "my comment 2";
expiration = 0;
TradeTransInfoRecord ttCloseInfoRecord = new TradeTransInfoRecord(
	TRADE_OPERATION_CODE.BUY, 
	TRADE_TRANSACTION_TYPE.ORDER_CLOSE, 
	price, sl, tp, symbol, volume, order, customComment, expiration);
TradeTransactionResponse closeTradeTransactionResponse = APICommandFactory.ExecuteTradeTransactionCommand(connector, ttCloseInfoRecord, true);

Here you can see the JSON from TradeTransactionResponse

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

Step 6. Check status of the closed transaction

  1. Execute TradeTransactionStatus with the data from closeTradeTransactionResponse from the previous step.
TradeTransactionStatusResponse ttsCloseResponse = APICommandFactory.ExecuteTradeTransactionStatusCommand(connector, closeTradeTransactionResponse.Order);

Here you can see the JSON from TradeTransactionStatusResponse

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

In the TradeTransactionStatusResponse 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.