
'#################################################################################################
' Script Name: MACD Gold Cross Indicator                                
' Author     : Wang XingXing                                            
' E-Mail     : fivestar71@hotmail.com                                   
' Skype      : surikae225                                               
' Date       : 31-10-2014 02:03:24                                      
' Descreption: This indicator uses Stochastic with periods 8,3,3 and RSI 3 cross RSI 13 and 
'              it use a confirmation trend macd cross zero.
'              If both are crossing together then you shall view the signal to open position 
'              so you just need to open position.
'              This is momentum-type indicator so you know how to buy or sell in the right time.
'##################################################################################################

dim g_fast_rsi
dim g_slow_rsi

dim g_stoch_main_buffer()
dim g_stoch_signal_buffer()

dim g_k_period
dim g_k_slowing
dim g_d_period

dim g_fast_ema
dim g_slow_ema

dim g_macd_buffer()
dim g_arrow_macd

dim g_buy()
dim g_sell()

dim g_ikey_buy
dim g_ikey_sell

dim g_point

dim g_init
dim g_high_buffer()
dim g_low_buffer()

dim g_bars
dim g_pre_bars

Public Function CalcMACD()
	
	dim close_buffer()
	dim fast_ema_buffer()
	dim slow_ema_buffer()
	
	copy_err = false
	
	copy = CopyClose(0, 1, CLNG(g_bars), close_buffer)
	if copy <> g_bars then
		copy_err = true
	end if
		 
	copy = CopyClose(0, 1, CLNG(g_bars), g_macd_buffer)
	if copy <> g_bars then
		copy_err = true
	end if
	
	if copy_err = true then
		CalcMACD = false
		exit Function
	end if

	SimpleMovingAverageOnArray close_buffer, CInt(g_fast_ema), fast_ema_buffer
	SimpleMovingAverageOnArray close_buffer, CInt(g_slow_ema), slow_ema_buffer


	for i = 1 to g_bars
		g_macd_buffer(i) = fast_ema_buffer(i) - slow_ema_buffer(i)
	next
	
	CalcMACD = true

End Function

Public Function CalcStochastic()

	dim open_buffer()
	dim high_buffer()
	dim low_buffer()
	dim close_buffer()
	
	copy_err = false
    
    copy = CopyOpen(0, 1, CLNG(g_bars), open_buffer)
    if copy <> g_bars then
		copy_err = true
	end if	 

    copy = CopyHigh(0, 1, CLNG(g_bars), high_buffer)
    if copy <> g_bars then
		copy_err = true
	end if	 

    copy = CopyLow(0, 1, CLNG(g_bars), low_buffer)
    if copy <> g_bars then
		copy_err = true
	end if	 

    copy = CopyClose(0, 1, CLNG(g_bars), close_buffer)
    if copy <> g_bars then
		copy_err = true
	end if
	
	if copy_err = true then
		CalcStochastic = false
		exit Function
	end if 

    StochasticOScillatorOnArray open_buffer, high_buffer, low_buffer, close_buffer, CInt(g_k_period), CInt(g_k_slowing), CInt(g_d_period), 1, g_stoch_main_buffer, 1
    StochasticOScillatorOnArray open_buffer, high_buffer, low_buffer, close_buffer, CInt(g_k_period), CInt(g_k_slowing), CInt(g_d_period), 1, g_stoch_signal_buffer, 2
	
	CalcStochastic = true

End Function

