| Overall Statistics |
|
Total Orders 109 Average Win 7.75% Average Loss -3.41% Compounding Annual Return 20.785% Drawdown 29.700% Expectancy 1.666 Start Equity 1000000 End Equity 17297761.74 Net Profit 1629.776% Sharpe Ratio 0.828 Sortino Ratio 0.884 Probabilistic Sharpe Ratio 26.823% Loss Rate 19% Win Rate 81% Profit-Loss Ratio 2.27 Alpha 0 Beta 0 Annual Standard Deviation 0.166 Annual Variance 0.028 Information Ratio 0.932 Tracking Error 0.166 Treynor Ratio 0 Total Fees $8174.84 Estimated Strategy Capacity $240000000.00 Lowest Capacity Asset GOOCV VP83T1ZUHROL Portfolio Turnover 0.49% |
import datetime as dt
from random import random
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
def on_data(self, data: Slice):
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