Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why Some Stop-Losses (as ExitLongStopMarket() ) are not working with OnBarClose ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Why Some Stop-Losses (as ExitLongStopMarket() ) are not working with OnBarClose ?

    Dear all,

    Is there any reason why some Stop-Losses {in a form of ExitLongStopMarket() / ExitShortStopMarket() } are not getting submitted, when using the Calculation.OnBarClose?

    Example of a Stop-Loss for a BUY order:
    Code:
    private int Quant = 5;
    private Order [B]BUY_entryOrder [/B]= null;
    private int [B]BUY_sumFilled [/B]= 0;
    
    private Order [B]BUY_stopOrder [/B]= null;
    
    protected override void [B]OnStateChange[/B]()
    {[INDENT]if (State == State.SetDefaults)
    {[/INDENT][INDENT=2]...
    Calculate = Calculate.[B]OnBarClose[/B];
    ...[/INDENT][INDENT]}
    else if (State == State.Configure)
    {[/INDENT][INDENT=2]AddDataSeries(i.FullName,BarsPeriodType.Minute, 15);  [/INDENT][INDENT]}
    else if (State == State.Realtime)
    {[/INDENT][INDENT=2]// one time only, as we transition from historical
    // convert any old historical order object references
    // to the new live order submitted to the real-time account
    
    if (BUY_entryOrder != null)
    BUY_entryOrder = GetRealtimeOrder(BUY_entryOrder);
    if (BUY_stopOrder != null)
    BUY_stopOrder = GetRealtimeOrder(BUY_stopOrder);[/INDENT][INDENT]}[/INDENT]
     
    
    
    
    
    }
    
    protected override void [B]OnBarUpdate[/B]()
    {[INDENT]if (CurrentBars[1] > 50) return;
    
    if(BarsInProgress == 1){[/INDENT][INDENT=2]
    if( [I]LONG Conditions[/I] ) {[/INDENT][INDENT=3][B]EnterLong([/B]1, Quant, @"[B]Buy[/B]"[B]);[/B]
    BUY_SL_Price = [I]SomePriceValue;[/I][/INDENT][INDENT=2]}
    
    if( [I]TP Conditions[/I] ){[/INDENT][INDENT=3][B]ExitLong([/B]1, Quant, "[B]TP for Buy[/B]", "[B]Buy[/B]"[B]);[/B][/INDENT][INDENT=2]}[/INDENT][INDENT]}[/INDENT]
     
     }
    
    protected override void [B]OnOrderUpdate[/B](){[INDENT]
    [B]//Detecting the Buy order.[/B]
    if(order.Name == "[B]Buy[/B]" && orderState==OrderState.Filled){[/INDENT][INDENT=2]BUY_entryOrder = order;
    
    [B]// Reset the entryOrder object to null if order was cancelled without any fill[/B]
    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    {[/INDENT][INDENT=3]BUY_entryOrder = null;
    BUY_sumFilled = 0;[/INDENT][INDENT=2]}[/INDENT][INDENT]}[/INDENT]
     
     }
    
    protected override void [B]OnExecutionUpdate[/B](){[INDENT]if (BUY_entryOrder != null && BUY_entryOrder == execution.Order)
    {[/INDENT][INDENT=2]if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled
    && execution.Order.Filled > 0))
    {[/INDENT][INDENT=3][B]// We sum the quantities of each execution making up the entry order[/B]
    BUY_sumFilled += execution.Quantity;
    
    [B]// Submit exit orders for partial fills[/B]
    if (execution.Order.OrderState == OrderState.PartFilled)
    {[/INDENT][INDENT=4]BUY_stopOrder = [B]ExitLongStopMarket[/B](1, true, execution.Order.Filled, BUY_SL_Price, "[B]SL_BUY[/B]", "[B]Buy[/B]");[/INDENT][INDENT=3]}
    [/INDENT][INDENT=3][B]// Update our exit order quantities once orderstate turns to filled and we have seen execution quantities match order quantities[/B]
    else if (execution.Order.OrderState == OrderState.Filled && BUY_sumFilled == execution.Order.Filled)
    {[/INDENT][INDENT=4][B]// Stop-Loss order for OrderState.Filled[/B]
    BUY_stopOrder = [B]ExitLongStopMarket[/B](1, true, execution.Order.Filled, BUY_SL_Price, "[B]SL_BUY[/B]", "[B]Buy[/B]");[/INDENT][INDENT=3]}
    [/INDENT][INDENT=3][B]// Resets the entryOrder object and the sumFilled counter to null / 0 after the order has been filled[/B]
    if (execution.Order.OrderState != OrderState.PartFilled && BUY_sumFilled == execution.Order.Filled)
    {[/INDENT][INDENT=4]BUY_entryOrder = null;
    BUY_sumFilled = 0;[/INDENT][INDENT=3]}[/INDENT][INDENT=2]}[/INDENT][INDENT]}
    
    [B]// Reset our stop order and target orders' Order objects after our position is closed. (1st Entry)[/B]
    if (BUY_stopOrder != null && BUY_stopOrder == execution.Order)
    {[/INDENT][INDENT=2]if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    {[/INDENT][INDENT=3]BUY_stopOrder = null;[/INDENT][INDENT=2]}[/INDENT][INDENT]}[/INDENT]
     
     }
    Regards,
    Last edited by PrQuantumH; 02-18-2022, 04:58 AM.

    #2
    Probably because the StopLoss order sometimes do not detect if the BUY order has been executed in the first place (when dealing with OnBarClose) ?
    If this is the case, what would be the solution, or I should use OnEachTick ?

    Even with OnEachTick, few times (very less than OnBarClose), the StopLoss order don't get executed.
    Last edited by PrQuantumH; 02-18-2022, 05:09 AM.

    Comment


      #3
      Hello PrQuantumH,

      From the code I can't see anything specifically out of place and the given description makes it sound like the logic to submit the exit orders is not happening as expected. You are using OnExecutionUpdate which is called for fills. If you see that the logic to call the exit is not happening that may be due to your conditions. As a test you could add a print at the top of OnExecutionUpdate to make sure the entry in question has its execution printed.

      Code:
      protected override void OnExecutionUpdate(...){
      Print(execution.ToString());
      If you see the entry execution but no target that would indicate the condition did not meet the critera so it was not submitted. You would need to use prints in that situation to see which part of the conditions are failing.




      Comment


        #4
        Hello Jesse,

        Thanks for your reply.

        Is it possible that when there is No Enough Contracts at the SLPrice. (When the Long-Order( Contracts = 100)) ==> Then, the ExitLongStopMarket( Contracts = 100, SLPrice) will be simply ignored ?

        Because, when I tried Small Long-Order( Contracts = 1) with a Small Distance from the Entry Price ==> Then, The ExitLongStopMarket( Contracts = 1, SLPrice) is working fine as a SL.

        Regards.

        Comment


          #5
          Hello PrQuantumH,

          If you are seeing a warning that specifically notes the order was ignored then it would have been ignored. If you are not seeing a warning about the order being ignored then I still would be unable to say why the script you are using may be missing some orders. The same comment from post 3 would still apply here if you have not checked by using a print if the entry execution was observed to call the target logic.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          71 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          143 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          76 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          47 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          51 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X