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

Multi-Timeframe OnEachTick

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

    #16
    Thank you and understood, I will definitely stick to my lane going forward!

    Ok, I have a second issue: the method works great except that it updates the current bar on the :01 and :06 minutes and not the :00 and :05 minutes. I actually need this to shift to the left by one minute in order to align perfectly with the :00 and :05 changes for the normal historical bars. The update at :01 creates a problem because the :00 and :01 values are different, which leads to the potential for :59, :00 and :01 having three different values rather than :59 and :00 having two different values when I am doing a calculation that involves the historical bars and the current bar.

    Please find below a simple code with the current bar updating at :01 and :06. I also attach a picture. Could you please let me know if there's a solution to this problem?

    I truly appreciate your help!!!

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class A1TEST3 : Strategy
    {

    private Series<double> test;
    private DateTime cacheTime;
    private int cacheBar;
    private double barOpen;
    private double barHigh;
    private double barLow;
    private double barClose;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "A1TEST3";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.AdoptAccountPosition;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    AddPlot(Brushes.WhiteSmoke, "StrategyPlot");
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;


    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 5);

    }
    else if (State == State.DataLoaded)
    {
    test = new Series<double>(this, MaximumBarsLookBack.Infinite);
    //int cacheHigh = 0;
    //int cacheLow = 0;
    //barHigh = new Series<double>(this, MaximumBarsLookBack.Infinite);
    //barLow = new Series<double>(this, MaximumBarsLookBack.Infinite);
    Plots[0].Width = 4;
    }


    }
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 5
    || CurrentBars[1] < 5)
    return;

    if (cacheTime != Times[0][0])
    {
    cacheTime = Times[0][0];
    cacheBar = BarsArray[1].GetBar(Times[0][0]);
    }

    barOpen = BarsArray[1].GetOpen(cacheBar);
    barHigh = BarsArray[1].GetHigh(cacheBar);
    barLow = BarsArray[1].GetLow(cacheBar);
    barClose = BarsArray[1].GetClose(cacheBar);

    StrategyPlot[0] = (barOpen + barHigh + barLow + barClose) / 4;

    // Set 1
    if (Close[0] > Close[1])
    {
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), (Close[0] + (-2 * TickSize)) , "");
    }

    // Set 2
    if (Close[0] < Close[1])
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    }

    // Set 3

    }

    #region Properties

    public Series<double> StrategyPlot
    {
    get { return Values[0]; }
    }
    #endregion

    }
    }

    Comment


      #17
      Hello catinabag,

      Thanks for your reply.

      Keep in mind that bars are timestamped with the end of bar time stamp.

      What you are looking at would be a line that is drawn from one level to the next and the lines are draw from the center of one candle to another. I would suggest changing the plot style from line type to Cross as this will show the individual data points. To help see this I have attached a screenshot of a chart with 1 minute candlesticks and have overlaid 5 minute bars using the chart style of Box and set their box color to be transparent and then set the up bar and down bar outlines as Magenta for down and conflowerblue for up bars. With reference to the previous code I posted in post #13, I set the StrategyPlot to be the Cross type in orangeRed. As you can see these match up so the values are correct it is just the the line drawing somewhat misrepresents visually.

      You can also print out the values and compare that way.


      . Click image for larger version

