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

SetProfitTarget Does Not Execute

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

    SetProfitTarget Does Not Execute

    I am in the midst of changing up the code of my automated strategy and I can't get the SetProfitTarget to work. In the earlier working version, I had the SetStopLoss included under the condition State == State.DataLoaded but now I'm adding SetStopLoss and SetProfitTarget under trade condition match... right after EnterLong or EnterShort. What am I doing wrong? When conditions match the SetStopLoss works correctly but the profit target does not get set. SL0 and TargetTick1 are provided by the user during strategy activation.
    Please advise.
    Thanks.

    EnterLong(Qty, "Long");
    SetProfitTarget("Long", CalculationMode.Ticks, TargetTick1);
    SetStopLoss("Long", CalculationMode.Ticks, SL0, false);​

    #2
    Any suggestions?

    Comment


      #3
      Hello givemefood,

      I have moved this thread to the Strategy Development section of the forums as this is a question about NinjaScript programming.

      Note, always call Set methods before calling the entry order method, not after.

      That said, what is the value of TargetTick1?

      How do you know the limit order for the target is not being submitted?
      Are you reviewing the output from TraceOrders and seeing the stop loss trace message but not the profit target?
      Are you reviewing output from printing the order object in OnOrderUpdate()?

      Please include the debugging output saved to a text file so that I may assist with analyzing the behavior.

      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        I re-ordered per your suggestion and moved the set methods to earlier and that worked!
        Thank you

        Comment


          #5
          ChelseaB, I'm wondering if I'm having a similar issue with the order of operations but let me explain a few additional details that may also contribute to my issue.
          • My strategy works during Playback mode and partially works during live sessions.
          • In Playback, my entry order gets executed when I click a custom button and the set stop and take profit orders post on the chart.
          • In Live mode, my entry order get executed but the stop and take profit orders don't get applied.
          • My entries are located in OnBarUpdate. I send the entry name to a method/function in order to set the stop and take profit. The method is located in the OnStateChange section.
          • Do I have a sequence issue? I thought I would need to enter the trade first then set the stop and tp based on my entry price.
          • Do I have some other structural issue using a method outside OnBarUpdate?


          if (tradecount == 0)
          {
          EnterLong(MaxContracts, "MyEntryLongInstant");
          StopandTargetInstant("MyEntryLongInstant");

          }

          if (tradecount == 1)
          {
          EnterLong(MaxContracts, "MyEntryLongInstant2");
          StopandTargetInstant("MyEntryLongInstant2");

          }​

          void StopandTargetInstant(string tradename)
          {
          if (Position.MarketPosition == MarketPosition.Long)

          {
          ExitLongStopMarket(0, true, Position.Quantity, Position.AveragePrice - Stop_ticks * TickSize, tradename, tradename);
          ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + Target_ticks * TickSize, tradename, tradename);

          }

          if (Position.MarketPosition == MarketPosition.Short)
          {
          ExitShortStopMarket(0, true, Position.Quantity, Close[0] + Stop_ticks * TickSize, tradename, tradename);
          ExitShortLimit(0, true, Position.Quantity, Close[0] - Target_ticks * TickSize, tradename, tradename);

          }
          return;
          }​
          Last edited by tak145; 10-15-2023, 05:58 AM.

          Comment


            #6
            Hello tak145,

            With Exit methods, submit these from OnExecutionUpdate() when the entry order fills.


            The ProfitChaseStopTrailExitOrdersExample example provides an example.


            Trying to submit an exit order when the order has not had time to fill and the position has not yet changed will result in the order being ignored.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by realblubb, Today, 09:28 AM
            0 responses
            1 view
            0 likes
            Last Post realblubb  
            Started by AaronKoRn, Yesterday, 09:49 PM
            1 response
            18 views
            0 likes
            Last Post Rikazkhan007  
            Started by ageeholdings, Today, 07:43 AM
            0 responses
            12 views
            0 likes
            Last Post ageeholdings  
            Started by pibrew, Today, 06:37 AM
            0 responses
            4 views
            0 likes
            Last Post pibrew
            by pibrew
             
            Started by rbeckmann05, Yesterday, 06:48 PM
            1 response
            14 views
            0 likes
            Last Post bltdavid  
            Working...
            X