Public Function iRSI(ByVal period, ByVal shift)

	dim rel
	dim negative
	dim positive
	
	dim close_buffer()
	dim rsi_buffer()
	dim pos_buffer()
	dim neg_buffer()

	copy_err = false
	
	copy = CopyClose(0, 1, CLNG(g_bars), close_buffer)
	if copy <> g_bars then
		copy_err = true
	end if	 

	copy = CopyClose(0, 1, CLNG(g_bars), rsi_buffer)
	if copy <> g_bars then
		copy_err = true
	end if	 

	copy = CopyClose(0, 1, CLNG(g_bars), pos_buffer)
	if copy <> g_bars then
		copy_err = true
	end if	 

	copy = CopyClose(0, 1, CLNG(g_bars), neg_buffer)
	if copy <> g_bars then
		copy_err = true
	end if
	
	if copy_err = true then
		iRSI = 0.0
		exit Function
	end if 

	for i = 1 to period
		rsi_buffer(i) = 0.0
	next

	for i = period to g_bars 
	
		sumn = 0.0
		sump = 0.0

		if i = period then

			for j = 2 to i 
				rel = close_buffer(j) - close_buffer(j - 1)
				if  rel > 0 then	
					sump = sump + rel
				else 
					sumn = sumn - rel
				end if
			next
			
			positive = sump / CDbl(period)
			negative = sumn / CDbl(period)
		
		else
		
			rel = close_buffer(i) - close_buffer(i - 1)

			if rel > 0 then	
				sump = rel
			else
				sumn = 0 - rel
			end if

			positive = (pos_buffer(i - 1) * (period - 1) + sump) / CDbl(period)
			negative = (neg_buffer(i - 1) * (period - 1) + sumn) / CDbl(period)
		end if

		pos_buffer(i) = positive
		neg_buffer(i) = negative

		if negative = 0.0 then
			rsi_buffer(i) = 0.0
		else 
			rsi_buffer(i) = 100.0 - 100.0 / (1.0 + positive / negative)
		end if

	next
		
	iRSI = rsi_buffer(g_bars - shift)

End Function



Public Sub OnInit()

	g_fast_rsi=3
	g_slow_rsi=13
	
	g_k_period=8
	g_k_slowing=3
	g_d_period=3

	g_fast_ema = 12
	g_slow_ema = 26
	g_arrow_macd = true

	dummy = SymbolInfoInteger(ChartSymbol(0), SYMBOL_PIP_LOCATION, pip_loc)
	g_point = 10 ^ pip_loc
	
	g_bars = Bars(0)

	dummy = CopyHigh(0, 1, CLNG(g_bars), g_sell)
	dummy = CopyLow(0, 1, CLNG(g_bars), g_buy)

	for i = 1 to g_bars
		g_sell(i) = 0.0
		g_buy(i) = 0.0		
	next
	
	g_init = true

	for x = 1 to 2 
		for y = 0 to x - 1

			obj_id = "signal" & CINT(x) & CINT(y)
			obj_x = x * 40 + 100
			obj_y = y * 20 + 210
			obj_txt = Chr(73)
			obj_font_size = 20
			obj_color = RGBColor(255,0,255)
			obj_font_name = "Wingdings"

			CreateObjectLabel 0, 0, CSTR(obj_id), CSTR(obj_x), CSTR(obj_y), obj_txt
			ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		next
	next

	obj_id = "signal"
	obj_x = 140
	obj_y = 170
	obj_txt = "Hold"
	obj_font_size = 10
	obj_color = RGBColor(255,0,255)
	obj_font_name = "TAHOMA"

	CreateObjectLabel 0, 0, CSTR(obj_id), CSTR(obj_x), CSTR(obj_y), obj_txt
	ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)

	obj_id = "trade"
	obj_x = 140
	obj_y = 145
	obj_txt = "Still in Good Condition"
	obj_font_size = 8
	obj_color = RGBColor(0,128,0)
	obj_font_name = "TAHOMA"

	CreateObjectLabel 0, 0, CSTR(obj_id), CSTR(obj_x), CSTR(obj_y), obj_txt
	ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)


	g_ikey_buy = AddCustomIndicator(0, g_buy, 1, false)
	SetDrawingStyle 0, CStr(g_ikey_buy), DRAW_ARROWS
	SetArrowStyle 0, CStr(g_ikey_buy), 233

	g_ikey_sell = AddCustomIndicator(0, g_sell, 1, false)
	SetDrawingStyle 0, CSTR(g_ikey_sell), DRAW_ARROWS
	SetArrowStyle 0, CSTR(g_ikey_sell), 234

End Sub