Name:	catinabag-1.PNG
Views:	248
Size:	117.1 KB
ID:	1092604

      Paul H.NinjaTrader Customer Service

      Comment


        #18
        Paul,

        Thank you very much for getting back: I'm still encountering this problem.

        I'm using two sets of data for the secondary dataseries:

        (x) Closes[1], and

        (y) BarsArray[1].GetClose(cacheBar) where cacheBar = BarsArray[1].GetBar(Times[0][0])

        In the attached chart, the blue crosses are (x) and the white crosses are (y) [the white crosses are actually a composite of OHLC using the (y) method].

        You can see the blue crosses are consistently 1 minute ahead of the white crosses.

        I actually need to create a ratio that involves both (x) and (y). The problem is that the 1 minute difference introduces a strange intermediate ratio that doesn't make sense.

        Could you please let me know if there is a way to get (x) and (y) to be the exact same timing?

        I truly appreciate your help! Thank you very much.

        [attached photo]

        Comment


          #19
          Hello catinabag,

          Thanks for your reply.

          If you are looking at historical data then the 5 minute bar values (Closes[1][0][ will be of the previous bar until the next 5 minute period. This might be easier to see if you compare the High Values and duplicate the chart I showed with the box chartstyle.

          Or, print out the values of each and compare them.

          Paul H.NinjaTrader Customer Service

          Comment


            #20
            Paul,

            Thanks for the quick reply.

            Ok, in your picture, please see 8:05. The 1 minute is 7587.75 (ties perfectly to 8:05), but the five minute is 7607.75, which ties to the 8:01 value. I believe this is the crux of my problem.

            I am trying to do a calculation that takes the prior 5-minute candle and the current 5-minute candle into account, and this is calculated every minute.

            The way this is set up, I'm taking an average of 8:05 and 8:06-8:10, then 8:10 with 8:06-8:10, then 8:10 with 8:11-8:15.]

            Presuming the current 5-minute candle is simply flat, It gives a bunch of values like this:

            * A value at 8:05
            * A value at 8:06-8:09
            * A value at 8:10
            * A value at 8:11-8:14
            * A value at 8:15

            Whereas I believe it would be preferable to be like this:
            A value at 8:00
            A value at 8:05
            A value at 8:10

            So, I think I need to do something like this:

            (A) 8:05: take a calculation of Closes[1][0] and that particular 1 minute bar (I'm using a 1-minute chart)

            (B) 8:06 - 8:09: take a calculation of Closes[1][0] and BarsArray[1].GetClose(cacheBar).

            (C) 8:10: take a calculation of Closes[1][0] and that particular 1 minute bar

            It seems to me that the only way to achieve (A) would be to code something that looks at the current time and says, "if it's on the 5-minute, use just the the 1-minute chart. Otherwise, calculate (B).

            Am I on the right track here? Does this make sense? Would you happen to have any suggested code lines that would address this issue?

            I greatly appreciate your insights. Thank you again for all your help!

            Comment


              #21
              Hello catinabag,

              Thanks for your reply.

              Regarding, "Ok, in your picture, please see 8:05. The 1 minute is 7587.75 (ties perfectly to 8:05), but the five minute is 7607.75, which ties to the 8:01 value. I believe this is the crux of my problem." The 8:01 candle created what would be the high of the 8:05 5 minute candle so I do not see your point. The 8:05 candle contains the data between 8:00:00:001 and 8:05:00:000 and this is what you have been looking for.

              I think we are going around in circles at this point I am not sure how best to assist you.






              Paul H.NinjaTrader Customer Service

              Comment


                #22
                Appreciate your efforts. Perhaps this makes my issue a bit more clear:

                * Closes[1[0]: the value would be the same for 8:05 - 8:09

                * The other construct (BarsArray[1].GetClose(cacheBar)) would be the same for 8:01 - 8:05 and then 8:06 - 8:10.

                If I have a computation that is the average of both of these values, I get this:

                1. 8:05: the new Closes[1][0] and the old other construct

                2. 8:06: the new Closes[1][0] and the new other construct

                3. 8:10: the next Closes[1][0] but still the prior other construct

                4. 8:11: the next Closes[1][0] and then the next other construct

                and so on.

                I would rather have both series update at 8:05 and 8:10, and the only way I see that occurring is to ask the code to get the simple 1-minute close each 8:05 and 8:10, etc. Basically, a brute force solution.

                I am just curious if you could please tell me if you understand my issue and if you are possibly familiar with any coding strategies that could work?

                Such as, is there a way to check if ToTime() is a minute that ends with 5 or 10? I'm not exactly sure how to check for that.

                Thank you for your help!

                Comment


                  #23
                  Hello catinabag,

                  Thanks for your reply.

                  Just to check, are you understanding that Closes[1][0] will actually be of the previous 5 minute bar and not of the current 5 minute bar (when looking at historical data)? If not I recommend reviewing the help guide section, " How Bars Data is Referenced" on this page: https://ninjatrader.com/support/help...nstruments.htm

                  Perhaps it would help if you could explain what you are trying to achieve overall.

                  " is there a way to check if ToTime() is a minute that ends with 5 or 10?"

                  Code:
                              for (int i = 0; i < 12; i++)
                              {
                                  int j = i * 5;
                  
                                  if (Time[0].Minute == j)
                                  {
                                      Print (Time[0]);
                                      break;
                                  }
                              }
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #24
                    Many thanks for another helpful and timely response, Paul.

                    I'll review and come back!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by helpwanted, Today, 03:06 AM
                    1 response
                    7 views
                    0 likes
                    Last Post sarafuenonly123  
                    Started by Brevo, Today, 01:45 AM
                    0 responses
                    7 views
                    0 likes
                    Last Post Brevo
                    by Brevo
                     
                    Started by aussugardefender, Today, 01:07 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post aussugardefender  
                    Started by pvincent, 06-23-2022, 12:53 PM
                    14 responses
                    242 views
                    0 likes
                    Last Post Nyman
                    by Nyman
                     
                    Started by TraderG23, 12-08-2023, 07:56 AM
                    9 responses
                    385 views
                    1 like
                    Last Post Gavini
                    by Gavini
                     
                    Working...
                    X