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
-2.882
Tracking Error
0.164
Treynor Ratio
0
Total Fees
$0.00
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
import pandas
import sklearn
import matplotlib.pyplot as plt
import pickle
OnOffGrandSpike = 0
class DynamicCalibratedPrism(QCAlgorithm):
    modelkey='felipe05'
    def Initialize(self):
        self.SetStartDate(2020, 7, 1)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        self.AddEquity("SPY", Resolution.Minute)
        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.RegisterIndicator("SPY", self.std, Resolution.Minute)
        self.RegisterIndicator("SPY", self.bb, Resolution.Minute)
        self.RegisterIndicator("SPY", self.rsi, Resolution.Minute)
        self.RegisterIndicator("SPY", self.mom, Resolution.Minute)
        self.RegisterIndicator("SPY", self.ema9, Resolution.Minute)
        self.RegisterIndicator("SPY", self.ema36, Resolution.Minute) 
        self.SetWarmUp(37, Resolution.Minute)
        self.TrainHistorical=False
        self.Train(self.Historical)
    def OnData(self, data):
        '''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
            Arguments:
                data: Slice object keyed by symbol containing the stock data
        '''
        if self.TrainHistorical:
            start_time = datetime(2017, 7, 6, 9, 31) # start datetime for history call
            end_time = datetime(2017, 7, 6, 10, 39) # end datetime for history call
            history = self.History(["SPY"], start_time, end_time, Resolution.Minute)
            self.bb.Update
            if self.bb.IsReady:
                history['upperband']=self.bb.UpperBand.Current.Value
                history['lowerband']=self.bb.LowerBand.Current.Value
            else:
                history['upperband']=11
                history['lowerband']=12
            self.std.Update
            if self.bb.IsReady:
                history['STD']=self.std.Current.Value
            else:
                history['STD']=21
            self.rsi.Update
            if self.bb.IsReady:
                history['RSI']=self.rsi.Current.Value
            else:
                history['RSI']=31
            self.mom.Update
            if self.bb.IsReady:
                history['MOM']=self.mom.Current.Value
            else:
                history['MOM']=41
            self.ema9.Update
            if self.bb.IsReady:
                history['EMA9']=self.ema9.Current.Value
            else:
                history['EMA9']=51
            self.ema36.Update
            if self.bb.IsReady:
                history['EMA36']=self.ema36.Current.Value
            else:
                history['EMA36']=61
            self.history=history
            #self.Debug("type:: " + str(type(history)))
            #self.Debug(history.shape)
            #self.Debug(history)
    
            # if not self.Portfolio.Invested:
            #    self.SetHoldings("SPY", 1)
    def Historical(self):
        self.TrainHistorical=True
    def OnEndOfAlgorithm(self):
        if self.TrainHistorical:
            self.Debug("type(history)::" + str(type(self.history)) + ": len:: " + str(len(self.history)))
            self.Debug("OEOA history::" + str(self.history))
        else:
            self.Debug("30, history OEOA:: " + str(self.history))
        subir=1
        if subir==1:
            if self.ObjectStore.ContainsKey(self.modelkey):
                self.ObjectStore.Delete(self.modelkey)
                self.Log("35 PREVIOUS ObjectStore.Delete")
            else:
                self.Log("37 FALSE NO PREVIOUS ObjectStore:: " + str(self.ObjectStore.ContainsKey(self.modelkey)))
            serializeddf = pickle.dumps('self.history')
            self.ObjectStore.SaveBytes(self.modelkey, serializeddf)
            self.Log("40 history has been SAVED:: " + str(self.ObjectStore.ContainsKey(self.modelkey)))
    #    self.Log("(n, 12); 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']))