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
1.465
Tracking Error
0.653
Treynor Ratio
0
Total Fees
$0.00
from load_grid import * 

class CalibratedMultidimensionalAutosequencers(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 3, 2)  # Set Start Date
        self.SetEndDate(2020,3,11)    #Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        self.AAL =self.AddEquity("AAL", Resolution.Daily).Symbol
        # self.grid_data = 
        self.grid =self.AddData(GetGrid, "grid", Resolution.Daily).Symbol
        # self.AddData(GetGrid, "grid", Resolution.Daily)


    def OnData(self, data):
        if data.ContainsKey(self.grid):
            self.Log(data[self.grid].GetProperty('blue_neg_one'))
        
        # history = self.History([self.grid], timedelta(1))
        '''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
                
        '''
        # curr_green_0 = data["grid"]#.green_0
        # self.Log(history.blue_neg_one)
        # if data.ContainsKey("grid") and self.AAL in data.Keys:
        #     # self.Log(data[self.AAL].Dividends)
        #     close = data["grid"].Close
        #     self.Log(close)
        #     neg_1 = data["grid"].blue_neg_one
        #     self.Log(neg_1)
            
            # new = data.Get("grid")
        # if self.grid_data in data.Keys:
        #     self.Log(str(self.Time) + " worked " + str(curr_green_0))
            
        # self.Debug(str(self.Time) + " blue_0: " + str(self.grid_data.blue_0))

        # if not self.Portfolio.Invested:
        #    self.SetHoldings("SPY", 1)
class GetGrid(PythonData):

    def GetSource(self, config, date, isLive):
        source = "https://www.dropbox.com/s/kpupx39fka61gfg/2020-02-11_to_2020-08-07_AAL_1d_COMBINED.csv?dl=1"
        return SubscriptionDataSource(source, SubscriptionTransportMedium.RemoteFile);


    def Reader(self, config, line, date, isLive):
        if not (line.strip() and line[0].isdigit()): return None

        data = line.split(',')
        yld = GetGrid()
        value = data[4]
        if value == 0: return None
        
        yld.Symbol = config.Symbol
        yld.Time = datetime.strptime(data[0], '%Y-%m-%d') 
        
        yld["Open"]        = float(data[1])
        yld["High"]        = float(data[2])
        yld["Low"]         = float(data[3])
        yld["Close"]       = float(data[4])
        yld["Adj_Close"]   = float(data[5])
        yld["Volume"]      = float(data[6])
        yld["blue_zero"]      = float(data[7])
        
        yld["blue_neg_one"]   = float(data[8])
        yld["blue_neg10"]  = float(data[9])
        yld["blue_neg11"]  = float(data[10])
        yld["blue_neg12"]  = float(data[11])
        yld["blue_neg13"]  = float(data[12])
        yld["blue_neg14"]  = float(data[13])
        yld["blue_neg15"]  = float(data[14])
        yld["blue_neg_two"]   = float(data[15])
        yld["blue_neg3"]   = float(data[16])
        yld["blue_neg4"]   = float(data[17])
        yld["blue_neg5"]   = float(data[18])
        yld["blue_neg6"]   = float(data[19])
        yld["blue_neg7"]   = float(data[20])
        yld["blue_neg8"]   = float(data[21])
        yld["blue_neg9"]   = float(data[22])
        yld["blue_pos_one"]   = float(data[23])
        yld["blue_pos10"]  = float(data[24])
        yld["blue_pos11"]  = float(data[25])
        yld["blue_pos12"]  = float(data[26])
        yld["blue_pos13"]  = float(data[27])
        yld["blue_pos14"]  = float(data[28])
        yld["blue_pos15"]  = float(data[29])
        yld["blue_pos_two"]   = float(data[30])
        yld["blue_pos3"]   = float(data[31])
        yld["blue_pos4"]   = float(data[32])
        yld["blue_pos5"]   = float(data[33])
        yld["blue_pos6"]   = float(data[34])
        yld["blue_pos7"]   = float(data[35])
        yld["blue_pos8"]   = float(data[36])
        yld["blue_pos9"]   = float(data[37])
        yld["green_0"]     = float(data[38])
        yld["green_neg_one"]  = float(data[39])
        yld["green_neg10"] = float(data[40])
        yld["green_neg11"] = float(data[41])
        yld["green_neg12"] = float(data[42])
        yld["green_neg13"] = float(data[43])
        yld["green_neg14"] = float(data[44])
        yld["green_neg15"] = float(data[45])
        yld["green_neg_two"]  = float(data[46])
        yld["green_neg3"]  = float(data[47])
        yld["green_neg4"]  = float(data[48])
        yld["green_neg5"]  = float(data[49])
        yld["green_neg6"]  = float(data[50])
        yld["green_neg7"]  = float(data[51])
        yld["green_neg8"]  = float(data[52])
        yld["green_neg9"]  = float(data[53])
        yld["green_pos_one"]  = float(data[54])
        yld["green_pos10"] = float(data[55])
        yld["green_pos11"] = float(data[56])
        yld["green_pos12"] = float(data[57])
        yld["green_pos13"] = float(data[58])
        yld["green_pos14"] = float(data[59])
        yld["green_pos15"] = float(data[60])
        yld["green_pos_two"]  = float(data[61])
        yld["green_pos3"]  = float(data[62])
        yld["green_pos4"]  = float(data[63])
        yld["green_pos5"]  = float(data[64])
        yld["green_pos6"]  = float(data[65])
        yld["green_pos7"]  = float(data[66])
        yld["green_pos8"]  = float(data[67])
        yld["green_pos9"]  = float(data[68])
    
        return yld
