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

CurrentBar looks like an index of ticks

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

    CurrentBar looks like an index of ticks

    Hello
    I have the code below, that produces different prints for the CurrentBar index than the second code for the bars index in OnEachTickCalculation. Also, the last bar has an index number plus one when compared with the data box (i.s the Data Box shows last bar index of 6382, while the Output window print shows 6383).

    I expected the blue and red highlighted prints would show identical bar number, but the CurrentBar index looks like it counts the actual ticks or something...

    Thank you

    Code:
    if(longOrderId.Length == 0 && longAtmId.Length == 0)
                        {
                            if (longCondPrice > open && !isLongAtmStrategyCreated)
                            {
                                longOrderId = GetAtmStrategyUniqueId();
                                longAtmId = GetAtmStrategyUniqueId();
                                AtmStrategyCreate(OrderAction.Buy, OrderType.StopLimit, currentAsk + offsetLimitStop * TickSize, currentAsk + offsetLimitStop * TickSize,
                                TimeInForce.Day, longOrderId, "3C", longAtmId, (atmCallbackErrorCode, atmCallBackId) =>
                                {
                                    //check that the atm strategy create did not result in error, and that the requested atm strategy matches the id in callback
                                    if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == longAtmId)
                                        isLongAtmStrategyCreated = true;
                                    barNumberOfLongOrder = CurrentBar;
                                    triggerPriceLong = GetCurrentAsk(0);                                    
                                    Print("Long ATM triggered at: " + Convert.ToString(triggerPriceLong) + " - Bar #" + barIndex + " open price is " + open +
                                    " - Current Bar: " + CurrentBar +  " - " + Time[0]);
                                    Print("##########");                                
                                });
    ....

    Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
            {
              base.OnRender(chartControl, chartScale);
              // loop through only the rendered bars on the chart    
                for(barIndex = ChartBars.FromIndex; barIndex <= ChartBars.ToIndex; barIndex++)
              {
                    ;
                // get the open price at the selected bar index value
                open = Bars.GetOpen(CurrentBar);
                Print("Bar #" + barIndex + " open price is " + open);
              }
            }
    Output window prints:

    Long ATM cancel cond 2 price: 56.19 - Bar #6383 open price is 56.19 - Current Bar: 1289690 - 25/10/2019 13:31:42
    >>> Long Order ID Reset Price: 56.19 - Bar #6383 open price is 56.19 - Current Bar: 1289691 - 25/10/2019 13:31:42
    >>## Long ATM ID Reset Price: 56.19 - Bar #6383 open price is 56.19 - Current Bar: 1289692 - 25/10/2019 13:31:42
    ----------
    Short condition at: 56.15 - Bar #6383 open price is 56.16 - Current Bar: 1289746 @ 25/10/2019 13:31:48
    R = 1 - Current Bid: 56.15 - Bar #6383 open price is 56.16 - Current Bar: 1289746 - 25/10/2019 13:31:48
    Short ATM triggered at: 56.15 - Bar #6383 open price is 56.16 - Current Bar: 1289746 - 25/10/2019 13:31:48
    ##########
    ++++++++++
    Long condition at: 56.15Bar #6384 open price is 56.14 - Current Bar: 1289940 @ 25/10/2019 13:32:02
    S = 1 - Current Ask: 56.15 - Bar #6384 open price is 56.14 - Current Bar: 1289940 - 25/10/2019 13:32:02
    Long ATM triggered at: 56.15 - Bar #6384 open price is 56.14 - Current Bar: 1289940 - 25/10/2019 13:32:02
    Last edited by itrader46; 10-29-2019, 01:19 PM.

    #2
    Hello itrader,

    Thanks for your post.

    I do not have exact context for how barIndex is getting set in the top snippet, so I can only touch on what you may be seeing. To understand why barIndex and CurrentBar are not the same, we will have to know how barIndex is assigned and with what value it is assigned.

    Is this a Multi Time Frame strategy? If so then CurrentBar will be relevant for the data series that is currently processing in OnBarUpdate. For example, if you add a single tick data series to the script, and that single tick data series is updating in OnBarUpdate, then CurrentBar will reflect the bar index for the bar of the single tick data series that is being processed. If you want to specifically reference a certain data series' CurrentBar index, you could use CurrentBars. For example: CurrentBars[0] will always represent the primary data series

    CurrentBars - https://ninjatrader.com/support/help...urrentbars.htm

    When using Calculation.OnEachTick, the developing bar will update in OnBarUpdate and the CurrentBar index will reflect the developing bar on the chart. When the script is using Calculate.OnBarClose, OnBarUpdate will update the only for the bar that has just closed, and the script's CurrentBar index will reflect the second to last bar on the chart. Please see demonstration below.



    Snippet:

    Code:
    if (BarsInProgress == 0)
        Print(String.Format("CurrentBar: {0} Bars.GetOpen(CurrentBar): {1} Open[0]: {2} GetCurrentBid(): {3} State: {4}", CurrentBar, Bars.GetOpen(CurrentBar), Open[0], GetCurrentBid(), State));
    I look forward to being of further assistance.
    JimNinjaTrader Customer Service

    Comment


      #3
      I thought the CurrentBar would just that, regardless of how the index is calculated and I mentioned that it shows the index shown by the data box plus 1. Is that OK with you?

      Anyway, I have 5 days x 1440 (1min bars) = 7200 bars, not 1289940!

      I am loading indeed a tick series and set the logic to calculate on it (if BarsInProgress != 1 return) and the only way the CurrentBar number makes sense is if the tick series actually adds 1 tick bars, so the CurrentBar shows the tick number, not the 1min bar number. Does that make sense and is this how is it supposed to work?

      I am using a tick replay indicator in my strategy, so that's what I thought I had to do.
      Last edited by itrader46; 10-30-2019, 04:21 AM.

      Comment


        #4
        Hello itrader46,

        if (BarsInProgess != 1) return; means: "If we are processing another data series other than the first data series added with AddDataSeries, do not process any further logic." In other words, OnBarUpdate will only process for BarsInProgress 1, your single tick data series, and CurrentBar would then reflect the single tick data series, because that is the only data series that processes that line of code.

        You could test the following based on the Multi Time Frame and Instruments documentation to better illustrate how CurrentBar changes depending on the data series being updated in OnBarUpdate.

        Code:
        protected override void OnBarUpdate()
        {
            if (BarsInProgress == 0)
            {
                Print(String.Format("BIP: {0}, CurrentBar: {1} CurrentBars[BarsInProgress]: {2}", BarsInProgress, CurrentBar, CurrentBars[BarsInProgress]));
            }
        
            if (BarsInProgress == 1)
            {
                 Print(String.Format("BIP: {0}, CurrentBar: {1} CurrentBars[BarsInProgress]: {2}", BarsInProgress, CurrentBar, CurrentBars[BarsInProgress]));
            }
        }
        Multi Time Frame and Instruments - https://ninjatrader.com/support/help...nstruments.htm

        Please let us know if you have any questions after testing the snippet above with a single tick data series added to the script.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by cre8able, Today, 03:20 PM
        0 responses
        5 views
        0 likes
        Last Post cre8able  
        Started by Fran888, 02-16-2024, 10:48 AM
        3 responses
        47 views
        0 likes
        Last Post Sam2515
        by Sam2515
         
        Started by martin70, 03-24-2023, 04:58 AM
        15 responses
        114 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by The_Sec, Today, 02:29 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Started by jeronymite, 04-12-2024, 04:26 PM
        2 responses
        31 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Working...
        X