Recently working on Option Algo using QuantBook in Jupyter notebook, realized that some CSharp classes actually convert everything to PyObject
In my C# Jupyter notebook:
`var data = optionHistory.GetAllData();`
` Console.WriteLine(data.GetPythonType());`
<class 'pandas.core.frame.DataFrame'>
After review some source code, the class has no easy way to use in CSharp (I don’t want to convert back PyObject into C# object)
https://www.lean.io/docs/v2/lean-engine/class-reference/OptionHistory_8cs_source.html
public OptionHistory(IEnumerable<Slice> data)
{
_data = data;
_converter = new PandasConverter();
_dataframe = _converter.GetDataFrame(_data); //After converted to PyObject here, then it’s hard to work with CSharp code
}
Question: I have used same API in Python and it’s really straightforward but in C# some guess work and conversion required, so am I doing something wrong here or it’s just in progress of support?
Mia Alissi
It seems like you're trying to work with the
OptionHistory
object in C#. TheOptionHistory
object is indeed converted to aPyObject
which is a pandas DataFrame in Python. This is straightforward to work with in Python, but can be a bit more challenging in C#.In C#, you can iterate through the
OptionHistory
object to access each slice and the option chains within it. Here's an example from the documentation:If you want to convert the
OptionHistory
object to a DataFrame in C#, you can use theGetAllData()
method:However, this will return a
PyObject
which is a pandas DataFrame. If you want to work with this data in C#, you might need to convert it back to a C# object. Unfortunately, this can be a bit cumbersome and might not be the most efficient way to handle the data.If you're comfortable with Python, you might find it easier to use the Python API for QuantConnect, as it provides more straightforward support for pandas DataFrames.
Sources:
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.
Hin Lam
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!