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

Passing variable to stop loss / profit target

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

    Passing variable to stop loss / profit target

    Probably newbie question, but I can't figure out what's wrong. Ninja 8 here, so what I want to do is to set stop loss at 2*ATR and set profit target at 5*ATR so here is the part of my code:

    protected override void OnBarUpdate()
    {

    if (Close[0]>Close[1];
    {
    EnterLong(1);
    TradeATR=ATR(14)[1];
    }
    }


    This way, the correct value is given to TradeATR, I confirmed it. Then, here's the second part of my code:

    else if (State == State.Configure)
    {

    SetProfitTarget(CalculationMode.Currency, TradeATR*5);
    SetStopLoss(CalculationMode.Currency, TradeATR*2);
    }

    However, this for some reason doesn't pick up the current TradeATR value, it just assumes that TradeATR is 0. It works a number (say 100), but not a variable TradeATR.

    How to properly do what I want and how to pass a variable from OnBarUpdate() to OnStateChange()?

    Thank you in advance!

    #2
    Hello hestati,

    Thanks for your post.

    In your code you are setting TradeATR a value in OnBarUpdate() which starts firing after State.Configure is reached. The Set methods do not yet have a valid price level when they are called.

    You may call SetProfitTarget() and SetStopLoss() from the OnBarUpdate() methods so the profit and target can use the value of a variable that changes throughout the life of your script, and the Set methods can also be used to update the target and stop once you are in position.

    Set methods prep NinjaTrader to submit protective orders when an entry order comes back as filled. Targets and stops should be updated when the strategy is flat before entering a new position to ensure the target and stop are submitted at the intended level. Once in position, these methods can then be called to update the target/stop to a new position.

    I'll provide links to our publicly available documentation on these methods for tips and notes and I will include a link to an example for demonstration.

    SetProfitTarget() - https://ninjatrader.com/support/help...ofittarget.htm

    SetStopLoss() - https://ninjatrader.com/support/help...etstoploss.htm

    SamplePriceModification - https://ninjatrader.com/support/help...of_stop_lo.htm

    Please let us know if you have any additional questions.
    JimNinjaTrader Customer Service

    Comment


      #3
      I see, thanks!

      I believe the example you sent sets a trailing stop, which updates on every bar update, which is great, since I did not know how to do that.

      What if I want to set a stop which does not update on every new bar? Example, at entry price is 100 and ATR is 1, I want to set a stop loss at 100-2*ATR (ATR at entry, which is 1 in this case) for the whole duration of the trade?

      Thank you!

      Comment


        #4
        Hello hestati,

        If you do not want the stop loss to update with each bar iteration, then you should only set the stop loss when the strategy position is flat and before you submit your entry method.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Mongo, Yesterday, 11:05 AM
        6 responses
        27 views
        0 likes
        Last Post Mongo
        by Mongo
         
        Started by ct, 05-07-2023, 12:31 PM
        7 responses
        206 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by wzgy0920, Yesterday, 09:53 PM
        1 response
        13 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by Rapine Heihei, Yesterday, 07:51 PM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by kaywai, Today, 06:26 AM
        1 response
        6 views
        0 likes
        Last Post kaywai
        by kaywai
         
        Working...
        X