Overall Statistics
Total Trades
8161
Average Win
0.03%
Average Loss
-0.05%
Compounding Annual Return
-4.032%
Drawdown
23.100%
Expectancy
-0.125
Net Profit
-23.104%
Sharpe Ratio
-2.428
Probabilistic Sharpe Ratio
0.000%
Loss Rate
41%
Win Rate
59%
Profit-Loss Ratio
0.49
Alpha
-0.043
Beta
0.015
Annual Standard Deviation
0.017
Annual Variance
0
Information Ratio
-1.052
Tracking Error
0.179
Treynor Ratio
-2.692
Total Fees
$31461.37
#######################   T   R   A   I   N   ########   A   N   D   ##########   P   R   E   D   I   C   T   ##   E   Q   U   I   T   Y   ###################################
# 1. EL Y ES EL CLOSE FILLED ORDER
# 2. Genera df con datos a testear.
# 3. Save y sube el df generado para ser procesado por Train and Plot Research.
# Genera clf para chequeo contra TrainandPlot Research
from datetime import datetime, timedelta
from dateutil import parser
import time
from threading import Timer
from System.Drawing import Color
import numpy as np
import pandas as pd
import sklearn
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
import pandas
import sklearn
import matplotlib.pyplot as plt
import pickle
OnOffGrandSpike = 0
GapMkt = 1 # > 1 qty. veces mayor a TakeProfit
GapTP = 0.00075 # > 0.0009 qty.veces mayor a std. Equity:0.002  Option: 0.005
GapSL = 0.0016 # > 0.0018 qty. veces mayor a std. Equity:0.004   Option: 0.010
#######################   T   R   A   I   N   ########   A   N   D   ##########   P   R   E   D   I   C   T   ##   E   Q   U   I   T   Y   ###################################
class Spikes(QCAlgorithm):
    stopMarketTicket = None
    stopMarketFillTime = datetime.min
    limitTicket = None
    limitFillTime = datetime.min
    modelkey='felipe05'
    long = 0
    short = 0
    Apertura=False
    # Model Object
    model = None
    # Model State Flag
    modelIsTraining = False
    def Initialize(self):
        self.SetStartDate(2014, 8, 6)
        #self.SetEndDate(2016, 9, 16)
        self.SetCash(1000000)
        self.AddEquity('SPY', Resolution.Minute)
#        self.AddEquity('GOOG', Resolution.Minute)
#        option = self.AddOption("SPY")
#        self.option_symbol = option.Symbol
        # set our strike/expiry filter for this option chain
#        option.SetFilter(-3, +3, timedelta(7), timedelta(14))
        #self.UniverseSettings.Resolution = Resolution.Minute
        self.SetUniverseSelection(ManualUniverseSelectionModel("SPY"))
        self.SetBenchmark("SPY")
        self.SetBrokerageModel(BrokerageName.AlphaStreams)
        if not self.LiveMode:
            self.ObjectStore.Delete(self.modelkey)
            if self.ObjectStore.ContainsKey(self.modelkey):
                serializedclf = bytes(self.ObjectStore.ReadBytes(self.modelkey))
                CLF= pickle.loads(serializedclf)
                clfDT=CLF[0]
                clfRF=CLF[1]
                #if type(clfRF) is sklearn.ensemble.forest.RandomForestClassifier and type(clfDT) is sklearn.tree.tree.DecisionTreeClassifier:
                clfok=0
                self.clfDT=clfDT
                self.clfRF=clfRF
                self.clfok=0
                self.Log("CLF already existed and it was loaded OK!")
                self.Log("66 modelIsTraining:: " + str(self.modelIsTraining))
            else:
                self.clfok=1
                self.Log("69 WARNING!! Model will be trained now, I do not know why")
                self.Log("70 modelIsTraining:: " + str(self.modelIsTraining))           
                self.Train(self.MyTraining)
                self.Log("72 after Train modelIsTraining:: " + str(self.modelIsTraining))           
        dict = {'SYM': [],
                'RSI': [], 
                'MOM': [], 
                'EMA9': [], 
                'EMA36': [], 
                'DIR':[],
                'MO': [],
                'QTY':[],
                'SLS': [],
                'TPS': [],
                'TPL': [],
                'SLL': [],
                'TIME':[],
                'yprobaDT':[],
                'yprobaRF':[],
                'Y': []}
        Tiempo=[]
        self.df=pd.DataFrame(dict, index = Tiempo)
        self.std = self.STD("SPY", 36)
        self.bb = self.BB("SPY", 36, 3.0)
        self.rsi = self.RSI("SPY", 5)
        self.mom = self.MOM("SPY", 5)
        self.ema9 = self.EMA("SPY", 9)
        self.ema36 = self.EMA("SPY", 36)
        #self.SPYLong = self.Identity("SPY", Resolution.Minute)
        #self.Consolidate("SPY", timedelta(minutes=2), self.OnDataConsolidated)
        #self.Train(self.MyTraining)
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(15, 55), self.ClosePositions)
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(9, 29), self.OpenSession)
        self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.At(15, 58), self.AfterClosePos)
        if self.LiveMode:
            self.Train(self.DateRules.Every(DayOfWeek.Sunday), self.TimeRules.At(8,0), self.MyTraining)
        short = Chart("Short")
        short.AddSeries(Series("Asset Price", SeriesType.Line, "", Color.White))
        #short.AddSeries(Series("EMA9", SeriesType.Line, "", Color.Yellow))
        #short.AddSeries(Series("EMA36", SeriesType.Line, "", Color.Blue))
        short.AddSeries(Series("UpperBand", SeriesType.Line, "", Color.Orange))
        short.AddSeries(Series("MktSht", SeriesType.Scatter, "", Color.Yellow, ScatterMarkerSymbol.TriangleDown))
        short.AddSeries(Series("SubTPSht", SeriesType.Scatter,"", Color.Yellow, ScatterMarkerSymbol.Circle))
        short.AddSeries(Series("SubSLSht", SeriesType.Scatter, "", Color.Yellow, ScatterMarkerSymbol.Circle))
        short.AddSeries(Series("FilTPSht", SeriesType.Scatter, "", Color.Yellow, ScatterMarkerSymbol.Diamond))
        short.AddSeries(Series("FilSLSht", SeriesType.Scatter, "", Color.Yellow, ScatterMarkerSymbol.Diamond))
        short.AddSeries(Series("LiquiSht", SeriesType.Scatter, "", Color.Green, ScatterMarkerSymbol.Diamond))
        self.AddChart(short)
        long = Chart('Long')
        long.AddSeries(Series("Asset Price", SeriesType.Line, "", Color.White))
        long.AddSeries(Series("LowerBand", SeriesType.Line, "", Color.Orange))
        long.AddSeries(Series("MktLng", SeriesType.Scatter, "", Color.Blue, ScatterMarkerSymbol.Triangle))
        long.AddSeries(Series("SubTPLng", SeriesType.Scatter, "", Color.Blue, ScatterMarkerSymbol.Circle))
        long.AddSeries(Series("SubSLLng", SeriesType.Scatter, "", Color.Blue, ScatterMarkerSymbol.Circle))
        long.AddSeries(Series("FilTPLng", SeriesType.Scatter, "", Color.Blue, ScatterMarkerSymbol.Diamond))
        long.AddSeries(Series("FilSLLng", SeriesType.Scatter, "", Color.Blue, ScatterMarkerSymbol.Diamond))
        long.AddSeries(Series("LiquiLng", SeriesType.Scatter, "", Color.Violet, ScatterMarkerSymbol.Diamond))
        self.AddChart(long)
        rsi = Chart('RSI')
        rsi.AddSeries(Series("RSI", SeriesType.Line, "", Color.Yellow))
        self.AddChart(rsi)
        mom = Chart('MOM')
        mom.AddSeries(Series("MOM", SeriesType.Line, "", Color.Violet))
        self.AddChart(mom)
