Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Wanting to convert this Simple Supertrend/CCI indicator from thinkscript to NT

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

    Wanting to convert this Simple Supertrend/CCI indicator from thinkscript to NT

    I ran across this simpler version of a Supertrend indicator and I've been struggling a bit to convert it to an NT8 indicator. Used to be a Java programmer back in the day but I'm extremely new to C# and never got along much with object oriented programming in the past anyway. C, Perl, and scripting was always my thing. Wondering if anyone might be willing to take a crack at it. Here's the Thinkscript. Thanks in advance!

    Mark

    Code:
    #SUPERTREND
    def ST_Atr_Mult = 1.0;
    def ST_Length = 4;
    def ST_AvgType = AverageType.HULL;
    
    def ATRSuper = MovingAverage(ST_AvgType, TrueRange(high, close, low), ST_Length);
    def UP = HL2 + (ST_Atr_Mult * ATRSuper);
    def DN = HL2 + (-ST_Atr_Mult * ATRSuper);
    def ST = if close < ST[1] then UP else DN;
    
    #CCI_ATR
    def pricedata = hl2;
    def lengthCCI = 50;
    def lengthATR = 21;
    def AtrFactorSuper = 1.0;
    def ATRcci = Average(TrueRange(high, close, low), lengthATR) * AtrFactorSuper;
    def price = close + low + high;
    def linDev = LinDev(price, lengthCCI);
    def CCI = if linDev == 0
    then 0
    else (price - Average(price, lengthCCI)) / linDev / 0.015;
    
    def MT1 = if CCI > 0
    then Max(MT1[1], pricedata - ATRcci)
    else Min(MT1[1], pricedata + ATRcci);
    
    def SuperTrendSignalLong = close > MT1 and close > ST;
    def SuperTrendSignalShort = close < MT1 and close < ST;
    Last edited by markdshark; 09-04-2022, 06:42 PM.

    #2
    Hello markdshark,

    Thank you for your post.

    Although the NinjaTrader support team does not provide hands-on development or debugging assistance, we will gladly leave this thread open for any users who would like to assist you with converting this indicator. If you would like more resources regarding NinjaScript and C# to try and work on the conversion yourself, please check out the following post from my colleague Chelsea:



    Another option would be to work directly with a NinjaScript Consultant who could write the conversion for you or assist you with converting the script. If this is an option that interests you, I would be glad to get you in touch with our Vendor Support team for additional information and resources to help you take advantage of consulting services.

    Please let us know if we may be of further assistance.

    Comment


      #3
      So here's my first attempt. My approach was to start with TSSuperTrend and replace the main code with my own. Then I hacked away old code from TSSuperTrend including unfortunately a lot of the indicator drawing stuff. My plan is once the key calculations in the code appears to be working I'll transfer it back to another copy of TSSuperTrend and this time not hack away quite so much. Meanwhile I'm struggling to wrap my head around all this object oriented stuff.

      Code:
      #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.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion
      
      //This namespace holds Indicators in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Indicators
      {
      public class A0MyTSSuperTrendCCI : Indicator
      {
      
      private double pricedata;
      private Series<bool> _trend;
      private Series<double> _avg;
      private Series<double>_st;
      private Series<double> rangeSeries;
      private Series<double> offsetSeries;
      private Series<double> atr;
      private Series<double>MT;
      
      private int _length = 14;
      private int _smooth = 14;
      private int _thisbar = -1;
      private double _multiplier = 1; //2.618;
      private int lengthCCI = 50;
      private int lengthATR = 21;
      
      private double up;
      private double down;
      private double atrCCI;
      private double linDev;
      private double diff;
      private double avgCLH;
      private double sumOfCLH;
      private double CCI;
      private double clh;
      
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"A0MyTSSuperTrendCCI Indicator developed by TradingStudies.com (Version 2.3)";
      Name = "A0MyTSSuperTrendCCI";
      Calculate = Calculate.OnBarClose;
      IsOverlay = true;
      DisplayInDataBox = true;
      DrawOnPricePanel = true;
      DrawHorizontalGridLines = true;
      DrawVerticalGridLines = true;
      PaintPriceMarkers = true;
      ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
      
      }
      else if (State == State.Configure)
      {
      _trend = new Series<bool>(this);
      _avg = new Series<double>(this);
      _st = new Series<double>(this);
      rangeSeries = new Series<double>(this);
      atr = new Series<double>(this);
      }
      }
      
      protected override void OnBarUpdate()
      {
      if (CurrentBar < 1)
      {
      _trend[0] = true;
      return;
      }
      
      //ST Section
      rangeSeries = ATR(Close, 1).Value;
      Print("Range0= " + rangeSeries[0]);
      atr[0] = HMA(rangeSeries, 4)[0];
      // Print("Atr0= " + atr[0]);
      
      up = (High[0] + Low[0])/2 + _multiplier * atr[0];
      down = (High[0] + Low[0])/2 + (-_multiplier *atr[0]);
      _st[0]=(Close[0] < _st[1]? up : down);
      // Print ("st0 = " + _st[0]);
      
      //CCI_ATR
      atrCCI=0;
      for(int i=0;i<21;atrCCI+=rangeSeries[i++]);
      atrCCI = atrCCI/21;
      // Thinkscripts Lindev ****
      sumOfCLH=0;
      for(int i=0; i<50;sumOfCLH+=(Low[i]+High[i]+Close[i++]));
      avgCLH = sumOfCLH/50;
      diff=0;
      for(int i=0;i<50;diff+=Math.Abs(avgCLH-Close[i++]));
      linDev = diff/50;
      
      CCI = (linDev==0?0:(Low[0]+High[0]+Close[0] - avgCLH)/linDev/0.015);
      MT[0] = (CCI>0 ? Math.Max(MT[1],(High[0] + Low[0])/2 - atrCCI) : Math.Min(MT[1],(High[0] + Low[0])/2 + atrCCI));
      
      if (Close[0] > MT[0] && Close[0] > _st[0]) _trend[0] = true;
      else if (Close[0] < MT[0] && Close[0] < _st[0]) _trend[0] = false;
      
      } // end protected override void OnBarUpdate()
      
      #region Properties
      [Range(1, int.MaxValue)]
      [NinjaScriptProperty]
      [Display(Name="Period", Description="ATR/DT Period", Order=2, GroupName="Parameters")]
      public int Length
      {
      get {return _length;}
      set {_length = value;}
      }
      
      [Range(0.0001, double.MaxValue)]
      [NinjaScriptProperty]
      [Display(Name="Multiplier", Description="ATR Multiplier", Order=3, GroupName="Parameters")]
      public double Multiplier
      {
      get {return _multiplier;}
      set {_multiplier = value;}
      }
      
      
      [Range(1, int.MaxValue)]
      [NinjaScriptProperty]
      [Display(Name="Smooth", Description="Smoothing Period", Order=5, GroupName="Parameters")]
      public int Smooth
      {
      get {return _smooth;}
      set {_smooth = value;}
      }
      
      
      
      [Browsable(false)]
      [XmlIgnore]
      public Series<double> UpTrend
      {
      get
      {return Values[0];}
      }
      
      [Browsable(false)]
      [XmlIgnore]
      public Series<double> DownTrend
      {
      get
      {return Values[1];}
      }
      #endregion
      
      }
      }
      
      #region NinjaScript generated code. Neither change nor remove.
      
      namespace NinjaTrader.NinjaScript.Indicators
      {
      public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
      {
      private A0MyTSSuperTrendCCI[] cacheA0MyTSSuperTrendCCI;
      public A0MyTSSuperTrendCCI A0MyTSSuperTrendCCI(int length, double multiplier, int smooth)
      {
      return A0MyTSSuperTrendCCI(Input, length, multiplier, smooth);
      }
      
      public A0MyTSSuperTrendCCI A0MyTSSuperTrendCCI(ISeries<double> input, int length, double multiplier, int smooth)
      {
      if (cacheA0MyTSSuperTrendCCI != null)
      for (int idx = 0; idx < cacheA0MyTSSuperTrendCCI.Length; idx++)
      if (cacheA0MyTSSuperTrendCCI[idx] != null && cacheA0MyTSSuperTrendCCI[idx].Length == length && cacheA0MyTSSuperTrendCCI[idx].Multiplier == multiplier && cacheA0MyTSSuperTrendCCI[idx].Smooth == smooth && cacheA0MyTSSuperTrendCCI[idx].EqualsInput(input))
      return cacheA0MyTSSuperTrendCCI[idx];
      return CacheIndicator<A0MyTSSuperTrendCCI>(new A0MyTSSuperTrendCCI(){ Length = length, Multiplier = multiplier, Smooth = smooth }, input, ref cacheA0MyTSSuperTrendCCI);
      }
      }
      }
      
      namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
      {
      public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
      {
      public Indicators.A0MyTSSuperTrendCCI A0MyTSSuperTrendCCI(int length, double multiplier, int smooth)
      {
      return indicator.A0MyTSSuperTrendCCI(Input, length, multiplier, smooth);
      }
      
      public Indicators.A0MyTSSuperTrendCCI A0MyTSSuperTrendCCI(ISeries<double> input , int length, double multiplier, int smooth)
      {
      return indicator.A0MyTSSuperTrendCCI(input, length, multiplier, smooth);
      }
      }
      }
      
      namespace NinjaTrader.NinjaScript.Strategies
      {
      public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
      {
      public Indicators.A0MyTSSuperTrendCCI A0MyTSSuperTrendCCI(int length, double multiplier, int smooth)
      {
      return indicator.A0MyTSSuperTrendCCI(Input, length, multiplier, smooth);
      }
      
      public Indicators.A0MyTSSuperTrendCCI A0MyTSSuperTrendCCI(ISeries<double> input , int length, double multiplier, int smooth)
      {
      return indicator.A0MyTSSuperTrendCCI(input, length, multiplier, smooth);
      }
      }
      }
      
      #endregion
      Last edited by markdshark; 09-05-2022, 08:26 PM.

      Comment


        #4
        Hello markdshark,

        Thank you for sharing your work with the NinjaTrader forum community.

        If you would like to export a script so that others may import it, you may do so by going to Control Center > Tools > Export > NinjaScript Addon. Select your indicator then Export to a .zip file. This file could then be uploaded as an attachment to the forums or sent to others easily.

        You mentioned this is your first attempt; is the indicator functioning how you'd expect or do you have any specific questions that you would like assistance with?

        Please don't hesitate to reach out with any additional questions or concerns.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        558 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        324 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        545 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        547 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X