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

Problems converting EasyLanguage code to Ninjascript for Fibonacci Retrace Channels

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

    Problems converting EasyLanguage code to Ninjascript for Fibonacci Retrace Channels

    Hi:

    This code continues to have the same error upon conversion (using ChatGPT-it finally threw its hands up lol)-here is the code conversion:

    // If there are extern alias declarations, they must come before using directives
    extern alias SomeAlias;

    // Using directives should come after extern alias declarations and before the namespace
    using NinjaTrader.Cbi.Tools;

    // Define the namespace
    namespace NinjaTrader.Gui.Tools
    {
    // Define the indicator class
    public class CustomIndicator : Indicator
    {
    // Declare inputs
    private double price;
    private int xAvgLen = 15;
    private int hiLoLen = 50;
    private double retrace = 0.382;

    // Declare variables
    private double xAvg = 0.0;
    private double hiHi = 0.0;
    private double loLo = 0.0;
    private double retracement = 0.0;

    // Initialize the indicator
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Custom Indicator";
    AddClose();
    }
    }

    // Calculate values on each bar update
    protected override void OnBarUpdate()
    {
    // Calculate XAverage
    xAvg = SMA(Closes[0], xAvgLen)[0];

    // Calculate HiHi and LoLo
    hiHi = MAX(Highs[0], hiLoLen)[0];
    loLo = MIN(Lows[0], hiLoLen)[0];

    // Calculate Retracement
    retracement = (hiHi - loLo) * retrace;

    // Plot values
    Values[0][0] = xAvg;
    Values[1][0] = hiHi - retracement;
    Values[2][0] = loLo + retracement;
    }
    }
    }

    // Using directives for any additional namespaces
    using NinjaTrader.Cbi.Tools;

    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.DrawingTools;
    #endregion

    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class FibonacciRetrace : Indicator
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "FibonacciRetrace";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    DisplayInDataBox = true;
    DrawOnPricePanel = true;
    DrawHorizontalGridLines = true;
    DrawVerticalGridLines = true;
    PaintPriceMarkers = true;
    ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
    //See Help Guide for additional information.
    IsSuspendedWhileInactive = true;
    AddPlot(Brushes.Aqua, "Cyan");
    AddPlot(Brushes.NavajoWhite, "Beige");
    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.
    }

    region Properties

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Cyan
    {
    get { return Values[0]; }
    }

    [Browsable(false)]
    [XmlIgnore]
    public Series<double> Beige
    {
    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 CustomIndicator[] cacheCustomIndicator;
    public CustomIndicator CustomIndicator()
    {
    return CustomIndicator(Input);
    }

    public CustomIndicator CustomIndicator(ISeries<double> input)
    {
    if (cacheCustomIndicator != null)
    for (int idx = 0; idx < cacheCustomIndicator.Length; idx++)
    if (cacheCustomIndicator[idx] != null && cacheCustomIndicator[idx].EqualsInput(input))
    return cacheCustomIndicator[idx];
    return CacheIndicator<CustomIndicator>(new CustomIndicator(), input, ref cacheCustomIndicator);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.CustomIndicator CustomIndicator()
    {
    return indicator.CustomIndicator(Input);
    }

    public Indicators.CustomIndicator CustomIndicator(ISeries<double> input )
    {
    return indicator.CustomIndicator(input);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.CustomIndicator CustomIndicator()
    {
    return indicator.CustomIndicator(Input);
    }

    public Indicators.CustomIndicator CustomIndicator(ISeries<double> input )
    {
    return indicator.CustomIndicator(input);
    }
    }
    }

    #endregion​


    and here is the error message on line 56:
    FibonacciRetrace.cs A using clause must precede all other elements defined in the namespace except extern alias declarations. 56 0
    zz0.lw262vzkffnzz

    Please, any help would be greatly appreciated-thanks

    Joe

    #2
    Hello rhoden,

    Welcome to the NinjaTrader forums!

    // If there are extern alias declarations, they must come before using directives
    extern alias SomeAlias;

    // Using directives should come after extern alias declarations and before the namespace
    using NinjaTrader.Cbi.Tools;

    // Define the namespace
    namespace NinjaTrader.Gui.Tools
    {
    // Define the indicator class
    public class CustomIndicator : Indicator
    {

    Are you trying to add two indicators to the same file? (I would recommend one file per indicator class)

    A Indicator that inherits from the Indicator class must be in the ​NinjaTrader.NinjaScript.Indicators namespace and won't work from the NinjaTrader.Gui.Tools namespace.

    There is no method declaration for AddClose() and this is not a NinjaTrader method.

    There is a lot of code in this that will not work.
    It might be easier to create a new script that has all of the default structure correct, then copy only the logic in OnBarUpdate and variables in the scope of the class.​
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I didn't think I was-I simply took what Ninjatrader had in the lower section and added the new chatgpt code above it...getting back to simplistic-the original code from Tradestation is:

      Inputs: Price(Close), XAvgLen(15), HiLoLen(50), Retrace(.382);
      Vars: XAvg(0), HiHi(0), LoLo(0), Retracement(0), BuySetup(0), SellSetup(0);
      XAvg = XAverage(Price, XAvgLen);
      HiHi = Highest(High, HiLoLen);
      LoLo = Lowest(Low, HiLoLen);
      Retracement = (HiHi - LoLo) * Retrace;
      Plot1(XAvg, "XAverage");
      Plot2(HiHi - Retracement, "HRetrace");
      Plot3(LoLo + Retracement, "LRetrace");

      If anyone here knows how to convert this from Easylanguage to ninjascript without all the error problems I'm having with GPT (down to one but it is still not working) or point me to an existing fibonacci retrace channel (advances with price)...I looked recently and couldnt find it...

      Any help would be greatly appreciated-thanks!

      Joe

      Comment


        #4
        Hello Joe,

        You have two classes inheriting from the Indicator class defined.

        public class CustomIndicator : Indicator

        public class FibonacciRetrace : Indicator

        You are attempting to put two indicator classes in the same file.

        GPT is typically not able to create function NinjaScripts.
        Below is a link to a forum thread that discusses.
        I'm not a pro coder, and I only learned C# out of necessity due to Ninjatrader. I have actually gained some new, recent knowledge about C# from ChatGPT, and it's been very forthcoming in providing sample code that works. Especially if it involves something off the beaten NT path and yet simple for many but perhaps not to me (a


        This thread will remain open for any community members that would like to convert this script from another language to NinjaScript as a convenience to you.

        You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.

        I am also providing a link to a forum post with helpful resources on getting started with NinjaScript should you decide to code this yourself.

        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by futtrader, 04-21-2024, 01:50 AM
        4 responses
        41 views
        0 likes
        Last Post futtrader  
        Started by Option Whisperer, Today, 09:55 AM
        1 response
        11 views
        0 likes
        Last Post bltdavid  
        Started by port119, Today, 02:43 PM
        0 responses
        7 views
        0 likes
        Last Post port119
        by port119
         
        Started by Philippe56140, Today, 02:35 PM
        0 responses
        7 views
        0 likes
        Last Post Philippe56140  
        Started by 00nevest, Today, 02:27 PM
        0 responses
        7 views
        0 likes
        Last Post 00nevest  
        Working...
        X