#######################   T   R   A   I   N   ########   A   N   D   ##########   P   R   E   D   I   C   T   ##   E   Q   U   I   T   Y   ###################################
    def OnData(self, slice):
        if not self.Apertura:
            if self.LiveMode:
                self.Log("Trading Live!")
            self.InitialPortfolioMarginRemaining = self.Portfolio.MarginRemaining
            self.maximo=100
            self.minimo=400
            self.maximoGOOG=600
            self.minimoGOOG=1200
            self.ClosePosition=0
            self.lendfOriginal=len(self.df)
            self.LastOrderTime = self.Time
            self.TimeDifference = datetime.min
            self.UnRepeat = "0:05:00" # 0:05:00 ok
            self.Plottear = 1
            self.EndHour=14
            self.yaDTmin=0.57
            self.yaRFmin=0.79
            self.Apertura=True
        rsi_value = self.rsi.Current.Value
        mom_value = self.mom.Current.Value
        ema9_value = self.ema9.Current.Value
        ema36_value = self.ema36.Current.Value
        #if (self.EndDate-self.StartDate).days < 1:
        #    self.Plot("Short", "Asset Price", self.Securities["SPY"].High)
        #    self.Plot("Long", "Asset Price", self.Securities["SPY"].Low)
        #    self.Plot("Short", "EMA9", self.ema9.Current.Value)
        #    self.Plot("Long", "EMA9", self.ema9.Current.Value)
        #    self.Plot("Short", "EMA36", self.ema36.Current.Value)
        #    self.Plot("Long", "EMA36", self.ema36.Current.Value)
        #    self.Plot("RSI", "RSI", self.rsi.Current.Value)
        #    self.Plot("MOM", "MOM", self.mom.Current.Value)
        #    self.Plot("Short", "UpperBand", self.bb.UpperBand.Current.Value)
        #    self.Plot("Long", "LowerBand", self.bb.LowerBand.Current.Value)
        #else:
        self.Plottear=0
        if self.Securities["SPY"].High > self.maximo:
            self.maximo=self.Securities["SPY"].High
        if self.Securities["SPY"].Low < self.minimo:
            self.minimo=self.Securities["SPY"].Low
        if (not self.bb.IsReady):
            return
        UpperBand = self.bb.UpperBand.Current.Value
        LowerBand = self.bb.LowerBand.Current.Value
        TakeProfit = GapTP * (-1.0) * np.log(float(str(self.std)))
        StopLoss = GapSL * (-1.0) * np.log(float(str(self.std)))
        InitialBuyingPower=self.Portfolio.Cash
        MaxQtySPYToBuy= InitialBuyingPower*0.5/self.Securities["SPY"].Close*0.8
        self.AvailableQtySPYToBuy=MaxQtySPYToBuy
        MaxQtyGOOGToBuy= InitialBuyingPower*0.5/self.Securities["SPY"].Close*0.8
        self.AvailableQtyGOOGToBuy=MaxQtyGOOGToBuy
        self.LimitShort = 1 - TakeProfit
        self.StopMarketShort = 1 + StopLoss
        if self.Securities["SPY"].High > UpperBand:
        # SHORT:
            self.TimeDifference = self.Time - self.LastOrderTime
            if self.modelIsTraining:
                if self.Securities["SPY"].High > (self.minimo * (1.0 + float(TakeProfit*GapMkt))) * OnOffGrandSpike and str(self.TimeDifference) > self.UnRepeat:
                    QtySPYToBuy= self.AvailableQtySPYToBuy*0.47
                    if QtySPYToBuy > 1 and self.Time.hour < self.EndHour:
                        self.short = 1
                        self.MarketOrder('SPY', (-1)*QtySPYToBuy)
                        insight = Insight.Price("SPY", Expiry.EndOfDay, InsightDirection.Down)
                        self.EmitInsights(insight)
                        self.LastOrderTime=self.Time
            else:
                spike=[rsi_value, mom_value, ema9_value, ema36_value,1]
                spike=pd.Series(spike)
                spike=spike.to_frame().T
                Spike = np.array(spike)
                #escalar = StandardScaler()
                yprobaDT = self.clfDT(Spike)
                self.yprobaDT = ("%.3f" % float(yprobaDT[:,1]))
                yprobaRF = self.clfRF(Spike)
                self.yprobaRF = ("%.3f" % float(yprobaRF[:,1]))
                if self.Securities["SPY"].High > (self.minimo * (1.0 + float(TakeProfit*GapMkt))) * OnOffGrandSpike and str(self.TimeDifference) > self.UnRepeat and float(self.yprobaDT)>self.yaDTmin and float(self.yprobaRF)>self.yaRFmin: # GO SHORT/ BUY PUTS
        #            self.Debug("165 " + str(slice.OptionChains))
        #            for kvp in slice.OptionChains:
        #                if kvp.Key != self.option_symbol: 
        #                    self.Debug("168 kvp.Key" + str(kvp.Key) + " != " + str(self.option_symbol))
        #                    continue
        #                chain = kvp.Value
        #                self.Debug("171 chain" + str(chain))
        #                ########## TO BUY PUTs, x.Right=0, reverse=False ###############################
        #                contracts = sorted(sorted(sorted(chain, \
        #                    key = lambda x: abs(chain.Underlying.Price - x.Strike)), \
        #                    key = lambda x: x.Expiry, reverse=True), \
        #                    key = lambda x: x.Right, reverse=True)
        #                #self.Log("177 PUTs x.Right, reverse=True, len(contracts:: " + str(len(contracts)))
        #                #self.Log("178 i  --  contract")
        #                # if found, trade it
        #                if len(contracts) == 0: continue
        #                self.symbol = contracts[0].Symbol
        #                #self.Log("182 contracts[0].Symbol" + str(contracts[0].Symbol))
        #                self.Debug("183 contracts[0].Symbol" + str(contracts[0].Symbol))
                    QtySPYToBuy= self.AvailableQtySPYToBuy*0.47
                    if QtySPYToBuy > 1 and self.Time.hour < self.EndHour:
                        self.short = 1
                        self.MarketOrder('SPY', (-1)*QtySPYToBuy)
                        insight = Insight.Price("SPY", Expiry.EndOfDay, InsightDirection.Down)
                        self.EmitInsights(insight)
                        self.LastOrderTime=self.Time
        TakeProfit = GapTP * (-1.0) * np.log(float(str(self.std)))
        StopLoss = GapSL * (-1.0) * np.log(float(str(self.std)))
        self.LimitLong = 1 + TakeProfit + 0.00058
        self.StopMarketLong = 1 - StopLoss - 0.0005
        if self.Securities["SPY"].Low < LowerBand:
        #LONG:
            self.TimeDifference=self.Time - self.LastOrderTime
            if self.modelIsTraining:
                if self.Securities["SPY"].Low * OnOffGrandSpike < (self.maximo * (1.0 - float(TakeProfit*GapMkt))) and str(self.TimeDifference) > self.UnRepeat:
                    QtyGOOGToBuy= self.AvailableQtyGOOGToBuy*0.47
                    if QtyGOOGToBuy > 1 and self.Time.hour < self.EndHour:
                        self.MarketOrder("SPY", QtyGOOGToBuy)
                        insight = Insight.Price("SPY", Expiry.EndOfDay, InsightDirection.Up)
                        self.EmitInsights(insight)
                        self.LastOrderTime=self.Time
            else:
                spike=[rsi_value, mom_value, ema9_value, ema36_value,0]
                spike=pd.Series(spike)
                spike=spike.to_frame().T
                Spike = np.array(spike)
                yprobaDT = self.clfDT(Spike)
                self.yprobaDT = ("%.3f" % float(yprobaDT[:,1]))
                yprobaRF = self.clfRF(Spike)
                self.yprobaRF = ("%.3f" % float(yprobaRF[:,1]))
                if self.Securities["SPY"].Low * OnOffGrandSpike < (self.maximo * (1.0 - float(TakeProfit*GapMkt))) and str(self.TimeDifference) > self.UnRepeat and float(self.yprobaDT) > self.yaDTmin and float(self.yprobaRF) > self.yaRFmin: # GO LONG/BUY CALLS
        #            self.Debug("210 " + str(slice.OptionChains))
        #            for kvp in slice.OptionChains:
        #                if kvp.Key != self.option_symbol: 
        #                    self.Debug("213 kvp.Key" + str(kvp.Key) + " != " + str(self.option_symbol))
        #                    continue
        #                chain = kvp.Value
        #                self.Debug("216 chain" + str(chain))
    #                for kvp in slice.OptionChains:
    #                    if kvp.Key != self.option_symbol: continue
    #                    chain = kvp.Value  
    #            ########## TO BUY CALLS, x,Right=0, reverse=True ###############################
    #                    # we sort the contracts to find at the money (ATM) contract with farthest expiration
        #                contracts = sorted(sorted(sorted(chain, \
        #                    key = lambda x: abs(chain.Underlying.Price - x.Strike)), \
        #                    key = lambda x: x.Expiry, reverse=True), \
        #                    key = lambda x: x.Right, reverse=False)
                        # if found, trade it
        #                if len(contracts) == 0: continue
        #                self.symbol = contracts[0].Symbol
        #                self.Log("229 contracts[0].Symbol" + str(contracts[0].Symbol))   
        #                self.Debug("230 contracts[0].Symbol" + str(contracts[0].Symbol)) 
                    QtyGOOGToBuy= self.AvailableQtyGOOGToBuy*0.47
                    if QtyGOOGToBuy > 1 and self.Time.hour < self.EndHour:
                        self.MarketOrder("SPY", QtyGOOGToBuy)
                        insight = Insight.Price("SPY", Expiry.EndOfDay, InsightDirection.Up)
                        self.EmitInsights(insight)
                        self.LastOrderTime=self.Time
