Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

New strat doesn't appear in Analyzer

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

    New strat doesn't appear in Analyzer

    I have created this code to trade on MA crossover and MACD. I get no complie errors but it does not appear in the Analyzer.

    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 TrendTrading : Strategy
        {
            private SMA SMA1;
            private SMA SMA2;
            private MACD MACD1;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                 = @"Enter the description for your new custom Strategy here.";
                    Name                                        = "TrendTrading";
                    Calculate                                   = Calculate.OnEachTick; // OnBarClose can be changed to OnEachTick or OnEachPriceChange
                    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;
                    AddDataSeries(Data.BarsPeriodType.Minute, 15);
                    IsInstantiatedOnEachOptimizationIteration   = true;
                    FastMA                                      = 10;
                    SlowMA                                      = 100;
                }
                else if (State == State.Configure)
                {
                }
                else if (State == State.DataLoaded)
                {                
                    SMA1                = SMA(Close, Convert.ToInt32(FastMA));
                    SMA2                = SMA(Close, Convert.ToInt32(SlowMA));
                    MACD1               = MACD(Closes[1], 12, 26, 9); // Use the 15-minute bars
                    SetProfitTarget("", CalculationMode.Pips, 100);
                    SetStopLoss("", CalculationMode.Pips, 50, false);
                }
            }
    
            protected override void OnBarUpdate()
            {
                // Ignore the first update to avoid any indexing issues
                if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
                    return;
    
                // Ensure we are processing the primary bars series (the default bars series)
                if (BarsInProgress == 0)
                {
                    // Combined trend trading and MACD logic using 15-minute MACD
                    if (CrossAbove(SMA1, SMA2, 1)
                        && MACD1[1] > 0
                        && Times[0][0].TimeOfDay >= new TimeSpan(14, 30, 0)
                        && Times[0][0].TimeOfDay < new TimeSpan(22, 0, 0))
                    {
                        EnterLong(Convert.ToInt32(DefaultQuantity), "");
                    }
                    
                    if (CrossBelow(SMA1, SMA2, 1)
                        && MACD1[1] < 0
                        && Times[0][0].TimeOfDay >= new TimeSpan(14, 30, 0)
                        && Times[0][0].TimeOfDay < new TimeSpan(22, 0, 0)
                        && MACD1.Avg[1] > 12)
                    {
                        EnterShort(Convert.ToInt32(DefaultQuantity), "");
                    }
                }
            }
    
            #region Properties
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="FastMA", Order=1, GroupName="Parameters")]
            public int FastMA
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="SlowMA", Order=2, GroupName="Parameters")]
            public int SlowMA
            { get; set; }
            #endregion
        }
    }
    
    ​

    #2
    Hello GodAtum,

    Welcome to the NinjaTrader forums!

    I am seeing the calling to AddDataSeries() is in State.SetDefaults which is likely causing a run time error appearing on the Log tab of the Control Center.

    From the help guide:

    "Warning:
    This method should ONLY be called from the OnStateChange() method during State.Configure​"


    Please try moving this to State.Configure, compile, then open the Strategies window and check the Log tab of the Control Center for any run time errors.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea, thank you so much for your help, that fixed it.

      Now it dones't look like it's using the 15 min timeframe for the MACD. I'm looking to use the 1 min for the main start, but 15 min for the MACD.

      Comment


        #4
        Hello GodAtum,

        That part looks correct.

        MACD1 = MACD(Closes[1], 12, 26, 9); // Use the 15-minute bars

        The MACD is using Closes[1] which would be the 15 minute series.

        if (BarsInProgress == 0)

        The logic is being processed when the primary chart series updates.

        if (CurrentBars[0] < 1 || CurrentBars[1] < 1)

        You've even correctly ensured all series have a bar, which is extra points!
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks for confirming. Is there a way to add the MACD 15 min onto the Analyzer chart so I can visually see?

          Comment


            #6
            Hello GodAtum,

            Unfortunately, this would not be possible as this is using an input series.

            "Notes
            An indicator being added via AddChartIndicator() cannot use any additional data series hosted by the calling strategy, but can only use the strategy's primary data series. If you wish to use a different data series for the indicator's input, you can add the series in the indicator itself and explicitly reference it in the indicator code (please make sure though the hosting strategy has the same AddDataSeries() call included as well)​"


            It would be necessary to redesign the MACD to internally call AddDataSeries(), and set the values to the plot on the primary series for this to work.
            An example of this is below.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thank you for the info. Looking at the backtest, shouldn't there be a Sell trade at 16:26 BST on the MNQ? As the 10 SMA crosses below the 100 SMA and the MACD is negative.

              Comment


                #8
                Hello GodAtum,

                To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

                In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.
                The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.
                The debugging print output should clearly show what the condition is, what time the conditions are being compared, all values being compared, and how they are being compared.

                Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

                Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

                I am happy to assist you with analyzing the output from the output window.

                Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

                Below is a link to a support article that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.


                Please let me know if I may further assist with analyzing the output or if you need any assistance creating a print or enabling TraceOrders.​
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thank for. The Output window gives me:

                  Code:
                  Time: 20/06/2024 16:25:00
                  SMA1: 20199.35
                  SMA2: 20196.6675
                  MACD1: -31.143657607081
                  MACD1.Avg: -20.8569396571622
                  Time: 20/06/2024 16:26:00
                  SMA1: 20197.325
                  SMA2: 20195.975
                  MACD1: -31.143657607081
                  MACD1.Avg: -20.8569396571622
                  Time: 20/06/2024 16:27:00
                  SMA1: 20195.1
                  SMA2: 20195.28
                  MACD1: -31.143657607081
                  MACD1.Avg: -20.8569396571622​
                  ​
                  So it looks like the SMA cross over?

                  Comment


                    #10
                    Hello GodAtum,

                    The condition we are investigating is:

                    (CrossBelow(SMA1, SMA2, 1)
                    && MACD1[1] < 0
                    && Times[0][0].TimeOfDay >= new TimeSpan(14, 30, 0)
                    && Times[0][0].TimeOfDay < new TimeSpan(22, 0, 0)
                    && MACD1.Avg[1] > 12)​

                    Is this correct?

                    An informative print for this would be:

                    Print(string.Format("{0} | SMA1[1]: {1} > SMA2[1]: {2} && SMA1[0]: {3} < SMA2[0]: {4} && MACD1[1]: {5} < 0 && Times[0][0].TimeOfDay: {6} >= {7} && Times[0][0].TimeOfDay: {6} < {8} && MACD1.Avg[1]: {9} > 12", Time[0], SMA1[1], SMA2[1], SMA1[0], SMA2[0], MACD1[1], Times[0][0].TimeOfDay, new TimeSpan(14, 30, 0), new TimeSpan(22, 0, 0), MACD1.Avg[1] ));

                    May I have you try printing this and providing the output?
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Thank you, my output is

                      Code:
                      20/06/2024 16:25:00 | SMA1[1]: 20201 > SMA2[1]: 20197.295 && SMA1[0]: 20199.35 < SMA2[0]: 20196.6675 && MACD1[1]: -31.143657607081 < 0 && Times[0][0].TimeOfDay: 16:25:00 >= 14:30:00 && Times[0][0].TimeOfDay: 16:25:00 < 22:00:00 && MACD1.Avg[1]: -20.8569396571622 > 12
                      20/06/2024 16:26:00 | SMA1[1]: 20199.35 > SMA2[1]: 20196.6675 && SMA1[0]: 20197.325 < SMA2[0]: 20195.975 && MACD1[1]: -31.143657607081 < 0 && Times[0][0].TimeOfDay: 16:26:00 >= 14:30:00 && Times[0][0].TimeOfDay: 16:26:00 < 22:00:00 && MACD1.Avg[1]: -20.8569396571622 > 12
                      20/06/2024 16:27:00 | SMA1[1]: 20197.325 > SMA2[1]: 20195.975 && SMA1[0]: 20195.1 < SMA2[0]: 20195.28 && MACD1[1]: -31.143657607081 < 0 && Times[0][0].TimeOfDay: 16:27:00 >= 14:30:00 && Times[0][0].TimeOfDay: 16:27:00 < 22:00:00 && MACD1.Avg[1]: -20.8569396571622 > 12​

                      Comment


                        #12
                        Hello GodAtum,

                        May I confirm you have also set TraceOrders = true; in State.Configure? (If set in State.SetDefaults remove the instance from the Configured list and add a new instance from the Available list to pull the new defaults)

                        I'm seeing the MACD.Avg[1], with a value of -20, was not greater than 12, so this condition did not evaluate as true.

                        20/06/2024 16:26:00 | SMA1[1]: 20199.35 > SMA2[1]: 20196.6675 && SMA1[0]: 20197.325 < SMA2[0]: 20195.975 && MACD1[1]: -31.143657607081 < 0 && Times[0][0].TimeOfDay: 16:26:00 >= 14:30:00 && Times[0][0].TimeOfDay: 16:26:00 < 22:00:00 && MACD1.Avg[1]: -20.8569396571622 > 12

                        Chelsea B.NinjaTrader Customer Service

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by JackyP, 07-25-2023, 08:39 AM
                        3 responses
                        317 views
                        0 likes
                        Last Post Lancer
                        by Lancer
                         
                        Started by TiffanyMiller, Yesterday, 10:19 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post TiffanyMiller  
                        Started by algospoke, 05-13-2024, 06:53 PM
                        12 responses
                        138 views
                        0 likes
                        Last Post algospoke  
                        Started by psangram, Yesterday, 06:35 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post psangram  
                        Started by Alaina19, 07-14-2024, 04:38 PM
                        2 responses
                        17 views
                        0 likes
                        Last Post Alaina19  
                        Working...
                        X