#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © Tradeiqofficial
#strategy("ChatGPT Algo Bitcoin", overlay=true)
# Converted by Sam4Cok@Samer800 - 08/2023
#// Input parameters
input smoothSslChannel = no;
input sslLength = 20;#, title="SSL Channel Length", minval=1)
input src = close;
input movAvgLength = 200;#, title="EMA Length", minval=1)
input MovAvgType = AverageType.EXPONENTIAL;
input atrLength = 14;#, title="ATR Length", minval=1)
input slMultiplier = 2.0;#, title="SL Multiplier", type=input.float)
input tpMultiplier = 4.0;#, title="TP Multiplier", type=input.float)
input thresholdHigh = 2.5;#, title="High Volume Threshold", type=input.float)
input thresholdMedium = 1.0;#, title="Medium Volume Threshold", type=input.float)
input volumeLength = 610;#, title="Volume MA Length", type=input.integer)
def na = Double.NaN;
#// Calculate SSL Channel
def avghh = ExpAverage(high,5);
def avgll = ExpAverage(low, 5);
def srcHi = if smoothSslChannel then avghh else high;
def srcLo = if smoothSslChannel then avgll else low;
def smaHigh = Average(srcHi, sslLength);
def smaLow = Average(srcLo, sslLength);
def Hlv = if src > smaHigh then 1 else
if src < smaLow then -1 else Hlv[1];
def sslDown = if Hlv < 0 then smaHigh else smaLow;
def sslUp = if Hlv < 0 then smaLow else smaHigh;
#// Calculate 200 EMA
def ema200 = MovingAverage(MovAvgType, src, movAvgLength);
#// Calculate ATR
def atr = ATR(LENGTH = atrLength);
#// Calculate Heatmap Volume
def volumeMean = Average(volume, volumeLength);
def volumeStd = stdev(volume, volumeLength);
def stdBar = (volume - volumeMean) / volumeStd;
#// Define Volume Filter
def volumeFilter = stdBar > thresholdMedium or stdBar > thresholdHigh;
#// Define Buy and Sell conditions
def crossUp = (sslUp > sslDown) and (sslUp[1] <= sslDown[1]);
def crossDn = (sslUp < sslDown) and (sslUp[1] >= sslDown[1]);
def bullishCross = crossUp and src < ema200 and volumeFilter;
def bearishCross = crossDn and src > ema200 and volumeFilter;
def buyCondition = bullishCross;
def sellCondition = bearishCross;
def LongEntry = if buyCondition then src[-1] else if(LongEntry[1]==0, src[-1], LongEntry[1]);
def shortEntry = if sellCondition then src[-1] else if(shortEntry[1]==0, src[-1], shortEntry[1]);
#// Plot SSL Channel and 200 EMA
def sslDnBand = sslDown; # "SSL Down"
def sslUpBand = sslUp; # "SSL Up"
plot emaLine = ema200; # "200 EMA"
emaLine.SetDefaultColor(Color.WHITE);
AddCloud(sslUp, sslDown, Color.DARK_GREEN, Color.DARK_RED, yes);
#// Plot long and short signals
AddChartBubble(buyCondition, low, "Buy",color.green, no);
AddChartBubble(sellCondition, high, "Sell", color.red, yes);
#-- Strategy
#// Set SL and TP levels
def longTakeProfit = LongEntry >= (src + atr * tpMultiplier);
def shortTakeProfit = shortEntry <= (src - atr * tpMultiplier);
def longStopLoss = LongEntry < (src - atr * slMultiplier);
def shortStopLoss = shortEntry > (src + atr * slMultiplier);
AddOrder(OrderType.BUY_TO_OPEN, if buyCondition then src[-1] else Double.NaN, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = "Buy");
AddOrder(OrderType.SELL_TO_OPEN,if sellCondition then src[-1] else Double.NaN, tickcolor = GetColor(0), arrowcolor = GetColor(0), name = "Sell");
AddOrder(OrderType.SELL_TO_CLOSE, longTakeProfit or longStopLoss, tickcolor = GetColor(4), arrowcolor = GetColor(4),
name = if longTakeProfit then "Long_Loss" else "Long_WIN");
AddOrder(OrderType.BUY_TO_CLOSE, if (shortStopLoss or shortTakeProfit) then src else Double.NaN,tickcolor=GetColor(5), arrowcolor = GetColor(5),
name = if shortTakeProfit then "Short_Loss" else "Short_Win");
#-- END of CODE