#######################   T   R   A   I   N   ########   A   N   D   ##########   P   R   E   D   I   C   T   ##   E   Q   U   I   T   Y   ###################################
    def OnOrderEvent(self, orderEvent):
        if self.modelIsTraining:
            self.yprobaDT = str(0)
            self.yprobaRF = str(0)
        Error = 1
        if orderEvent.Status == OrderStatus.Submitted:
       # SUBMITTED: 
            if orderEvent.LimitPrice is not None:
            # SUBMIT LIMIT
                if orderEvent.Quantity > 0:
                    # SUBMIT LIMIT for TAKEPROFIT: Y=9, SHORT:  DIR=1, SubTPSht
                    FT = str(self.Time.strftime('%H:%M'))
                    dictLimSht = {'Tiempo': self.Time.strftime('%Y-%m-%d %H:%M'),
                        'SYM': str(orderEvent.Symbol),
                        'RSI': ("%.1f" % self.rsi.Current.Value),
                        'MOM': ("%.3f" % self.mom.Current.Value),
                        'EMA9': ("%.1f" % self.ema9.Current.Value),
                        'EMA36': ("%.1f" % self.ema36.Current.Value),
                        'DIR': str(1),
                        'MO': str(0),
                        'QTY': str(orderEvent.Quantity),
                        'SLS': str(0),
                        'TPS': str(orderEvent.OrderId),
                        'TPL': str(0),
                        'SLL': str(0),
                        'TIME': str(FT),
                        'yprobaDT': self.yprobaDT,
                        'yprobaRF': self.yprobaRF,
                        'Y': ("%.1f" % 9)}
                    self.df = self.df.append(dictLimSht,ignore_index=True)
                    if self.Plottear==1:
                        self.Plot("Short", "SubTPSht", orderEvent.LimitPrice)
                else: # Quantity < 0
                    # SUBMIT LIMIT for TAKEPROFIT: Y=9; LONG: DIR=0, SubTPLng
                    FT = str(self.Time.strftime('%H:%M'))
                    dictLimLng = {'Tiempo': self.Time.strftime('%Y-%m-%d %H:%M'),
                        'SYM': str(orderEvent.Symbol),
                        'RSI': ("%.1f" % self.rsi.Current.Value),
                        'MOM': ("%.3f" % self.mom.Current.Value),
                        'EMA9': ("%.1f" % self.ema9.Current.Value),
                        'EMA36': ("%.1f" % self.ema36.Current.Value),
                        'DIR': str(0),
                        'MO': str(0),
                        'QTY': str(orderEvent.Quantity),
                        'SLS': str(0),
                        'TPS': str(0),
                        'TPL': str(orderEvent.OrderId),
                        'SLL': str(0),
                        'TIME': str(FT),
                        'yprobaDT': self.yprobaDT,
                        'yprobaRF': self.yprobaRF,
                        'Y': ("%.1f" % 9)}
                    self.df = self.df.append(dictLimLng,ignore_index=True)
                    if self.Plottear==1:
                        self.Plot("Long", "SubTPLng", orderEvent.LimitPrice)
                Error = 0
            if orderEvent.StopPrice is not None:
                #if self.short==1:
                if orderEvent.Quantity > 0:
                # SUBMIT STOPORDER for STOPLOSS: Y=9; SHORT: DIR=1, SubSLSht
                    FT = str(self.Time.strftime('%H:%M'))
                    dictStpSht = {'Tiempo': self.Time.strftime('%Y-%m-%d %H:%M'),
                        'SYM': str(orderEvent.Symbol),
                        'RSI': ("%.1f" % self.rsi.Current.Value),
                        'MOM': ("%.3f" % self.mom.Current.Value),
                        'EMA9': ("%.1f" % self.ema9.Current.Value),
                        'EMA36': ("%.1f" % self.ema36.Current.Value),
                        'DIR': str(1),
                        'MO': str(0),
                        'QTY': str(orderEvent.Quantity),
                        'SLS': str(orderEvent.OrderId),
                        'TPS': str(0),
                        'TPL': str(0),
                        'SLL': str(0),
                        'TIME': str(FT),
                        'yprobaDT': self.yprobaDT,
                        'yprobaRF': self.yprobaRF,
                        'Y': ("%.1f" % 9)}
                    self.df = self.df.append(dictStpSht,ignore_index=True)
                    if self.Plottear==1:
                        self.Plot("Short", "SubSLSht", orderEvent.StopPrice)
                else: # Quantity < 0
                # SUBMIT STOPORDER for STOPLOSS: Y=9; LONG: DIR=0, SubSLLng
                    FT = str(self.Time.strftime('%H:%M'))
                    dictStpLng = {'Tiempo': self.Time.strftime('%Y-%m-%d %H:%M'),
                        'SYM': str(orderEvent.Symbol),
                        'RSI': ("%.1f" % self.rsi.Current.Value),
                        'MOM': ("%.3f" % self.mom.Current.Value),
                        'EMA9': ("%.1f" % self.ema9.Current.Value),
                        'EMA36': ("%.1f" % self.ema36.Current.Value),
                        'DIR': str(0),
                        'MO': str(0),
                        'QTY': str(orderEvent.Quantity),
                        'SLS': str(0),
                        'TPS': str(0),
                        'TPL': str(0),
                        'SLL': str(orderEvent.OrderId),
                        'TIME': str(FT),
                        'yprobaDT': self.yprobaDT,
                        'yprobaRF': self.yprobaRF,
                        'Y': ("%.1f" % 9)}
                    self.df = self.df.append(dictStpLng,ignore_index=True)
                    if self.Plottear==1:
                        self.Plot("Long", "SubSLLng", orderEvent.StopPrice)
            Error = 0
            return
