Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Super Trend Trail Strategy

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

    Super Trend Trail Strategy

    Hello Ninjatrader Support,
    I need assistance with TSSupertrend Strategy that enters a trade at the break of the supertrend line and trail the current price. Without Set 7, it works fine. But I have revised the code to include Set# 7 below which should flatten an open position when the TSsupertrend line slope equal zero. Basically when the TSsupertrend line not sloping then flatten the open position. While compile it I got the following errors: (See attached). Please help and let me know how to fix this.

    region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class SuperTrendTrailCode : Strategy
    {
    private double SuperTrendLong;
    private double SuperTrendShort;

    private TSSuperTrend TSSuperTrend1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "SuperTrendTrailCode";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    PositionSize = 1;
    LongTarget = 1000;
    ShortTarget = -1000;
    SuperTrendLong = 1;
    SuperTrendShort = 1;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    TSSuperTrend1 = TSSuperTrend(Close, SuperTrendMode.ATR, 14, 2.618, MovingAverageType.HMA, 14, false, false, false);
    TSSuperTrend1.Plots[0].Brush = Brushes.Green;
    TSSuperTrend1.Plots[1].Brush = Brushes.Red;
    AddChartIndicator(TSSuperTrend1);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if ((Close[0] >= TSSuperTrend1.UpTrend[0])
    && (TSSuperTrend1.UpTrend[0] != 0)
    && (TSSuperTrend1.DownTrend[0] == 0)
    && (TSSuperTrend1.DownTrend[1] != 0))
    {
    EnterLong(PositionSize, "EntryLong");
    SuperTrendLong = TSSuperTrend1.UpTrend[0];
    ExitLongStopMarket(Position.Quantity, SuperTrendLong, "StopLong", "EntryLong");
    ExitLongLimit(Position.Quantity, (Position.AveragePrice + (LongTarget * TickSize)), "TargetLong", "EntryLong");
    }

    // Set 2
    if ((Position.MarketPosition == MarketPosition.Long)
    && (GetCurrentAsk(0) > TSSuperTrend1.UpTrend[0])
    && (GetCurrentBid(0) > TSSuperTrend1.UpTrend[0])
    && (TSSuperTrend1.UpTrend[0] > SuperTrendLong))
    {
    SuperTrendLong = TSSuperTrend1.UpTrend[0];
    }

    // Set 3
    if (Position.MarketPosition == MarketPosition.Long)
    {
    ExitLongStopMarket(Position.Quantity, SuperTrendLong, "StopLong", "EntryLong");
    ExitLongLimit(Position.Quantity, (Position.AveragePrice + (LongTarget * TickSize)), "TargetLong", "EntryLong");
    }

    // Set 4
    if ((Close[0] <= TSSuperTrend1.DownTrend[0])
    && (TSSuperTrend1.DownTrend[0] != 0)
    && (TSSuperTrend1.UpTrend[0] == 0)
    && (TSSuperTrend1.UpTrend[1] != 0))
    {
    EnterShort(PositionSize, "EntryShort");
    SuperTrendShort = TSSuperTrend1.DownTrend[0];
    ExitShortStopMarket(Position.Quantity, SuperTrendShort, "StopShort", "EntryShort");
    ExitShortLimit(Position.Quantity, (Position.AveragePrice + (ShortTarget * TickSize)), "TargetShort", "EntryShort");
    }

    // Set 5
    if ((Position.MarketPosition == MarketPosition.Short)
    && (GetCurrentAsk(0) < TSSuperTrend1.DownTrend[0])
    && (GetCurrentBid(0) < TSSuperTrend1.DownTrend[0])
    && (TSSuperTrend1.DownTrend[0] < SuperTrendShort))
    {
    SuperTrendShort = TSSuperTrend1.DownTrend[0];
    }

    // Set 6
    if (Position.MarketPosition == MarketPosition.Short)
    {
    ExitShortStopMarket(Position.Quantity, SuperTrendShort, "StopShort", "EntryShort");
    ExitShortLimit(Position.Quantity, (Position.AveragePrice + (ShortTarget * TickSize)), "TargetShort", "EntryShort");
    }

    // Set 7 - Flatten position when Tssupertrend slope equals zero
    if (Position.MarketPosition != MarketPosition.Flat && TSSuperTrend1.Slope[0] == 0)
    {
    if (Position.MarketPosition == MarketPosition.Long)
    {
    ExitLong(Position.Quantity, "ExitLong");
    }
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    ExitShort(Position.Quantity, "ExitShort");
    }
    }
    }

    region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="PositionSize", Order=1, GroupName="Parameters")]
    public int PositionSize
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="LongTarget", Order=2, GroupName="Parameters")]
    public int LongTarget
    { get; set; }

    [NinjaScriptProperty]
    [Display(Name="ShortTarget", Order=3, GroupName="Parameters")]
    public int ShortTarget
    { get; set; }
    #endregion
    }
    }
    ​​
    Attached Files

    #2
    Hello pickles1774,

    From the errors pictured you are using a value incorrectly by placing bars ago on it that's the [0]. You also have incorrectly used the exit methods, you can see the ways the exit methods can be used in the following link:




    Comment


      #3
      Thank you for getting back to me. Could you kindly guide me on the process of closing open positions in the strategy builder when the Supertrend slope reaches zero? I don't have coding expertise, so if you could provide step-by-step instructions, along with code snippets or a video tutorial, it would be greatly appreciated. Your assistance is invaluable.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      50 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      126 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      69 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      42 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