Back

Moving Average Cross in Python

Here is the Moving Average Cross example from QuantConnect University in Python.

We will be posting other examples in Python soon.

Update Backtest






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.



... excited to see python is getting some love for those amongst us contemplating switching from Q/pian to QC.

2

I am getting build error when I clone this project and build it in my project.

0

We are working on a new python platform that will shipped soon. We expect that this build error disappear with it.

Meanwhile, could you please retry? It is working for me.
 

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.


"We are working on a new python platform that will shipped soon. "

 

Do you have an estimated time frame as to when this new python platform will be shipped ? I am really excited to give it a shot :). 

0

@alexandre its working for me now. 

Could you explain what is the meaning of this code statement ?

# only once per day
if self.previous is not None and self.previous.Date == self.Time.Date:
return

 

As I want to try it in hourly timeframe and not daily 

 

0

Hopefully we will ship the new python framework before Christmas.

This line of code:

# only once per day
if self.previous is not None and \
self.previous.Date == self.Time.Date:
return

assures that we only check for a moving average cross once per day after the exchange opens.
In order to evaluate the moving average cross every hour, please modify the code for:

# only once per hour
if self.previous is not None and \
self.previous.Hour == self.Time.Hour:
return

If we do not want to work with C# System.DateTime, we can convert the IAlgorithm.Time object to a python datetime object:

from datetime import datetime

def lang_time_convert(self, time):
return datetime(time.Year, time.Month, time.Day, time.Hour, \
time.Minute, time.Second, time.Millisecond)

# In OnData:
# only once per hour
py_time = lang_time_convert(self.Time)
if self.previous is not None and \
self.previous.hour == py_time.hour:
return

 

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.


@Alexandre Catarino

Will there be an announcement for the new python framework?

0

Yes, there will be an announcement.
At this moment, the most used features works locally. We are working on documentation and making it work on the cloud (Algorithm Lab).

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.


This code has been deprecated as we've replaced it with PythonNet. It will no longer work in the QC Environment. You will need to use the new PythonNet example posted here: 

https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/MovingAverageCrossAlgorithm.py

 

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.


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