Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Help With Strategy

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

    Help With Strategy

    Hello,

    Here is the problem I'm trying to solve:

    I have 2 periods in my strategy: daily and minute.

    Every day (in the MINUTE period), right before market close (ie 2:15pm) I'd like to compute the next value of the DAILY MACD chain, ie. compute MACD based off all prior daily values and then the values at 2:15pm instead of waiting for the session close at 2:30pm. That way I could exit a position before waiting for the next morning.

    I cannot use CalculateOnBarClose=false as other parts of my strategy rely on keeping that true.

    I am very able with C# and have worked some with Bars/IBar etc, so I just need to know the easiest way to do this.

    Thanks, help is much appreciated.




    EDIT:

    Here is what I've tried:

    Code:
    	Bars test =BarsArray[dailyBars];
    							int index=0;
    							DateTime lastTime = test.Get(0).Time;
    					
    								
    								for (int i=0; i<test.Count; i++)
    								{
    									if (test.Get(i).Time > lastTime && test.Get(i).Time<Time[0] && test.Get(i).Time.Day!=Time[0].Day)
    									{
    										lastTime = test.Get(i).Time;
    										index=i;
    									}
    									
    								}
    							
    							Bars evalBars = new Bars(test.Instrument,test.Period,test.From,Time[0],test.SplitAdjusted,test.DividendAdjusted);
    							for (int i=0; i<index; i++)
    								{
    									evalBars.Add(test.Get(i).Open,test.Get(i).High,test.Get(i).Low,test.Get(i).Close,test.Get(i).Time,test.Get(i).Volume,1,false);
    								}
    								
    											
    							IBar sampleBar = test.Get(index);
    											
    							Print("LAST: OPEN: " + sampleBar.Open + " HIGH: " + sampleBar.High + " LOW: " + sampleBar.Low + " CLOSE: " + sampleBar.Close + " TIME: " +sampleBar.Time.ToString() + " VOL: "+sampleBar.Volume); 
    							Print("NEW: OPEN: "+ CurrentDayOHL().CurrentOpen[0] + " HIGH: " + CurrentDayOHL().CurrentHigh[0] + " LOW: " + CurrentDayOHL().CurrentLow[0] + " CLOSE: " + Close[0] + " TIME: " +Time[0].ToString() + " VOL: "+volumeCount);
    						    Print("EVAL COUNT: " + evalBars.Count);
    						    Print("Daily COUNT: " + test.Count);
    								
    							evalBars.Add(CurrentDayOHL().CurrentOpen[0],CurrentDayOHL().CurrentHigh[0],CurrentDayOHL().CurrentLow[0],Close[0],Time[0],volumeCount,1,false);

    Then use MACD(evalBars,etc,etc,etc) to compare...
    Last edited by JohnnyChimpo; 06-27-2011, 12:22 PM.

    #2
    JohnnyChimpo,

    The easiest way is to use CalculateOnBarClose = false. You can use this despite your requirement to use true in some parts of your code. What you can do is set it to false and then add some if-statements around the areas of code that you want it to be running in true mode.

    Code:
    if (FirstTickOfBar)
    {
         if (Close[1] > Open[1])
               // Do something;
    }
    The opening event of a bar is the exact same event as the closing event of a bar. This means on the very first tick of a bar, our prior bar just closed so if we used [1] index instead of [0] we will get the values of the bar that just closed. This is the exact same equivalent of CalculateOnBarClose = true.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Josh, I'll implement that but one more question, how can I backtest CalculateOnBarClose=false, for example if my primary series period is set to 1 minute, and i set calculateonbarclose=false, when backtesting and I'm in my minute bars period ie:

      protected override void OnBarUpdate()
      {

      if (BarsInProgress == minuteBars)
      {
      // if I CHECK the MACD[dailyBars][0] will I get the up-to-minute result? or will it still be yesterdays?
      }

      }

      Comment


        #4
        Unfortunately you cannot backtest CalculateOnBarClose = false.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Ugh, no work around? That's why I was attempting to create my own set of Bars. I really need the ability to back test and optimize strategies on large amounts of historical data. Is there anything else you can think of?

          Comment


            #6
            Probably easiest would be to add a secondary Bar series of a lower time frame and trade close to the close of the day? Maybe just have it trade at 2:14:59 or something like that.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Josh View Post
              Probably easiest would be to add a secondary Bar series of a lower time frame and trade close to the close of the day? Maybe just have it trade at 2:14:59 or something like that.
              I thought about that, but wouldn't that mean it would be using incorrect values for prior days? Like it would be using the prior days values at 2:14:59 instead of the actual close for those days? I want to use the actual closes for past days and the 2:14:59 value for the current day.

              Comment


                #8
                For prior days you can just use PriorDayOHLC() and it would always be accurate values.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  Josh,

                  I think I still have a problem though, so lets say I have 2 MACD indicators now:

                  MACD(DailyPeriod)

                  and

                  MACD(slightyLessThanDailyPeriod)


                  The MACD(slightlyLessThanDailyPeriod) will still be using the wrong close for all prior days right? So the PriorDayOHLC() thing doesn't really help me in an indicator? Unless I'm understanding wrong.

                  Thanks again.

                  Comment


                    #10
                    You can't call MACD at all. That would require you to run it on a DataSeries which you just don't have one with the values you are looking for. Sure you can add a daily bars object to your script and run MACD on that Bars object, but that doesn't solve your issue with the current day. This means you just need to mimic the logic and just run it through values you do have which can be acquired through PriorDayOHLC() and your lower time frame series for the current day.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      This means you just need to mimic the logic
                      Is it possible to look at the MACD indicator source code somewhere?

                      Comment


                        #12
                        You should be able to open up the code for any of the indicators by going Edit NinjaScript > Indicators and then selecting the one you want to look at.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_Josh View Post
                          You should be able to open up the code for any of the indicators by going Edit NinjaScript > Indicators and then selecting the one you want to look at.
                          I looked at this and it seems super complex, especially when dealing with multiple periods on say the Stochastics indicator. Is there any unofficial work around for simulating CalculateOnBarClose=false ? I'm desperate at this point.

                          Comment


                            #14
                            You would need to recode the logic needed based on a much lower granular series - for example a 100 Period SMA on 1 min chart approximates a 20 Period one on a 5 min chart.

                            Comment


                              #15
                              Originally posted by NinjaTrader_Bertrand View Post
                              You would need to recode the logic needed based on a much lower granular series - for example a 100 Period SMA on 1 min chart approximates a 20 Period one on a 5 min chart.
                              Hi Bertrand,

                              Thanks for the reply. I've been spinning my wheels on this for the past couple days. With that solution I think we'd still have this problem:

                              Say we have a daily period and a minute period (the lower granular series).

                              Say we set the interval of the minute period to 385 minutes (5 minutes before close, just under 6.5 hours which is the daily period in my template). If I compute the MACD of the minute period, MACD will use past values from the lower granular series (5 minutes prior to close) and won't use the actual close/high/low/volume values from previous days.

                              In a nutshell, here's the problem:

                              I want to simulate CalculateOnBarClose=false so that, 5 minutes before market close I want to compute the next value of my daily MACD so I can exit if necessary before the next day gaps down.

                              ^^ There has to be some way to solve this!

                              Pretty straight forward problem but I cannot figure out any way to replicate the daily bars and insert a new one etc. (you can add/remove to the end of the bars array but not insert a bar into the correct position)

                              I love your software but this is a major limitation to backtesting and it would take forever to gather enough data live to see if this is working correctly or not.

                              Thanks again, appreciate the brainstorming.
                              Last edited by JohnnyChimpo; 06-29-2011, 09:56 AM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              650 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              370 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              577 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X