Overall Statistics
Total Trades
38
Average Win
0.05%
Average Loss
-0.03%
Compounding Annual Return
1.144%
Drawdown
0.300%
Expectancy
0.153
Net Profit
0.099%
Sharpe Ratio
1.44
Loss Rate
53%
Win Rate
47%
Profit-Loss Ratio
1.43
Alpha
-0.007
Beta
0.087
Annual Standard Deviation
0.008
Annual Variance
0
Information Ratio
-3.555
Tracking Error
0.056
Treynor Ratio
0.13
Total Fees
$38.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)

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


    def OnData(self, data):
        pass


    def OnOrderEvent(self, orderEvent):
        
        # 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
            if (order.Type == OrderType.MarketOnOpen) and (self.Portfolio.Invested == True):
                
                # Set market on close also
                self.MarketOnCloseOrder(order.Symbol, -100, "goodbye")