| Overall Statistics |
|
Total Trades 1923 Average Win 1.10% Average Loss -1.11% Compounding Annual Return 0.643% Drawdown 41.700% Expectancy 0.017 Net Profit 1.262% Sharpe Ratio 0.153 Sortino Ratio 0.166 Probabilistic Sharpe Ratio 9.624% Loss Rate 49% Win Rate 51% Profit-Loss Ratio 0.98 Alpha 0.087 Beta 1.474 Annual Standard Deviation 0.426 Annual Variance 0.182 Information Ratio 0.222 Tracking Error 0.36 Treynor Ratio 0.044 Total Fees $66974.11 Estimated Strategy Capacity $370000.00 Lowest Capacity Asset MCY R735QTJ8XC9X Portfolio Turnover 34.56% |
from QuantConnect.Algorithm import QCAlgorithm
from io import StringIO
import pandas as pd
from QuantConnect import Resolution
from datetime import timedelta
from datetime import datetime
class MyAlgorithm(QCAlgorithm):
def Initialize(self):
# Initialize algorithm settings (cash, start-end dates, etc.)
self.SetCash(1000000)
self.SetStartDate(2022, 1, 1)
self.SetEndDate(2023, 12, 15)
data=self.Download("https://docs.google.com/spreadsheets/d/e/2PACX-1vQJGBy6bzWWPkOUIUDUuddF_S8X2YIKtVifCtMfGCKqSMrDn--stcC_rWwEeoy_1kFooypdb9xHvOcr/pub?gid=2050710775&single=true&output=csv")
#rows=[]
try:
headers = ['Start Date', 'Symbol']
df = pd.read_csv(StringIO(data), header=1, names=headers)
# print(df.head())
self.Debug(f"Start Date types: {df['Start Date'].dtype}")
df['Start Date'] = pd.to_datetime(df['Start Date'])
self.Debug(f"Start Date types after conversion: {df['Start Date'].dtype}")
df = df[df['Start Date'] >= self.StartDate]
for symbol in df['Symbol'].unique():
self.AddEquity(symbol, Resolution.Daily)
# self.Debug(f"Added {symbol}")
self.Debug("CSV file successfully loaded.")
except Exception as e:
self.Debug(f"Error loading CSV file: {str(e)}")
return
if 'Start Date' in df.columns:
self.trading_schedule = df.groupby('Start Date')['Symbol'].apply(list).to_dict()
self.Debug("Trading schedule successfully created.")
# for symbol in df['Symbol'].unique():
# self.AddEquity(symbol, Resolution.Minute)
# for date, symbols in self.trading_schedule.items():
# self.Debug(f"{date}: {symbols}")
else:
self.Debug("Error: 'Start Date' column not found in the data.")
def OnData(self, data):
current_date = self.Time
if current_date.weekday() == 4:
self.Liquidate()
self.Debug(f"Liquidating all positions on {current_date}")
if current_date in self.trading_schedule:
symbols_today = self.trading_schedule[current_date]
if len(symbols_today) == 0:
self.Debug(f"No symbols to trade on {current_date}")
return
cash_per_stock = self.Portfolio.Cash / len(symbols_today)
for symbol_str in symbols_today:
symbol = self.Symbol(symbol_str)
for symbol in symbols_today:
if symbol in data and data[symbol] is not None and data[symbol].Price > 0:
quantity = int(cash_per_stock / data[symbol].Price)
self.Buy(symbol, quantity)
self.Debug(f"Buying {quantity} of {symbol} at {data[symbol].Price}")
else:
self.Debug(f"No price data available for {symbol} at {self.Time}")
else:
self.Debug(f"No trading schedule for {current_date}")
# def ExecuteTrades(self):
# week_start_date = None
# week_end_date = None
# current_date = self.Time.strftime("%Y-%m-%d")
# if current_date in self.trading_schedule:
# for symbol in self.trading_schedule[current_date]:
# quantity = 100
# if self.Time.weekday() == 4:
# week_start_date = self.Time
# week_end_date = self.Time + timedelta(days=6)
# self.SetHoldings(symbol, quantity)
# if week_start_date is not None and self.Time >= week_end_date:
# self.Liquidate(symbol)
# week_start_date = None
# week_end_date = None