Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 ZenCortexAuCost, Today, 04:22 AM
        0 responses
        0 views
        0 likes
        Last Post ZenCortexAuCost  
        Started by SantoshXX, Today, 03:09 AM
        0 responses
        13 views
        0 likes
        Last Post SantoshXX  
        Started by DanielTynera, Today, 01:14 AM
        0 responses
        2 views
        0 likes
        Last Post DanielTynera  
        Started by yertle, 04-18-2024, 08:38 AM
        9 responses
        42 views
        0 likes
        Last Post yertle
        by yertle
         
        Started by techgetgame, Yesterday, 11:42 PM
        0 responses
        14 views
        0 likes
        Last Post techgetgame  
        Working...
        X