| Overall Statistics |
|
Total Orders 109 Average Win 7.79% Average Loss -4.17% Compounding Annual Return 20.269% Drawdown 30.800% Expectancy 1.336 Start Equity 1000000 End Equity 16215114.08 Net Profit 1521.511% Sharpe Ratio 0.803 Sortino Ratio 0.857 Probabilistic Sharpe Ratio 23.398% Loss Rate 19% Win Rate 81% Profit-Loss Ratio 1.87 Alpha 0 Beta 0 Annual Standard Deviation 0.168 Annual Variance 0.028 Information Ratio 0.906 Tracking Error 0.168 Treynor Ratio 0 Total Fees $8350.09 Estimated Strategy Capacity $340000000.00 Lowest Capacity Asset GOOCV VP83T1ZUHROL Portfolio Turnover 0.50% |
import datetime as dt
from random import random, seed
from AlgorithmImports import *
def add_months(current_date, months_to_add):
year = current_date.year
month = current_date.month + months_to_add
while month > 12:
year += 1
month -= 12
new_date = dt.datetime(year, month, 1)
while new_date.weekday() >= 5:
new_date += dt.timedelta(days=1)
return new_date
class MyAlgorithm(QCAlgorithm):
def initialize(self):
self.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, AccountType.CASH)
self.tickers = ["MSFT", "AAPL", "SPY", "GOOG"]
self.hold_months = 12
self.END_YEAR = 2025
self.set_start_date(2010, 1, 1)
self.set_end_date(self.END_YEAR, 2, 1)
self.set_cash(1000000)
for ticker in self.tickers:
self.add_equity(ticker, Resolution.DAILY, leverage=1)
self.next_sell_time = self.Time
self.next_buy_time = self.Time
seed(2)
def on_data(self, data):
if self.Time.weekday() >= 5:
return
if self.Time > dt.datetime(self.END_YEAR, 1, 1):
self.liquidate()
elif self.next_sell_time <= self.Time:
self.liquidate()
self.next_sell_time = add_months(self.Time, self.hold_months)
self.next_buy_time = self.Time + dt.timedelta(days=5)
elif self.next_buy_time <= self.Time:
random_allocations = {ticker: random() for ticker in self.tickers}
total = sum(random_allocations.values())
random_allocations = {ticker: 0.99 * percentage / total for ticker, percentage in random_allocations.items()}
self.log(f"{sum(random_allocations.values())} {random_allocations}")
for ticker, percentage in random_allocations.items():
self.set_holdings(ticker, percentage)
self.next_buy_time += dt.timedelta(days=100000) # Never