#######################   T   R   A   I   N   ########   A   N   D   ##########   P   R   E   D   I   C   T   ##   E   Q   U   I   T   Y   ###################################
        # FILLED:
        if orderEvent.Status == OrderStatus.Filled:
            if orderEvent.LimitPrice is None and orderEvent.StopPrice is None and self.ClosePosition==1:
            # LONG:
                FT = str(self.Time.strftime('%H:%M'))
                dictMktLng = {'Tiempo': self.Time.strftime('%Y-%m-%d %H:%M'),
                    'SYM': str(orderEvent.Symbol),
                    'RSI': ("%.1f" % self.rsi.Current.Value),
                    'MOM': ("%.3f" % self.mom.Current.Value),
                    'EMA9': ("%.1f" % self.ema9.Current.Value),
                    'EMA36': ("%.1f" % self.ema36.Current.Value),
                    'DIR': str(3),
                    'MO': str(orderEvent.OrderId),
                    'QTY': str(orderEvent.Quantity),
                    'SLS': str(0),
                    'TPS': str(0),
                    'TPL': str(0),
                    'SLL': str(0),
                    'TIME': str(FT),
                    'yprobaDT': self.yprobaDT,
                    'yprobaRF': self.yprobaRF,
                    'Y': ("%.1f" % 32)}
                self.df = self.df.append(dictMktLng,ignore_index=True)
            if orderEvent.LimitPrice is None and orderEvent.StopPrice is None and self.ClosePosition==0:
            # MARKET:
                if orderEvent.Quantity > 0:
                # LONG:
                    FT = str(self.Time.strftime('%H:%M'))
                    dictMktLng = {'Tiempo': self.Time.strftime('%Y-%m-%d %H:%M'),
                        'SYM': str(orderEvent.Symbol),
                        'RSI': ("%.1f" % self.rsi.Current.Value),
                        'MOM': ("%.3f" % self.mom.Current.Value),
                        'EMA9': ("%.1f" % self.ema9.Current.Value),
                        'EMA36': ("%.1f" % self.ema36.Current.Value),
                        'DIR': str(0),
                        'MO': str(orderEvent.OrderId),
                        'QTY': str(orderEvent.Quantity),
                        'SLS': str(0),
                        'TPS': str(0),
                        'TPL': str(0),
                        'SLL': str(0),
                        'TIME': str(FT),
                        'yprobaDT': self.yprobaDT,
                        'yprobaRF': self.yprobaRF,
                        'Y': ("%.1f" % 30)}
                    self.df = self.df.append(dictMktLng,ignore_index=True)
                    if self.Plottear==1:
                        self.Plot("Long", "MktLng", orderEvent.FillPrice)
                    self.qtylong=orderEvent.Quantity
                    # TRIGGER LimitOrder for TakeProfit Long
                    self.limitTicket = self.LimitOrder("SPY", (-1)*self.qtylong, self.LimitLong * orderEvent.FillPrice)
                    # TRIGGER StopOrder for StopLoss Long
                    self.stopMarketTicket = self.StopMarketOrder("SPY", (-1)*self.qtylong, self.StopMarketLong * orderEvent.FillPrice)
                    Error = 0
                else:
                # SHORT:
                    FT = str(self.Time.strftime('%H:%M'))
                    dictMktSht = {'Tiempo': self.Time.strftime('%Y-%m-%d %H:%M'),
                        'SYM': str(orderEvent.Symbol),
                        'RSI': ("%.1f" % self.rsi.Current.Value),
                        'MOM': ("%.3f" % self.mom.Current.Value),
                        'EMA9': ("%.1f" % self.ema9.Current.Value),
                        'EMA36': ("%.1f" % self.ema36.Current.Value),
                        'DIR': str(1),
                        'MO': str(orderEvent.OrderId),
                        'QTY': str(orderEvent.Quantity),
                        'SLS': str(0),
                        'TPS': str(0),
                        'TPL': str(0),
                        'SLL': str(0),
                        'TIME': str(FT),
                        'yprobaDT': self.yprobaDT,
                        'yprobaRF': self.yprobaRF,
                        'Y': ("%.1f" % 30)}
                    self.df = self.df.append(dictMktSht,ignore_index=True)
                    if self.Plottear==1:
                        self.Plot("Short", "MktSht", orderEvent.FillPrice)
                    self.qtyshort=orderEvent.Quantity
                    # TRIGGER StopOrder for StopLoss Short
                    self.stopMarketTicket = self.StopMarketOrder("SPY", (-1)*self.qtyshort, self.StopMarketShort * orderEvent.FillPrice)
                    # TRIGGER LimitOrder for TakeProfit Short
                    self.limitTicket = self.LimitOrder("SPY", (-1)*self.qtyshort, self.LimitShort * orderEvent.FillPrice)
                    Error = 0
            if orderEvent.LimitPrice is not None:
                if self.Plottear==1: 
                    self.Plot("Short", "FilLimSht", orderEvent.LimitPrice)
                FilledSymbol=orderEvent.Symbol
                if orderEvent.Quantity > 0:
                    # FILLED LimitOrder for TakeProfit: Y=1;  SHORT: DIR=1; FilTPSht; TPS
                    Qty=str(orderEvent.Quantity)
                    for i in range (self.lendfOriginal, len(self.df['RSI'])):
                        if self.df.loc[i,'TPS'] == str(orderEvent.OrderId) and self.ClosePosition==0: 
                            self.df.loc[i,'Y']=("%.1f" % 1)
                            self.df.loc[i,'TIME']=str(self.Time.strftime('%H:%M'))
                            if self.Plottear==1:
                                self.Plot("Short", "FilTPSht", orderEvent.LimitPrice)
                    for i in range (self.lendfOriginal, len(self.df['RSI'])):
                        if self.df.loc[i,'QTY']==Qty and self.df.loc[i,'DIR'] == str(1) and self.df.loc[i,'SLS']!=str(0) and self.ClosePosition==0:
                            orderToCancelId=self.df.loc[i,'SLS']
                            OrderCancelled=self.Transactions.CancelOrder(orderToCancelId)
                    if self.Plottear==1:
                        self.Plot("Short", "FilTPSht", orderEvent.LimitPrice)
                else: # Quantity < 0
                    # Filled Limit for TAKEPROFIT: Y=1; LONG: Dir=0, FilTPLng, TPL
                    Qty=str(orderEvent.Quantity)
                    for i in range (self.lendfOriginal, len(self.df['RSI'])):
                        if self.df.loc[i,'TPL'] == str(orderEvent.OrderId) and self.ClosePosition==0:
                            self.df.loc[i,'Y']=("%.1f" % 1)
                            self.df.loc[i,'TIME']=str(self.Time.strftime('%H:%M'))
                            if self.Plottear==1:    
                                self.Plot("Long", "FilTPLng", orderEvent.LimitPrice)
                    for i in range (self.lendfOriginal, len(self.df['RSI'])):
                        if self.df.loc[i,'QTY']==Qty and self.df.loc[i,'DIR'] == str(0) and self.df.loc[i,'SLL']!=str(0)  and self.ClosePosition==0:
                            orderToCancelId=self.df.loc[i,'SLL']
                            OrderCancelled=self.Transactions.CancelOrder(orderToCancelId)
                    if self.Plottear==1:
                        self.Plot("Long", "FilTPLng", orderEvent.LimitPrice)
                insight = Insight.Price(FilledSymbol, Expiry.EndOfDay, InsightDirection.Flat)
                self.EmitInsights(insight)
                error=0
            if orderEvent.StopPrice is not None:
                FilledSymbol=orderEvent.Symbol
                if orderEvent.Quantity > 0:
                # Filled StopOrder for StopLoss: Y=0 SHORT: DIR=1, FilSLSht, SLS
                    Qty=str(orderEvent.Quantity)
                    for i in range (self.lendfOriginal, len(self.df['RSI'])):
                        if self.df.loc[i,'SLS'] == str(orderEvent.OrderId) and self.ClosePosition==0: 
                            self.df.loc[i,'Y']=("%.1f" % 0)
                            self.df.loc[i,'TIME']=str(self.Time.strftime('%H:%M'))
                            if self.Plottear==1:    
                                self.Plot("Short", "FilSLSht", orderEvent.StopPrice)
                    for i in range (self.lendfOriginal, len(self.df['RSI'])):
                        if self.df.loc[i,'QTY']==Qty and self.df.loc[i,'DIR'] == str(1) and self.df.loc[i,'TPS']!=str(0) and self.ClosePosition==0:
                            orderToCancelId=self.df.loc[i,'TPS']
                            OrderCancelled=self.Transactions.CancelOrder(orderToCancelId)
                    if self.Plottear==1:
                        self.Plot("Short", "FilSLSht", orderEvent.StopPrice)
                else: # Quantity < 0
                # Filled StopOrder for StopLoss: Y=0; LONG: DIR=0, FilSLLng, SLL
                    Qty=str(orderEvent.Quantity)
                    for i in range (self.lendfOriginal, len(self.df['RSI'])):
                        if self.df.loc[i,'SLL'] == str(orderEvent.OrderId) and self.ClosePosition==0: 
                            self.df.loc[i,'Y']=("%.1f" % 0)
                            self.df.loc[i,'TIME']=str(self.Time.strftime('%H:%M'))
                            if self.Plottear==1:
                                self.Plot("Long", "FilSLLng", orderEvent.StopPrice)
                    for i in range (self.lendfOriginal, len(self.df['RSI'])):
                        if self.df.loc[i,'QTY']==Qty and self.df.loc[i,'DIR'] == str(0) and self.df.loc[i,'TPL']!=str(0) and self.ClosePosition==0:
                            orderToCancelId=self.df.loc[i,'TPL']
                            OrderCancelled=self.Transactions.CancelOrder(orderToCancelId)
                insight = Insight.Price(FilledSymbol, Expiry.EndOfDay, InsightDirection.Flat)
                self.EmitInsights(insight)
                Error = 0
            return
        if orderEvent.Status == OrderStatus.CancelPending:
        #CANCEL PENDING
            # CANCEL PENDING for LimitOrder SHORT, TPS
            self.df.loc[self.df['TPS']==str(orderEvent.OrderId),'Y']=("%.1f" % 8)
            self.df.loc[self.df['TPS']==str(orderEvent.OrderId),'TIME']=str(self.Time.strftime('%H:%M'))
            # CANCEL PENDING for LimitOrder LONG, TPL
            self.df.loc[self.df['TPL']==str(orderEvent.OrderId),'Y']=("%.1f" % 8)
            self.df.loc[self.df['TPL']==str(orderEvent.OrderId),'TIME']=str(self.Time.strftime('%H:%M'))
            # CANCEL PENDING for StopOrder SHORT, SLS
            self.df.loc[self.df['SLS']==str(orderEvent.OrderId),'Y']=("%.1f" % 8)
            self.df.loc[self.df['SLS']==str(orderEvent.OrderId),'TIME']=str(self.Time.strftime('%H:%M'))
            # CANCEL PENDING for StopOrder LONG, SLL
            self.df.loc[self.df['SLL']==str(orderEvent.OrderId),'Y']=("%.1f" % 8)
            self.df.loc[self.df['SLL']==str(orderEvent.OrderId),'TIME']=str(self.Time.strftime('%H:%M'))
            error=0
            return
        if orderEvent.Status == OrderStatus.Canceled:
        #CANCELED
            # CANCELED for LimitOrder SHORT, TPS
            self.df.loc[self.df['TPS']==str(orderEvent.OrderId),'Y']=("%.1f" % 5)
            self.df.loc[self.df['TPS']==str(orderEvent.OrderId),'TIME']=str(self.Time.strftime('%H:%M'))
            # CANCELED for LimitOrder LONG, TPL
            self.df.loc[self.df['TPL']==str(orderEvent.OrderId),'Y']=("%.1f" % 5)
            self.df.loc[self.df['TPL']==str(orderEvent.OrderId),'TIME']=str(self.Time.strftime('%H:%M'))
            # CANCELED for StopOrder SHORT, SLS
            self.df.loc[self.df['SLS']==str(orderEvent.OrderId),'Y']=("%.1f" % 5)
            self.df.loc[self.df['SLS']==str(orderEvent.OrderId),'TIME']=str(self.Time.strftime('%H:%M'))
            # CANCELED for StopOrder LONG, SLL
            self.df.loc[self.df['SLL']==str(orderEvent.OrderId),'Y']=("%.1f" % 5)
            self.df.loc[self.df['SLL']==str(orderEvent.OrderId),'TIME']=str(self.Time.strftime('%H:%M'))
            error=0
            return
        if Error == 1:
            self.Log("564 ERROR!! orderEvent :: " + str(orderEvent) + "SYM:: " + str(orderEvent.Symbol))
            self.Log("565 ERROR!! Status :: " + str(orderEvent.Status))
            self.Log("566 ERROR!! Quantity :: " + str(orderEvent.Quantity))
