Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting trail stops & take profits in ninjascript question

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

    Setting trail stops & take profits in ninjascript question

    Hey, I need some help with my coding!

    I have a question about writing my code in ninjascript, I have unlocked the code and am trying to set take profits and stop losses,

    What I want to do is to set a take profit of [high[0] - low[0] ]+ 6 ticks and a stop loss to [high[0] - low[0] ]+ 2 ticks.

    Here is what I have:



    //Take Profits & Stops

    SetStopLoss( CalculationMode.Price, (Low[0] -.02));

    SetProfitTarget(CalculationMode.Price, (High[0] +.06 + Math.Abs(High[0] - Low[0]) ) );​



    This was for CL so I used .02 and .06 since they were in "ticks" but I know this is wrong, since it's giving me very bad backtesting results and on the chart it looks odd, as you can see from the photo the take profit triggered below the entry price, which is completely weird.


    I'd appreciate some help on how to do this!


    Additionally, is there a way to set an ATM in ninjascript where an initial stop loss is set, and then a trail stop is set once a profit trigger has been hit? I've been looking for this in the methods but all I can find is "setstoploss" and "settrailstop" and I found that they cant be used concurrently or the "setstoploss" takes precedence. I want to use a trail stop, but If i use this, then this means I cant set a regular stop loss in case the price goes against me? Is this true?

    Thanks so much!

    Tim



    #2
    Hello Tim,

    Use TickSize multiplied by the number of ticks.


    For example:
    (High[0] - Low[0]) + 6 * TickSize

    Use prints to understand behavior.



    "is there a way to set an ATM in ninjascript where an initial stop loss is set, and then a trail stop is set once a profit trigger has been hit"

    To activate an Atm Strategy template use AtmStrategy methods.
    Join the official NinjaScript Developer Community for comprehensive resources, documentation, and community support. Build custom indicators and automated strategies for the NinjaTrader platforms with our extensive guides and APIs.



    " I want to use a trail stop, but If i use this, then this means I cant set a regular stop loss in case the price goes against me? Is this true?"

    This is correct, you cannot use both. But you can use logic to change the price of SetStopLoss() so this trails.
    Below is a link to an example.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    55 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    37 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    17 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    19 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    49 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X