Public Function UpdateIndicatorValue(ByVal index)

	dim i
	dim i_buy_value
	dim i_sell_value
	dim macd

	i = index

	i_buy_value = 0.0
	i_sell_value = 0.0
	macd = 0

	if i = 1 then
		macd0 = g_macd_buffer(i)
		macd1 = 0.0
	else 
		macd0 = g_macd_buffer(i)
		macd1 = g_macd_buffer(i - 1)
	end if

	if macd0 > 0 and macd1 < 0 then
		
		macd = 1

		if g_arrow_macd = true then
			
			i_buy_value = g_low_buffer(i) - 5 * g_point

			if (ChartPeriod(0) >= PERIOD_M30)	then	i_buy_value = i_buy_value - 8 * g_point		end if
			if (ChartPeriod(0) >= PERIOD_H1)	then	i_buy_value = i_buy_value - 8 * g_point		end if
			if (ChartPeriod(0) >= PERIOD_H4)	then	i_buy_value = i_buy_value - 8 * g_point		end if
			if (ChartPeriod(0) >= PERIOD_D1)	then	i_buy_value = i_buy_value - 8 * g_point		end if
			if (ChartPeriod(0) >= PERIOD_W1)	then	i_buy_value = i_buy_value - 12 * g_point	end if
			if (ChartPeriod(0) >= PERIOD_MN1)	then	i_buy_value = i_buy_value - 60 * g_point	end if

			ObjectSeriesSetValue 0, CSTR(g_ikey_buy), CLNG(i), CDBL(i_buy_value)

		end if
	end if

	if macd0 < 0 and macd1 > 0 then
		
		macd = 2

		if g_arrow_macd = true then
			
			i_sell_value = g_high_buffer(i) + 5 * g_point
  
			if (ChartPeriod(0) >= PERIOD_M30)	then	i_sell_value = i_sell_value + 8 * g_point	end if
			if (ChartPeriod(0) >= PERIOD_H1)	then	i_sell_value = i_sell_value + 8 * g_point	end if
			if (ChartPeriod(0) >= PERIOD_H4)	then	i_sell_value = i_sell_value + 8 * g_point	end if
			if (ChartPeriod(0) >= PERIOD_D1)	then	i_sell_value = i_sell_value + 8 * g_point	end if
			if (ChartPeriod(0) >= PERIOD_W1)	then	i_sell_value = i_sell_value + 12 * g_point	end if
			if (ChartPeriod(0) >= PERIOD_MN1)	then	i_sell_value = i_sell_value + 60 * g_point	end if

			ObjectSeriesSetValue 0, CSTR(g_ikey_sell), CLNG(i), CDBL(i_sell_value)

		end if
	end if

	UpdateIndicatorValue = macd

End Function

