Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multi-Time Frame code issue.

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

    #16
    Here is the code.

    #region Variables
    privateint myInput0 = 1; // Default setting for MyInput0
    #endregion
    bool HasTradedLong = false;


    protectedoverridevoid Initialize()
    {
    Add(PeriodType.Minute, 60);

    SetProfitTarget(50);
    SetStopLoss(45,false);

    CalculateOnBarClose = true;
    }


    protectedoverridevoid OnBarUpdate()

    {
    if (Bars.FirstBarOfSession)
    {
    HasTradedLong = false;
    }

    if (BarsInProgress != 0)
    return;


    // Condition set 1
    if (Closes[1][0] > SMA(BarsArray[1], 200)[0]
    && !HasTradeLong);

    {
    HasTradedLong = true;
    EnterLongLimit(1000, Close[0], "");
    }

    }


    Thanks Bertrand

    Comment


      #17
      Check your EntriesPerDirection setting on the Strategy Dialogue window when you run the strategy.
      Josh P.NinjaTrader Customer Service

      Comment


        #18
        Entries per direction is set to 1.

        Entries per direction is set to 1. There must be something else.

        Something in the code but I can't get a handle on it.

        Comment


          #19
          Hi strategy1, please put your BarsInProgress check at the start of the OnBarUpdate() in your code.

          Comment


            #20
            Still way to many trades.

            Although I already put it in the beginning of OnBarUpdate yesterday, I did it again now. Still showing too many trades.

            Also, the issue is not having more than 1 trade per direction but having more than 1 trade per direction per day.

            It's something else - probably something very small - but I have now spent 6 hours trying to make this work.

            Thanks

            Comment


              #21
              To create a strategy trading only once per day, you want to create a variable which is reset to 0 every day. Then set it to 1 when you get into a trade and you should get way fewer trades.

              Comment


                #22
                I already have that in my code.

                I already have that in my code. And it used to work prior to me adding the second timeframe.

                You requested I post my code - did you review it?

                Comment


                  #23
                  strategy1,

                  Move BarsInProgress to the top. From your code you are resetting it on every single process of your second time frame. You will need to post every rendition of your code because we do not know exactly what it looks like and if you've made the proper changes or not.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #24
                    Moved it around - still doesn't work

                    Do you mean to move it to the top of OnBarUpdate - I did that already. I have also tried moving it to the area of variable declaration and it still doesn't work.

                    Also, this just started to happen now - when I open the backtest dialogue it displays not the last strategy I had backtested, but the first in alphabetical order. Then, when I change it to the one I need - the data in the box doesn't change. Can there be some kind of corruption?

                    Comment


                      #25
                      Yes, move it to the start of your OnBarUpdate().

                      If you feel your database got corrupt, please disconnect from all feeds and go to Tools > Options > Misc and Repair your DB.

                      You can also export us the strategy as a zip file and we will take a look. Thanks!

                      Comment


                        #26
                        Here is the entire code

                        I have already moved it to the start of the OnBarUpdate two posts ago, as per your suggesion. Also I have done it numerous times yesterday, putting it in various different places in the code.

                        I am not sure how to zip it out, but here is the 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 Test : Strategy
                        {
                        #region Variables
                        // Wizard generated variables
                        private int myInput0 = 1; // Default setting for MyInput0
                        // User defined variables (add any user defined variables below)
                        #endregion
                        bool HasTradedLong = false;

                        /// <summary>
                        /// This method is used to configure the strategy and is called once before any strategy method is called.
                        /// </summary>
                        protected override void Initialize()
                        {
                        Add(PeriodType.Minute, 60);

                        Add(VOL());

                        Add(SMA(15));
                        SMA(15).Plots[0].Pen.Color = Color.Yellow;
                        SMA(15).Plots[0].Pen.Width = 2;

                        Add(SMA(30));
                        SMA(30).Plots[0].Pen.Color = Color.Cyan;
                        SMA(30).Plots[0].Pen.Width = 2;


                        SetProfitTarget(50);
                        SetStopLoss(40,false);


                        CalculateOnBarClose = true;

                        }


                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>

                        protected override void OnBarUpdate()

                        {
                        if (BarsInProgress != 0)
                        return;


                        if (Bars.FirstBarOfSession)
                        {
                        HasTradedLong = false;
                        Print(HasTradedLong);
                        }



                        // Condition set 1

                        if (Closes[1][0] > SMA(BarsArray[1], 100)[0]
                        && !HasTradedLong);

                        {
                        HasTradedLong = true;

                        EnterLongLimit(100, Close[0], "");

                        }


                        }

                        }

                        }

                        Comment


                          #27
                          Are you loading new instances of the strategy? Please ensure you are doing this instead of just refreshing.

                          To zip it out please go to File->Utilities->Export NinjaScript and then attach the file here.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #28
                            Here is the zip.

                            "Are you loading new instances of the strategy? Please ensure you are doing this instead of just refreshing."

                            I am not sure what you mean by this. Every time I make a change, I compile, then backtest. Sometimes I also save prior to backtesting. Is this wrong?

                            The code is attached.
                            Attached Files

                            Comment


                              #29
                              What time frame are you running on for primary bars?
                              Josh P.NinjaTrader Customer Service

                              Comment


                                #30
                                5 minute

                                Sorry,
                                I should have included it with the previous post. Time frame for primary bars is 5 minutes

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                597 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                343 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                103 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                556 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                555 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X