Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
0%
Compounding Annual Return
60.357%
Drawdown
1.300%
Expectancy
0
Net Profit
3.930%
Sharpe Ratio
5.682
Probabilistic Sharpe Ratio
93.235%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.319
Beta
-1.431
Annual Standard Deviation
0.071
Annual Variance
0.005
Information Ratio
4.754
Tracking Error
0.096
Treynor Ratio
-0.28
Total Fees
$0.00
Estimated Strategy Capacity
$740000.00
Lowest Capacity Asset
AUDCHF 8G
# region imports
from AlgorithmImports import *
# endregion

class CustomIndexStrategy(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2019, 2, 3) 
        self.SetEndDate(2019,3,3)
        self.SetCash(100000)  
        self.SetBrokerageModel(BrokerageName.OandaBrokerage)
        self.AUDCHF = self.AddForex("AUDCHF", Resolution.Daily, Market.Oanda)
        self.USDZAR = self.AddForex("USDZAR", Resolution.Daily, Market.Oanda)
        #self.SetBenchmark("SPY")
        self.symbols = ['USDZAR', 'AUDCHF']
        self.prevPrices = { symbol : RollingWindow[QuoteBar](3) for symbol in self.symbols }


    def OnData(self,data):
        
        # Add data to rolling windows
        for symbol in self.symbols:
            if data.ContainsKey(symbol):
                self.prevPrices[symbol].Add( data[symbol] )

        # Check if all windows ready
        if not all([ window.IsReady for window in self.prevPrices.values() ]):
            return
            
        # Now if you want closing prices for USDZAR 1 and 2 days ago
        USDZAR_window = self.prevPrices['USDZAR']
        USDZAR_2D = USDZAR_window[2].Close
        USDZAR_1D = USDZAR_window[1].Close
        
        # etc (or put into a for-loop over `self.symbols` if many symbols)
        AUDCHF_window = self.prevPrices['AUDCHF']
        AUDCHF_2D = AUDCHF_window[2].Close
        AUDCHF_1D = AUDCHF_window[1].Close    

        # sum of prices 2 days ago and 1 day ago
        Sum2D = USDZAR_2D + AUDCHF_2D
        Sum1D = USDZAR_1D + AUDCHF_1D

        self.Debug((Sum2D, Sum1D))

        # check if we have any open positions
        if self.Portfolio.Invested:
            return
        
        # Make trades
        if not self.Portfolio.Invested:

            #trading rule 1: if sum of closing prices from 1 day ago is lower than from 2 days ago: 
            if Sum1D > Sum2D:
                self.Debug(" Sum1D > Sum2D") 
                self.SetHoldings([ PortfolioTarget('AUDCHF', -0.5), PortfolioTarget('USDZAR', 0.5) ])
                #self.MarketOrder(self.AUDCHF, -2000, 0.95*self.Securities["AUDCHF"].Close)
                #self.MarketOrder(self.USDZAR, 2000, 0.95*self.Securities["USDZAR"].Close)
    
            #trading rule 2: if sum of closing prices from 1 day ago is higher than from 2 days ago: 
            if Sum1D < Sum2D:
                self.Debug(" Sum1D < Sum2D") 
                self.SetHoldings([ PortfolioTarget('AUDCHF', 0.5), PortfolioTarget('USDZAR', -0.5) ])
                #self.MarketOrder(self.AUDCHF, 2000, 0.95*self.Securities["AUDCHF"].Close)
                #self.MarketOrder(self.USDZAR, -2000, 0.95*self.Securities["USDZAR"].Close)

        return