Public Sub OnTick(symbolName)

	if symbolName <> ChartSymbol(0) then
		exit sub
	end if
	
	g_pre_bars = g_bars
	g_bars = Bars(0)

	copy = CopyHigh(0, 1, CLNG(g_bars), g_high_buffer)
	if copy = -1 then
		exit sub
	end if

	copy = CopyLow(0, 1, CLNG(g_bars), g_low_buffer)
	if copy = -1 then
		exit sub
	end if

	g_bars = copy
	
	if CalcMACD() = false then
		'MsgBox "CalcMACD Error"
		exit sub
	end if
	
	dim pos
	dim macd
	
	if g_init = true then
		pos = 1
		g_init = false
	else
		pos = g_pre_bars
	end if
	
	for i = pos to g_bars
		macd = UpdateIndicatorValue(i)
	next
	
	dim r
	dim s
   
	if CalcStochastic() = false then
		'MsgBox "CalcStochastic Error"
		exit sub
	end if

	istoch_main = g_stoch_main_buffer(g_bars)
	istoch_signal = g_stoch_signal_buffer(g_bars)
	
	if (istoch_main > 80.0)  or (istoch_main < 20.0) then

		obj_id = "trade"
		obj_txt = "Over Bought!, Changing the direction of take profit."
		obj_font_size = 8
		obj_font_name = "Tahoma"
		obj_color = RGBColor(255,99,71)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)
	else 

		obj_id = "trade"
		obj_txt = "Still in Good Condition"
		obj_font_size = 8
		obj_font_name = "Tahoma"
		obj_color = RGBColor(0,128,0)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)
	
	end if
       
	if  istoch_main > istoch_signal then

		obj_id = "signal20"
		obj_txt = Chr(221)
		obj_font_size = 8
		obj_font_name = "Wingdings"
		obj_color = RGBColor(0,128,0)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)

		s = 1
	else

		obj_id = "signal20"
		obj_txt = Chr(222)
		obj_font_size = 8
		obj_font_name = "Wingdings"
		obj_color = RGBColor(255,99,71)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)

		s = 2
    
	end if

	irsi_fast = iRSI(g_fast_rsi, 0)
	irsi_slow = iRSI(g_slow_rsi, 1)
	
	if irsi_fast > irsi_slow then
		
		obj_id = "signal21"
		obj_txt = Chr(221)
		obj_font_size = 8
		obj_font_name = "Wingdings"
		obj_color = RGBColor(0,128,0)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)

		r = 1
	else
 
 		obj_id = "signal21"
		obj_txt = Chr(222)
		obj_font_size = 8
		obj_font_name = "Wingdings"
		obj_color = RGBColor(255,99,71)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)

		r = 2
	end if
      
	if (r = 1) and (s = 1) and (macd = 1) then
		obj_id = "signal10"
		obj_txt = Chr(67)
		obj_font_size = 20
		obj_font_name = "Wingdings"
		obj_color = RGBColor(0,128,0)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)
	  
	
		obj_id = "signal"
		obj_txt = "STRONG BUY"
		obj_font_size = 10
		obj_font_name = "TAHOMA"
		obj_color = RGBColor(0,128,0)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)
	 
	end if
	
	if (r =1) and (s = 1) and (macd = 2) then
		obj_id = "signal10"
		obj_txt = Chr(67)
		obj_font_size = 20
		obj_font_name = "Wingdings"
		obj_color = RGBColor(0,128,0)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)
	  
	
		obj_id = "signal"
		obj_txt = "BUY"
		obj_font_size = 10
		obj_font_name = "TAHOMA"
		obj_color = RGBColor(0,128,0)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)

	end if
	

	
	if (r = 2) and (s = 2) and (macd = 2) then
		obj_id = "signal10"
		obj_txt = Chr(68)
		obj_font_size = 20
		obj_font_name = "Wingdings"
		obj_color = RGBColor(255,99,71)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)
	  
	
		obj_id = "signal"
		obj_txt = "STRONG SELL"
		obj_font_size = 10
		obj_font_name = "TAHOMA"
		obj_color = RGBColor(255,99,71)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)

	end if
	
	if (r = 2) and (s = 2) and (macd = 1) then
	
		obj_id = "signal10"
		obj_txt = Chr(68)
		obj_font_size = 20
		obj_font_name = "Wingdings"
		obj_color = RGBColor(255,99,71)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)
	  
	
		obj_id = "signal"
		obj_txt = "SELL"
		obj_font_size = 10
		obj_font_name = "TAHOMA"
		obj_color = RGBColor(255,99,71)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)
	
	end if
	
	if (r = 2) and (s = 1) then
	
		obj_id = "signal10"
		obj_txt = Chr(73)
		obj_font_size = 20
		obj_font_name = "Wingdings"
		obj_color = RGBColor(255,99,71)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)
	  
	
		obj_id = "signal"
		obj_txt = "Hold"
		obj_font_size = 10
		obj_font_name = "TAHOMA"
		obj_color = RGBColor(255,0,255)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)

	end if
	
	if (r = 1) and (s = 2) then
	
		obj_id = "signal10"
		obj_txt = Chr(73)
		obj_font_size = 20
		obj_font_name = "Wingdings"
		obj_color = RGBColor(255,99,71)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)
	  
	
		obj_id = "signal"
		obj_txt = "Hold"
		obj_font_size = 10
		obj_font_name = "TAHOMA"
		obj_color = RGBColor(255,0,255)
		
		ObjectSetText 0, CSTR(obj_id), CINT(obj_font_size), CSTR(obj_font_name), CLNG(obj_color)
		Objectset CLNG(0),CSTR(obj_id), 2, CSTR(obj_txt)
	
	end if

End Sub


Public Sub main()

End Sub


Public Sub OnDeInit()
	ObjectDeleteAll (0)
End Sub

Public Sub OnTimer()

End Sub

Public Sub OnCalculate(symbol, symbolPeriod, openVal, highVal, lowVal, closeVal)

End Sub

Public Sub AllDataLoaded()

End Sub

Public Sub AccountSelected(accountNumber)

End Sub

Public Sub OnOrderTrade(actionType ,orderID , returnValue)

End Sub

Public Sub OnPositionTrade (actionType ,ticketID)

End Sub

public sub OnManageOrdersReceived(manageOrders)

End Sub
