Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

strategy only follows the logic 99% of the times

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

    strategy only follows the logic 99% of the times

    Hi.
    below is an extract from my strategy that relates to catching the order execution.
    It works 99% of the time. but sometimes I dont get the "targetreached = true;" set and also the Pint command is not printed.

    what can be the issue please?


    Code:
    
            //***********************************************************************************************************                
            protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
                int quantity, int filled, double averageFillPrice,
                Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
            {
    
    
                if (order.Name == "Entry5" )
                {    
                    entryOrder = order;
                    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)    entryOrder = null;
                }
    
                if (order.Name == "MyStop" ||order.Name == "MyStop5"  || order.Name == "extraStop" || order.Name == "TrailingStop")      stopOrder = order;
                if (order.Name == "MyTarget" || order.Name == "MyTarget5"  || order.Name == "Profit target")      targetOrder = order;
    
            } //END of OnOrderUpdate()
            //***********************************************************************************************************                
            ​
    
            //***********************************************************************************************************                
            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {    
                if (entryOrder != null && entryOrder == execution.Order )
                {
                    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                    {    
    
                    //STOP LOSS
                        if (Position.MarketPosition == MarketPosition.Long && execution.Order.Name == "Entry5")
                        {                                
                            stopOrder = ExitLongStopMarket(1, true, Position.Quantity, Math.Min(StopLevel,(GetCurrentBid() - 1*TickSize)), "MyStop5", "Entry5");            
                        }
    
                    //Target
                    //=============    
    
                        if (Position.MarketPosition == MarketPosition.Long && execution.Order.Name == "Entry5")
                        {    
                            targetOrder = ExitLongLimit(0, true, Position.Quantity , TargetLevel, "MyTarget5", "Entry5");
                        }    
    
                    }
                }
    
    
    
                if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order)
                {
                    if (execution.Order.OrderState == OrderState.Filled )
                    {
    [B]if (targetOrder != null && targetOrder == execution.Order)    
                        {
                            targetreached = true;
                            Print("TARGET reached");
                            NumberOfTimesStopTrailed = 0;
                        }[/B]
    
                        ProcessPerDayCounter++;
                    }
    
                }                        
            } //End of OnExecutionUpdate()​
    //************************************************** ************************************************** *******

    #2
    Hello dadarara,

    Thanks for your post.

    If the logic of your script works sometimes and other times it does not work, further debugging would need to be done by reducing code and adding prints to find the exact line of code causing the behavior.

    If a print is not occurring in the Output window then this means the condition to call the Print statement likely did not become true. One line above your condition to call the Print statement, add a prints that print out each value used in the condition to see how the condition is evaluating.

    The Playback connection could be used to try and consistently reproduce the unexpected behavior.

    Playback: https://ninjatrader.com/support/help...connection.htm

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Brandon
      thank you for the reply
      However I am already following this principle all the time in all the code I write. I have a PRINT statement at the start of OnExecutionUpdate and also OnOrderUpdate. I took it out of the extract above not to complicate things.,

      this specific issue shows it seems that the operation of Ninjatrader is not consistent ALL the time.
      the logic I have posted WORKS. the problem is that it SOMETIMES doesnt.
      the key word is SOMETIMES.

      I am looking for your experience to say that maybe the way I catch the execution in OnExecutionUpdate is not correct or can be improved.
      Or maybe you can tell me that the UNMANAGED order entry can be more robust ?

      I am thinking that Ninja code being multithreaded , it can be an issue of sequence of events.
      But then again, if the processing reach the point where the TARGET order is FILLED , then why the IF loop with targetreached = true; is not executed?
      also, keep in mind that the ORDER IS BEING EXECUTED . the trade exits. when target is reached.

      maybe the : if (targetOrder != null && targetOrder == execution.Order) is not the right way to catch it ?

      Last edited by dadarara; 10-02-2023, 05:25 AM.

      Comment


        #4
        Hello dadarara,

        Thanks for your notes.

        I do not see anything in the code you shared in post # 1 standing out as being incorrect.

        (targetOrder != null && targetOrder == execution.Order) would be the correct syntax for checking if the targetOrder object is not null and the targetOrder object is the order being executed.

        You could consider checking if the execution.Order.Name is equal to the signal name of the target order in your condition that checks if the execution.Order.OrderState == OrderState.Filled instead of checking if (targetOrder != null && targetOrder == execution.Order) a second time.

        We have a reference sample here that demonstrates tracking order objects in OnExecutionUpdate() and OnOrderUpdate() that you could view: https://ninjatrader.com/support/help...and_onexec.htm

        Ultimately, if the strategy does not work sometimes but does work other times then you would have to debug your logic further than you already are to understand exactly how the strategy is behaving.

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.
        https://ninjatrader.com/support/foru...121#post791121
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Peppo, 04-13-2025, 12:59 PM
        11 responses
        89 views
        0 likes
        Last Post Peppo
        by Peppo
         
        Started by michelz, 02-18-2025, 08:30 AM
        31 responses
        1,018 views
        0 likes
        Last Post rockmanx00  
        Started by TraderYoda, 05-24-2019, 03:33 AM
        43 responses
        2,119 views
        1 like
        Last Post pwmmmm
        by pwmmmm
         
        Started by jb805, 02-04-2025, 12:34 PM
        5 responses
        168 views
        0 likes
        Last Post Swagpapifrio  
        Started by Newtrader101, Today, 11:27 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Working...
        X