import pandas as pd
import numpy as np

from sklearn.linear_model import LinearRegression

from Risk.MaximumDrawdownPercentPerSecurity import MaximumDrawdownPercentPerSecurity

class GRID(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2006, 9, 23)  # Set Start Date
        # self.SetEndDate(2020, 8, 7)  # Set End Date
        self.SetCash(100000)  # Set Strategy Cash

        self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.01))


        self.gold = self.AddCfd("XAUUSD", Resolution.Minute, Market.Oanda).Symbol
        self.yld = self.AddData(TenYrYield, "yld", Resolution.Daily).Symbol
        
        self.model = LinearRegression()
        
        self.fit_model()
        
        self.Schedule.On(self.DateRules.MonthStart("XAUUSD"), \
                        self.TimeRules.At(0,0), \
                        self.fit_model)
        
    
    def fit_model(self):
        history = self.History([self.gold, self.yld], timedelta(365 * 1))
        
        gold_prices = history.loc[self.gold].unstack(level=1)["close"].apply(np.log)
        bond_yield = history.loc[self.yld].unstack(level=1)["value"]
        
        data = pd.DataFrame()
        
        data["gold"] = gold_prices
        data["yield"] = bond_yield
        data.dropna(inplace=True)
        
        y = data['gold'].values.reshape((-1, 1))
        x = data['yield'].values.reshape((-1, 1))
        
        self.model.fit(x, y)


    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.yld in data.Keys and self.gold in data.Keys:
            gold_price = data[self.gold].Value
            bond_yield = data[self.yld].Value
            
            predicted_price = np.exp(self.model.predict([[bond_yield]]))
            
            if predicted_price > gold_price:
                self.SetHoldings(self.gold, 1)
            else:
                self.SetHoldings(self.gold, 0)
    


class TenYrYield(PythonData):

    def GetSource(self, config, date, isLive):
        source = "https://www.dropbox.com/s/qops20awe8wzsf0/DFII10.csv?dl=1"
        return SubscriptionDataSource(source, SubscriptionTransportMedium.RemoteFile);


    def Reader(self, config, line, date, isLive):
        if not (line.strip() and line[0].isdigit()): return None

        data = line.split(',')
        yld = TenYrYield()
        yld.Symbol = config.Symbol
        yld.Time = datetime.strptime(data[0], '%Y-%m-%d') 
        yld.Value = data[1]
        
        return yld

# Your New Python File