Login Page - Create Account

Support Board


Date/Time: Wed, 12 Feb 2025 18:53:41 +0000



Post From: AutoTrade Replay Back Test and s_SCTradeOrder

[2020-10-31 18:07:27]
gfx2trade - Posts: 48
Hi,
I am using an in-house function ( here below ) to calculate the order quantity of pending orders and filled orders.
I use the integer qtyType to specify which quantity I want to calculate.

int iOrderQty(SCStudyInterfaceRef sc, int qtyType){
  
  s_SCTradeOrder qtyOrderList;
  s_SCTradeOrder qtyOrderListTradeData;
  
  int pQy;
  
  vMessageToLog(sc, 1,0," >>> Calculation of order quantity ", 0);
  ServiceLogStr.Format(" >>> pQy(1) : %i ", pQy);
  vMessageToLog(sc, 0,0,ServiceLogStr, 0);
          
  oIndex = 0;
  oResult = 0;
        
    while( sc.GetOrderByIndex (oIndex, qtyOrderList) != SCTRADING_ORDER_ERROR)  {
    
      oIndex++;    
      
      int oResult = sc.GetOrderByOrderID(qtyOrderList.InternalOrderID, qtyOrderListTradeData);
      
      if (( qtyType == 2 && qtyOrderListTradeData.OrderStatusCode == 5) || ( qtyType == 1 && qtyOrderListTradeData.OrderStatusCode == 8)) { // OPEN ||FILLED
      
        ServiceLogStr.Format(" >>> Order Id is : %i and status code is %i ", qtyOrderListTradeData.InternalOrderID, qtyOrderListTradeData.OrderStatusCode);
        vMessageToLog(sc, 1,0,ServiceLogStr, 0);
        ServiceLogStr.Format(" >>> Order Price is : %f ", qtyOrderListTradeData.Price1);
        vMessageToLog(sc, 0,0,ServiceLogStr, 0);
            
        if (qtyOrderListTradeData.IsAttachedOrder()) {
          ServiceLogStr.Format(" >>> Is an Attached Order. Quantity not relevant ");
          vMessageToLog(sc, 0,0,ServiceLogStr, 0);
          }
        else {
            vMessageToLog(sc, 0,0," >>> Is a Parent Order. ", 0);
            pQy = pQy + qtyOrderListTradeData.OrderQuantity;
            ServiceLogStr.Format(" >>> Quantity is now %i ", pQy);
            vMessageToLog(sc, 0,0,ServiceLogStr, 0);    
          }
        }
    } // while( sc.GetOrderByIndex (oIndex, qtyOrderList) != SCTRADING_ORDER_ERROR)  {
  
  ServiceLogStr.Format(" >>> pQy(2) : %i ", pQy);
  vMessageToLog(sc, 0,0,ServiceLogStr, 0);
  
  return pQy;
              
}

When running a AutoTrade Replay Back Test at normal speed (1) I get the right numbers with almost no delay.
If I speed up the Replay (which is key to me when back testing) it seems that the order book is not updated at the speed of the replay and therefore returned quantity are incorrect.

Screen 1 : quantity is correct when 3 contracts are placed.
Screen 2 : quantity is correct when 3 contracts are filled.
Screen 3 : 1 contract has reached take profit. Quantity is incorrect still showing 3 contracts.
Screen 4 : After two candles more, quantity is correct showing 2 contracts filled. ( I can't attach to post due to file limit, but trust me after a while the number is correct)

Where does the delay come from ? I can't back test if the s_SCTradeOrder does not provide the exact information after each order event.

Thanks
imagescreen1.JPG / V - Attached On 2020-10-31 18:06:26 UTC - Size: 85.74 KB - 209 views
imagescreen2.JPG / V - Attached On 2020-10-31 18:06:31 UTC - Size: 87.44 KB - 214 views
imagescreen3.JPG / V - Attached On 2020-10-31 18:06:35 UTC - Size: 85.8 KB - 212 views