| Overall Statistics |
|
Total Orders 4992 Average Win 0.00% Average Loss 0.00% Compounding Annual Return 379.738% Drawdown 5.400% Expectancy -0.117 Start Equity 2000000 End Equity 2151532.98 Net Profit 7.577% Sharpe Ratio 2.862 Sortino Ratio 3.765 Probabilistic Sharpe Ratio 60.044% Loss Rate 72% Win Rate 28% Profit-Loss Ratio 2.13 Alpha 1.629 Beta -1.574 Annual Standard Deviation 0.307 Annual Variance 0.094 Information Ratio 0.894 Tracking Error 0.449 Treynor Ratio -0.558 Total Fees $6985.38 Estimated Strategy Capacity $91000.00 Lowest Capacity Asset QQQ XUERCY3UAAUE|QQQ RIWIV7K5Z9LX Portfolio Turnover 964.76% |
from AlgorithmImports import *
import pandas as pd
import numpy as np
class FormalAsparagusFrog(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 12, 1)
self.SetEndDate(2021, 12, 17)
self.SetCash(2000000)
equity = self.AddEquity('QQQ', Resolution.Minute)
option = self.AddOption('QQQ', Resolution.Minute)
self.symbol = option.Symbol
self.stock = equity.Symbol
option.SetFilter(-30, 30, 0, 500)
self.trade = True
self.contract = None
self.size = 203
self.sign = 1 # Buying ITM calls
self.highest_pnl = 0 # Track the highest PnL
def OnData(self, slice: Slice) -> None:
if self.trade:
chain = slice.OptionChains.get(self.symbol)
if chain:
# Select ITM calls (strike price lower than the underlying price)
contract_list = [x for x in chain if x.Expiry == DateTime(2021, 12, 17) and x.Right == 0 and x.Strike < x.UnderlyingLastPrice]
self.itm_call_strike = sorted(contract_list, key=lambda x: x.Strike, reverse=True)[0].Strike
self.contract = [contract for contract in contract_list if contract.Strike == self.itm_call_strike][0]
# Calculate historical volatility
daily_returns = self.History(self.stock, 25, Resolution.Daily)['close'].pct_change()[1:]
volatility = daily_returns.std() * 252**0.5 # Annualized volatility
self.Log(volatility)
self.Log(self.contract.ImpliedVolatility)
# Trading decision based on volatility comparison
if volatility > self.contract.ImpliedVolatility:
self.sign = 1 # Buy ITM call
else:
self.sign = -1 # Sell ITM call
# Check margin availability before placing trade
margin_remaining = self.Portfolio.MarginRemaining
required_margin = self.contract.LastPrice * self.size
if margin_remaining > required_margin:
self.trade = False
self.size = self.size * self.sign
self.MarketOrder(self.contract.Symbol, self.size)
self.MarketOrder(self.stock, -100 * self.size * self.contract.Greeks.Delta) # Hedge by shorting stock
self.Debug(f"Delta: {self.contract.Greeks.Delta}")
self.previous = self.contract.Greeks.Delta
else:
chain = slice.OptionChains.get(self.symbol)
if chain:
contract_list = [x for x in chain if x.Expiry == DateTime(2021, 12, 17) and x.Right == 0 and x.Strike < x.UnderlyingLastPrice]
self.itm_call_strike = sorted(contract_list, key=lambda x: x.Strike, reverse=True)[0].Strike
self.contract = [contract for contract in contract_list if contract.Strike == self.itm_call_strike][0]
self.MarketOrder(self.stock, -100 * self.size * (self.contract.Greeks.Delta - self.previous))
self.previous = self.contract.Greeks.Delta
# Track highest PnL
current_pnl = self.Portfolio.TotalProfit
if current_pnl > self.highest_pnl:
self.highest_pnl = current_pnl
self.Debug(f"New highest PnL: {self.highest_pnl}")