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

Historical tick side (ask/bid)

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

    Historical tick side (ask/bid)

    Hello Support –

    Please, help!

    I’m developing a script and I need data of historical ticks. I need the tick’s time, price, volume and whether it was filled at ask or at bid.

    For real-time trades I used the following code inside OnMarketData() method

    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if (e.MarketDataType == MarketDataType.Last)
    {
    string side = null;
    if(e.Price > GetCurrentBid())
    {
    side = "ask";
    }
    if(e.Price < GetCurrentAsk())
    {
    side = "bid";
    }
    }
    }

    I have read the help guide and found the following solution for historical ticks.

    protected override void Initialize()
    {
    Add("CL", PeriodType.Tick, 1, MarketDataType.Ask);
    Add("CL", PeriodType.Tick, 1, MarketDataType.Bid);
    }

    Then I tried to check the tick data by the following code inside OnStartUp() method

    protected override void OnStartUp()
    {
    for(int i = 0; i < 10; i++)
    {
    Print(Close[1][i]);
    Print(Close[2][i]);
    }
    }

    The same code I tried inside OnBarUpdate() method.
    Each time I’ve got two zeros in Output window, not even 10 times of two zeros as a size of the loop.

    My question is what is wrong with my code? How can I get whether the historical tick happened at ask or bid side?

    I will really appreciate any code example to solve my problem.

    Regards.

    #2
    Hello,
    You will need to add the CL with the full contract month as well such as
    Code:
    Add("CL 01-16", PeriodType.Tick, 1, MarketDataType.Ask);
    You will also need to use the Closes collection and not Cose. In your loop you will do the following.
    Code:
    for(int i = 0; i < 10; i++)	
    {
        Print(Closes[1][i]);
        Print(Closes[2][i]);
    }
    Please see the following link on using Closes. https://ninjatrader.com/support/help...nt7/closes.htm

    If we can be of any other assistance please let us know.
    Cody B.NinjaTrader Customer Service

    Comment


      #3
      thank you for the reply

      I've changed the code as you wrote, but got the following error

      Error on calling 'OnBarUpdate' method for indicator 'LNT' on bar 0: You are accessing an index with a value that is invalid since its out of range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

      what is wrong?

      Comment


        #4
        Hello,
        You will need to use logic to check for the bars and what to do if they are not there.
        At the beginning of OnBarUpdate() please enter in the following:
        Code:
        if(CurrentBars[1] < 10 || CurrentBars[2] < 10)
        return;
        For more information on this please see the following link, http://ninjatrader.com/support/forum...ead.php?t=3170

        In addition I did want to mention that your loop will need to be ran.will need to be ran in OnBarUpdate(), as OnStartup() is only ran once immdeiately before the the start of the processing in the Script. If you want the loop to only occur once you could create a bool to control this. For example:
        Code:
        private bool alreadyRan = false;
        
        protected override void OnBarUpdate()
        {
            if (!alreadyRan)
            {
                if(CurrentBars[1] < 10 || CurrentBars[2] < 10)
                {
                     return;
                }
                for(int i = 0; i < 10; i++)	
                {
                    Print(Closes[1][i]);
                    Print(Closes[2][i]);
        
                }
                alreadyRan = true;
            }
        }
        Please also the following link on the OnStartUp() method.https://ninjatrader.com/support/help.../onstartup.htm
        Cody B.NinjaTrader Customer Service

        Comment


          #5
          Hello CodyB,

          thank you for the reply. You helped me, your code works.

          Could you please tell me how to load a specific numbers of bars to my script via
          Add("CL 01-16", PeriodType.Tick, 1, MarketDataType.Ask);
          or any other methods/ ways.

          Regards.

          Comment


            #6
            Hello,
            This would not be done through custom programming. The data serreis will load the number of bars set by the data series on the chart. To change the number of bars to load you will right click on the chart> Data Series> Make sure the Setting Load data based on is set to Bars> Change the value for Bars to load to desired number of bars> Press OK.
            Cody B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by GLFX005, Today, 03:23 AM
            0 responses
            1 view
            0 likes
            Last Post GLFX005
            by GLFX005
             
            Started by XXtrader, Yesterday, 11:30 PM
            2 responses
            11 views
            0 likes
            Last Post XXtrader  
            Started by Waxavi, Today, 02:10 AM
            0 responses
            6 views
            0 likes
            Last Post Waxavi
            by Waxavi
             
            Started by TradeForge, Today, 02:09 AM
            0 responses
            14 views
            0 likes
            Last Post TradeForge  
            Started by Waxavi, Today, 02:00 AM
            0 responses
            3 views
            0 likes
            Last Post Waxavi
            by Waxavi
             
            Working...
            X