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

OHLC Forming BAR multi series

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

    OHLC Forming BAR multi series

    Hello,
    I want to print the OHLC for the bar that is currently forming.
    I'M in MARKET REPLAY

    if i do this;:

    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "Working on it";
    Calculate = Calculate.OnPriceChange;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.ImmediatelySubmit;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = true;
    RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;​
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Instrument.FullName, Data.BarsPeriodType.Tick, 1);
    AddDataSeries(Instrument.FullName, Data.BarsPeriodType.Minute, 60);
    AddHeikenAshi(Instrument.FullName, BarsPeriodType.Minute, 60, MarketDataType.Last);
    }​


    protected override void OnMarketData(MarketDataEventArgs e)
    {

    if (e.MarketDataType == MarketDataType.Last && BarsInProgress == 1)
    {
    Print(Times[2][0] + " " + Closes[2][0]);​
    }
    }

    It prints always the closing price of the previous candle and not the one forming. I need to have the one that is currently forming in the OnMarketData for the 1h timeframe
    Even in the OnBarUpdate() would be ok to have it.


    it gives me the last bar close even for OnEachTick


    thanks

    Last edited by StefanoMuscariello; 10-17-2023, 12:36 PM.

    #2
    Hello StefanoMuscariello,

    Just as a heads up using Instrument.FullName for AddDataSeries is not valid, you can replace that with null to assume the primary instrument:

    Code:
    AddDataSeries(null, Data.BarsPeriodType.Tick, 1);
    AddDataSeries(null, Data.BarsPeriodType.Minute, 60);
    AddHeikenAshi(null, BarsPeriodType.Minute, 60, MarketDataType.Last);​
    To do what you are asking I would suggest using a private variable like the following:

    Code:
    private double closePrice;
    protected override void OnMarketData(MarketDataEventArgs e)
    {
        Print( " Close " + closePrice);
    }
    
    protected override void OnBarUpdate()
    {
    closePrice = Closes[2][0];
    }​
    JesseNinjaTrader Customer Service

    Comment


      #3
      hello, .fullname actually works fine are you sure about this null thing? . Doing Closes[2][0] doesn't work and print the last closed bar close

      thanks

      Comment


        #4
        Hello StefanoMuscariello,

        You can read about not using Instrument with AddDataSeries in the documentation in the help guide. That is known to fail in certain use cases, as it cannot be guaranteed the right data will be loaded that should be avoided. using null is also documented as the correct way to assume the instrument name.

        https://ninjatrader.com/support/help...ghtsub=adddata

        Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided.
        4. For the instrument name parameter null could be passed in, resulting in the primary data series instrument being used.
        Please see the sample I had provided in the previous post for a way to access the building bar.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi,
          I have a similar issue, but in BACKTEST:
          I am not able to get the updated values on every incoming tick of the current High, Low, Close of a second data series of 10 min that is forming. Primary data series is 30 min.
          I am using the following code:
          Calculate = Calculate.OnBarClose;

          else if (State == State.Configure)

          AddDataSeries(Data.BarsPeriodType.Tick, 1);
          AddDataSeries(null, BarsPeriodType.Minute, 10, Data.MarketDataType.Last);
          }
          }

          protected override void OnBarUpdate()
          {
          if (BarsInProgress == 1)
          {
          DateTime time = Time[0];
          double open0 = Opens[2][0];
          double high0 = Highs[2][0];
          double low0 = Lows[3][0];
          double close0 = Closes[3][0];
          Print("Time= " + time + " Open= " + open0 + " High= " + high0 + " Low= " + low0 + " Close= " + close0);
          }
          ETC.

          What I’d like to get is the high/low/close values of the forming 10 Minute bar, in backtest, while the print is giving the Time of the last tick, but the open/high/low/close of the already closed 10 min bar. What should I do?
          Thank you
          Martin

          Comment


            #6
            Hello martin70,

            You can only access closed bars while using OnBarClose which is the type of processing the backtest uses. To access a forming bar you would need to test in realtime.

            As a side note your code is accessing invalid indexes, Closes[3][0] would be a 3rd added series but you have only added 2 additional series. The first parameter for the plural series is the BarsInProgress. Closes[2][0] would be your 10 minute series that you added. Time[0] is the equivalent of Times[1][0] because you have that inside a BarsInProgress 1 condition. To get the time of the 10 minute bar you can use Times[2][0].
            JesseNinjaTrader Customer Service

            Comment


              #7
              Dear jesse,
              i would like to remark the fact that in market replay i still cannot access the forming bar using the playback connection, do you know how i can access the forming bar?
              for now i implemented my own track of the forming one for the heiken ashi forming candle, but i'd like to have the official one. Thanks

              Comment


                #8
                Hello StefanoMuscariello,

                I had posted a sample of code you can use to do that in post 2, you could use a variable for each of the values you wanted and set them from OnBarUpdate.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Hi Jesse, thank you for the explanations.
                  I am trying to find away to achieve the a.m. bar values coding it in the strategy creating a class that recreates the 10 minute bar. Is there a way to detect when the second data series is opening, I mean on the first tick, without using IsFirstTickOfBar? (that is not working on secondary data series)
                  Thank you again
                  Martin

                  Comment


                    #10
                    Hello martin70,

                    For historical bars you can use the IsFirstBarOfSession to find the first closed bar in the session. If you were trying to get the first 10 minute bar values you could make a condition using IsFirstBarOfSession in a condition and then store the values to a variable.

                    JesseNinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by lightsun47, Today, 03:51 PM
                    0 responses
                    4 views
                    0 likes
                    Last Post lightsun47  
                    Started by 00nevest, Today, 02:27 PM
                    1 response
                    8 views
                    0 likes
                    Last Post 00nevest  
                    Started by futtrader, 04-21-2024, 01:50 AM
                    4 responses
                    44 views
                    0 likes
                    Last Post futtrader  
                    Started by Option Whisperer, Today, 09:55 AM
                    1 response
                    13 views
                    0 likes
                    Last Post bltdavid  
                    Started by port119, Today, 02:43 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post port119
                    by port119
                     
                    Working...
                    X