BV's MACD SIGNAL TESTER
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © vuagnouxb

//@version=4
strategy("BV's MACD SIGNAL TESTER", overlay=true)

//------------------------------------------------------------------------
//----------            Confirmation Calculation              ------------ INPUT
//------------------------------------------------------------------------

// Getting inputs
fast_length = input(title="Fast Length", type=input.integer, defval=12)
slow_length = input(title="Slow Length", type=input.integer, defval=26)
src = input(title="Source", type=input.source, defval=close)
signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)
sma_source = input(title="Simple MA(Oscillator)", type=input.bool, defval=false)
sma_signal = input(title="Simple MA(Signal Line)", type=input.bool, defval=false)

// Plot colors
col_grow_above = #26A69A
col_grow_below = #FFCDD2
col_fall_above = #B2DFDB
col_fall_below = #EF5350
col_macd = #0094ff
col_signal = #ff6a00

// Calculating
fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)
slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)
hist = macd - signal

// plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )
// plot(macd, title="MACD", color=col_macd, transp=0)
// plot(signal, title="Signal", color=col_signal, transp=0)

// -- Trade entry signals

signalChoice = input(title = "Choose your signal", defval = "Continuation", options = ["Continuation", "Reversal", "Histogram", "MACD Line ZC", "Signal Line ZC"])

continuationSignalLong = signalChoice == "Continuation" ? crossover(macd, signal) and macd > 0 :
   signalChoice == "Reversal" ? crossover(macd, signal) and macd < 0 : 
   signalChoice == "Histogram" ? crossover(hist, 0) : 
   signalChoice == "MACD Line ZC" ? crossover(macd, 0) :
   signalChoice == "Signal Line ZC" ? crossover(signal, 0) :
   false
   
continuationSignalShort = signalChoice == "Continuation" ? crossunder(macd, signal) and macd < 0 :
   signalChoice == "Reversal" ? crossover(signal, macd) and macd > 0 : 
   signalChoice == "Histogram" ? crossunder(hist, 0) : 
   signalChoice == "MACD Line ZC" ? crossunder(macd, 0) :
   signalChoice == "Signal Line ZC" ? crossunder(signal, 0) :
   false

longCondition = continuationSignalLong

shortCondition = continuationSignalShort

//------------------------------------------------------------------------
//----------             ATR MONEY MANAGEMENT                 ------------
//------------------------------------------------------------------------

SLmultiplier = 1.5
TPmultiplier = 1

JPYPair = input(type = input.bool, title = "JPY Pair ?", defval = false)
pipAdjuster = JPYPair ? 1000 : 100000


ATR = atr(14) * pipAdjuster // 1000 for jpy pairs : 100000
SL = ATR * SLmultiplier
TP = ATR * TPmultiplier

//------------------------------------------------------------------------
//----------                  TIME FILTER                     ------------
//------------------------------------------------------------------------

YearOfTesting = input(title = "How many years of testing ?" , type = input.integer, defval = 3)

_time = 2020 - YearOfTesting

timeFilter = (year > _time) 

//------------------------------------------------------------------------
//---------                 ENTRY FUNCTIONS                    ----------- INPUT
//------------------------------------------------------------------------

if (longCondition and timeFilter)  
    strategy.entry("Long", strategy.long)

if (shortCondition and timeFilter) 
    strategy.entry("Short", strategy.short)
    
//------------------------------------------------------------------------
//---------                 EXIT  FUNCTIONS                    -----------
//------------------------------------------------------------------------


strategy.exit("ATR", from_entry = "Long", profit = TP, loss = SL)  

strategy.exit("ATR", from_entry = "Short", profit = TP, loss = SL)  