-
Notifications
You must be signed in to change notification settings - Fork 0
/
TradingRange.py
74 lines (56 loc) · 3 KB
/
TradingRange.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#AI generated code extracted from Sapate, Uttam B, Trading Range Breakout Test on Daily Stocks of Indian Markets (November 10, 2017).
#Available at SSRN: https://ssrn.com/abstract=3068852 or http://dx.doi.org/10.2139/ssrn.3068852
from AlgorithmImports import *
class MyTradingAlgorithm(QCAlgorithm):
def Initialize(self):
# Set the start and end date for backtesting
self.SetStartDate(2024, 1, 1)
self.SetEndDate(2024, 12, 1)
# Set the initial cash balance
self.SetCash(100000)
# Add the assets to trade
self.symbols = [self.AddEquity("SPY", Resolution.Daily).Symbol,
self.AddEquity("AAPL", Resolution.Daily).Symbol]
# Set the benchmark
self.SetBenchmark("SPY")
# Initialize indicators
self.sma_short = {symbol: self.SMA(symbol, 50, Resolution.Daily) for symbol in self.symbols}
self.sma_long = {symbol: self.SMA(symbol, 200, Resolution.Daily) for symbol in self.symbols}
# Initialize risk management parameters
self.stop_loss_pct = 0.05
self.trailing_stop_pct = 0.02
# Initialize portfolio construction parameters
self.max_position_size = 0.5 # Max 50% of portfolio in one position
# Schedule the rebalancing function
self.Schedule.On(self.DateRules.MonthStart(), self.TimeRules.AfterMarketOpen("SPY", 30), self.Rebalance)
def OnData(self, data):
for symbol in self.symbols:
if not data.ContainsKey(symbol):
continue
# Check if indicators are ready
if not self.sma_short[symbol].IsReady or not self.sma_long[symbol].IsReady:
continue
# Strategy logic: Golden Cross
if self.sma_short[symbol].Current.Value > self.sma_long[symbol].Current.Value:
self.SetHoldings(symbol, self.max_position_size)
else:
self.Liquidate(symbol)
# Risk management: Trailing stop loss
if self.Portfolio[symbol].Invested:
self.SetTrailingStopLoss(symbol, self.trailing_stop_pct)
def Rebalance(self):
# Rebalance the portfolio
for symbol in self.symbols:
self.SetHoldings(symbol, self.max_position_size)
def SetTrailingStopLoss(self, symbol, trailing_stop_pct):
# Set a trailing stop loss for the given symbol
if self.Portfolio[symbol].Invested:
stop_price = self.Portfolio[symbol].Price * (1 - trailing_stop_pct)
self.StopMarketOrder(symbol, -self.Portfolio[symbol].Quantity, stop_price)
def OnOrderEvent(self, orderEvent):
# Log order events for debugging
self.Debug(f"Order event: {orderEvent}")
def OnEndOfAlgorithm(self):
# Log final portfolio value
self.Debug(f"Final Portfolio Value: {self.Portfolio.TotalPortfolioValue}")
# Helper functions can be added here if needed