Overall Statistics |
Total Trades 119 Average Win 0.07% Average Loss -0.05% Compounding Annual Return -5.192% Drawdown 17.900% Expectancy 0.004 Net Profit -6.534% Sharpe Ratio -0.235 Probabilistic Sharpe Ratio 8.288% Loss Rate 56% Win Rate 44% Profit-Loss Ratio 1.26 Alpha 0.002 Beta -0.363 Annual Standard Deviation 0.166 Annual Variance 0.027 Information Ratio -0.369 Tracking Error 0.407 Treynor Ratio 0.107 Total Fees $119.00 |
import numpy as np import random class CustomModelsAlgorithm(QCAlgorithm): def Initialize(self): self.SetStartDate(2019,1,1) # Set Start Date self.security = self.AddEquity("SPY", Resolution.Hour) self.security.SetFillModel(CustomFillModel(self)) self.spy = self.security.Symbol def OnData(self, data): open_orders = self.Transactions.GetOpenOrders(self.spy) if len(open_orders) != 0: return if self.Time.day > 10 and self.security.Holdings.Quantity <= 0: self.MarketOrder(self.spy, 100, True) elif self.Time.day > 20 and self.security.Holdings.Quantity >= 0: self.MarketOrder(self.spy, -100, True) # inherit Immediate Fill Model class CustomFillModel(ImmediateFillModel): def __init__(self, algorithm): self.algorithm = algorithm self.absoluteRemainingByOrderId = {} def MarketFill(self, asset, order): absoluteRemaining = order.AbsoluteQuantity if order.Id in self.absoluteRemainingByOrderId.keys(): absoluteRemaining = self.absoluteRemainingByOrderId[order.Id] # Create the object fill = super().MarketFill(asset, order) # Set this fill amount fill.FillQuantity = np.sign(order.Quantity) * 10 if absoluteRemaining == fill.FillQuantity: fill.Status = OrderStatus.Filled if self.absoluteRemainingByOrderId.get(order.Id): self.absoluteRemainingByOrderId.pop(order.Id) else: fill.Status = OrderStatus.PartiallyFilled absoluteRemaining = absoluteRemaining - fill.FillQuantity self.absoluteRemainingByOrderId[order.Id] = absoluteRemaining price = self.algorithm.Securities["SPY"].Close self.algorithm.Debug(f"{self.algorithm.Time} - Partial Fill - Remaining {absoluteRemaining} Price - {price}") return fill