Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

automated custom supertrend

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

    automated custom supertrend

    I'm relatively new to Ninjascript and I've tried converting the code I have on thinkorswim so I can automate this strategy but I can't really get it to work also (not sure if I'm inserting on Ninjatrade properly) . so if someone could look at my code and help me out I would greatly appreciate it.

    ninjascript i have
    using System;
    using NinjaTrader.Cbi;

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MySupertrendStrategy : Strategy
    {
    private double AtrMult = 0.7;
    private int nATR = 4;
    private AverageType AvgType = AverageType.Hull;
    private bool PaintBars = true;
    private bool showsignals = true;

    private double ATR(double[] high, double[] close, double[] low)
    {
    return SMA(Typical, nATR)[0];
    }

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "Supertrend Strategy";
    Name = "MySupertrendStrategy";
    Calculate = Calculate.OnEachTick;
    IsOverlay = true;
    }
    else if (State == State.Configure)
    {
    AddPlot(Brushes.White, "SupertrendUp");
    AddPlot(Brushes.Cyan, "SupertrendDown");
    }
    }

    protected override void OnBarUpdate()
    {
    double ATRValue = ATR(Highs, Closes, Lows);
    double UPValue = Highs[0] + (AtrMult * ATRValue);
    double DNValue = Highs[0] + (-AtrMult * ATRValue);
    double STValue = Close[0] < STValue[1] ? UPValue : DNValue;

    PlotBrushes[0][0] = Brushes.Transparent;
    PlotBrushes[1][0] = Brushes.Transparent;

    if (Close[1] > Close[2])
    PlotBrushes[0][0] = Brushes.Green;
    else if (Close[1] < Close[2])
    PlotBrushes[1][0] = Brushes.Red;

    if (STValue.CrossBelow(Close[0], 1))
    {
    PlotBrushes[0][0] = Brushes.White;
    if (PaintBars && Close[0] < STValue)
    BarBrush = Brushes.Red;
    }
    else if (STValue.CrossAbove(Close[0], 1))
    {
    PlotBrushes[1][0] = Brushes.Cyan;
    if (PaintBars && Close[0] > STValue)
    BarBrush = Brushes.Green;
    }

    if (Close[1] < Open[1] && Close[0] > Open[0] && PlotBrushes[0][0] == Brushes.Green)
    Draw.ArrowUp(this, "MyUpArrow" + CurrentBar, true, 0, Lows[0] - TickSize, Brushes.Green);
    else if (Close[1] > Open[1] && Close[0] < Open[0] && PlotBrushes[1][0] == Brushes.Red)
    Draw.ArrowDown(this, "MyDownArrow" + CurrentBar, true, 0, Highs[0] + TickSize, Brushes.Red);
    }
    }
    }​

    thinkorswim script
    input AtrMult = .7;
    input nATR = 4;
    input AvgType = AverageType.HULL;
    input PaintBars = yes;
    input showsignals = yes;
    #def
    def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
    def UP = HL2 + (AtrMult * ATR);
    def DN = HL2 + (-AtrMult * ATR);
    def ST = if close < ST[1] then UP else DN;
    def Vinculum = ST;
    def plotlong = if ST crosses below close then 1 else 0;
    def plotshort = if ST crosses above close then 1 else 0;

    plots
    plot x = plotlong;
    Alert (plotlong, Sound.Ring);
    x.SetPaintingStrategy(paintingStrategy = PaintingStrategy.BOOLEAN_ARROW_UP);
    AssignPriceColor(if x then Color.WHITE else Color.CYAN);

    plot y = plotshort;
    Alert (Vinculum, Sound.Ring);
    y.SetPaintingStrategy(paintingStrategy = PaintingStrategy.BOOLEAN_ARROW_DOWN);
    AssignPriceColor(if x then Color.CYAN else Color.WHITE);

    x.AssignValueColor(if close < ST then Color.RED else Color.CYAN);
    AssignPriceColor(if PaintBars and close < ST

    then Color.RED

    else if PaintBars and close > ST

    then Color.GREEN
    else Color.CURRENT);

    x.SetHiding(!showsignals);
    y.SetHiding(!showsignals);



    def ATR1 = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
    def UP1 = HL2 + (AtrMult * ATR1);
    def DN1 = HL2 + (-AtrMult * ATR1);
    def ST1 = if close < ST[1] then UP1 else DN1;

    def trendChangeUp = ST1 crosses above ST[1];
    def closeHigherAfterTrendChange = close > open and close[1] > open[1];
    def trendIsUp = ST1 > ST[1];

    plot BuySignal = trendChangeUp and closeHigherAfterTrendChange and trendIsUp;
    BuySignal.SetPaintingStrategy(PaintingStrategy.BOO LEAN_ARROW_UP);
    BuySignal.SetDefaultColor(Color.GREEN);
    BuySignal.SetLineWeight(2);

    ##### down #####



    def ATR2 = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
    def UP2 = HL2 + (AtrMult * ATR2);
    def DN2 = HL2 + (-AtrMult * ATR2);
    def ST2 = if close > ST[1] then dn2 else up2;

    def trendChangedn = ST2 crosses below ST[1];
    def closelowerAfterTrendChange = close < open and close[1] < open[1];
    def trendIsdn = ST2 < ST[1];

    plot BuySignal1 = trendChangedn and closelowerAfterTrendChange and trendIsdn;
    BuySignal1.SetPaintingStrategy(PaintingStrategy.BO OLEAN_ARROW_down);
    BuySignal1.SetDefaultColor(Color.red);
    BuySignal1.SetLineWeight(2);​
    Last edited by Dalton8883; 08-15-2023, 08:38 AM.

    #2
    Hello Dalton8883,

    Thanks for your post.

    " I can't really get it to work "

    So I may accurately assist you, what exactly does not work in the script?

    Are you seeing error messages in the Log tab of the Control Center? If so, what do they report?

    Do you have compilation errors when compiling the strategy? If so, what do they report?

    Have you added debugging prints to the script to understand exactly how the strategy's logic is behaving?

    If not, debugging prints should be added to the strategy to see how it is evaluating logic.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    <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
      Originally posted by NinjaTrader_BrandonH View Post
      Hello Dalton8883,

      Thanks for your post.

      " I can't really get it to work "

      So I may accurately assist you, what exactly does not work in the script?

      Are you seeing error messages in the Log tab of the Control Center? If so, what do they report?

      Do you have compilation errors when compiling the strategy? If so, what do they report?

      Have you added debugging prints to the script to understand exactly how the strategy's logic is behaving?

      If not, debugging prints should be added to the strategy to see how it is evaluating logic.

      Below is a link to a forum post that demonstrates how to use prints to understand behavior.
      https://ninjatrader.com/support/foru...121#post791121
      I'm getting an error code CS0246 and no I have not added debugging prints.

      Comment


        #4
        Hello Dalton8883,

        Thanks for your notes.

        So I may accurately assist, what does the error message in the Error column of the compile error state (not to be confused with the Code column)?

        Please send me a screenshot of the compilation error message with the full error message visible in the Error column.
        • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
        • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.
        ​I look forward to assisting 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
          Originally posted by NinjaTrader_BrandonH View Post
          Hello Dalton8883,

          Thanks for your notes.

          So I may accurately assist, what does the error message in the Error column of the compile error state (not to be confused with the Code column)?

          Please send me a screenshot of the compilation error message with the full error message visible in the Error column.
          • To send a screenshot with Windows 10 or newer I would recommend using the Windows Snipping Tool.
          • Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save it as a jpeg file and send the file as an attachment.
          ​I look forward to assisting further.
          here is the screenshot

          Click image for larger version

Name:	image.png
Views:	163
Size:	696.4 KB
ID:	1264881

          Comment


            #6
            Hello Dalton8883,

            Thanks for that information.

            This error message indicates that you are referencing 'AverageType' in your script but the namespace cannot be found.

            Is AverageType an indicator that you are trying to access in the script?

            Is AverageType a user-defined enum property that you are trying to access in the script?

            If this is an enum, you would need to create a custom namespace in the script and define the enum within that namespace.

            You could see how to create and reference enum properties in this reference sample from the help guide: https://ninjatrader.com/support/help...ned_parame.htm
            <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, Today, 05:17 AM
            0 responses
            52 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            130 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            70 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            44 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            49 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X