Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unable to Test Strategy

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

    Unable to Test Strategy

    Hello,

    Can someone please take a look at my below strategy, for some reason I an unable to test. When I put it into the Strategy Analyzer and hit RUN, nothing happens:

    #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 MyCustomStrategy : Strategy
    {
    private HMA HMA1;
    private HMA HMA2;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "MyCustomStrategy";
    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 = 14;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    HMA1 = HMA(Close, 13);
    HMA2 = HMA(Open, 13);

    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < BarsRequiredToTrade)
    return;

    // Set 1
    if (CrossAbove(HMA1, HMA2, 0))
    {
    EnterLong();

    }

    else if (CrossBelow(HMA1, HMA2, 0))
    {
    EnterShort();

    }




    }
    }
    }








    #2
    Hello omermirza,
    I think you need to define profit & stop loss in strategy since there is no exit rule defined so strategy analyzer may not be able to analyze it. For now its just entering long or short. Hope it helps!

    Comment


      #3
      Hi omermirza,
      You will have to change the lookback period to at least ONE.
      if (CrossAbove(HMA1, HMA2, 1)).
      if (CrossBelow(HMA1, HMA2, 1)).
      https://ninjatrader.com/support/help...crossabove.htm
      For debugging, I also suggest to i) add plot your indicators to the chart, ii) use Prints to the Output Window to check your calculations, HMA1, HMA2, CrossAbove, CrossBelow, ....
      NT-Roland

      Comment


        #4
        Originally posted by NT-Roland View Post
        Hi omermirza,
        You will have to change the lookback period to at least ONE.
        if (CrossAbove(HMA1, HMA2, 1)).
        if (CrossBelow(HMA1, HMA2, 1)).
        https://ninjatrader.com/support/help...crossabove.htm
        For debugging, I also suggest to i) add plot your indicators to the chart, ii) use Prints to the Output Window to check your calculations, HMA1, HMA2, CrossAbove, CrossBelow, ....
        NT-Roland
        Thank you, but unfortunately nothing comes up still when I run the analyzer.

        -Omer

        Comment


          #5
          Originally posted by s.kinra View Post
          Hello omermirza,
          I think you need to define profit & stop loss in strategy since there is no exit rule defined so strategy analyzer may not be able to analyze it. For now its just entering long or short. Hope it helps!
          I don't think that's the case since the SampleMACrossOver strategy enters and exits at the crossovers.

          protected override void OnBarUpdate()
          {
          if (CurrentBar < BarsRequiredToTrade)
          return;

          if (CrossAbove(smaFast, smaSlow, 1))
          EnterLong();
          else if (CrossBelow(smaFast, smaSlow, 1))
          EnterShort();
          }

          Comment


            #6
            Hi Omer,
            Did you build the logic in StrategyBuilder or NinjaScript?
            Small differences can have huge impact. Please always start with the StrategyBuilder until you are fully familiar with Ninja code logic.
            The below (StrategyBuilder only) works just fine at my end.

            //This namespace holds Strategies in this folder and is required. Do not change it.
            namespace NinjaTrader.NinjaScript.Strategies
            {
            public class omermirza : Strategy
            {
            private HMA HMA1;
            private HMA HMA2;

            protected override void OnStateChange()
            {
            if (State == State.SetDefaults)
            {
            Description = @"Enter the description for your new custom Strategy here.";
            Name = "omermirza";
            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)
            {
            }
            else if (State == State.DataLoaded)
            {
            HMA1 = HMA(Close, 13);
            HMA2 = HMA(Open, 13);
            HMA1.Plots[0].Brush = Brushes.Goldenrod;
            HMA2.Plots[0].Brush = Brushes.DeepPink;
            AddChartIndicator(HMA1);
            AddChartIndicator(HMA2);
            }
            }

            protected override void OnBarUpdate()
            {
            if (BarsInProgress != 0)
            return;

            if (CurrentBars[0] < 1)
            return;

            // Set 1
            if (CrossAbove(HMA1, HMA2, 1) == true)
            {
            EnterLong(Convert.ToInt32(DefaultQuantity), "");
            }

            // Set 2
            if (CrossBelow(HMA1, HMA2, 1) == true)
            {
            EnterShort(Convert.ToInt32(DefaultQuantity), "");
            }

            }
            }
            }

            NT-Roland

            Comment


              #7
              Originally posted by NT-Roland View Post
              Hi Omer,
              Did you build the logic in StrategyBuilder or NinjaScript?
              Small differences can have huge impact. Please always start with the StrategyBuilder until you are fully familiar with Ninja code logic.
              The below (StrategyBuilder only) works just fine at my end.

              //This namespace holds Strategies in this folder and is required. Do not change it.
              namespace NinjaTrader.NinjaScript.Strategies
              {
              public class omermirza : Strategy
              {
              private HMA HMA1;
              private HMA HMA2;

              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = @"Enter the description for your new custom Strategy here.";
              Name = "omermirza";
              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)
              {
              }
              else if (State == State.DataLoaded)
              {
              HMA1 = HMA(Close, 13);
              HMA2 = HMA(Open, 13);
              HMA1.Plots[0].Brush = Brushes.Goldenrod;
              HMA2.Plots[0].Brush = Brushes.DeepPink;
              AddChartIndicator(HMA1);
              AddChartIndicator(HMA2);
              }
              }

              protected override void OnBarUpdate()
              {
              if (BarsInProgress != 0)
              return;

              if (CurrentBars[0] < 1)
              return;

              // Set 1
              if (CrossAbove(HMA1, HMA2, 1) == true)
              {
              EnterLong(Convert.ToInt32(DefaultQuantity), "");
              }

              // Set 2
              if (CrossBelow(HMA1, HMA2, 1) == true)
              {
              EnterShort(Convert.ToInt32(DefaultQuantity), "");
              }

              }
              }
              }

              NT-Roland
              I get the attached error when trying to compile.

              Attached Files

              Comment


                #8
                Hello Omer,

                This strategy appears to have been created with the Strategy Builder.

                This error would be unexpected for a strategy that is still locked for editing in the Strategy Builder and has not been unlocked.

                This may be due to the using statements being altered.

                Is this strategy still locked for editing in the Strategy Builder?

                Please try excluding this script and creating a new strategy in the strategy builder with this logic. Does the new strategy also have this compile error?
                https://ninjatrader.com/support/help...romCompilation

                If a new strategy that is locked for editing in the Strategy Builder also has this error, we may be looking at missing system files and I would want to schedule a call with you to investigate.


                Regarding a strategy that is not producing results:

                If the strategy is applied to a chart, is there data with new bars appearing on the chart the Strategy is applied to?
                Is the strategy showing as enabled on the Strategies tab of the Control Center?
                If the strategy is in the Strategy Analyzer, is there data appearing on the Chart Display of the Strategy Analyzer?
                Are there errors appearing on the Log tab of the Control Center?

                If the strategy is getting data and the strategy is enabled or a backtest is run with no errors in the Log tab of the Control Center, then would likely indicate the logic conditions did not evaluate as true or orders are being ignored or cancelled.
                In order to better understand how the code is working, it will be necessary to use Print and enable TraceOrders to see how the conditions are evaluating and if orders are being submitted, ignored, or cancelled.

                Below is a link to a forum post that demonstrates using prints to understand behavior.
                https://ninjatrader.com/support/foru...121#post791121

                Enable TraceOrders, print the time of the bar and all values used in the conditions that submit entry orders.
                Let me know if you need any assistance creating a print or enabling TraceOrders.
                Save the output from the output window to a text file and provide this with your reply.
                I'll be happy to assist with analyzing the output.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by omermirza View Post

                  I get the attached error when trying to compile.
                  Hello omermirza,
                  I have created a sample HMA cross over strategy for you, it plots on chart & runs in Strategy Analyzer as well, you get it here SampleHMAcross.zip
                  In case you're still getting the compile errors, please go to to NinjaScript Editor & look for test.cs, this could be another indicator / strategy you might have created earlier which is now giving the error, it can be fixed easily you just need to open it & check your code in line no. 6 & 7. I guess here you would have given period by mistake. Hope it helps!

                  Comment


                    #10
                    Originally posted by s.kinra View Post

                    Hello omermirza,
                    I have created a sample HMA cross over strategy for you, it plots on chart & runs in Strategy Analyzer as well, you get it here [ATTACH]n1124543[/ATTACH]
                    In case you're still getting the compile errors, please go to to NinjaScript Editor & look for test.cs, this could be another indicator / strategy you might have created earlier which is now giving the error, it can be fixed easily you just need to open it & check your code in line no. 6 & 7. I guess here you would have given period by mistake. Hope it helps!
                    Thank you so much for this - it works perfectly!!!

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello Omer,

                      This strategy appears to have been created with the Strategy Builder.

                      This error would be unexpected for a strategy that is still locked for editing in the Strategy Builder and has not been unlocked.

                      This may be due to the using statements being altered.

                      Is this strategy still locked for editing in the Strategy Builder?

                      Please try excluding this script and creating a new strategy in the strategy builder with this logic. Does the new strategy also have this compile error?
                      https://ninjatrader.com/support/help...romCompilation

                      If a new strategy that is locked for editing in the Strategy Builder also has this error, we may be looking at missing system files and I would want to schedule a call with you to investigate.


                      Regarding a strategy that is not producing results:

                      If the strategy is applied to a chart, is there data with new bars appearing on the chart the Strategy is applied to?
                      Is the strategy showing as enabled on the Strategies tab of the Control Center?
                      If the strategy is in the Strategy Analyzer, is there data appearing on the Chart Display of the Strategy Analyzer?
                      Are there errors appearing on the Log tab of the Control Center?

                      If the strategy is getting data and the strategy is enabled or a backtest is run with no errors in the Log tab of the Control Center, then would likely indicate the logic conditions did not evaluate as true or orders are being ignored or cancelled.
                      In order to better understand how the code is working, it will be necessary to use Print and enable TraceOrders to see how the conditions are evaluating and if orders are being submitted, ignored, or cancelled.

                      Below is a link to a forum post that demonstrates using prints to understand behavior.
                      https://ninjatrader.com/support/foru...121#post791121

                      Enable TraceOrders, print the time of the bar and all values used in the conditions that submit entry orders.
                      Let me know if you need any assistance creating a print or enabling TraceOrders.
                      Save the output from the output window to a text file and provide this with your reply.
                      I'll be happy to assist with analyzing the output.
                      Thank you for your support. S. Kinra was kind enough to fix the strategy for me. I will keep note of these points should I have any errors in the future.

                      Thanks,

                      -Omer

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Hyper, Today, 05:23 PM
                      0 responses
                      4 views
                      0 likes
                      Last Post Hyper
                      by Hyper
                       
                      Started by ballboy11, Today, 05:20 PM
                      0 responses
                      4 views
                      0 likes
                      Last Post ballboy11  
                      Started by Hyper, Today, 03:02 PM
                      1 response
                      16 views
                      0 likes
                      Last Post NinjaTrader_ChrisL  
                      Started by aworldnervelink, Yesterday, 11:55 AM
                      7 responses
                      41 views
                      0 likes
                      Last Post NinjaTrader_Kate  
                      Started by PhilT, Today, 02:18 PM
                      4 responses
                      19 views
                      0 likes
                      Last Post PhilT
                      by PhilT
                       
                      Working...
                      X