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
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Data import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from QuantConnect.Python import PythonQuandl
# from QuantConnect.Data.Custom import Quandl

from datetime import timedelta

import pandas as pd


class Example(QCAlgorithm):
    '''TODO: XXX  Write a comment about the strategy'''

    def Initialize(self):
        # Backtest initialization
        # self.SetStartDate(2011, 1, 1)
        # self.SetEndDate(2013, 2, 1)
        # self.SetEndDate(2018, 1, 1)
        
        self.SetStartDate(2012, 6, 12)
        self.SetEndDate(2017, 7, 18)
        self.SetCash(100000)

        self.ASSET_UNDERLYING = 'SPXL'
        
        self.OPEN_TRADNG_HOURS = [10]
        
        self._daily_history = {}


        # Add the equities to universe
        self._asset_underlying = self.AddEquity(self.ASSET_UNDERLYING, Resolution.Minute)

        # Work with split adjustment (not dividend adjustment)
        self._asset_underlying.SetDataNormalizationMode(DataNormalizationMode.SplitAdjusted)


        oneDayConsolidator = self.ResolveConsolidator(self.ASSET_UNDERLYING, Resolution.Daily)
        oneDayConsolidator.DataConsolidated += self.daily_history_consolidator
        self.SubscriptionManager.AddConsolidator(self.ASSET_UNDERLYING, oneDayConsolidator)

        # Set trading events
        for minutes in self.OPEN_TRADNG_HOURS:
            self.Schedule.On(
                self.DateRules.EveryDay(self.ASSET_UNDERLYING),
                self.TimeRules.AfterMarketOpen(self.ASSET_UNDERLYING, minutes),
                Action(self._trade)
            )


    def daily_history_consolidator(self, sender, bar):
        ticker = bar.Symbol.Value
        current_quotes_df = pd.DataFrame(
            {
                'open': [float(bar.Open)],
                'high': [float(bar.High)],
                'low': [float(bar.Low)],
                'close': [float(bar.Close)],
                'volume': [int(bar.Volume)]
            },
            index=[pd.to_datetime(self.Time)]
        )

        self.Log("Ticker: %s" % ticker)
        if ticker in self._daily_history:
            self._daily_history[ticker] = self._daily_history[ticker].append(
                current_quotes_df)
        else:
            self._daily_history[ticker] = current_quotes_df


    def _trade(self):
        if self.ASSET_UNDERLYING not in self._daily_history:
            # No history enough
            return
        # Get all historic quotes
        underlying_price = float(self.Securities[self.ASSET_UNDERLYING].Price)

        self.Log('Asset %s close price: %.02f' % (self.ASSET_UNDERLYING, underlying_price))