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 Hwop38, 05-04-2026, 07:02 PM
        0 responses
        152 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Started by CaptainJack, 04-24-2026, 11:07 PM
        0 responses
        305 views
        0 likes
        Last Post CaptainJack  
        Started by Mindset, 04-21-2026, 06:46 AM
        0 responses
        244 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by M4ndoo, 04-20-2026, 05:21 PM
        0 responses
        345 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Started by M4ndoo, 04-19-2026, 05:54 PM
        0 responses
        176 views
        0 likes
        Last Post M4ndoo
        by M4ndoo
         
        Working...
        X