Overall Statistics
#
#   QuantConnect Basic Template:
#	Fundamentals to using a QuantConnect algorithm.
#
#	You can view the QCAlgorithm base class on Github: 
#	https://github.com/QuantConnect/Lean/tree/master/Algorithm
#

import numpy as np
import statsmodels.api as sm
import pandas as pd
import math

class ForexLive(QCAlgorithm):

	def Initialize(self):
		
		# Set the cash we'd like to use for our backtest
		# This is ignored in live trading 
		self.SetCash(100000)
		
		# Start and end dates for the backtest.
		# These are ignored in live trading.
		self.SetStartDate(2015,7,1)
		self.SetEndDate(2017,8,20)
		
		forex = "EURUSD"
		resolution = Resolution.Hour
		
		# Add assets you'd like to see
		self.forex = self.AddForex(forex, resolution).Symbol
		self.bb = self.BB(forex, 30, 2, MovingAverageType.Exponential, resolution)
		self.rsi = self.RSI(forex,14, MovingAverageType.Exponential, resolution)
		self.ao = self.AROON(forex,14,14,resolution)
		
		self.bbupCount = 0
		self.bblowCount = 0
		self.rsi70Count = 0
		self.rsi30Count = 0
		self.aoupCount = 0
		self.aolowCount = 0

		
	def OnData(self, slice):
		if not self.bb.IsReady: return
		if not self.rsi.IsReady: return
		if not self.ao.IsReady: return

		close = slice[self.forex].Close
	
		bbup = self.bb.UpperBand.Current.Value < close
		bblow = self.bb.LowerBand.Current.Value > close
		rsi70 = self.rsi.Current.Value > 70
		rsi30 = self.rsi.Current.Value < 30
		aoup = self.ao.Current.Value > 50 
		aolow = self.ao.Current.Value < -50

		self.bbupCount += bbup
		self.bblowCount += bblow
		self.rsi70Count += rsi70
		self.rsi30Count += rsi30
		self.aoupCount += aoup
		self.aolowCount += aolow

		if (bbup and self.bbupCount>1) and ((rsi70 and self.rsi70Count>0) or (aoup and self.aoupCount > 2)) :
			self.SetHoldings(self.forex,-1)
			self.bbupCount = self.bblowCount = self.rsi70Count = self.rsi30Count = self.aoupCount = self.aolowCount = 0
		elif (bblow and self.bblowCount>1) or (rsi30 and self.rsi30Count>0 and aolow and self.aolowCount > 2): 
			self.SetHoldings(self.forex,1)
			self.bbupCount = self.bblowCount = self.rsi70Count = self.rsi30Count = self.aoupCount = self.aolowCount = 0