| Overall Statistics |
|
Total Trades 1807 Average Win 0.00% Average Loss 0.00% Compounding Annual Return 0.821% Drawdown 0.800% Expectancy 1.201 Net Profit 2.487% Sharpe Ratio 0.949 Loss Rate 15% Win Rate 85% Profit-Loss Ratio 1.59 Alpha -0.004 Beta 0.626 Annual Standard Deviation 0.009 Annual Variance 0 Information Ratio -1.36 Tracking Error 0.009 Treynor Ratio 0.013 Total Fees $1807.00 |
import datetime
import random
class TestAlgo(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2014, 10, 16) # Set Start Date
self.SetEndDate(2017, 10, 16)
self.SetCash(100000) # Set Strategy Cash
# Set Parameter
self.sellSize = 1
#self.future = self.AddFuture("OJ", Resolution.Hour)
self.future = self.AddSecurity(SecurityType.Equity, "SPY", Resolution.Hour)
self.sym = self.future.Symbol
self.Debug('New asset added - Symbol: %s, Resolution: %s, Type: %s' % (self.future.Symbol, self.future.Resolution, self.future.Type))
self.Debug('is portfolio invested? {}'.format(self.Portfolio.Invested))
self.Debug(datetime.datetime(2014, 10, 16))
def OnData(self, data):
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
Arguments:
data: Slice object keyed by symbol containing the stock data
'''
#self.Debug(f'Symbol present? {data.ContainsKey(self.future.Symbol)}')
#self.Debug(f'w/o bars: {data[self.sym].Low} vs. w/ bars: {data.Bars[self.sym].Low}')
if data.Time > datetime.datetime(2016, 10, 16) and data.Bars.ContainsKey(self.sym):
if self.Portfolio[self.future.Symbol].Quantity < 100:
self.buyOrder = self.MarketOrder(self.future.Symbol, 1, self.Time, "Buy")
# order is still an orderTicket with no Price, not filled yet
#self.stopLoss = self.StopMarketOrder(self.future.Symbol, -1, (self.buyOrder.Price * .9))#, self.Time, "fdsfs")
# StopMarketOrder with tag unrecognized... :(
#self.stopLoss = self.StopMarketOrder(self.future.Symbol, -1.0, (data.Bars[self.sym].Close * .9), self.Time, "Stop Loss")
self.stopLoss = self.StopMarketOrder(self.future.Symbol, -1.0, (data.Bars[self.sym].Close * .9))
if self.Portfolio[self.future.Symbol].Quantity >= 1:
if random.random() >= .5:
self.MarketOrder(self.future.Symbol, -self.sellSize, self.Time, "Sell")
def OnOrderEvent(self, OrderEvent):
"""Event when the order is filled. Debug log the order fill. :OrderEvent:"""
if OrderEvent.FillQuantity == 0:
return
else:
#self.Debug(f'tag: {self.buyOrder.Tag}')
order = self.Transactions.GetOrderById(OrderEvent.OrderId)
self.Debug("Order Filled! Symbol: {}. Quantity: {}. Direction: {}. Price: {}. Tag: {}. Total Holding {}."
.format(str(OrderEvent.Symbol),
str(OrderEvent.FillQuantity),
str(OrderEvent.Direction),
str(OrderEvent.FillPrice),
order.Tag,
self.Portfolio[self.future.Symbol].Quantity))