Back

I get Error when retrieving tick data. Help please!

I get an error when trying to retrieve tick data. The error message is:

"Runtime Error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index (Open Stacktrace)"

Error goes away when I comment out the following line:

tick_price = data["EURUSD"][-1].LastPrice;       

What is causing the problem? Help much appreciated.       

 

////////////////////////////////Code //////////////////////////

public override void Initialize()

{

     SetStartDate(2019,4,15);

     SetEndDate(2019,4,22);

     SetCash(1000);

     AddForex("EURUSD", Resolution.Tick, Market.FXCM);

     SetWarmUp(TimeSpan.FromDays(5));

}

 

public void OnData(Slice data)                 

{

Decimal tick_price = 0;

     if (data.ContainsKey("EURUSD"))

     {           

           tick_price = data["EURUSD"][-1].LastPrice;

     }       

}     

///////////////////////////////////////End//////////////////////////


                                                                                                                                                                                                 

                                                                                      

                                                                                      

                                                          

 

 

                                               

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi,

Not sure where you got the [-1] indexing from, that's not needed. Please see the documentation section on handling data.

0

Hi Douglas,

I got it from the documentation page on "Handling data". There is an example under section "Ticks" that says: "use the [-1] indexer to access the most recent tick that arrived". The example is copied and pasted down below. However, I work in C# and need to access forex tick data on "EURUSD" in OnData(Slice data), or alternatively OnData(Ticks data). Can you help me out? What should I do? Thank you.

//////////From Handling data, the section on Ticks///////

self.AddEquity("IBM", Resolution.Tick) ## Subscribe to tick-level IBM data

def OnData(self, data): ## Use the [-1] indexer to access to most recent tick that arrived

self.Log(f"Last price: {data['IBM'][-1].LastPrice}")

self.Log(f"Last price: {data['IBM'][-1].Quantity}")

/////////////////////End/////////////


 

0

Apologies - you're absolutely right about that.

Two things to take notice of:

  1. As the data object coming into the OnData method comes indirectly from a broker (the "real world"), you should never assume it contains anything. You must always check that the data object actually contains a) an "EURUSD" index and b) that the "EURUSD" index contains any ticks at all. You should never assume that you have any data - always check the contents of data before using it.
  2. I notice you're not using the "override" keyword on public void OnData(Slice data), I am surprised your code runs at all without it.
1

Hi Douglas!

I made the proposed changes and still get the same error. Down below is the modified code. What could be the reason? This is very weird.  

public override void Initialize()
{
        SetStartDate(2019,7,20);
        SetEndDate(2019,8,1);
        SetCash(1000);
        
        AddForex("EURUSD", Resolution.Tick);
        SetWarmUp(TimeSpan.FromDays(5));
}

public override void OnData(Slice data)
{
    if( !data.ContainsKey("EURUSD") ) return;
    Decimal tick_price = data["EURUSD"][-1].LastPrice;
    Debug("Current tick price: " + tick_price);

}
 

0

My apologies again - I see now that you're writing in C#. The [-1] indexer does not work in C# - if you see the documentation, the code example refers to Python, there's no example in C# for that section yet.

The data["EURUSD"] object is some sort of enumerable of ticks - I would imagine you can do data["EURUSD"].Last() or data["EURUSD"].First() (depending on which order the ticks are stored). Remember to check that data["EURUSD"].Count > 0 before trying to access the ticks, though.

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed