Overall Statistics
Total Trades
685
Average Win
0.02%
Average Loss
-0.03%
Compounding Annual Return
-91.750%
Drawdown
4.700%
Expectancy
-0.582
Net Profit
-4.672%
Sharpe Ratio
-28.649
Loss Rate
75%
Win Rate
25%
Profit-Loss Ratio
0.66
Alpha
-1.482
Beta
-32.124
Annual Standard Deviation
0.07
Annual Variance
0.005
Information Ratio
-28.74
Tracking Error
0.07
Treynor Ratio
0.062
Total Fees
$5072.87
import numpy as np
from decimal import Decimal

### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class BasicTemplateAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(2013,1,7)  #Set Start Date
        self.SetEndDate(2013,1,11)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        self.aaa = self.AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute)
        #self.aaa = self.AddForex("EURUSD", Resolution.Minute)
        self.syl = self.aaa.Symbol
        self.trade_flag = 0
    
    
        self.window = RollingWindow[TradeBar](2)
        
        self.Schedule.On(self.DateRules.EveryDay(self.syl), self.TimeRules.At(9, 59), Action(self.Start))
        self.Schedule.On(self.DateRules.EveryDay(self.syl), self.TimeRules.At(14, 59), Action(self.End))
        
        self.SetWarmUp(32, Resolution.Minute)
        self.holding = "none"
    
    def Start(self):
        self.trade_flag = 1
    
    def End(self):
        self.trade_flag = 0
        self.SetHoldings(self.syl, 0)
        self.holding = "none"
        
    def OnData(self, data):
        
        if data.ContainsKey(self.syl):
            self.window.Add(data[self.syl])
            
        if not (self.window.IsReady): return

        
        currBar = self.window[0]
        pastBar = self.window[1]
        
        try:
            past_change = 100* (pastBar.Close - pastBar.Open)/pastBar.Open
        except:
            return
        
        tol = 0.001
        
        
        if self.trade_flag == 1:
            
            if self.holding == "none":
                
                if past_change > tol:
                    
                    self.SetHoldings(self.syl, -1)
                    self.holding = "short"
        
                if past_change < -tol:
                    
                    self.SetHoldings(self.syl, 1)
                    self.holding = "long"
                    
                
            if self.holding == "short":
                    
                if past_change > 0:
                    return
                    
                if 0 > past_change > -tol:
                    self.SetHoldings(self.syl, 0)
                    self.holding = "none"
                        
                if past_change < -tol:
                    self.SetHoldings(self.syl, 1)
                    self.holding = "long"
                        
            if self.holding == "long":
                    
                if past_change < 0:
                    return
                    
                if tol > past_change > 0:
                    self.SetHoldings(self.syl, 0)
                    self.holding = "none"
                    
                if past_change > tol:
                    self.SetHoldings(self.syl, -1)
                    self.holding = "short"
            
            
            
#End