Overall Statistics
Total Trades
3
Average Win
0.61%
Average Loss
0%
Compounding Annual Return
31.031%
Drawdown
10.100%
Expectancy
0
Net Profit
13.916%
Sharpe Ratio
1.19
Probabilistic Sharpe Ratio
51.477%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.282
Beta
0.007
Annual Standard Deviation
0.239
Annual Variance
0.057
Information Ratio
-0.039
Tracking Error
0.544
Treynor Ratio
43.072
Total Fees
$16.83
from load_grid import * 
from QuantConnect.Data.Custom.USTreasury import *
import pandas as pd

def get_change(current, previous):
    if current == previous:
        return 0
    try:
        return round((abs(current - previous) / previous) * 100.0,2)
    except ZeroDivisionError:
        return float('inf')
        
class CalibratedMultidimensionalAutosequencers(QCAlgorithm):
    


    def Initialize(self):
        self.SetStartDate(2020, 2, 12)  # Set Start Date
        self.SetEndDate(  2020, 8,  5)    #Set End Date
        self.SetCash(100000)  # Set Strategy Cash
        
        #Add the data in here
        self.ABC =self.AddEquity("ABC", Resolution.Daily).Symbol
        self.SetBenchmark("ABC")
        self.grid = self.AddData(GetGrid, "grid", Resolution.Daily).Symbol
        
        #This is where the retrace stuff goes
        self.HIGH_HIST = []
        self.LOW_HIST = []
        self.retrace = 0
        
        #This is the trade logic gates
        self.POINT_B           = False
        self.SHORT_TRADE_ENTER = False
        self.already_shorted   = False
        self.LONG_TRADE_ENTER  = False
        self.LONG_A            = False
        
        
    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 not data.ContainsKey(self.grid):
            return
        if not data.ContainsKey(self.ABC):
            return
        
        self.Debug(f"Time: {self.Time}, green_pos1: {data['grid'].GetProperty('green_pos1')}")
        
        #This gets the data from our file - should use the offical OLHCV instead 
        # self.HIGH_HIST.append(data['grid'].GetProperty('High'))
        # self.LOW_HIST.append( data['grid'].GetProperty('Low'))
        
        self.HIGH_HIST.append(data['ABC'].High)
        self.LOW_HIST.append( data['ABC'].Low)
        
        self.CHART_HIGH = max(self.HIGH_HIST)
        self.CHART_LOW  = min(self.LOW_HIST)
        
        OP_DIFF = self.CHART_HIGH - self.CHART_LOW
        POINT_N_DIFF = self.CHART_HIGH - data['ABC'].Low
        
        # self.Debug(f"Chart High {self.CHART_HIGH}")
        # self.Debug(f"Chart Low  {self.CHART_LOW}")
        # self.Debug(f"OP_DIFF: {OP_DIFF}")
        # self.Debug(f"POINT_N_DIFF: {POINT_N_DIFF}")
        
        self.retrace =  round(100 - get_change(POINT_N_DIFF, OP_DIFF),2)
        # self.Debug(f"retrace: {self.retrace}")
        
        
        if not self.Portfolio.Invested:
            if self.retrace >= 30 and data['grid'].GetProperty('green_pos1') >= data['ABC'].Low and self.POINT_B == False:
                self.Debug("retrace greater than 30%  passed throguh green pos 1")
                self.Debug(f"Low: {data['ABC'].Low}")
                self.Debug("FOUND SHORT B!")
                self.POINT_B = True
            
            if self.POINT_B == True:
                self.Debug("look for SHORT trade entry")
                if data['grid'].GetProperty('green_0') < data['ABC'].High:
                    self.POINT_B = False
                    self.SHORT_TRADE_ENTER = True
                    self.already_shorted = True
                    self.Debug("________________Found point C________________")
                    self.Debug("High is higer than green_0")
                    self.Debug(f" green_0: {data['grid'].GetProperty('green_0')}")
                    self.Debug(f" high: {data['ABC'].High}")
                    self.Debug("Enter short trade")
                    self.SetHoldings("ABC", -1)

        else:
            if data['grid'].GetProperty('green_pos1') >= data['ABC'].Low and self.SHORT_TRADE_ENTER:
                self.Debug("________________Close Trade__________________")
                self.Debug("Low is lower than green pos 1")
                self.Debug(f" green_pos1: {data['grid'].GetProperty('green_pos1')}")
                self.Debug(f" Low: {data['ABC'].Low}")
                self.SetHoldings("ABC", 0)
                self.POINT_B = False
                self.SHORT_TRADE_ENTER = False
        
        if self.Portfolio.Invested and self.already_shorted and data['ABC'].Low > data['grid'].GetProperty('green_0') and self.LONG_TRADE_ENTER==False:
            self.Debug("________________close open short trades here________________")
            # self.LONG_A = True
            self.SHORT_TRADE_ENTER = False
            self.SetHoldings("ABC", 0)
        
        if not self.Portfolio.Invested and self.already_shorted and data['ABC'].Low > data['grid'].GetProperty('green_0'): 
            self.LONG_A = True
            
            if self.LONG_A:
                self.Debug("look for LONG trade entry ")
                
                if data['ABC'].Low >= data['grid'].GetProperty('green_0'):
                    self.LONG_A = False 
                    self.LONG_TRADE_ENTER = True
                    self.Debug("________________Found green_0 LONG point C________________")
                    self.Debug("Low is greater than green_0")
                    self.Debug("Enter LONG trade next candle Open")
                    self.SetHoldings("ABC", 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"
        source = "https://www.dropbox.com/s/55e6hu6iwggskzo/2020-02-11_to_2020-08-07_ABC_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_0"]      = float(data[7])
        
        yld["blue_neg1"]   = 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_neg2"]   = 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_pos1"]   = 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_pos2"]   = 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_neg1"]  = 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_neg2"]  = 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_pos1"]  = 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_pos2"]  = 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