Overall Statistics
Total Trades
Average Win
Average Loss
Compounding Annual Return
Net Profit
Sharpe Ratio
Probabilistic Sharpe Ratio
Loss Rate
Win Rate
Profit-Loss Ratio
Annual Standard Deviation
Annual Variance
Information Ratio
Tracking Error
Treynor Ratio
Total Fees
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.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.
                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))
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
        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, None, None, None, 0.25))
                self.symbolData[i] = -1
            elif pair_.Price < lowerthreshold_ and self.symbolData[i] != 1: 
                insights_.append(Insight.Price(pair_.Symbol, self.period, InsightDirection.Up, None, None, None, 0.25))
                self.symbolData[i] = 1
            elif self.symbolData[i] != 2:
                insights_.append(Insight.Price(pair_.Symbol, self.period, InsightDirection.Flat, None, None, None, 0.25))
                self.symbolData[i] = 2

            i += 1

        return Insight.Group(insights_)
    def OnSecuritiesChanged(self, algorithm, changes):
        for x in changes.AddedSecurities:
        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]