Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
-0.06
Tracking Error
0.13
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
from AlgorithmImports import *


class MovingAverageCrossAlgorithm(QCAlgorithm):

    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(2015, 1, 1)  
        self.SetEndDate(datetime.now())  
        self.SetCash(100000) 

        self.symbols = ["TSLA", 
                        "AAPL",
                        "TLT",
                        "SPY"]
                        
        for symbol in self.symbols:
            self.AddEquity(symbol, Resolution.Daily)
            self.fast = self.SMA(symbol, 20, Resolution.Daily)
            self.slow = self.SMA(symbol, 200, Resolution.Daily)

        self.previous = None
        
    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''

        # wait for our slow MA to fully initialize 
        if not self.slow.IsReady:
            return

        # want portfolio to be put through a variable
        holdings = self.Portfolio[self.symbols].Quantity
        weight = len(self.symbols)

        # we only want to go long if we're currently short or flat
        if holdings <= 0:
            # if the fast is greater than the slow, we'll go long
            if self.fast.Current.Value > self.slow.Current.Value: #*(1 + tolerance):
                #self.Log("BUY  >> {0}".format(self.Securities[self.ticker].Price))
                for symbol in self.symbols:
                    self.SetHoldings(symbol, weight)

        # we only want to liquidate if we're currently long
        # if the fast is less than the slow we'll liquidate our long
        if holdings > 0 and self.fast.Current.Value < self.slow.Current.Value:
            #self.Log("SELL >> {0}".format(self.Securities[self.ticker].Price))
                for symbol in self.symbols:
                    self.Liquidate(symbol)

        self.previous = self.Time