| Overall Statistics |
|
Total Trades 0 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio 0 Probabilistic Sharpe Ratio 0% Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio -0.596 Tracking Error 0.222 Treynor Ratio 0 Total Fees $0.00 Estimated Strategy Capacity $0 Lowest Capacity Asset |
from AlgorithmImports import *
from QuantConnect.Indicators import *
class MyAlgorithm(QCAlgorithm):
def Initialize(self):
# Set the start and end date for the backtest
self.SetStartDate(2019, 1, 15)
#self.SetEndDate(2022, 12, 31)
# Set the initial cash amount
self.SetCash(100000)
# Add the ETF QQQ to the list of symbols and set the benchmark as QQQ
res = Resolution.Daily
self.STOCKS = [self.AddEquity('QQQ', res).Symbol]
self.benchmark = [self.AddEquity('QQQ', res).Symbol]
self.SetBenchmark(self.benchmark[0])
# Create a Bollinger indicator using Quantconnect.Indicators
self.Bolband = self.BB(self.STOCKS[0], 18, 1, MovingAverageType.Simple, Resolution.Daily)
# Plot Bollinger band
self.PlotIndicator(
"Indicators",
self.Bolband.LowerBand,
self.Bolband.MiddleBand,
self.Bolband.UpperBand,
)
def daily_check(self):
res = Resolution.Daily
self.STOCKS = [self.AddEquity('QQQ', res).Symbol]
# Check if the price of QQQ exceeds the upper Bollinger band
print("Price: ", self.Securities[self.STOCKS[0]].Price, " UpperBand: ", self.Bolband.UpperBand)
if self.Securities[self.STOCKS[0]].Price > self.Bolband.UpperBand:
# buy current price
self.Buy(self.STOCKS[0], 100)
# Check if the price of QQQ falls below the lower Bollinger band
if self.Bolband.LowerBand[-1] > self.Securities[self.STOCKS[0]].Price:
self.Sell(self.STOCKS[0], 100)