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)