I am trying to use Parabolic SAR on the e-mini SPX. I have seen the example here when using it on forex:
I'm using the Futures Template and this is what my code looks like right now:
class BasicTemplateFuturesAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 1, 27)
self.SetEndDate(2019, 1, 28)
self.SetCash(1000000)
#Set timezone
self.SetTimeZone(TimeZones.NewYork)
#Brokerage model and account type:
self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
# Subscribe and set our expiry filter for the futures chain
futureES = self.AddFuture(Futures.Indices.SP500EMini)
futureES.SetFilter(timedelta(0), timedelta(182))
futureGC = self.AddFuture(Futures.Metals.Gold)
futureGC.SetFilter(timedelta(0), timedelta(182))
benchmark = self.AddEquity("SPY");
self.SetBenchmark(benchmark.Symbol);
# sar info
self.psar = self.PSAR(self.Futures.Indices.SP500EMini, 0.02, 0.02, 0.2, Resolution.Daily)
The line used in the Forex example is:
self.psar = self.PSAR(self.forex.Symbol, 0.02, 0.02, 0.2, Resolution.Daily)
I've tried a lot of different combinations like this:
self.psar = self.PSAR(self.futures.Symbol, 0.02, 0.02, 2, Resolution.Daily)
self.psar = self.PSAR(futureES, 0.02, 0.02, 2, Resolution.Daily)
psar = PSAR(futureES, 0.02, 0.02, 2, Resolution.Daily)
I just keep getting errors either that my object doesn't have the attribute or something along those lines.
Does anyone know how to use the parabolic sar indicator on futures contracts?
Halldor Andersen
Hi Austin.
Constructing an indicator using future prices is a challenge as future contracts expire on their expiration date. By splicing several futures contracts together, it is possible to create a 'continuous futures contract', an extended price series. Constructing an indicator based on the extended price series can be done, but it is more difficult than establishing a trading signal using price series from an ETF, such as SPY.
In the attached backtest, I've demonstrated how to trade the S&P500 E-Mini futures contract based on a trading signal produced by the indicator PSAR, using SPY price series.
Austin P Doty
I see ok, thanks for your help Halldor.
That's a good workaround, but it wont make any trades after 4pm EST right? As SPY is closed and ES is still running all night.
Also I had a question on going short on SPY, as self.Liquidate() is just going to close your open positions correct? How would you enter a short on SPY with MarketOrder?
Thanks for your help!
Halldor Andersen
Hi Austin.
I'm glad I could help.
You can specify a condition using self.exchange.ExchangeOpen to trade only during regular market hours, when SPY is active.
First, in the method Initialize(), we need to specify the exchange for SPY:
self.exchange = self.Securities["SPY"].Exchange;
Then, in the method OnData(), we add the condition to return if the exchange is not open:
# Only trade during regular market hours if not self.exchange.ExchangeOpen: return
You are right, self.Liquidate() will close your open positions. To enter a short position on SPY using MarketOrder, enter a negative quantity:
self.MarketOrder(contract.Symbol , -1)
I've attached a backtest for your reference. Good luck!
Austin P Doty
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!