Back

Iterating over TradeBars collection

I'm writing my first strategy and am struggling with something that seems ridiculous. I've added some securities in my portfolio, over which I'd now like to iterate.

public void OnData(TradeBars data)
{

foreach(KeyValuePair<string, string> pair in data)
{
// do something with entry.Value or entry.Key
}
}

which generates the bizarre error

Cannot convert type 'System.Collections.Generic.KeyValuePair' to 'System.Collections.Generic.KeyValuePair'

Um? Why is something so basic throwing errors?

Update Backtest








I just discovered the Slice override, which looks like it'll let me iterate over.

1

The data is a TradeBar collection not a string. Check out the examples in the QC University in the left tab and read the relevant documentation here. TradeBar's are a class with OHLC values.

1

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.


As an example you can iterate the tradebars this way:

 

public void OnData(TradeBars data)
{
foreach (var bar in data)
{
Debug(String.Format("For symbol {0} Open was {1} and Close was {2}",
bar.Key.ToString(),
bar.Value.Open.ToString("#.##"),
bar.Value.Close.ToString("#.##"))
);
}
}

 

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.


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