CDC ActionZone BF for ETHUSD-1D
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @version=4
// © PRoSkYNeT-EE
// Based on improvements from "Kitti-Playbook Action Zone V.4.2.0.3 for Stock Market"
// Based on improvements from "CDC Action Zone V3 2020 by piriya33"

// Based on Triple MACD crossover between 9/15, 21/28, 15/28 for filter error signal (noise) from CDC ActionZone V3
// MACDs generated from the execution of millions of times in the "Brute Force Algorithm" to backtest data from the past 5 years. ( 2017-08-21 to 2022-08-01 )
// Released 2022-08-01

// ***** The indicator is used in the ETHUSD 1 Day period ONLY *****
// Recommended Stop Loss : -4 % (execute stop Loss after candlestick has been closed)

// Backtest Result ( Start $100 )
// Winrate 63 % (Win:12, Loss:7, Total:19)
// Live Days 1,806 days
// B : Buy
// S : Sell
// SL : Stop Loss

// 2022-07-19 07 - 1,542 : B 6.971 ETH
// 2022-04-13 07 - 3,118 : S      8.98 %      $10,750      12,7,19     63 %
// 2022-03-20 07 - 2,861 : B 3.448 ETH
// 2021-12-03 07 - 4,216 : SL      -8.94 %      $9,864      11,7,18     61 %
// 2021-11-30 07 - 4,630 : B 2.340 ETH
// 2021-11-18 07 - 3,997 : S      13.71 %      $10,832      11,6,17     65 %
// 2021-10-05 07 - 3,515 : B 2.710 ETH
// 2021-09-20 07 - 2,977 : S      29.38 %      $9,526      10,6,16     63 %
// 2021-07-28 07 - 2,301 : B 3.200 ETH
// 2021-05-20 07 - 2,769 : S      50.49 %      $7,363      9,6,15     60 %
// 2021-03-30 07 - 1,840 : B 2.659 ETH
// 2021-03-22 07 - 1,681 : SL      -8.29 %      $4,893      8,6,14     57 %
// 2021-03-08 07 - 1,833 : B 2.911 ETH
// 2021-02-26 07 - 1,445 : S      279.27 %      $5,335      8,5,13     62 %
// 2020-10-13 07 - 381 : B 3.692 ETH
// 2020-09-05 07 - 335 : S      38.43 %      $1,407      7,5,12     58 %
// 2020-07-06 07 - 242 : B 4.199 ETH
// 2020-06-27 07 - 221 : S      28.49 %      $1,016      6,5,11     55 %
// 2020-04-16 07 - 172 : B 4.598 ETH
// 2020-02-29 07 - 217 : S      47.62 %      $791      5,5,10     50 %
// 2020-01-12 07 - 147 : B 3.644 ETH
// 2019-11-18 07 - 178 : S      -2.73 %      $536      4,5,9     44 %
// 2019-11-01 07 - 183 : B 3.010 ETH
// 2019-09-23 07 - 201 : SL      -4.29 %      $551      4,4,8     50 %
// 2019-09-18 07 - 210 : B 2.740 ETH
// 2019-07-12 07 - 275 : S      63.69 %      $575      4,3,7     57 %
// 2019-05-03 07 - 168 : B 2.093 ETH
// 2019-04-28 07 - 158 : S      29.51 %      $352      3,3,6     50 %
// 2019-02-15 07 - 122 : B 2.225 ETH
// 2019-01-10 07 - 125 : SL      -6.02 %      $271      2,3,5     40 %
// 2018-12-29 07 - 133 : B 2.172 ETH
// 2018-05-22 07 - 641 : S      5.95 %      $289      2,2,4     50 %
// 2018-04-21 07 - 605 : B 0.451 ETH
// 2018-02-02 07 - 922 : S      197.42 %      $273      1,2,3     33 %
// 2017-11-11 07 - 310 : B 0.296 ETH
// 2017-10-09 07 - 297 : SL      -4.50 %      $92      0,2,2     0 %
// 2017-10-07 07 - 311 : B 0.309 ETH
// 2017-08-22 07 - 310 : SL      -4.02 %      $96      0,1,1     0 %
// 2017-08-21 07 - 323 : B 0.310 ETH

//****************************************************************************//

study("AGS ActionZone BF for ETHUSD-1D", overlay=true, precision=6)

//****************************************************************************//
// Define User Input Variables

xSource = input(title="Source Data",type=input.source, defval=close)
FMAp1 = input(title="Fast EMA period 1", type=input.integer, defval=9)
FMAp2 = input(title="Slow EMA period 2", type=input.integer, defval=15)
FMAp3 = input(title="Fast EMA period 3", type=input.integer, defval=21)
FMAp4 = input(title="Slow EMA period 4", type=input.integer, defval=15)
FMAp5 = input(title="Fast EMA period 5", type=input.integer, defval=28)

