Hi,
I am trying to create a simple strategy for Futures Nasdaq:
Calculate return of the instrument from 9:30 to 15:30. If the return is positive, open a long position for last 30 min of the session. If it is negative, then open a short position for the last 30 min of the session.
Unfortunately, the strategy does not open any positions. Could you please help me with this ?
from AlgorithmImports import *
from datetime import datetime
import numpy as np
from datetime import timedelta
class MomentumAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2023, 1, 1)
self.SetCash(100000)
self.opening_price = None
self.position_value = 0
self.open_position = False
self.closing_price = None
self.pnl = []
self.accuracy = []
self.cumulative_profit = 0.0
self.initial_capital = 100000
self.er = self.AddFuture(Futures.Indices.NASDAQ100EMini)
self.contract = None
self.Schedule.On(self.DateRules.EveryDay(self.er.Symbol), self.TimeRules.At(9, 30), self.OnMarketOpen)
self.Schedule.On(self.DateRules.EveryDay(self.er.Symbol), self.TimeRules.At(15, 30), self.OnTradeBar)
self.Schedule.On(self.DateRules.EveryDay(self.er.Symbol), self.TimeRules.At(16, 0), self.OnEndOfDay)
def OnData(self, data):
for chain in data.FutureChains.Values:
for contract in chain.Contracts.Values:
self.contract = contract
break
break
def OnMarketOpen(self):
if self.contract is None or self.contract.Symbol not in self.CurrentSlice.Bars:
return
bar = self.CurrentSlice.Bars[self.contract.Symbol]
self.opening_price = bar.Open
def OnTradeBar(self):
if self.contract is None or self.contract.Symbol not in self.CurrentSlice.Bars or self.opening_price is None:
return
bar = self.CurrentSlice.Bars[self.contract.Symbol]
return_value = bar.Close - self.opening_price
# opening long/short at 15:30
if return_value > 0:
self.position_value = bar.Close
self.open_position = "long"
self.SetHoldings(self.contract.Symbol, 1)
elif return_value < 0:
self.position_value = bar.Open
self.open_position = "short"
self.SetHoldings(self.contract.Symbol, -1)
def OnEndOfDay(self):
if self.contract is None:
return
if self.contract.Symbol not in self.CurrentSlice.Bars or not self.open_position:
return
bar = self.CurrentSlice.Bars[self.contract.Symbol]
return_value = bar.Close - bar.Open
# check for end of session at 16:00
if self.open_position == "long":
self.closing_price = bar.Open
elif self.open_position == "short":
self.closing_price = bar.Close
if self.closing_price:
self.Liquidate(self.contract.Symbol)
Mia Alissi
From the provided code, it seems like you are trying to implement a strategy that trades based on the return of the NASDAQ100EMini futures contract from 9:30 to 15:30. If the return is positive, you open a long position for the last 30 minutes of the session, and if it's negative, you open a short position for the last 30 minutes.
However, there are a few issues with your code that might be causing the strategy not to open any positions:
Contract Selection: In your
OnData
method, you are selecting the first contract from the futures chain. However, this might not always be the front month contract. You should filter and sort the contracts based on expiry and then select the front month contract.Market Hours: You are scheduling events at 9:30, 15:30, and 16:00. However, futures markets do not follow the same market hours as equity markets. The NASDAQ100EMini futures contract trades nearly 24 hours a day, from Sunday evening to Friday evening. You need to adjust your schedule according to the futures market hours.
Position Opening/Closing: In your
OnTradeBar
method, you are opening positions based on the return value and setting holdings to 1 or -1. However, in yourOnEndOfDay
method, you are not checking if the position is open before trying to close it. You should add a check to ensure that a position is open before trying to close it.Here is a simplified version of your code with the above issues addressed:
```python class MomentumAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2023, 1, 1) self.SetCash(100000)
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.
Sebastian Wozniczka
I asked ChatGPT for help as well, but could not help me unfortunately..
Sebastian Wozniczka
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!