Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Limit order with SetProfitTarget

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

    Limit order with SetProfitTarget

    I use the following code:

    EnterShortLimit(DefaultQuantity, entry_priceShort, "");

    SetProfitTarget(CalculationMode.Ticks, a);

    When price hits the entry_priceShort, it's short in the market. I use 60min bars.

    If the bar that triggers the short also has prices of the profittarget (in the bar), the strategy automatic calculates this as a profitable trade. However, if you look at a shorter timeframe you see that price first hits the profittarget and then the entry_short. So actually there is no profit, the trade is still on...

    How can I avoid this? How can I set for example profit target one bar later?

    #2
    Hello no111,

    Thank you for your post.

    For historical backtesting you would need to use an Exit() method instead of the Set() method. Then either wait a set number of bars after entry (BarsSinceEntry: http://www.ninjatrader.com/support/h...sinceentry.htm) or submit the order to another time frame: http://www.ninjatrader.com/support/f...ead.php?t=6652

    Please let me know if I may be of further assistance.

    Comment


      #3
      Ok thanks.

      Would this be a correct solution?
      Barindex 0 = 1min
      Barindex 1 = 120min

      PHP Code:
      
         protected override void OnBarUpdate()
              {
      
      if (BarsInProgress == 1){
      entry_price = EMA(BarsArray[1],20)[1];    
      
      
        if ( Closes[1][0] > Closes[1][1] 
                      )
                  { 
                      x=1;
                      Print("Long setup");     
                  }
        if ( Closes[1][0] < Closes[1][1] 
                      )
                  { 
                      x=0;
                      Print("No setup");     
                  }
      
      }
      }
      
      if (BarsInProgress == 0){
              if ( x==1 && Position.Quantity == 0  
                      ) {
                                      
                  LongEntryOrder = EnterLongLimit(0,true, 1, Instrument.MasterInstrument.Round2TickSize(entry_price), "LongEntry");
                  
                          
                              
                  }
      
      }
      
            protected override void OnExecution(IExecution execution)
              {
                  
              
              
                  if (LongEntryOrder != null && LongEntryOrder == execution.Order && BarsInProgress == 0)
                  {
                      if (  execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                      {
                          LongstopOrder     = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 4, "MyStop", "LongEntry");
                          
                          LongtargetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 4, "MyTarget", "LongEntry");
                          x=0;
      
                          if (execution.Order.OrderState != OrderState.PartFilled)
                          {
                              LongEntryOrder     = null;
                          }
                      }
                  }
      
              if ((LongstopOrder != null && LongstopOrder == execution.Order) || (LongtargetOrder != null && LongtargetOrder == execution.Order))
                  {
                      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
                      {
                          LongstopOrder = null;
                          LongtargetOrder = null;
                      }
                  }
      
      } 
      

      Comment


        #4
        Hello no111,

        Thank you for your response.

        That would be correct for entering on the secondary series and exiting on the primary series.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by CaptainJack, 05-29-2026, 05:09 AM
        0 responses
        475 views
        0 likes
        Last Post CaptainJack  
        Started by CaptainJack, 05-29-2026, 12:02 AM
        0 responses
        315 views
        0 likes
        Last Post CaptainJack  
        Started by charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        253 views
        1 like
        Last Post charlesugo_1  
        Started by DannyP96, 05-18-2026, 02:38 PM
        1 response
        340 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        305 views
        0 likes
        Last Post CarlTrading  
        Working...
        X