Hi Douglas,
Thanks for the reply.
I've attached my code below. I based this code on the MACD and Nifty Templates that were provided on GitHub, and made some modifications by myself.
I was trying to import EURUSD CSV data from 1986 onwards to use for backtesting.
When I used this code for backtesting, there were no errors, but the Algo made zero trades or activity during the backtest period.
I'm new to Python and I would appreciate any help or advice.
Thanks
class MACDTrendAlgorithm(QCAlgorithm):
def Initialize(self):
'''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''
self.SetStartDate(2008, 1, 1) #Set Start Date
self.SetEndDate(2018, 1, 1) #Set End Date
self.SetCash(10000) #Set Strategy Cash
# Find more symbols here: http://quantconnect.com/data
eurusd = self.AddData(EuroDollar, "EURUSD", Resolution.Daily).Symbol
self.UniverseSettings.Leverage = 50
# define our daily macd(12,26) with a 9 day signal
self.sma_actual = self.SMA(eurusd,2,Resolution.Daily)
self.sma = self.SMA(eurusd,100, Resolution.Daily)
self.psar = self.PSAR(eurusd, 0.04,0.04,0.4,Resolution.Daily)
self.rsi = self.RSI(eurusd,100, MovingAverageType.Simple,Resolution.Daily)
self.previous = datetime.min
def OnData(self, data):
# only once per day
if self.previous.date() == self.Time.date(): return
if data.ContainsKey("eurusd"):
self.today = CorrelationPair(self.Time)
self.today.CurrencyPrice = data["eurusd"].Close
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.'''
# define a small tolerance on our checks to avoid bouncing
tolerance = 0.0025
buysignal = (self.sma_actual.Current.Value > self.sma.Current.Value and self.rsi.Current.Value>50 and self.sma_actual.Current.Value>self.psar.Current.Value)
sellsignal = (self.sma_actual.Current.Value < self.sma.Current.Value and self.rsi.Current.Value<50 and self.sma_actual.Current.Value<self.psar.Current.Value)
sellexit = (self.sma_actual.Current.Value > self.sma.Current.Value)
buyexit = (self.sma_actual.Current.Value < self.sma.Current.Value)
holdings = self.Portfolio["EURUSD"].Quantity
# Long Order
if holdings <= 0 and buysignal == True:
self.SetHoldings("EURUSD",1.0)
# Short Order
if holdings <= 0 and sellsignal == True:
self.SetHoldings("EURUSD",-1.0)
# Long Exit
if holdings < 0 and sellexit == True:
self.Liquidate("EURUSD")
#Sell Exit
if holdings > 0 and buyexit == True:
self.Liquidate("EURUSD")
self.previous = self.Time
class EuroDollar(PythonData):
'''EURUSD Custom Data Class'''
def GetSource(self, config, date, isLiveMode):
return SubscriptionDataSource("https://www.pornhub.com/s/6q49hksqtz48sy9/EURUSD1440.csv?dl=1", SubscriptionTransportMedium.RemoteFile)
def Reader(self, config, line, date, isLiveMode):
if not (line.strip() and line[0].isdigit()): return None
# New Nifty object
currency = EuroDollar()
currency.Symbol = config.Symbol
try:
# Example File Format:
# Date, Open High Low Close Volume Turnover
# 2011-09-13 7792.9 7799.9 7722.65 7748.7 116534670 6107.78
data = line.split(',')
currency.Time = datetime.strptime(data[0], "%Y-%m-%d")
currency.Value = data[4]
currency["Open"] = float(data[1])
currency["High"] = float(data[2])
currency["Low"] = float(data[3])
currency["Close"] = float(data[4])
except ValueError:
# Do nothing
return None
return currency