Overall Statistics
Total Trades
14
Average Win
0.02%
Average Loss
-0.03%
Compounding Annual Return
-5.499%
Drawdown
0.200%
Expectancy
-0.744
Net Profit
-0.134%
Sharpe Ratio
-12.329
Loss Rate
86%
Win Rate
14%
Profit-Loss Ratio
0.79
Alpha
-0.041
Beta
-0.035
Annual Standard Deviation
0.004
Annual Variance
0
Information Ratio
-5.631
Tracking Error
0.047
Treynor Ratio
1.365
Total Fees
$14.00
import clr
clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import numpy as np
import pandas as pd

class TwiceADay(QCAlgorithm):

    def Initialize(self):
        self.SetCash(100000)
        # Start and end dates for the backtest.
        self.SetStartDate(2017,1,30)
        self.SetEndDate(2017,2,7)
        self.SetBenchmark("SPY")
        self.spy = self.AddEquity("SPY", Resolution.Minute)
        self.trade_symbols=['SPY']

        # store fitted models here
        self.models=[]
        self.Schedule.On(self.DateRules.EveryDay("SPY"),  self.TimeRules.AfterMarketOpen("SPY",15),Action(self.openingTrade))
        self.Schedule.On(self.DateRules.EveryDay("SPY"),  self.TimeRules.BeforeMarketClose("SPY",15),Action(self.closingTrade))
        
        
    def openingTrade(self):
        for s in self.trade_symbols:
            ''' get a short history of daily data '''
            data_daily=self.History(s,5,Resolution.Daily)
            df=[]
            for slice in data_daily:
                df.append([np.float(slice.Close),np.float(slice.Open),np.float(slice.High), np.float(slice.Low),np.float(slice.Volume)])
            df=pd.DataFrame(df, columns=['c','o','h','l','v'])
            ''' sample signal '''
            signal=(df.c-df.o).mean()/(df.h-df.l).mean()
            ''' place a trade '''
            self.SetHoldings(s,signal)
            
    def closingTrade(self):
        for s in self.trade_symbols:
            if self.Portfolio[s].AbsoluteQuantity>0:
                self.SetHoldings(s,0.0)
                
    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))