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

AddDataSeries works ambiguously

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

    AddDataSeries works ambiguously

    Hello
    I`m creating multi-instrument strategy. For reading quotes (Ask,Bid) from Instruments I`m using at first
    Code:
     if (State == State.Configure)
         {
    	AddDataSeries(BarsPeriodType.Minute, 60);
    	 AddDataSeries(BarsPeriodType.Day, 1);
          }
    I want to have possibility for reading quotes for any Instruments that I have in Chart , so I don`t use "instrument name" inside AddDataSeries.

    And after, in OnBarUpdate() I`m using
    Code:
    if(BarsInProgress == 0) //for Main Instrument
    				{
    					double current_price_Instrument_Main   = GetCurrentAsk(0);
    				}
    			if(BarsInProgress == 1) //for Second Instrument
    				{
    					double current_price_Instrument_Second = GetCurrentAsk(1);		
    				}
    But, variable current_price_Instrument_Second will have quotes from Main Instrument, not from Second!

    In Chart, I`m using Data series with Future for CL(Oil) and M6E

    P.S. if I will use "instrument name" inside AddDataSeries - I will have correct quotes from second Instrument. But its not a good idea for futures (rollovers and ets)

    I don`t understand what is a problem.
    Can somebody help me solve this problem?

    #2
    Hello sergey_z,

    Thanks for your post.

    Bid and Ask Series can be added with the AddDataSeries() overload that includes a parameter for the MarketDataType. Without specifying an instrument, you could add use the AddDataSeries() overload for a custom BarsPeriod with the MarketDataType defined in the BarsPeriod.

    Code:
    AddDataSeries(new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick, Value = 1, MarketDataType = MarketDataType.Bid });
    AddDataSeries(new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick, Value = 1, MarketDataType = MarketDataType.Ask });
    Bid and Ask received from GetCurrentBid() and GetCurrentAsk() fetch the most current Bid/Ask value NinjaTrader receives at the time the method was called and may not be the same bid and ask that would be seen in a PriceSeries with OnBarUpdate() or OnMarketData() when those methods process that data. GetCurrentBid() and GetCurrentAsk() will also retrieve realtime bid and asks outside of the context of the period of the data series that calls it since these methods "cut ahead" of other NinjaTrader logic. The parameter for BarsInProgress in GetCurrentBid() and GetCurrentAsk() reflect a reference to another instrument added to the NinjaScript.

    I'll include publicly available documentation links to the items discussed and a forum post for further reading with GetCurrentBid()/GetCurrentAsk().

    GetCurrentBid() - https://ninjatrader.com/support/help...dataseries.htm

    AddDataSeries() - https://ninjatrader.com/support/help...dataseries.htm

    Historical Bid/Ask (for further reading) - https://ninjatrader.com/support/help..._ask_serie.htm

    Forum post explaining GetCurrentBid() and GetCurrentAsk in further detail - https://ninjatrader.com/support/foru...d.php?t=110393

    Please let us know if you have any additional questions.
    Last edited by NinjaTrader_Jim; 07-11-2018, 09:18 AM.
    JimNinjaTrader Customer Service

    Comment


      #3
      ok. I did what you recommend with AddDataSeries, BUT problem not solved. I have quotes from Main Instrument in GetCurrentBid(1) for Second Instrument

      Comment


        #4
        Hello sergey_z,

        In the code you have provided, you have specified a change in the BarsPeriodType and using the same instrument. Since each data series uses the same instrument, a call to GetCurrentBid(1) will fetch the most recent bid for the same instrument as your primary data series.

        GetCurrentBid() and GetCurrentAsk() are not tied to the period specified in the data series, it is the most recent bid/ask directly from the level 1 data feed at the time the method gets called. If you would like to get bid and ask in reference to another period, I suggest using the AddDataSeries() to add a Bid/Ask Series as described in my previous post.

        If you have any further questions, please don't hesitate to ask.
        JimNinjaTrader Customer Service

        Comment


          #5
          Sorry, your answer is not clear for me

          Can you explane step by step what I need to do for reading current price of Second Instrument, without Instrument Name in code.

          Thanks a lot!
          Regards

          Comment


            #6
            Hello sergey_z,

            When you call AddDataSeries(), we are adding an additional Data Series, not specifically a new Instrument. GetCurrentBid() and GetCurrentAsk() fetch the most recent Bid/Ask without relation to the BarsPeriod defined in a data series. It is simply the most current bid and ask when the method is called.

            In your code you are adding two additional data series of the same Instrument. The GetCurrentBid() calls will get the most recent bid of that instrument no matter what BarsInProgress you call the method: The data series are of the same instrument, so each call will reference the current bid of that instrument at-that-time. This CurrentBid value is not bound to the data series you describe in AddDataSeries().

            To get the Bid/Ask in reference to the BarsPeriod you describe with AddDataSeries(), you could add the data series like I have mentioned in my previous reply, and reference Close[0] for that BarsInProgress iteration. I have attached more code that could be used as a demonstration.

            Code:
            protected override void OnStateChange()
            {
            	if (State == State.SetDefaults)
            	{
            		...
            	}
            	else if (State == State.Configure)
            	{
            		AddDataSeries(new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick, Value = 1, MarketDataType = MarketDataType.Bid });
            		AddDataSeries(new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick, Value = 1, MarketDataType = MarketDataType.Ask });
            	}
            }
            
            protected override void OnBarUpdate()
            {
            	if(BarsInProgress == 0)
            		Print("Last: " + Close[0]);
            	else if(BarsInProgress == 1)
            		Print("Bid: " + Close[0]);
            	else if(BarsInProgress == 2)
            		Print("Ask: " + Close[0]);
            }
            You may also wish to leave your AddDataSeries() methods as is without taking the approach above and to fetch the Ask/Bid on the desired BarsInProgress iteration with Bars.GetBid(CurrentBar) and Bars.GetAsk(CurrentBar). It should be noted that this approach will fetch the tick at the given index. If we are calling this from a data series of MarketDataType.Last, this will be the Bid/Ask when that Last price occurred.

            GetBid() - https://ninjatrader.com/support/help...-us/getbid.htm

            Get Ask() - https://ninjatrader.com/support/help...-us/getask.htm

            Example:
            Code:
            protected override void OnStateChange()
            {
            	if (State == State.SetDefaults)
            	{
            		...
            	}
            	else if (State == State.Configure)
            	{
            		AddDataSeries(new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick, Value = 1, MarketDataType = MarketDataType.Last });
            		AddDataSeries(new BarsPeriod { BarsPeriodType = BarsPeriodType.Tick, Value = 1, MarketDataType = MarketDataType.Ask });
            	}
            }
            
            protected override void OnBarUpdate()
            {
            	if(BarsInProgress == 1)
            		Print("Last Ask: " + Bars.GetAsk(CurrentBar));
            	else if(BarsInProgress == 2)
            		Print("Ask: " + Close[0]);
            }
            Please let me know if this information does not resolve your inquiry.
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Radano, 06-10-2021, 01:40 AM
            19 responses
            604 views
            0 likes
            Last Post Radano
            by Radano
             
            Started by KenneGaray, Today, 03:48 AM
            0 responses
            3 views
            0 likes
            Last Post KenneGaray  
            Started by thanajo, 05-04-2021, 02:11 AM
            4 responses
            470 views
            0 likes
            Last Post tradingnasdaqprueba  
            Started by aa731, Today, 02:54 AM
            0 responses
            5 views
            0 likes
            Last Post aa731
            by aa731
             
            Started by Christopher_R, Today, 12:29 AM
            0 responses
            11 views
            0 likes
            Last Post Christopher_R  
            Working...
            X