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

Error in Compiling Custom Strategy

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

    Error in Compiling Custom Strategy

    Can someone please help? I'm not even sure that this code is in the correct syntax, but i keep getting errors and nothing on this forum is pointing me in the right direction. Here is the code, along with a screenshot of the errors.

    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.Indicator;
    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;


    namespace MyNamespace
    {
    public class MyStrategy : Strategy
    {
    // Define your indicators and variables here
    private MovingAverage ma;
    private RSI rsi;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    // Set the default values for your strategy here
    }
    else if (State == State.Configure)
    {
    // Configure your strategy here
    }
    else if (State == State.DataLoaded)
    {
    // Initialize your indicators and variables here
    ma = MovingAverage(Close, 20);
    rsi = RSI(Close, 14);
    }
    }

    protected override void OnBarUpdate()
    {
    // Check if the Moving Average is trending upwards and the RSI is above 50
    if (ma.CrossAbove(Close, 1) && rsi.Value > 50)
    {
    // If the conditions are met, enter a long trade
    EnterLong(1, "Long Trade");

    // Set the stop loss and take profit levels for the trade
    SetStopLoss(CalculationMode.Ticks, 5);
    SetProfitTarget(CalculationMode.Ticks, 10);
    }

    // Check if the Moving Average is trending downwards and the RSI is below 50
    if (ma.CrossBelow(Close, 1) && rsi.Value < 50)
    {
    // If the conditions are met, enter a short trade
    EnterShort(1, "Short Trade");

    // Set the stop loss and take profit levels for the trade
    SetStopLoss(CalculationMode.Ticks, 5);
    SetProfitTarget(CalculationMode.Ticks, 10);
    }
    }
    }
    }



    Click image for larger version

Name:	image.png
Views:	153
Size:	19.9 KB
ID:	1228339

    #2
    Hello kotatuck,

    Thank you for your note.

    I suspect that this has to do with your using statements at the beginning of the script. It even appears that some of the statements have been duplicated. What I suggest is to start a new strategy from scratch by going to the NinjaScript Editor, then right-click the Strategies folder and select New Strategy.

    The Strategy wizard will pop up. Select the desired settings and then generate your strategy and copy the necessary logic over. I have created a video demonstrating what this process might look like:


    Regarding the MovingAverage comments I made in the video, there are many moving average indicators that come with NinjaTrader by default. If you take a look at the following link, we have the System Indicator Methods listed in the help guide:


    They are listed in alphabetical order, and conveniently all of the moving averages are grouped together. I suspect that you will need to further define which type of moving average you wish to use in your strategy. This would involve changing the line "private MovingAverage ma;" to something like "private EMA ma;" if you were to use the exponential moving average, for example. Please change this to whichever moving average you plan to use.

    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      kotatuck It could be because your namespace is not correct? It should be: namespace NinjaTrader.NinjaScript.Strategies

      And as NinjaTrader_Emily says, probably a good idea to tidy up the using statements.

      Thanks.
      Multi-Dimensional Managed Trading
      jeronymite
      NinjaTrader Ecosystem Vendor - Mizpah Software

      Comment


        #4
        Originally posted by NinjaTrader_Emily View Post
        Hello kotatuck,

        Thank you for your note.

        I suspect that this has to do with your using statements at the beginning of the script. It even appears that some of the statements have been duplicated. What I suggest is to start a new strategy from scratch by going to the NinjaScript Editor, then right-click the Strategies folder and select New Strategy.

        The Strategy wizard will pop up. Select the desired settings and then generate your strategy and copy the necessary logic over. I have created a video demonstrating what this process might look like:


        Regarding the MovingAverage comments I made in the video, there are many moving average indicators that come with NinjaTrader by default. If you take a look at the following link, we have the System Indicator Methods listed in the help guide:
        https://ninjatrader.com/support/help...indicators.htm

        They are listed in alphabetical order, and conveniently all of the moving averages are grouped together. I suspect that you will need to further define which type of moving average you wish to use in your strategy. This would involve changing the line "private MovingAverage ma;" to something like "private EMA ma;" if you were to use the exponential moving average, for example. Please change this to whichever moving average you plan to use.

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



        Alright, so i have made the necessary changes in the code, but i am getting a few more errors now. Here is the updated code :


        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;


        namespace NinjaTrader.NinjaScript.Strategies
        {
        public class MyStrategy : Strategy
        {
        // Define your indicators and variables here
        private EMA ma;
        private RSI rsi;

        protected override void OnStateChange()
        {
        if (State == State.SetDefaults)
        {
        // Set the default values for your strategy here
        }
        else if (State == State.Configure)
        {
        // Configure your strategy here
        }
        else if (State == State.DataLoaded)
        {
        // Initialize your indicators and variables here
        ma = EMA(Close, 20);
        rsi = RSI(Close, 14);
        }
        }

        protected override void OnBarUpdate()
        {
        // Check if the Moving Average is trending upwards and the RSI is above 50
        if (ma.CrossAbove(Close, 1) && rsi.Value > 50)
        {
        // If the conditions are met, enter a long trade
        EnterLong(1, "Long Trade");

        // Set the stop loss and take profit levels for the trade
        SetStopLoss(CalculationMode.Ticks, 5);
        SetProfitTarget(CalculationMode.Ticks, 10);
        }

        // Check if the Moving Average is trending downwards and the RSI is below 50
        if (ma.CrossBelow(Close, 1) && rsi.Value < 50)
        {
        // If the conditions are met, enter a short trade
        EnterShort(1, "Short Trade");

        // Set the stop loss and take profit levels for the trade
        SetStopLoss(CalculationMode.Ticks, 5);
        SetProfitTarget(CalculationMode.Ticks, 10);
        }
        }
        }
        }

        ​and i attached the screenshots of the errors
        Attached Files

        Comment


          #5
          Hello kotatuck,

          Thank you for your reply.

          The error messages do indicate which line is causing the error, and you could also double-left-click a message to move your cursor to that line of the script. For example, the first two error messages are related to line 64 of your script where you define the rsi as the RSI indicator. RSI(Close, 14) is not valid because Close is an ISeries<double> object and the argument is looking for an int. The syntax for RSI may be found in the help guide here, or you could use the intelliprompt within the NinjaScript Editor to see the method signatures available. For more information about CrossAbove and CrossBelow, which are mentioned in the other errors, please see the following help guide sections:



          While the scripting support team does not offer hands-on debugging, development, or C# educational services we are happy to answer any specific questions you may have about NinjaScript. We are glad to provide any resources in our help guide as well as simple examples. We can assist with guiding you through the debugging process, though if you seek more hands-on guidance you could 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 our NinjaTrader Ecosystem team to follow up with you with 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 appreciate your patience. Please let me know if I may be of further assistance.
          Emily C.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Emily View Post
            Hello kotatuck,

            Thank you for your reply.

            The error messages do indicate which line is causing the error, and you could also double-left-click a message to move your cursor to that line of the script. For example, the first two error messages are related to line 64 of your script where you define the rsi as the RSI indicator. RSI(Close, 14) is not valid because Close is an ISeries<double> object and the argument is looking for an int. The syntax for RSI may be found in the help guide here, or you could use the intelliprompt within the NinjaScript Editor to see the method signatures available. For more information about CrossAbove and CrossBelow, which are mentioned in the other errors, please see the following help guide sections:
            https://ninjatrader.com/support/help...crossabove.htm
            https://ninjatrader.com/support/help...crossbelow.htm

            While the scripting support team does not offer hands-on debugging, development, or C# educational services we are happy to answer any specific questions you may have about NinjaScript. We are glad to provide any resources in our help guide as well as simple examples. We can assist with guiding you through the debugging process, though if you seek more hands-on guidance you could 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 our NinjaTrader Ecosystem team to follow up with you with 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 appreciate your patience. Please let me know if I may be of further assistance.
            Thank you so much! This an a little bit of research finally fixed the code! However, when I go to backtest, it gives me this error in the log:

            Click image for larger version

