Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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==&& Position.Quantity == 0  
                      
      ) {
                                      
                  
      LongEntryOrder EnterLongLimit(0,true1Instrument.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(0trueexecution.Order.Filledexecution.Order.AvgFillPrice 4"MyStop""LongEntry");
                          
                          
      LongtargetOrder ExitLongLimit(0trueexecution.Order.Filledexecution.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 burtoninlondon, Today, 12:38 AM
        0 responses
        9 views
        0 likes
        Last Post burtoninlondon  
        Started by AaronKoRn, Yesterday, 09:49 PM
        0 responses
        14 views
        0 likes
        Last Post AaronKoRn  
        Started by carnitron, Yesterday, 08:42 PM
        0 responses
        11 views
        0 likes
        Last Post carnitron  
        Started by strategist007, Yesterday, 07:51 PM
        0 responses
        13 views
        0 likes
        Last Post strategist007  
        Started by StockTrader88, 03-06-2021, 08:58 AM
        44 responses
        3,983 views
        3 likes
        Last Post jhudas88  
        Working...
        X