| Overall Statistics |
|
Total Trades 24 Average Win 0.08% Average Loss 0% Compounding Annual Return 0.920% Drawdown 0.600% Expectancy 0 Net Profit 0.233% Sharpe Ratio 0.635 Probabilistic Sharpe Ratio 41.259% Loss Rate 0% Win Rate 100% Profit-Loss Ratio 0 Alpha 0.003 Beta 0.094 Annual Standard Deviation 0.01 Annual Variance 0 Information Ratio -0.39 Tracking Error 0.079 Treynor Ratio 0.069 Total Fees $120.00 Estimated Strategy Capacity $6000000.00 Lowest Capacity Asset MWD R735QTJ8XC9X Portfolio Turnover 0.43% |
#region imports
from AlgorithmImports import *
#endregion
from datetime import datetime
from datetime import timedelta
class BasicTemplateAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2023,6,21)
self.SetCash(10000000)
self.Data_Symbol = {}
tickers = ["SPY", "AAPL", "MSFT", "AMZN", "INTC","MS","VZ","COP","UNP","AMGN","NEE", ]
self.SetWarmUp(30, Resolution.Daily)
for stock in tickers:
symbol = self.AddEquity(stock, Resolution.Minute).Symbol
self.Data_Symbol[symbol] = SymbolData(self, symbol)
## Dictionary for whether we can trade this symbol; we will reset this at end of each day
self.canTradeSymbol = { symbol: True for symbol in self.Data_Symbol.keys() }
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.BeforeMarketClose('SPY', 0),
self.ResetSymbolTraded)
self.Schedule.On(self.DateRules.EveryDay("SPY"),
self.TimeRules.Every(timedelta(minutes=1)),
self.EveryDayAfterMarketOpen)
def EveryDayAfterMarketOpen(self):
if self.IsWarmingUp: return
for symbol, symbol_data in self.Data_Symbol.items():
if not symbol_data.cci.IsReady: continue
holdings = self.Portfolio[symbol]
invested = holdings.Invested
nowprice = holdings.Price
aveprice = holdings.AveragePrice
quantity = holdings.Quantity
OpenOrders = self.Transactions.GetOpenOrders(symbol)
cci = symbol_data.cci.Current.Value
close = symbol_data.close.Current.Value
openprice = symbol_data.open.Current.Value
high = symbol_data.high.Current.Value
low = symbol_data.low.Current.Value
# Check whether we can trade this symbol
can_trade = self.canTradeSymbol[symbol]
if not can_trade:
continue
if OpenOrders: return #Store only 1 order per ticker
#Buy symbol if CCI < 100
if (not invested) and (cci < -100) and nowprice > openprice:
self.MarketOrder(symbol, 1000)
self.Debug(f'Bought {symbol} at {self.Time}')
# Sell symbol
if invested and (nowprice > aveprice * 1.05):
self.MarketOrder (symbol, (-1 * quantity))
self.Debug(f'Liquidated {symbol} at {self.Time}.')
# Now after a buy/sell, we prevent future buys for the rest of the day
self.canTradeSymbol[symbol] = False
def ResetSymbolTraded(self):
## We reset this to Trade at end of each day
self.canTradeSymbol = { symbol: True for symbol in self.Data_Symbol.keys() }
class SymbolData:
def __init__ (self,algo,symbol):
self.algorithm = algo
self.symbol = symbol
#CCI Functions
self.cci = algo.CCI(symbol, 14, MovingAverageType.Simple, Resolution.Daily)
self.close = algo.Identity(symbol, Resolution.Daily,Field.Close)
self.open = algo.Identity(symbol, Resolution.Daily,Field.Open)
self.high = algo.Identity(symbol, Resolution.Daily,Field.High)
self.low = algo.Identity(symbol, Resolution.Daily,Field.Low)