Name:	image.png
Views:	159
Size:	3.4 KB
ID:	1228712

            And here is the updated 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.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 Testing : Strategy
            {
            // Define your indicators and variables here
            private EMA ma;
            private RSI rsi;
            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"Enter the description for your new custom Strategy here.";
            Name = "Testing";
            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;
            }
            else if (State == State.Configure)
            {
            // Configure your strategy here
            }
            else if (State == State.DataLoaded)
            {
            // Initialize your indicators and variables here
            double ma = EMA(Close, 20)[0];
            double rsi = RSI(Close, 14, 3)[0];
            }
            }

            protected override void OnBarUpdate()
            {
            // Check if the Moving Average is trending upwards and the RSI is above 50
            if (CrossAbove(ma,Close, 1) && rsi[0] > 50.0)
            {
            // If the conditions are met, enter a long trade
            EnterLong(1, "Long Trade");

            // Set the stop loss and take profit levels for the trade
            SetStopLoss(CalculationMode.Ticks, 5);
            SetProfitTarget(CalculationMode.Ticks, 10);
            }

            // Check if the Moving Average is trending downwards and the RSI is below 50
            if (CrossBelow(ma,Close, 1) && rsi[0] < 50.0)
            {
            // If the conditions are met, enter a short trade
            EnterShort(1, "Short Trade");

            // Set the stop loss and take profit levels for the trade
            SetStopLoss(CalculationMode.Ticks, 5);
            SetProfitTarget(CalculationMode.Ticks, 10);
            }
            }
            }
            }



            Thank you for helping!!!!

            Comment


              #7
              Hello kotatuck,

              Thank you for your reply.

              I am glad you were able to make some progress and resolve the compile errors. As for the error you are receiving during the backtest, this usually means that your script is reference an object that has a null value. This can be addressed by adding null reference checks in your script as described in the following page from our help guide:


              Another way to check for null objects is to add debugging prints in your script to print the variable values. This can help so you can see which prints were successful before the error comes up and narrow down which part of your script is throwing the error. For more information about using debugging prints:


              Please let us know if you have any additional questions or concerns.
              Emily C.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Jonafare, 12-06-2012, 03:48 PM
              5 responses
              3,985 views
              0 likes
              Last Post rene69851  
              Started by Fitspressorest, Today, 01:38 PM
              0 responses
              2 views
              0 likes
              Last Post Fitspressorest  
              Started by Jonker, Today, 01:19 PM
              0 responses
              2 views
              0 likes
              Last Post Jonker
              by Jonker
               
              Started by futtrader, Today, 01:16 PM
              0 responses
              7 views
              0 likes
              Last Post futtrader  
              Started by Segwin, 05-07-2018, 02:15 PM
              14 responses
              1,792 views
              0 likes
              Last Post aligator  
              Working...
              X