Overall Statistics
Total Trades
20
Average Win
0.05%
Average Loss
-0.03%
Compounding Annual Return
-0.367%
Drawdown
0.100%
Expectancy
-0.127
Net Profit
-0.032%
Sharpe Ratio
-0.667
Loss Rate
70%
Win Rate
30%
Profit-Loss Ratio
1.91
Alpha
-0.009
Beta
0.026
Annual Standard Deviation
0.005
Annual Variance
0
Information Ratio
-3.591
Tracking Error
0.059
Treynor Ratio
-0.137
Total Fees
$20.00
#
# QuantConnect Basic Template:
# Fundamentals to using a QuantConnect algorithm.
#
# You can view the QCAlgorithm base class on Github:
# https://github.com/QuantConnect/Lean/tree/master/Algorithm
#

import numpy as np
from datetime import timedelta

class BasicTemplateAlgorithm(QCAlgorithm):

    def Initialize(self):
        # Set the cash we'd like to use for our backtest
        # This is ignored in live trading
        self.SetCash(100000)

        # Start and end dates for the backtest.
        # These are ignored in live trading.
        self.SetStartDate(2017,1,1)
        self.SetEndDate(2017,2,1)

        # Add assets you'd like to see
        self.spy = self.AddEquity("SPY", Resolution.Minute).Symbol
        self.rWindow = RollingWindow[TradeBar](2)

        consolidator = TradeBarConsolidator(timedelta(1))
        consolidator.DataConsolidated += self.OnDailyData
        self.SubscriptionManager.AddConsolidator(self.spy, consolidator)

    def OnDailyData(self, sender, bar):
        self.rWindow.Add(bar) # I added, so can view previous bars..

        # Place open orders:
        self.MarketOnOpenOrder(bar.Symbol, 100, "hello")
        self.Log("OnDailyData triggered")


    def OnData(self, data):
        self.rWindow.Add(data["SPY"])
        pass


    def OnOrderEvent(self, orderEvent):

        # Wait for window to be ready..
        if not self.rWindow.IsReady: return
    
        # First check order filled.. else no need to do anything..
        if orderEvent.Status == OrderStatus.Filled:

            # Get this order..
            order = self.Transactions.GetOrderById(orderEvent.OrderId)

            self.Log("ORDER EVENT")

            # If MarketOnOpen, set two orders; 1) StopMarketOrder 2) MarketOnClose
            if (order.Type == OrderType.MarketOnOpen): # and (self.Portfolio.Invested == True):
                self.Log("Market On Open")

                # Set a stop below yesterday low..

                # Bug could be here 'stop
                self.StopMarketOrder(order.Symbol, -100, self.rWindow[1].Low, "Stop order goodbye")

                # Set market on close also
                self.MarketOnCloseOrder(order.Symbol, -100, "goodbye")


                # If Stop Market Order..
            elif (order.Type == OrderType.StopMarket): ## Not sure why this MarketOnCloseOrder not working..
                self.Log("StopMarketOrder order")

                # Cancel all open orders (i.e. remaining MOC order)..
                self.Transactions.CancelOpenOrders("SPY")



            # If Market on Close..
            elif (order.Type == OrderType.MarketOnClose): ## Not sure why this MarketOnCloseOrder not working..
                self.Log("Market close order")

                # Cancel all open orders (i.e. remaining SM order)..
                self.Transactions.CancelOpenOrders("SPY")