Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Correct my trail logic please

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

    Correct my trail logic please

    HI i have a logic for trail, first it sets initial SL value, then once SAR is below price then SAR becomes SL. All is working ok, however, if later SAR becomes above the price, SL becomes iniial price again and I dont want that to happen.
    Thank you.


    Code:
    if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true )
                        {
                            MySarStopL = (Position.AveragePrice - (StopTicksLong * TickSize)) ;
    //                        Print("MySarStopL In trail:" + MySarStopL);
                        }
                        if ((Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true )
                                 && (ParabolicSAR1[0] > High[0]))
    
    
                            {
                                if(MySarStopL <= currentbid ){
    //                                Print("condition1");
                                ExitLongStopMarket(1, true, Position.Quantity, MySarStopL, "SLL", "TML");
                                }
                            }
    
                        {
    
                            if ((Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true)
                                 && (ParabolicSAR1[0] <= Low[0]) )
    
                            {
    //                                Print("condition2");
    //                            Print("MySarStopL" + MySarStopL);
                                    MySarStopL = ParabolicSAR1[0];
                            }    
    
                            if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true  )
                            {
                                 if(MySarStopL <= currentbid ){
    //                                Print("condition3");
    //                                 Print("MySarStopL" + MySarStopL);
                                     ExitLongStopMarket(1, true, Position.Quantity, MySarStopL, "SLL", "TML");
                                 }
                            }
                            if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true  )
                            {
                                 if(MySarStopL >= currentbid ){
    //                                Print("condition4");
    //                                 Print("MySarStopL" + MySarStopL);
                                     ExitLong(1, Position.Quantity,"SLL", "TML");
    //                                 ExitLongStopMarket(1, true, Position.Quantity, MySarStopL, "SLL", "TML");
                                 }
                            }​

    #2
    Hello tkaboris,

    You are setting the MySarStopL to the initial price any time the the position is long and myFreeTradeLongTrail is true, on every bar update.

    if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true )
    {​
    MySarStopL = (Position.AveragePrice - (StopTicksLong * TickSize)) ;
    Should it not be reassigned the value of ParabolicSAR1[0], due to this being greater than the bid, then that first assignment will still be assigned.

    When do you want to use the first assignment? Are you wanting to assign this calculated value when the entry fills and / or the position updates?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you
      I want to assign first assignment open the entry only.

      Comment


        #4
        Hello tkaboris,

        In this case, move that condition and assignment to OnExecutionUpdate() or OnPositionUpdate().


        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Would it look something like this then?

          Code:
          if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true )
          
                              if ((Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true )
                                       && (ParabolicSAR1[0] > High[0]))
          
          
                                  {
                                      if(MySarStopL <= currentbid ){
          //                                Print("condition1");
                                      ExitLongStopMarket(1, true, Position.Quantity, MySarStopL, "SLL", "TML");
                                      }
                                  }
          
                              {
          
                                  if ((Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true)
                                       && (ParabolicSAR1[0] <= Low[0]) )
          
                                  {
          //                                Print("condition2");
          //                            Print("MySarStopL" + MySarStopL);
                                          MySarStopL = ParabolicSAR1[0];
                                  }    
          
                                  if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true  )
                                  {
                                       if(MySarStopL <= currentbid ){
          //                                Print("condition3");
          //                                 Print("MySarStopL" + MySarStopL);
                                           ExitLongStopMarket(1, true, Position.Quantity, MySarStopL, "SLL", "TML");
                                       }
                                  }
                                  if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true  )
                                  {
                                       if(MySarStopL >= currentbid ){
          //                                Print("condition4");
          //                                 Print("MySarStopL" + MySarStopL);
                                           ExitLong(1, Position.Quantity,"SLL", "TML");
          //                                 ExitLongStopMarket(1, true, Position.Quantity, MySarStopL, "SLL", "TML");
                                       }
                                  }​

          Code:
          protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
          {
          
          
          if (execution.Name == "TML")
          {
          MySarStopL = (Position.AveragePrice - (StopTicksLong * TickSize)) ;
          }
          
          
          }​
          Last edited by tkaboris; 08-02-2023, 01:46 PM.

          Comment


            #6
            Hello tkaboris,

            The code would not have to change
            Code:
            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {​
            if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true )
            {
            MySarStopL = (Position.AveragePrice - (StopTicksLong * TickSize)) ;
            // Print("MySarStopL In trail:" + MySarStopL);
            }​
            }
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi i dont understand what i am doing wrong but below clause breaks up my code.
              Everything looks right syntax wise but its breaking my code..

              Code:
              protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity,
                          MarketPosition marketPosition, string orderId, DateTime time)
                      {
              
                          if (Position.MarketPosition == MarketPosition.Long && myFreeTradeLongTrail == true )
                              {
                                  MySarStopL = (Position.AveragePrice - (StopTicksLong * TickSize)) ;                
                              }​
              
                          if (Position.MarketPosition == MarketPosition.Short && myFreeTradeShortTrail == true )
                              {
                                  MySarStopS = (Position.AveragePrice + (StopTicksShort * TickSize)) ;                        
                              }        
              
                      }    ​

              Comment


                #8
                Hello tkaboris,

                May I have a screenshot of the errors?

                Is the OnExecutionUpdate within the scope of the class?
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  HI i am sorry for late response. I was able to do it like this and I get this error comes up but OnExecutionUpdate doesnt get called and i never get print condition outputed.
                  question about the scope. is OnExecutionUpdate has to be after onBarUpdate closing bracket?


                  OnBarupdate(){
                  }
                  OnExecutionUpdate(){
                  }
                  properties go below


                  my protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity,
                  MarketPosition marketPosition, string orderId, DateTime time)
                  {

                  if ((Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short)
                  && (myFreeTradeLongTrail == true || myFreeTradeShortTrail == true))
                  {
                  Print("condition 0: ");
                  MySarStopL = (Position.AveragePrice - (StopTicksLong * TickSize));
                  MySarStopS = (Position.AveragePrice + (StopTicksShort * TickSize));
                  }​

                  }​

                  Click image for larger version

