Overall Statistics
Total Trades
291
Average Win
2.48%
Average Loss
-1.68%
Compounding Annual Return
-62.821%
Drawdown
42.600%
Expectancy
-0.090
Net Profit
-22.283%
Sharpe Ratio
-0.889
Loss Rate
63%
Win Rate
37%
Profit-Loss Ratio
1.47
Alpha
-0.483
Beta
3.184
Annual Standard Deviation
0.648
Annual Variance
0.42
Information Ratio
-0.865
Tracking Error
0.633
Treynor Ratio
-0.181
Total Fees
$0.00
import numpy as np
import decimal
import datetime, time
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
### <summary>
### Basic template algorithm simply initializes the date range and cash. This is a skeleton
### framework you can use for designing an algorithm.
### </summary>
class BasicTemplateAlgorithm(QCAlgorithm):
    
    def Initialize(self):
        self.SetStartDate(2018,8,20)  #Set Start Date
        self.SetEndDate(2018,11,20)    #Set End Date
        self.SetCash(2000)           #Set Strategy Cash
        self.forex = self.AddForex("EURUSD", Resolution.Hour, Market.Oanda)
        self.load_symbols()
        self.SetBenchmark("EURUSD")
        
        for symbol in self.symbols:
            
            self.WilliamsPercentR = self.WILR(symbol, 20, Resolution.Hour) #7200
            #fast_period = 300
            slow_period = 40 #was 5/27 resolution.HF
            #self.fast = self.EMA(symbol, fast_period)
            self.slow = self.EMA(symbol, slow_period)
            
            overlayPlot = Chart("Overlay Plot")
            overlayPlot.AddSeries(Series("EURUSD", SeriesType.Line, 0))
            overlayPlot.AddSeries(Series("Buy", SeriesType.Scatter, 0))
            overlayPlot.AddSeries(Series("Sell", SeriesType.Scatter, 0))
            overlayPlot.AddSeries(Series("WilliamsPercentR", SeriesType.Line, 1))
            #overlayPlot.AddSeries(Series("GBPNZD", SeriesType.Line, 2))
            overlayPlot.AddSeries(Series("EMA_slow", SeriesType.Line, 2))
            self.AddChart(overlayPlot)
            
            self.SetWarmup(slow_period)  
            self.first = True
    def OnData(self, data):
        for symbol in self.symbols:
            if not self.WilliamsPercentR.IsReady: 
                return
            # get the indicator value 
            if self.first and not self.IsWarmingUp:
                self.first = False
            current = data[symbol].Value
            
            buy_signal_triggered, sell_signal_triggered = False, False
            willsignal = self.WilliamsPercentR.Current.Value
            
            if self.Portfolio["EURUSD"].Invested == False: 
                if willsignal > -10 and self.Securities["EURUSD"].Price > self.slow.Current.Value: #go short
                    self.Debug("Going SHORT, EUR invested is " + str(self.Portfolio["EURUSD"].Invested))
                    #self.Debug("portfolio invested is " + str(self.Portfolio.Cash))    
                    takeProfit = decimal.Decimal(0.0200) 
                    stoploss = decimal.Decimal(0.0037) 
                    allocation = decimal.Decimal(5)
                    invested = self.Portfolio.TotalPortfolioValue * allocation
                    #Buy stop limit , stop price should be lower than limit price
                                
                    limitPrice = self.Securities["EURUSD"].Price - takeProfit
                    stopLimit = self.Securities["EURUSD"].Price + stoploss
                        
                    self.MarketOrder("EURUSD", -invested)
                    self.LimitOrder("EURUSD", invested, limitPrice)
                    self.StopMarketOrder("EURUSD", invested, stopLimit) #verified it works because my losses didn't exceed 2.5% a day       
                    
                    sell_signal_triggered = True
            if self.Portfolio["GBPNZD"].Invested == False: 
                if willsignal > -10 and self.Securities["GBPNZD"].Price > self.slow.Current.Value: #go short
                    self.Debug("Going SHORT, gbp invested is " + str(self.Portfolio["GBPNZD"].Invested))
                    
                    takeProfit = decimal.Decimal(0.0200) 
                    stoploss = decimal.Decimal(0.0037) 
                    allocation = decimal.Decimal(5)
                    invested = self.Portfolio.TotalPortfolioValue * allocation
                    #Buy stop limit , stop price should be lower than limit price
                                
                    limitPrice = self.Securities["GBPNZD"].Price - takeProfit
                    stopLimit = self.Securities["GBPNZD"].Price + stoploss
                        
                    self.MarketOrder("GBPNZD", -invested)
                    self.LimitOrder("GBPNZD", invested, limitPrice)
                    self.StopMarketOrder("GBPNZD", invested, stopLimit) #verified it works because my losses didn't exceed 2.5% a day       
                    
                    sell_signal_triggered = True
            if self.Portfolio["EURUSD"].Invested == False:
                if willsignal < -90 and self.Securities[symbol].Price < self.slow.Current.Value: #go long
                    
                    self.Debug("Going Long EUR invested is " + str(self.Portfolio["EURUSD"].Invested))
                        
                    takeProfit = decimal.Decimal(0.0200) 
                    stoploss = decimal.Decimal(0.0037) 
                    allocation = decimal.Decimal(5)
                    invested = self.Portfolio.TotalPortfolioValue * allocation
                    #Buy stop limit , stop price should be lower than limit price
                                
                    limitPrice = self.Securities["EURUSD"].Price + takeProfit
                    stopLimit = self.Securities["EURUSD"].Price - stoploss
                                
                    self.MarketOrder("EURUSD", invested)
                    self.LimitOrder("EURUSD", -invested, limitPrice)
                    self.StopMarketOrder("EURUSD", -invested, stopLimit)  #verified it works because my losses didn't exceed 2.5% a day
                        
                    #self.Debug(" portfolio invested NOW is " + str(self.Portfolio.Invested))
                    buy_signal_triggered = True
            if self.Portfolio["GBPNZD"].Invested == False:
                if willsignal < -90 and self.Securities["GBPNZD"].Price < self.slow.Current.Value: #go long
                    
                    self.Debug("Going Long GBP invested is " + str(self.Portfolio["GBPNZD"].Invested))
                        
                    takeProfit = decimal.Decimal(0.0200) 
                    stoploss = decimal.Decimal(0.0037) 
                    allocation = decimal.Decimal(5)
                    invested = self.Portfolio.TotalPortfolioValue * allocation
                    #Buy stop limit , stop price should be lower than limit price
                                
                    limitPrice = self.Securities["GBPNZD"].Price + takeProfit
                    stopLimit = self.Securities["GBPNZD"].Price - stoploss
                                
                    self.MarketOrder("GBPNZD", invested)
                    self.LimitOrder("GBPNZD", -invested, limitPrice)
                    self.StopMarketOrder("GBPNZD", -invested, stopLimit)  #verified it works because my losses didn't exceed 2.5% a day
                        
                    #self.Debug(" portfolio invested NOW is " + str(self.Portfolio.Invested))
                    buy_signal_triggered = True
            if self.Portfolio["GBPNZD"].Invested == True:
                if self.Portfolio["GBPNZD"].IsLong:
                    if willsignal > -50 and (self.Securities["GBPNZD"].Price <= self.slow.Current.Value):
                        self.Liquidate("GBPNZD")
                        self.Debug("GBPNZD liquidated is " + str(self.Portfolio["GBPNZD"].Invested))
                        #self.Debug("post sale cash is " + str(self.Portfolio.Cash))
                if self.Portfolio["GBPNZD"].IsShort:
                    if willsignal < -50 and (self.Securities["GBPNZD"].Price >= self.slow.Current.Value):
                        self.Liquidate("GBPNZD")
                        
            if self.Portfolio["EURUSD"].Invested == True:
                if self.Portfolio["EURUSD"].IsLong:
                    if willsignal > -50 and (self.Securities["EURUSD"].Price <= self.slow.Current.Value):
                        self.Liquidate("EURUSD")
                        self.Debug("EUR liquidated is " + str(self.Portfolio["EURUSD"].Invested))
                        #self.Debug("post sale cash is " + str(self.Portfolio.Cash))
                if self.Portfolio["EURUSD"].IsShort:
                    if willsignal < -50 and (self.Securities["EURUSD"].Price >= self.slow.Current.Value):
                        self.Liquidate("EURUSD")
                        
            if buy_signal_triggered:
                    self.Plot("Overlay Plot", "Buy", current)
            elif sell_signal_triggered:
                self.Plot("Overlay Plot", "Sell", current)
            self.Plot("Overlay Plot", "EURUSD", current)
            self.Plot("Overlay Plot", "EMA_slow", self.slow.Current.Value)
            self.Plot("Overlay Plot", "WilliamsPercentR", self.WilliamsPercentR.Current.Value)
            
    def load_symbols(self) :
        syl_list = [
            'EURUSD', 
            'GBPNZD',
            ]
        
        self.symbols = []
        for i in syl_list:
            self.symbols.append(self.AddForex(i, Resolution.Hour, Market.Oanda).Symbol)