Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Pine Script to NinjaScript Fisher Transformation of Price from EMA

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

    Pine Script to NinjaScript Fisher Transformation of Price from EMA

    On tradingview there is an indicator called "ema exhaustion". I believe it calculates a 9 period fisher transformation of the distance of price from the 50 period ema. I asked CoPilot (chatgpt4) to covert the pine script to ninjascript. I was able to compile the ninjascript but nothing plotted and I was getting the following error in the output window, "fish1: 0.342828254415394 fish2: 0 Indicator 'FisherDistanceFromEma': Error on calling 'OnBarUpdate' method on bar 9: Index was outside the bounds of the array."

    The problem seems to be with the IF statement in OnBarUpdate(). None of the suggestions CoPilot gave me worked so here I am looking for a kind-hearted coder

    Here's the pine script and ninjascript code. Thanks.

    Pinescript
    Code:
    //@version=4
    study(title="Fisher Transform with Distance from EMA", shorttitle="Fisher Distance from EMA", format=format.price, precision=2, resolution="")
    len = input(9, minval=1, title="Fisher Transform Length")
    
    emaLength = input(title="EMA Length", defval=50, type=input.integer)
    emaValue = ema(close*100000000, emaLength)
    distFromEMA = (close*100000000 - emaValue)
    
    high_ = highest(distFromEMA, len)
    low_ = lowest(distFromEMA, len)
    round_(val) => val > .99 ? .999 : val < -.99 ? -.999 : val
    value = 0.0
    value := round_(.66 * ((distFromEMA - low_) / max(high_ - low_, .001) - .5) + .67 * nz(value[1]))
    fish1 = 0.0
    fish1 := .5 * log((1 + value) / max(1 - value, .001)) + .5 * nz(fish1[1])
    fish2 = fish1[1]
    hline(1.5, "1.5", color=#FFC5B5)
    hline(0.75,"0.75", color=#C6C6C6)
    hline(0, "0", color=#FFC5B5)
    hline(-0.75, "-0.75", color=#C6C6C6)
    hline(-1.5, "-1.5", color=#FFC5B5)
    plot(fish1, color=#0094FF, title="Fisher")
    plot(fish2, color=#FFD800, title="Trigger")​
    Ninjascript
    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Windows.Media;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    using System.Xml.Serialization;
    #endregion
    
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public class FisherTransformWithDistanceFromEMA : Indicator
        {
            private int len = 9;
            private int emaLength = 50;
            private EMA ema;
            private Series<double> distFromEMA;
            private Series<double> value;
            private Series<double> fish1;
            private Series<double> fish2;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description = @"Fisher Transform with Distance from EMA";
                    Name = "Fisher Transform with Distance from EMA";
                    Calculate = Calculate.OnBarClose;
                    IsOverlay = false;
                    DisplayInDataBox = true;
                    DrawOnPricePanel = true;
                    DrawHorizontalGridLines = true;
                    DrawVerticalGridLines = true;
                    PaintPriceMarkers = true;
                    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                }
                else if (State == State.Configure)
                {
                    ema = EMA(Close, emaLength);
                    distFromEMA = new Series<double>(this);
                    value = new Series<double>(this);
                    fish1 = new Series<double>(this);
                    fish2 = new Series<double>(this);
                }
            }
    
            protected override void OnBarUpdate()
            {
                if (CurrentBar >= len) return;
    
                distFromEMA[0] = Close[0] * 100000000 - ema[0];
                double high = MAX(distFromEMA, len)[0];
                double low = MIN(distFromEMA, len)[0];
                value[0] = Math.Min(Math.Max(.66 * ((distFromEMA[0] - low) / Math.Max(high - low, .001) - .5) + .67 * value[1], -.999), .999);
                fish1[0] = .5 * Math.Log((1 + value[0]) / Math.Max(1 - value[0], .001)) + .5 * fish1[1];
                fish2[0] = fish1[1];
    
                PlotBrushes[0][0] = Brushes.Blue;
                PlotBrushes[1][0] = Brushes.Yellow;
                Values[0][0] = fish1[0];
                Values[1][0] = fish2[0];
            }
    
            #region Properties
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> Fisher { get { return Values[0]; } }
    
            [Browsable(false)]
            [XmlIgnore]
            public Series<double> Trigger { get { return Values[1]; } }
            #endregion
        }
    }
    ​

    #2
    Hello rc5781,

    The Values collection is populated with plot series when AddPlot() is called.

    As AddPlot() is not being called in your script, Values[0] and Values[1] do not exist.

    Below is a link to a support article on adding plots and setting the values.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by StockTrader88, 03-06-2021, 08:58 AM
    44 responses
    3,967 views
    3 likes
    Last Post jhudas88  
    Started by rbeckmann05, Today, 06:48 PM
    0 responses
    4 views
    0 likes
    Last Post rbeckmann05  
    Started by rhyminkevin, Today, 04:58 PM
    4 responses
    55 views
    0 likes
    Last Post dp8282
    by dp8282
     
    Started by iceman2018, Today, 05:07 PM
    0 responses
    6 views
    0 likes
    Last Post iceman2018  
    Started by lightsun47, Today, 03:51 PM
    0 responses
    8 views
    0 likes
    Last Post lightsun47  
    Working...
    X