Name:	image.png
Views:	101
Size:	388.9 KB
ID:	1263204
                  Last edited by tkaboris; 08-05-2023, 06:03 AM.

                  Comment


                    #10
                    This is my scope
                    protected override void OnBarUpdate()
                    {​


                    if (BarsInProgress == 0)
                    {​

                    if(!ShowHistoricalTrades && State == State.Historical)
                    return;
                    else
                    {​

                    ......
                    ......
                    ......

                    } // else !historical


                    } // onBarInProgress



                    } // onbarupdate closing​

                    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity,
                    MarketPosition marketPosition, string orderId, DateTime time)
                    {

                    if ((Position.MarketPosition == MarketPosition.Long || Position.MarketPosition == MarketPosition.Short)
                    && (myFreeTradeLongTrail == true || myFreeTradeShortTrail == true))
                    {
                    Print("condition 0: ");
                    MySarStopL = (Position.AveragePrice - (StopTicksLong * TickSize));
                    MySarStopS = (Position.AveragePrice + (StopTicksShort * TickSize));
                    }

                    }​

                    Comment


                      #11
                      Well apparently one of my bools is reading FALSE in OnExecutionUpdate.
                      I set myFreeTradeLongTrail to true when i make an order and never set it to false, only when flat. However when in onExecutionUpdate its FALSE. why does this bool sets to false?

                      Comment


                        #12
                        Hello tkaboris,

                        The placement of the OnExecutionUpdate() override after the closing curly brace of OnBarUpdate() so that it is within the scope of the class appears correct.

                        I'm uncertain of the Statement expected error. May I have you provide the .cs file so I may check there is proper syntax?

                        Regarding a bool value, where have you set the value to true? Place a print one line below each line of code that assigns this variable a value. Print the time of the bar, the name of the method this was called from, and the value of the variable.

                        Chelsea B.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Today, 05:17 AM
                        0 responses
                        44 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        126 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        65 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        42 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        46 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X