Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy does not start

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

    Strategy does not start

    Heeeelp !
    The strategy takes off at the beginning of the current code :

    protected override void OnBarUpdate()
    {

    // Enter for 1st Enter
    if (//Bars.BarsSinceSession < 50 &&
    N_3 == 1 &&
    Close[0] > Open[0] &&
    CrossAbove(SMA(7), SMA(14), 2)
    )

    {
    N=1;
    SetProfitTarget("", CalculationMode.Ticks, TP);
    SetStopLoss("", CalculationMode.Ticks, SL, false);
    EnterLong(N, "");
    N_3 = 2;
    }

    // Enter Long if LastWin
    if (//Bars.BarsSinceSession > 50 &&
    N_3 != 1 &&
    Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency > 0 &&
    Close[0] > Open[0] &&
    Position.MarketPosition != MarketPosition.Long &&
    Position.MarketPosition != MarketPosition.Short &&
    CrossAbove(SMA(7), SMA(14), 2)
    )
    {
    N=1;
    SetProfitTarget("", CalculationMode.Ticks, TP);
    SetStopLoss("", CalculationMode.Ticks, SL, false);
    EnterLong(N, "");
    }


    It compiles okay.
    N_3 =1 (int)
    If make via : Bars.BarsSinceSession < 50
    and here and there , all running at 50 pc , at < 40 - no.
    I do not want to lose the bars since the beginning of the session.
    What 's the problem ?

    #2
    Hello,

    I am sorry but I do not fully understand the question.

    You said:

    If make via : Bars.BarsSinceSession < 50
    and here and there , all running at 50 pc , at < 40 - no.
    I do not want to lose the bars since the beginning of the session.
    Is the condition you created not becoming true when you think it should?

    Could you clarify what you are asking?

    I look forward to being of further assistance.

    Comment


      #3
      If I make like this:
      ________________________________
      protected override void OnBarUpdate()
      {
      if (N_3 == 1 &&
      Close[0] > Open[0] &&
      CrossAbove(SMA(7), SMA(14), 2))
      {
      N=1;
      SetProfitTarget("", CalculationMode.Ticks, TP);
      SetStopLoss("", CalculationMode.Ticks, SL, false);
      EnterLong(N, "");
      N_3 = 2;
      }

      if (N_3 != 1 &&
      Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency > 0 &&
      Close[0] > Open[0] &&
      Position.MarketPosition != MarketPosition.Long &&
      Position.MarketPosition != MarketPosition.Short &&
      CrossAbove(SMA(7), SMA(14), 2))
      {
      N=1;
      SetProfitTarget("", CalculationMode.Ticks, TP);
      SetStopLoss("", CalculationMode.Ticks, SL, false);
      EnterLong(N, "");
      }
      _____________________________________________

      It does not work at all. Compalining, but not starts.
      Why?
      I just need to make 1st trade close to session start.

      Comment


        #4
        Cause all next trades realized over conditions

        Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency > 0
        or
        Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency < 0

        Comment


          #5
          Hello,

          Thank you for the reply.

          To know why a condition does not become true, you may need to add Print(); statements to print information on why that is not happening.

          An example could be:

          Code:
          Print("N_3 " + N_3 + " &&  " + Close[0] + " > " + Open[0] + " && " + CrossAbove(SMA(7), SMA(14), 2));
          
          if (N_3 == 1 && 
          Close[0] > Open[0] &&
          CrossAbove(SMA(7), SMA(14), 2))
          If you print the value to the Tools -> Output Window, you can see why the condition is not happening on a specific bar to know why.

          I look forward to being of further assistance.

          Comment


            #6
            Tnx, it's realy comfort to seen reason.

            N_3 1 && 2165,5 > 2165,5 && False
            N_3 1 && 2165,5 > 2165,5 && False
            N_3 1 && 2166 > 2165,5 && True
            **NT** Error on calling 'OnBarUpdate' method for strategy 'SMAMarti/928847aff6124a84bfcf4ec3637ce286': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

            PHP Code:
            protected override void OnBarUpdate()
            {
            if (N_3 == 1 && 
            Close[0] > Open[0] &&
            CrossAbove(SMA(7), SMA(14), 2))
            {
            N=1;
            SetProfitTarget("", CalculationMode.Ticks, TP);
            SetStopLoss("", CalculationMode.Ticks, SL, false);
            EnterLong(N, ""); 
            N_3 = 2;
            } 
            
            All the rest code is unactive.

            But I do not understand what bars he means?

            Comment


              #7
              Hello,

              Thank you for the reply.

              This specific message:

              **NT** Error on calling 'OnBarUpdate' method for strategy 'SMAMarti/928847aff6124a84bfcf4ec3637ce286': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

              May not be related to what you have provided, is this the entire OnBarUpdate region?

              This error comes when you use a BarsAgo that does not exist, assume you are on bar 0 and use 1 bar ago. This error would be generated as you need to wait until bar 1 to access 1 bar ago.

              Additionally this could happen if you are using a multi series script.

              If you can show the rest of the OnBarUpdate section it would be more apparent why this error is happening.

              I look forward to being of further assistance.

              Comment


                #8
                Sure, all code:
                PHP Code:
                #region Using declarations
                using System;
                using System.ComponentModel;
                using System.Diagnostics;
                using System.Drawing;
                using System.Drawing.Drawing2D;
                using System.Xml.Serialization;
                using NinjaTrader.Cbi;
                using NinjaTrader.Data;
                using NinjaTrader.Indicator;
                using NinjaTrader.Gui.Chart;
                using NinjaTrader.Strategy;
                #endregion
                
                // This namespace holds all strategies and is required. Do not change it.
                namespace NinjaTrader.Strategy
                {
                    /// <summary>
                    /// Enter the description of your strategy here
                    /// </summary>
                    [Description("Enter the description of your strategy here")]
                    public class SMAMarti : Strategy
                    {
                        #region Variables
                        // Wizard generated variables
                        private int tP = 5; // Default setting for TP
                        private int sL = 5; // Default setting for SL
                        private int wT = 1; // Default setting for WT
                        private int n = 1; // Default setting for N
                        private int n_1 = 1; // Default setting for N_1
                        private int n_2 = 1; // Default setting for N_2
                        private int n_3 = 1; // Default setting for N_3
                        // User defined variables (add any user defined variables below)
                        #endregion
                
                        /// <summary>
                        /// This method is used to configure the strategy and is called once before any strategy method is called.
                        /// </summary>
                        protected override void Initialize()
                        {
                            CalculateOnBarClose = true;
                            Add(SMA(7));
                            Add(SMA(14));
                        }
                
                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()
                        {
                            Print("N_3 " + N_3 + " &&  " + Close[0] + " > " + Open[0] + " && " + CrossAbove(SMA(7), SMA(14), 2));
                
                
                            // Enter for 1st Enter
                            if (Bars.BarsSinceSession > 23 &&
                                  N_3 == 1 &&    
                                     (Close[0] > Open[0]) &&
                                        CrossAbove(SMA(7), SMA(14), 2)
                                )
                            {
                            N=1;
                            SetProfitTarget("", CalculationMode.Ticks, TP);
                            SetStopLoss("", CalculationMode.Ticks, SL, false);
                            EnterLong(N, "");    
                            N_3 = 2;
                            }    
                    
                        }
                            
                            
                
                        #region Properties
                        [Description("")]
                        [GridCategory("Parameters")]
                        public int TP
                        {
                            get { return tP; }
                            set { tP = Math.Max(1, value); }
                        }
                
                        [Description("")]
                        [GridCategory("Parameters")]
                        public int SL
                        {
                            get { return sL; }
                            set { sL = Math.Max(1, value); }
                        }
                
                        [Description("")]
                        [GridCategory("Parameters")]
                        public int WT
                        {
                            get { return wT; }
                            set { wT = Math.Max(1, value); }
                        }
                
                        [Description("")]
                        [GridCategory("Parameters")]
                        public int N
                        {
                            get { return n; }
                            set { n = Math.Max(1, value); }
                        }
                
                        [Description("")]
                        [GridCategory("Parameters")]
                        public int N_1
                        {
                            get { return n_1; }
                            set { n_1 = Math.Max(1, value); }
                        }
                
                        [Description("")]
                        [GridCategory("Parameters")]
                        public int N_2
                        {
                            get { return n_2; }
                            set { n_2 = Math.Max(1, value); }
                        }
                
                        [Description("")]
                        [GridCategory("Parameters")]
                        public int N_3
                        {
                            get { return n_3; }
                            set { n_3 = Math.Max(1, value); }
                        }
                        #endregion
                    }
                } 
                
                N_3 1 && 2165,5 > 2165,5 && False
                N_3 1 && 2165,5 > 2165,5 && False
                N_3 1 && 2166 > 2165,5 && True
                **NT** Error on calling 'OnBarUpdate' method for strategy 'SMAMarti/928847aff6124a84bfcf4ec3637ce286': You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
                **NT** Enabling NinjaScript strategy 'SMAMarti/928847aff6124a84bfcf4ec3637ce286' : On starting a real-time strategy - StrategySync=WaitUntilFlat SyncAccountPosition=False EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=True/ triggering 30 before close Set order quantity by=Strategy ConnectionLossHandling=KeepRunning DisconnectDelaySeconds=10 CancelEntryOrdersOnDisable=False CancelExitOrdersOnDisable=True CalculateOnBarClose=True MaxRestarts=4 in 5 minutes
                N_3 2 && 2165,5 > 2165,5 && False
                N_3 2 && 2165,5 > 2165,5 && False
                ...

                Comment


                  #9
                  Hello,

                  Thank you for that.

                  I tried this on my end but do not get the same error nor do I see anything that should cause this error.

                  Could you try removing the instance of this strategy by going to the Control Center -> Strategy tab -> Right click the strategy and select Remove. Do this for all instances of the strategy.

                  Now re apply the strategy to the chart, do you get the same error?

                  If you do can you tell me what instrument you are using and timeframe?

                  I look forward to being of further assistance.

                  Comment


                    #10
                    Make all you say. Yes, same error(

                    ES 09-16 5M
                    02.08.2016

                    Comment


                      #11
                      sorry sorry sorry

                      You are right, all working!
                      Tnx tnx tnx!!!!

                      Comment


                        #12
                        Hello,

                        Thank you for trying that.

                        I am glad that was the resolution.

                        In certain cases, depending on that changes you make you may need to remove the instance of the strategy or indicator and then re add it. This is important for any Initialize changes.

                        I look forward to being of further assistance.

                        Comment


                          #13
                          demo148070

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          566 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          330 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          101 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          547 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          548 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X