Overall Statistics
Total Trades
35
Average Win
0.22%
Average Loss
-0.12%
Compounding Annual Return
27.995%
Drawdown
1.300%
Expectancy
1.053
Net Profit
2.095%
Sharpe Ratio
4.691
Loss Rate
29%
Win Rate
71%
Profit-Loss Ratio
1.91
Alpha
0.105
Beta
0.676
Annual Standard Deviation
0.053
Annual Variance
0.003
Information Ratio
0.91
Tracking Error
0.039
Treynor Ratio
0.37
Total Fees
$49.67
import math
import numpy as np
import pandas as pd
import talib

class BasicTemplateAlgorithm(QCAlgorithm):

    def Initialize(self):

        self.SetCash(100000)
        self.SetStartDate(2017, 1, 1)
        self.SetEndDate(2017, 1, 31)
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)
            
        # Add securities 
        self.equities = ["SPY","QQQ"]
        # Get the data
        self.SMA1eq = []
        self.SMA5eq = []
        for s in self.equities:
            self.AddEquity(s, Resolution.Minute)
            self.SMA1eq.append(self.SMA(s, 1, Resolution.Daily))
            self.SMA5eq.append(self.SMA(s, 5, Resolution.Daily))
    
        ## Credit
        # Schedule trades at 10am    
        self.Schedule.On(self.DateRules.EveryDay("SPY"),
                 self.TimeRules.AfterMarketOpen("SPY", 30),       
                 Action(self.Rebalance))
        
    def OnData(self, slice):
        pass
    
    def Rebalance(self):
        
        # global variables
        allocation = 1.0
        vol = 20+1
        min_pct = 0.01
        max_pct = 0.20
        long_mult = 1.0
        short_mult = -1.0
        target_weight = 0.5
        
        for i in range(len(self.equities)):
            #current_price = self.Securities[i].Price ### runtime error if you remove #
            if self.SMA1eq[i].Current.Value >= self.SMA5eq[i].Current.Value:
                self.SetHoldings(self.equities[i], target_weight*long_mult)
            elif self.Portfolio.Invested:
                self.Liquidate()
                
        #self.Debug("{0} : sma1: {1} : sma5: {2}".format(self.credit[i], self.SMA1[i].Current.Value,self.SMA5[i].Current.Value))