| Overall Statistics |
|
Total Orders 586 Average Win 0.38% Average Loss -0.57% Compounding Annual Return -9.577% Drawdown 13.000% Expectancy -0.016 Start Equity 100000 End Equity 95087.75 Net Profit -4.912% Sharpe Ratio -0.783 Sortino Ratio -0.822 Probabilistic Sharpe Ratio 11.280% Loss Rate 41% Win Rate 59% Profit-Loss Ratio 0.66 Alpha -0.095 Beta -0.163 Annual Standard Deviation 0.146 Annual Variance 0.021 Information Ratio -1.264 Tracking Error 0.182 Treynor Ratio 0.701 Total Fees $1462.50 Estimated Strategy Capacity $17000000.00 Lowest Capacity Asset AAPL R735QTJ8XC9X Portfolio Turnover 302.90% |
#region imports
from AlgorithmImports import *
#endregion
"""
QC Example 1 -- Buy Low and Sell High
"""
# You need to import necessary Python libraries
from AlgorithmImports import *
class Example1(QCAlgorithm):
def Initialize(self):
# In this function, you set up all global parameters for your algo
# Use "self." to denote the class variables to be used across all methons within the class
# Set the backtest period
self.SetStartDate(2024, 2, 1)
self.SetEndDate(2024, 8, 1)
# Use this to get more data beyond the backtest period. In the example
# below, you get data 10 days before start date
self.SetWarmup(10)
# Set the initial capital
self.SetCash(100000)
# We are dealing with only one stock
self.ticker = 'AAPL'
# Add an equity with this ticker symbol and daily market data
self.AddEquity(self.ticker, Resolution.Hour)
self.sym = self.Symbol(self.ticker)
# Keep track of backtesting days/time steps
self.count = 0
def OnData(self, data):
# This is called once for each backtesting time step
# Get the price at the time
ticket = None
if self.IsWarmingUp: return
self.px = self.Securities[self.ticker].Price
# Print time and price for debugging purpose
self.Debug(" Time: " + str(self.Time) + " Price: " + str(self.px))
if self.count >= 1: # Ignore the first date
retn = (self.px - self.px0) / self.px0
self.Debug("Return " + str(retn) + " px " + str(self.px) + "-" + str(self.px0))
if retn > 0 and not self.Portfolio[self.ticker].IsShort:
ticket = self.MarketOrder(self.sym, -500)
elif retn < 0 and not self.Portfolio[self.ticker].IsLong:
ticket = self.MarketOrder(self.sym, 500)
if ticket:
self.Debug(f"Quantity filled: {ticket.QuantityFilled}; Fill price: {ticket.AverageFillPrice}")
self.count = self.count + 1
# This is the previous price vs the current price self.px
self.px0 = self.px