Back

SMA futures algorithm

Hello, I am currently having problem with my futures algoirthm. It is just a basic buy and sell based on sma crossover. I am only dealing with the s and p 500 mini futures but am just trying to understand the basics with it first before I go onto more complex methods. Also, we are using indicators from SPY and applying it to trade S&P future since it is easier to get this data.

I seem to get runtime errors with different starting dates. Try running it from the beginning  of the year and then try running it more recent. I think the main problems with it are the calendars used between regular US equities and futures. Thank you for the help I appreciate it.

Update Backtest








Sorry it will not allow me to attach a backtest

0


I have written a simple algorithm as an example.
Basically, it will look for the sma cross and buy/sell the nearest futures contract and liquidate the position at the end of the day.
The SMA Cross will just define the quantity:

var quantity = _sma5 > _sma9 ? 2 : -2;

since the logics for entering the trade is similar.

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.


Thanks so much for the response. I ran your code and ran into this problem and wanted to see if you were aware of it.

The begining it runs correctly and chooses the right future contract (ESH17) shown in the first pic. Though, in march when it switches to ESM17 it flatlines and keeps buying and selling at the same price for a loss. I attached the pictures to show. Let me know what you think. Thanks again

Here is the overview picture and you can see in march where the first contract expire it stops working correctly

 

Below is the ESH17 and everything looks right

 

Below is the ESM17 contract and when it is supposed to switch to this one the program runs into the problem and behaves as in the picture below (buys and sells about 25 cents apart and does not follow the price of the futures contract)

0

Please rerun the code. 
It was a data issue that has been fixed.

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