Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help Getting Custom Trailing Stop working

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

    Help Getting Custom Trailing Stop working

    I recently migrated to NT8 after being a long time ToS and Pine Script user. I'm currently almost done converting my strategy into Ninja Script with just one small hiccup. I eventually learned that you cannot use SetTrailStop and SetStopLoss for the same position and that you must just have SetStopLoss begin to change its value after a certain point. I've been crutching ChatGPT a good bit (ik ik not great), but i cant seem to find out why my SetStopLoss "Trailing Stop" isn't being respected until one candle later. I've provided some images showing where the strategy is exiting, but with a red arrow pointing to where it should be exiting (this is where my pine strategy exits). If anyone knows how I can fix this or can point me in the direction of where a fix is that would be super helpful!!! Sorry if this is a newbie question

    Here is my code:

    if (Position.MarketPosition == MarketPosition.Long)
    {
    double unrealizedProfit = Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]);
    if (!trailStopActivated && unrealizedProfit >= TpPoints * TickSize * Position.Quantity * 5)
    {
    currentStopLoss = Position.AveragePrice;
    SetStopLoss("Long", CalculationMode.Price, currentStopLoss, false);
    trailStopActivated = true;
    tickCount = 0;
    }

    if (trailStopActivated)
    {
    double newStopLoss = High[0] - TrailOffset * TickSize;
    if (newStopLoss > currentStopLoss)
    {
    currentStopLoss = newStopLoss;
    SetStopLoss("Long", CalculationMode.Price, currentStopLoss, false);
    tickCount++;
    }
    }
    }

    if (Position.MarketPosition == MarketPosition.Short)
    {
    double unrealizedProfit = Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]);
    if (!trailStopActivated && unrealizedProfit >= TpPoints * TickSize * Position.Quantity * 5 )
    {
    currentStopLoss = Position.AveragePrice;
    SetStopLoss("Short", CalculationMode.Price, currentStopLoss, false);
    trailStopActivated = true;
    tickCount = 0;
    }

    if (trailStopActivated)
    {
    double newStopLoss = Math.Min(currentStopLoss, Low[0] + TrailOffset * TickSize);
    if (newStopLoss < currentStopLoss)
    {
    currentStopLoss = newStopLoss;
    SetStopLoss("Short", CalculationMode.Price, currentStopLoss, false);
    tickCount++;
    }​

    #2
    Hello Graeham9,

    Below is a link to an example, ProfitChaseStopTrailSetMethodsExample, that you can use as a template.


    If you want to debug your script to understand the behavior so the logic can be adjusted, add debugging prints. Print the date and time of the bar and price being supplied to SetStopLoss() one line above where the method is called. Enable TraceOrders and print the order.ToString() object in OnBarUpdate().

    Below is a link to a support article on using prints and TraceOrders to understand behavior.


    Save the output to a text file (right-click the output window > Save as) and attach this to your next post.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you!!!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      30 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      124 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      64 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      41 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X