| Overall Statistics |
|
Total Trades 7 Average Win 0% Average Loss -0.17% Compounding Annual Return 3.123% Drawdown 6.600% Expectancy -1 Net Profit 13.583% Sharpe Ratio 0.03 Sortino Ratio 0.037 Probabilistic Sharpe Ratio 23.026% Loss Rate 100% Win Rate 0% Profit-Loss Ratio 0 Alpha -0.01 Beta 0.131 Annual Standard Deviation 0.031 Annual Variance 0.001 Information Ratio -0.513 Tracking Error 0.162 Treynor Ratio 0.007 Total Fees $7.00 Estimated Strategy Capacity $43000000.00 Lowest Capacity Asset IWM RV0PWMLXVHPH Portfolio Turnover 0.05% |
# region imports
from AlgorithmImports import *
# endregion
STOCKS = ["SPY", "IWM", "QQQ"]
class MyAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1)
#self.SetEndDate(2016, 12, 3)
self.SetCash(100000)
self.high = {}
self.low = {}
self.symbol = {}
self.resistance_touches = {}
self.support_touches = {}
self.ticket = {}
self.ticket2 = {}
self.open_order ={}
self.stocks = [self.AddEquity(ticker, Resolution.Minute).Symbol for ticker in STOCKS]
for sec in self.stocks:
self.high[sec] = self.MAX(sec, 240, Resolution.Minute)
self.low[sec] = self.MIN(sec, 240, Resolution.Minute)
self.resistance_touches[sec] = 0
self.support_touches[sec] = 0
self.ticket[sec] = None
self.ticket2[sec] = None
self.open_order[sec] = 0
self.SetWarmUp(150)
#self.symbol = self.AddEquity("SPY", Resolution.Minute).Symbol
#self.high = self.MAX(self.symbol, 1, Resolution.Hour)
#self.low = self.MIN(self.symbol, 1, Resolution.Hour)
#self.resistance_touches = 0
#self.support_touches = 0
#self.ticket = None
self.stop = None
self.profit = None
def OnData(self, data):
if self.IsWarmingUp: return
if self.Time.hour < 10: return
for sec in self.stocks:
if not self.high[sec].IsReady or not self.low[sec].IsReady: continue
self.open_order[sec] = self.Transactions.GetOpenOrders(sec)
if self.ticket[sec] and (self.UtcTime - self.ticket[sec].Time).days > 1:
if self.ticket[sec].Status != OrderStatus.Filled:
self.ticket[sec].Cancel("Order cancelled after 1 day")
if self.ticket2[sec] and (self.UtcTime - self.ticket[sec].Time).days > 1:
if self.ticket2[sec].Status != OrderStatus.Filled:
self.ticket2[sec].Cancel("Order cancelled after 1 day")
if not self.Portfolio[sec].Invested and len(self.open_order[sec]) > 0:
continue
if self.Portfolio[sec].Invested: continue
if self.Securities[sec].Close >= self.high[sec].Current.Value or self.Securities[sec].High >= self.high[sec].Current.Value:
self.resistance_touches[sec] += 1
#self.Debug(f"Resistance level touched {self.resistance_touches[sec]} times ---- {sec}")
if self.resistance_touches[sec] >= 10:
quantity = self.CalculateOrderQuantity(sec, 0.1)
self.ticket[sec] = self.StopMarketOrder(sec, quantity, self.high[sec].Current.Value * 1.005, tag = f"{self.Securities[sec].Close}")
# Place limit order logic here
self.resistance_touches[sec] = 0 # Reset the counter
if self.Securities[sec].Close <= self.low[sec].Current.Value or self.Securities[sec].Low <= self.low[sec].Current.Value:
self.support_touches[sec] += 1
#self.Debug(f"Support level touched {self.support_touches[sec]} times ---- {sec}")
if self.support_touches[sec] >= 10:
quantity = self.CalculateOrderQuantity(sec, 0.1)
self.ticket2[sec] = self.StopMarketOrder(sec, -quantity, self.low[sec].Current.Value * 0.995, tag = f"{self.Securities[sec].Close}")
# Place limit order logic here
self.support_touches[sec] = 0 # Reset the counter
def OnOrderEvent(self, orderEvent):
order = self.Transactions.GetOrderById(orderEvent.OrderId)
if order.Status == OrderStatus.Filled and order.Direction == OrderDirection.Buy:
for sec in self.stocks:
if self.ticket[sec] is not None:
if orderEvent.OrderId == self.ticket[sec].OrderId:
quantity = self.Portfolio[order.Symbol].Quantity
fill_price = self.Portfolio[order.Symbol].AveragePrice
stop_price = fill_price * 0.99
profit_price = fill_price * 1.2
self.stop = self.StopMarketOrder(order.Symbol, -quantity, stop_price)
self.profit = self.LimitOrder(order.Symbol, -quantity, profit_price)
self.Debug(f" {self.Time} -- -- Bought {order.Symbol} for {self.Portfolio[order.Symbol].AveragePrice} // quantity -- {quantity} // Limit Price {profit_price} // Stop Price {stop_price}")
continue
continue
if order.Status == OrderStatus.Filled and order.Direction == OrderDirection.Sell:
for sec in self.stocks:
if self.ticket2[sec] is not None:
if orderEvent.OrderId == self.ticket2[sec].OrderId:
quantity = self.Portfolio[order.Symbol].Quantity
fill_price = self.Portfolio[order.Symbol].AveragePrice
stop_price = fill_price * 1.005
profit_price = fill_price * 0.8
self.stop = self.StopMarketOrder(order.Symbol, -quantity, stop_price)
self.profit = self.LimitOrder(order.Symbol, -quantity, profit_price)
self.Debug(f" {self.Time} -- -- Sold {order.Symbol} for {self.Portfolio[order.Symbol].AveragePrice} // quantity -- {quantity} // Limit Price {profit_price} // Stop Price {stop_price}")
continue
continue
if order.Status == OrderStatus.Filled:
if order.Type == OrderType.Limit or order.Type == OrderType.StopMarket:
for sec in self.stocks:
if self.ticket2[sec] is not None and self.ticket[sec] is not None:
if orderEvent.OrderId != self.ticket2[sec].OrderId or orderEvent.OrderId != self.ticket[sec].OrderId:
self.Transactions.CancelOpenOrders(order.Symbol, "Hit Profit price")
self.Debug(f" {self.Time} -- -- Closed {order.Symbol} for {self.Portfolio[order.Symbol].LastTradeProfit} Profit")
continue
continue
continue
#if order.Status == OrderStatus.Filled and order.Type == OrderType.Limit and order.Direction == OrderDirection.Sell:
#self.Transactions.CancelOpenOrders(order.Symbol, "Hit Profit price")
#self.Debug(f" {self.Time} -- -- Closed {order.Symbol} for {self.Portfolio[order.Symbol].LastTradeProfit} Profit")
#if order.Status == OrderStatus.Filled and order.Type == OrderType.StopMarket and order.Direction == OrderDirection.Sell:
#self.Transactions.CancelOpenOrders(order.Symbol, "Hit stop price")
#self.Debug(f" {self.Time} -- -- Closed {order.Symbol} for {self.Portfolio[order.Symbol].LastTradeProfit} Loss")
#if order.Status == OrderStatus.Filled and order.Type == OrderType.Limit and order.Direction == OrderDirection.Buy:
#self.Transactions.CancelOpenOrders(order.Symbol, "Hit Profit price")
#self.Debug(f" {self.Time} -- -- Closed {order.Symbol} for {self.Portfolio[order.Symbol].LastTradeProfit} Profit")
#if order.Status == OrderStatus.Filled and order.Type == OrderType.StopMarket and order.Direction == OrderDirection.Buy:
#self.Transactions.CancelOpenOrders(order.Symbol, "Hit stop price")
#self.Debug(f" {self.Time} -- -- Closed {order.Symbol} for {self.Portfolio[order.Symbol].LastTradeProfit} Loss")