Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

NinjascriptCode-Strategy stoploss-profittarget

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

    NinjascriptCode-Strategy stoploss-profittarget

    Hello,

    I want to ask if it is the correct way to have the setstoploss and setprofittarget in onbarupdate like

    if (Positions[4].MarketPosition != MarketPosition.Short)
    {SetStopLoss("rS1a", CalculationMode.Ticks, st1s1, false);
    SetStopLoss("rS1b", CalculationMode.Ticks, st2s1, false);
    SetProfitTarget("rS1a", CalculationMode.Ticks, t1s1);
    }

    Is this ok or do I need it to have it also in state.configure....?

    And I want to ask please if with this code in script-strategy the stoploss and profittarget are working oco (if one is hit the other is cancelled) when working with NT-brokerage and continuum datafeed. How does it recognize for working oco, by the name "rS1a"? (is this here working same as doing a trade with a chart-trader ATM?)

    Thank you!
    Tony
    Last edited by tonynt; 10-06-2022, 09:28 AM. Reason: translation error

    #2
    Hello tonynt,

    Thanks for your post.

    Yes, you could call SetStopLoss() and SetProfitTarget() in the OnBarUpdate() method as you have described.

    Note that when calling Set methods in OnBarUpdate(), you should always reset the stop loss price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position.

    Yes, SetStopLoss() and SetProfitTarget() would be tied together by OCO. This would be similar to using an Atm Strategy Template on Chart Trader.

    If you give your SetProfitTarget() and SetStopLoss() methods a fromEntrySignal argument of 'rS1a', once an Entry order method with a signalName 'rS1a' is filled, the Profit Target and Stop Loss would be submitted for that entry order and tied together by OCO.

    See this reference sample from the NinjaTrader 8 help guide demonstrating using Set methods in OnBarUpdate(): https://ninjatrader.com/support/help...of_stop_lo.htm

    And, see the help guide pages linked below for more information about SetStopLoss() and SetProfitTarget().
    SetStopLoss(): https://ninjatrader.com/support/help...etstoploss.htm
    SetProfitTarget(): https://ninjatrader.com/support/help...ofittarget.htm

    Let me know if I may assist further.
    Last edited by NinjaTrader_BrandonH; 10-06-2022, 09:52 AM.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hello Brandon,

      thank you for your reply. I know it has to be reset and so I wanted to ask with my code "if (Positions[4].MarketPosition != MarketPosition.Short)"... if this is doing reset with "... != short", no?

      And what I was asking for is - I know the help guide pages - if one need to have the setstoploss (and setprofittarget) in state.configue because its there and thats why I ask for but I have seen samples where it is not in state.configure. What is the purpose having it in state.configure or not?

      Thank you for clearifying this.

      Best regards
      Tony

      Comment


        #4
        Hello tonynt,

        Thanks for your note.

        Yes, this condition could be used to check if you are not in a short market position and reset the Set methods.

        It is suggested to call Set methods from within the strategy OnStateChange() method if your stop loss and profit target price/offset is static.

        You may call Set methods from within the strategy OnBarUpdate() method should you wish to dynamically change the stop loss price while in an open position.

        This could be seen in the Tips section of the SetStopLoss() and SetProfitTarget() help guide pages linked in post # 2.

        Let me know if I may assist further.​​
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Hello Brandon,

          thank you for your reply. I dont want to bother but want to understand 100% for not asking again one day...

          ok, when its static then in onstatechange it is suggested, but why? There must be a reason when its suggested. Why not also in onbarupdate like with dynamically stoploss?

          And when only in onbarupdate, then it sounds like the suggested stop is missing(?) This sounds like onstatechange is the more correct or safe position (english is not my language, sorry for a misunderstanding)

          Thank you!
          Tony

          Comment


            #6
            Hello tonynt,

            Thanks for your note.

            We suggest using Set methods in OnStateChange() when the price is static because when Set methods are called in OnStateChange(), they have the same value each time they are submitted. The price cannot be changed dynamically when calling Set methods this way.

            When you call Set methods in OnBarUpdate(), the price is no longer static since the method is updated each time you call it. If you want to dynamically change the price of Set methods, you would have to call them in OnBarUpdate().

            Please let me know if I may assist further.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            71 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            143 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            76 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            47 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            51 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X