Overall Statistics
import clr
clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Brokerages import *
from QuantConnect.Indicators import *
from QuantConnect.Data.Market import *

class BasicTemplateAlgorithm(QCAlgorithm):
	
	def __init__(self):
		self._macd = None
		self._previous = datetime.min

	def Initialize(self):
		self.SetCash(1000)
		self.SetStartDate(2012,1,1)
		self.SetEndDate(2015,1,1)
		self.SetBrokerageModel(BrokerageName.OandaBrokerage)
		self.AddForex("EURUSD") # Default to minute bars
		sef._macd = self.MACD("EURUSD", 9, 26, 9, MovingAverageType.Exponential, Resolution.Daily)
		
	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: TradeBars IDictionary object with your stock data
		'''
		# wait for our macd to fully initialize
		if not self.__macd.IsReady: return    
		
		pyTime = datetime(self.Time)
		
		
		# define a small tolerance on our checks to avoid bouncing
		tolerance = 0.0025;
		
		holdings = self.Portfolio["EURUSD"].Quantity
		
		signalDeltaPercent = (self.__macd.Current.Value - self.__macd.Signal.Current.Value)/self.__macd.Fast.Current.Value
		
		# if our macd is greater than our signal, then let's go long
		if holdings <= 0 and signalDeltaPercent > tolerance:  # 0.01%
		    # longterm says buy as well
		    self.SetHoldings("EURUSD", 1.0)
		
		# of our macd is less than our signal, then let's go short
		elif holdings >= 0 and signalDeltaPercent < -tolerance:
		    self.Liquidate("EURUSD")  
		
		# plot both lines
		self.Plot("MACD", self.__macd, self.__macd.Signal)
		self.Plot("EURUSD", self.__macd.Fast, self.__macd.Slow)
		
		self.__previous = pyTime