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 Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        560 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        325 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        547 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        547 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X