#######################   T   R   A   I   N   ########   A   N   D   ##########   P   R   E   D   I   C   T   ##   E   Q   U   I   T   Y   ##################################
    def ClosePositions(self):
        self.ClosePosition=1
        insights=[]
        for i in range (0, len(self.df['RSI'])):
            if self.df.loc[i,'Y'] ==  ("%.1f" % 30):
                x=self.df.loc[i,'SYM']
                self.Liquidate(x)
                insight = Insight.Price(x, Expiry.EndOfDay, InsightDirection.Flat)
                if insight not in insights:
                    insights.append(insight)
                self.df.loc[i,'Y'] =  ("%.1f" % 31)
                if self.Plottear==1:
                    self.Plot("Short", "LiquiSht", self.Securities["SPY"].Close)
                    self.Plot("Long", "LiquiLng", self.Securities["SPY"].Close)
        self.EmitInsights(insights)
    def OpenSession(self):
        if self.LiveMode:
            self.Log("Trading Live!")
        self.InitialPortfolioMarginRemaining = self.Portfolio.MarginRemaining
        self.maximo=100
        self.minimo=400
        self.maximoGOOG=600
        self.minimoGOOG=1200
        self.ClosePosition=0
        self.lendfOriginal=len(self.df)
        self.LastOrderTime = self.Time
        self.TimeDifference = datetime.min
        self.UnRepeat = "0:05:00" # 0:05:00 ok
        self.Plottear = 1
        self.EndHour=14
        self.yaDTmin=0.57
        self.yaRFmin=0.79
        self.Apertura=True
    def AfterClosePos(self):
        if self.Portfolio.Invested:
