Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to incorporate previous day's close into a strategy

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

    How to incorporate previous day's close into a strategy

    I've created a strategy for 1 minute charts. Assume that it is for a stock. The strategy is both multi instrument and multi period. These are the items added to the charts

    Add("NQ 03-15", PeriodType.Minute, 1);
    Add("NQ 03-15", PeriodType.Minute, 5);
    Add(PeriodType.Minute, 3);
    Add(PeriodType.Day, 1);
    Add("^COMP", PeriodType.Day, 1);
    Add("^COMP", PeriodType.Minute, 1);

    What I'm trying to do is add a line to the code which compares the stock's performance relative to the Nasdaq Index. So for example if the strategy is to trigger a long trade I want the stock to be outperforming the market. To do this I created the following formula which will compare the stocks performance measured by its close the previous day to the Nassaq's performance measured by it's close the previous day. Both are measured in percentage




    && ((Close[0]-Closes[4][1])/Closes[4][1])*100>((Closes[6][0]-Closes[5][1])/Closes[5][1])*100


    So for example let's assume it's today and the stock is trading at 30.50 at 9:35. Assume also that it closed yesterday at 30.00 The first half of the formula should yield the following:


    (30.50 - 30)/30*100 =1.6667




    Assume the nasdaq yesterday closed at 4937. It is currently at 4928.
    the second half of the formula would yield as follows:


    4928-4937/4937*100= -.18229

    Since the first portion of the formula is greater than the second the statement would be true. When I enable the strategy though this line of code is not working. I have the strategy with and without this line and can compare the two and see when it should be working - and it is not.




    The problem I'm getting is referencing the previous day's close for both the stock (I.E. "Closes[4][1]") and Nasdaq ("Closes[5][1]"). Apparenly when you add a dayperiod time frame, the current day is not "Bar[0]" until the day closes so a reference to Bar[1] during the day is invalid. If this is so, how do you ever reference the previous day's close in a formula during the day you are running the chart. So if it's Wednesday in the middle of the day, how do you ever refer to the Tuesday close since the Wednesday bar only becomes Bar[0] at Wednesday's close.

    I don't know if that makes sense but it anyone can help I would appreciate it.

    #2
    Hello mdb4403,

    You will still reference Bar[0] as this will be the last closed bar when CalculateOnBarClose is set to true

    When you set CalculateOnBarClose then the current day information becomes Bar[0] and yesterdays is Bar[1]

    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      So if you look at my current formula


      && ((Close[0]-Closes[4][1])/Closes[4][1])*100>((Closes[6][0]-Closes[5][1])/Closes[5][1])*100

      would changing it to this work


      && ((Close[0]-Closes[4][0])/Closes[4][0])*100>((Closes[6][0]-Closes[5][0])/Closes[5][0)*100


      So if it is the middle of the day Thursday the reference to Closes[4][0] and Closes[5][0] would refer to Wednesday's close?

      I am using the Onbarclose method

      Comment


        #4
        mdb4403,

        Your understanding is correct.
        Cal H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        633 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        364 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        567 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        568 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X