xSmooth = input(title="Smoothing period", type=input.integer, defval=1)
xFixTF = input(title="** Fixed Time Frame", type=input.bool, defval=false)
xTF = input(title="** Fix to time frame)", type=input.resolution, defval="D")

//****************************************************************************//
//Calculate Indicators

xPrice = ema(xSource,xSmooth)
yTF = xTF == "" ? timeframe.period : xTF
FMAi1 = xFixTF ? ema(security(syminfo.tickerid,yTF,ema(xSource,FMAp1)),xSmooth):ema(xPrice,FMAp1)
FMAi2 = xFixTF ? ema(security(syminfo.tickerid,yTF,ema(xSource,FMAp2)),xSmooth):ema(xPrice,FMAp2)
FMAi3 = xFixTF ? ema(security(syminfo.tickerid,yTF,ema(xSource,FMAp3)),xSmooth):ema(xPrice,FMAp3)
FMAi4 = xFixTF ? ema(security(syminfo.tickerid,yTF,ema(xSource,FMAp4)),xSmooth):ema(xPrice,FMAp4)
FMAi5 = xFixTF ? ema(security(syminfo.tickerid,yTF,ema(xSource,FMAp5)),xSmooth):ema(xPrice,FMAp5)

Bull = FMAi1 > FMAi3 and FMAi4 > FMAi5 and FMAi2 > FMAi5
Bear = FMAi1 < FMAi3 and FMAi4 < FMAi5

//****************************************************************************//
// Define Color Zones

Green = Bull // Buy
Blue = Bear and xPrice>FMAi1 and xPrice>FMAi2 //Pre Buy 2
LBlue = Bear and xPrice>FMAi1 and xPrice<FMAi2 //Pre Buy 1

Red = Bear  // Sell
Orange = Bull and xPrice < FMAi1 and xPrice < FMAi2 // Pre Sell 2
Yellow = Bull and xPrice < FMAi1 and xPrice > FMAi2 // Pre Sell 1

//****************************************************************************//
// Display color on chart

fillSW = input(title="Fill Bar Colors", type=input.bool, defval=true)
bColor = Green ? color.rgb(103, 204, 58) : 
     Blue ? color.rgb(255, 209, 66) : 
     LBlue ? color.rgb(255, 209, 66) : 
     Red ? color.rgb(247, 75, 75) : 
     Orange ? color.rgb(255, 209, 66) : 
     Yellow ? color.rgb(255, 209, 66) : 
      color.black
barcolor(color=fillSW? bColor : na)

//****************************************************************************//
// Display MA lines

fastSW3 = input(title="1 Fast MA On/Off", type=input.bool, defval=true)
slowSW9 = input(title="2 Slow MA On/Off", type=input.bool, defval=true)
FastL3 = plot(fastSW3 ? FMAi1 : na,"FMAp1",color=color.rgb(247, 75, 75))
SlowL9 = plot(slowSW9 ? FMAi2 : na,"FMAp3",color=color.gray)

fastSW12 = input(title="4 Fast MA On/Off", type=input.bool, defval=true)
slowSW26 = input(title="5 Slow MA On/Off", type=input.bool, defval=true)
FastL12 = plot(fastSW12 ? FMAi4 : na,"FMAp4",color=color.yellow)
SlowL26 = plot(slowSW26 ? FMAi5 : na,"FMAp5",color=color.gray)

fastSW9 = input(title="3 Fast MA On/Off", type=input.bool, defval=true)
FastL9 = plot(fastSW9 ? FMAi2 : na,"FMAp2",color=color.gray)

FillColor = Bull ? color.green : Bear ? color.red : color.black
fill(FastL3,SlowL9,FillColor)
fill(FastL12,SlowL26,FillColor)
fill(FastL9,SlowL26,FillColor)



//****************************************************************************//
// Define Buy and Sell condition
// This is only for the basic usage of CDC Actionzone (EMA Crossover) 
// ie. Buy on first green bar and sell on first red bar

BuyCond = Green and Green[1]==0
SellCond = Red and Red[1]==0

Bullish = barssince(BuyCond) < barssince(SellCond)
Bearish = barssince(SellCond) < barssince(BuyCond)

buy= Bearish[1] and BuyCond
sell= Bullish[1] and SellCond

//****************************************************************************//
// Plot Buy and Sell point on chart

plotshape(buy, style=shape.circle,
     title = "Buy Signal",
     location = location.belowbar,
     color = color.rgb(103, 204, 58))

plotshape(sell, style=shape.circle,
     title = "Sell Signal",
     location=location.abovebar,
     color = color.rgb(247, 75, 75))
// Alert conditions

alertcondition(buy, 
     title="Buy Alert",
     message= "Buy {{exchange}}:{{ticker}}")
     
alertcondition(sell, 
     title="Sell Alert",
     message= "Sell {{exchange}}:{{ticker}}")

//****************************************************************************//