#            self.Log('612 ERROR!!!! Final Portfolio.Invested:: ' + str(self.Portfolio.Invested))
            OpenOrders=[]
            OpenOrders= self.Transactions.GetOpenOrders()
    #        if OpenOrders != []:
  #              self.Log('616 ERROR Final OpenOrders:: ' + str(OpenOrders))
   #             df1=self.df.loc[:,('SYM','DIR','MO','QTY','SLS','TPS','TPL','SLL','TIME','Y')]
#                self.Debug("618 df1::" + str(df1))
        #        self.Debug("631 (n, 15); df.shape:: " + str(self.df.shape))
    def OnEndOfAlgorithm(self):
        if self.modelIsTraining:
            #self.Log("(n, 16); df.shape:: " + str(self.df.shape))
            #self.Log("df[0,1] .... df[0,Tiempo]:: " + ".... " + str(self.df.iloc[1,1]) + " ... " + str(self.df.loc[1,'Tiempo']))
            #self.Log("df[n,0] .... df[n,Tiempo]:: " + str(self.df.iloc[len(self.df.Tiempo)-1,0]) + " .....  ... " + str(self.df.loc[len(self.df.Tiempo)-1,'Tiempo']))
            self.df.drop(self.df.index[self.df['Y']==("%.1f" % 5)], inplace=True)
            self.df.drop(self.df.index[self.df['Y']==("%.1f" % 30)], inplace=True)
            self.df.drop(self.df.index[self.df['Y']==("%.1f" % 31)], inplace=True)
            self.df.drop(self.df.index[self.df['Y']==("%.1f" % 32)], inplace=True)
            self.df.drop(self.df.index[self.df['Y']==("%.1f" % 9)], inplace=True)
            self.df.drop(self.df.index[self.df['Y']==("%.1f" % 8)], inplace=True)
            self.df[['RSI', 'MOM', 'EMA9', 'EMA36', 'DIR', 'Y']]=self.df[['RSI', 'MOM', 'EMA9', 'EMA36', 'DIR', 'Y']].astype('float')
            X=self.df.loc[:,'RSI':'DIR']
            X = np.array(X)
            # X = np.reshape(X, X.shape[0])
            from sklearn.preprocessing import StandardScaler
            escalar = StandardScaler()
            X_train = X # escalar.fit_transform(X)
            y = self.df.loc[:,'Y']
            y_train= np.array(y)
            DIRtrain=X_train[0:len(X_train),4]
            ######## DECITION TREE  ########## TRAIN & TEST ######### DECITION TREE ####### MATRICES ########### no se usa.
            algoritmo = DecisionTreeClassifier(criterion = 'entropy')
            #Entreno al modelo
            clfDT=algoritmo.fit(X_train, y_train)
            y_predDTTRAIN=clfDT.predict(X_train)
            yaDTTRAIN = clfDT.predict_proba(X_train)
            from sklearn.metrics import confusion_matrix
            matrizDTTRAIN = confusion_matrix(y_train, y_predDTTRAIN)
            self.Log(" ")
            self.Log('Matriz de Confusion DECITION TREE TRAIN')
            self.Log(str(matrizDTTRAIN[0]))
            self.Log(str(matrizDTTRAIN[1]))
            from sklearn.metrics import accuracy_score
            exactitudDTTRAIN = accuracy_score(y_train, y_predDTTRAIN)
            self.Log('TRAIN DECITION TREE accuracy_score:: ' + str(exactitudDTTRAIN))
            ##### RANDOM FOREST ####### TRAIN & TEST ############# RANDOM FOREST ##### MATRICES #################
            clfFRST = RandomForestClassifier(n_estimators = 15, criterion = 'entropy')
            clfRF=clfFRST.fit(X_train, y_train)
            y_predRFTRAIN=clfRF.predict(X_train)
            yaRFTRAIN = clfRF[0].predict_proba(X_train)
            matrizRFTRAIN = confusion_matrix(y_train, y_predRFTRAIN)
            self.Log(" ")
            self.Log('Matriz de Confusion RANDOM FOREST TRAIN')
            self.Log(str(matrizRFTRAIN[0]))
            self.Log(str(matrizRFTRAIN[1]))
            exactitudRFTRAIN = accuracy_score(y_train, y_predRFTRAIN)
            self.Log('TRAIN RANDOM FOREST accuracy_score:: ' + str(exactitudRFTRAIN))
            self.Log(" ")
            clfDT=clfDT.predict_proba
            clfRF=clfRF[0].predict_proba
            CLF=[clfDT,clfRF]
            subir=1 # 1 para subir
            if subir==1:
                if self.ObjectStore.ContainsKey(self.modelkey):
                    self.ObjectStore.Delete(self.modelkey) 
                    self.Log("PREVIOUS OjectStore.delete")
                else:
                    self.Log(" No previous ObjectStore:: " + str(self.ObjectStore.ContainsKey(self.modelkey)))
                serializedCLF = pickle.dumps(CLF)
                self.ObjectStore.SaveBytes(self.modelkey, serializedCLF)
                if self.ObjectStore.ContainsKey(self.modelkey):
                    self.Log(" clf DT y RF proba have been SAVED OK ")
                else:
                    self.Log("Fatal error. CLF has not been SAVED")
                    self.Debug(self.df)
            else:
                self.Log("clf NOT LOADED. subir== " + str(subir))
        self.Log("679 modelIsTraining:: " + str(self.modelIsTraining))
        self.modelIsTraining = False
        self.Log("691 modelIsTraining:: " + str(self.modelIsTraining))
    def MyTraining(self):
        self.Log("683 modelIsTraining:: " + str(self.modelIsTraining))
        self.modelIsTraining = True
        self.Log("685 modelIsTraining:: " + str(self.modelIsTraining))
#######################   T   R   A   I   N   ########   A   N   D   ##########   P   R   E   D   I   C   T   ##   E   Q   U   I   T   Y   #######