Login Page - Create Account

Support Board


Date/Time: Wed, 12 Feb 2025 19:04:36 +0000



AutoTrade Replay Back Test and s_SCTradeOrder

View Count: 535

[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
[2020-11-01 01:37:43]
Sierra Chart Engineering - Posts: 104368
To understand this requires very detailed debugging and analysis which is not within the scope of our support.

However, consider that a filled order cannot necessarily be obtained with sc.GetOrderByOrderID because they become removed from the order list. Otherwise, the order list is up-to-date as of the moment that function is called.

To get the order fills, use this function:
Automated Trading From an Advanced Custom Study: sc.GetOrderFillEntry()
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2020-11-01 01:38:07
[2020-11-01 08:08:17]
gfx2trade - Posts: 48
Hi,
Thks for your prompt answer.
I understand that it may be quite some work to understand where the issue comes from, but to acknowledge that the issue exists is quite straight forward.
And clearly, I do think there is a bug and it seems now obvious as you wrote "because they become removed from the order list."
And that's clearly the issue. When an open order is closed as price reaches SL or TP level, it remains in the sc.GetOrderByOrderID list for a while.
To me it should be removed immediately so that I can obtain the same result with the in-house function as I would with :

s_SCPositionData PositionData;
posQty = PositionData.PositionQuantity;

Hope you can identify and fix the issue.
Best regards,
gfx
[2020-11-02 23:05:23]
Sierra Chart Engineering - Posts: 104368
How is this a bug?:
And clearly, I do think there is a bug and it seems now obvious as you wrote "because they become removed from the order list."

Just never include in any quantity calculation, nonworking orders when obtaining them from the orders list.
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
Date Time Of Last Edit: 2020-11-02 23:06:02

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account