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.436
Tracking Error
0.121
Treynor Ratio
0
Total Fees
$0.00
from QuantConnect import *
from Selection.ManualUniverseSelectionModel import ManualUniverseSelectionModel

class G10CurrencySelectionModel(ManualUniverseSelectionModel):
    def __init__(self):
        super().__init__([Symbol.Create(x, SecurityType.Forex, Market.Oanda) for x in [ "EURUSD", "GBPUSD", "USDJPY", "AUDUSD", "NZDUSD","USDCAD", "USDCHF", "USDNOK", "USDSEK"]])
class InflationRateAlphaModel(AlphaModel):
    
    def __init__(self, algorithm, insightDuration = 30):
        ## Add monthly US inflation rate data
        self.inflationUS = algorithm.AddData(QuandlInflationRate, 'RATEINF/INFLATION_USA', Resolution.Daily).Symbol
        ## Add monthly UK inflation rate
        self.inflationUK = algorithm.AddData(QuandlInflationRate, 'RATEINF/INFLATION_GBR', Resolution.Daily).Symbol
        ## Add monthly US interest rate data
        self.interestUS = algorithm.AddData(QuandlInterestRate, 'FRED/FEDFUNDS', Resolution.Daily).Symbol
        ## Add daily UK interest rate data
        self.interestUK = algorithm.AddData(QuandlInterestRate, 'BOE/IUDBEDR', Resolution.Daily).Symbol
        
        self.insightDuration = TimeSpan.FromDays(insightDuration)
        
    def Update(self, algorithm, data):
        insights = []
            
        ## Check to make sure Fed Quandl Symbols are in current data Slice
        ## Fed Rate data appears on the 2nd of the month while the other data appears on the first, so we use the latter
        ## to ensure that there is updated data for all Symbols
        if not data.ContainsKey(self.interestUS):
            return []
            
        ## Typically, a country with a consistently lower inflation rate exhibits a rising 
        ## currency value, as its purchasing power increases relative to other currencies. 
        ## Those countries with higher inflation typically see depreciation in their 
        ## currency in relation to the currencies of their trading partners. This is also usually
        ## accompanied by higher interest rates. Currency traders, then, hope to predict future
        ## exchange rate movements by paying attention to the relative levels of inflation in
        ## the countries of their target currency pairs in addition to where each country is in
        ## its monetary policy cycle, and the size and pace of currency flows moving into and
        ## out of each country.
        
        return insights

class QuandlInflationRate(PythonQuandl):
    
    def __init__(self):
        ## Rename the Quandl object column to the data we want, which is the 'Value' column
        ## of the CSV that our API call returns
        self.ValueColumnName = 'Value'
        
class QuandlInterestRate(PythonQuandl):
    
    def __init__(self):
        ## Rename the Quandl object column to the data we want, which is the 'Value' column
        ## of the CSV that our API call returns
        self.ValueColumnName = "Value"
from InflationRateAlphaModel import InflationRateAlphaModel
from Execution.ImmediateExecutionModel import ImmediateExecutionModel
from G10CurrencySelectionModel import G10CurrencySelectionModel

class TransdimensionalUncoupledPrism(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 4, 20)  # Set Start Date
        self.SetEndDate(2019, 5, 20)
        self.SetCash(100000)  # Set Strategy Cash
        
        
        self.AddAlpha(BollingerBandTradingAlphaModel())
        
        self.SetExecution(ImmediateExecutionModel())

        self.SetPortfolioConstruction(InsightWeightingPortfolioConstructionModel())
        #self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())

        
        self.UniverseSettings.Resolution = Resolution.Hour
        self.UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw
        
        self.AddUniverseSelection( G10CurrencySelectionModel())

    
    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 self.Portfolio.Invested:
        #    self.SetHoldings("SPY", 1)
        
    def OnEndOfDay(self, symbol):
        self.Log("Taking a position of " + str(self.Portfolio[symbol].Quantity) + " units of symbol " + str(symbol))
        pass
    
    
    
class BollingerBandTradingAlphaModel(AlphaModel):

    def __init__(self):
        self.pair = [ ]
        self.period = timedelta(hours=2)
        
        self.spreadMean_list = []
        self.spreadStd_list = []
        
        self.symbolData = []
        
    def Update(self, algorithm, data):
        
        insights_ = []
        i = 0
        
        algorithm.Log(str(len(self.pair)))
        for pair_ in self.pair:
            self.spreadMean_list[i].Update(algorithm.Time, pair_.Price)
            self.spreadStd_list[i].Update(algorithm.Time, pair_.Price)
            
            upperthreshold_ = self.spreadMean_list[i].Current.Value + self.spreadStd_list[i].Current.Value
            lowerthreshold_ = self.spreadMean_list[i].Current.Value - self.spreadStd_list[i].Current.Value
          
            
            if pair_.Price > upperthreshold_ and self.symbolData[i] != -1:
                insights_.append(Insight.Price(pair_.Symbol, self.period, InsightDirection.Down))
                #algorithm.Log("Down")
                self.symbolData[i] = -1
            elif pair_.Price < lowerthreshold_ and self.symbolData[i] != 1: 
                insights_.append(Insight.Price(pair_.Symbol, self.period, InsightDirection.Up))
                #algorithm.Log("Up")
                self.symbolData[i] = 1
            elif self.symbolData[i] != 2:
                insights_.append(Insight.Price(pair_.Symbol, self.period, InsightDirection.Flat))
                self.symbolData[i] = 2
                

            i += 1

       
        return Insight.Group(insights_)
    
    def OnSecuritiesChanged(self, algorithm, changes):
     
        for x in changes.AddedSecurities:
            self.pair.append(x)
            self.spreadMean_list.append(SimpleMovingAverage(500))
            self.spreadStd_list.append(StandardDeviation(500))
            self.symbolData.append(0)
            
      
        history = algorithm.History([x.Symbol for x in self.pair], 500)

        history = history.askclose.unstack(level=0)
    
        for tuple in history.itertuples():
            time_ = tuple[0]
            
            j = 0
            for i in range(1, len(tuple)):
                self.spreadMean_list[j].Update(time_, tuple[i])
                self.spreadStd_list[j].Update(time_, tuple[i])
                j += 1
class MyPCM(InsightWeightingPortfolioConstructionModel):
    leverage = 0.5
    
    def CreateTargets(self, algorithm, insights):
        targets = super().CreateTargets(algorithm, insights)
        return [PortfolioTarget(x.Symbol, x.Quantity*(1+self.leverage)) for x in targets]