#/ This source code is subject to the terms of the Mozilla Public License 2.0
#// © Complector
#// *** ITG Scalper by Complector
#// *** Thanks to Iain M. Banks for making my life richer :-)
#study(shorttitle="ITG_Scalper", title="ITG Scalper Buy/Sell [Complector]", overlay=true)
# Converted by Sam4Cok@Samer800 - 10/2023
input ShowMovAvgLine = yes; # "Plot TEMA?"
input changeTrendColor = yes; # "Green/redshift on/off?") // Trend color change?
input showPriceValue = no; # "Show buy/sell?") // Show buy & sell signals?
input showSignals = {Default "Bubbles", "Arrows", "Don't Show Signals"};
input showChangeSinceLastSignal = no; # "Show % change since last buy/sell"
input enableNoiseFilter = yes; # "Noise filter on/off?"
input useCurrentTimeframe = yes; # "Use Current Chart Resolution for filter?"
input resCustom = AggregationPeriod.HOUR; # "Use Different Timeframe for filter? Uncheck Box Above"
input movingAvgLength = 14; # "TEMA period"
input filterFastLength = 12; # "Filter fast length"
input filterSlowLength = 26; # "Filter slow length"
input filterSignalLength = 9; # "Filter signal length"
def na = Double.NaN;
def Arrow = showSignals==showSignals."Arrows";
def bubble = showSignals==showSignals."Bubbles";
def sigChange = showChangeSinceLastSignal;
#//Get real close price
def realC = close(GetSymbol());
def isconfirmed = !isNaN(Close);
#/Truncate function used for rounding variables
Script truncate {
input number = 0;
input decimals = 1;
def factor = power(10, decimals);
def truncate = floor(number * factor) / factor;
plot out = truncate;
}
#//Triple EMA definition
def ema1 = ExpAverage(realC, movingAvgLength);
def ema2 = ExpAverage(ema1, movingAvgLength);
def ema3 = ExpAverage(ema2, movingAvgLength);
#//Triple EMA trend calculation
def avg = 3 * (ema1 - ema2) + ema3;
def out = avg;
def out1 = out;#security(t_id, timeframe.period, out)
def ma_up = out1 >= out1[1];
def ma_down = out1 < out1[1];
def col = if changeTrendColor then if ma_up then 1 else
if ma_down then -1 else 1 else 0;
def t_UP = ma_up;
def t_DOWN = ma_down;
def t_NON = t_UP == t_DOWN;
#//Filter formula
def Fsource = close(GetSymbol());
def FsourceMTF = close(GetSymbol(), Period = resCustom);
def macd_colorChange = yes;
def Fres = if useCurrentTimeframe then Fsource else FsourceMTF;
def FfastMA = ExpAverage(Fres, filterFastLength);
def FslowMA = ExpAverage(Fres, filterSlowLength);
def Fmacd = FfastMA - FslowMA;
def Fsignal = Average(Fmacd, filterSignalLength);
def outMacD = Fmacd;
def outSignal = Fsignal;
def Fbuy = (outMacD >= outSignal) or !enableNoiseFilter;
def Fsell = (outMacD < outSignal) or !enableNoiseFilter;
#//Triple EMA plot
plot TEMA = if ShowMovAvgLine then out1 else na; # "TEMA"
TEMA.AssignValueColor(if col > 0 then Color.GREEN else
if col < 0 then Color.RED else Color.CYAN);
#//Entry & exit conditions
def buyprice;def sellprice;def last_tran;
def long = t_UP and !last_tran[1] and !t_NON and Fbuy and isconfirmed;
def short = t_DOWN and last_tran[1] and !t_NON and Fsell and isconfirmed;
if long {
buyprice = realC;
sellprice = sellprice[1];
last_tran = yes;
} else if short {
buyprice = buyprice[1];
sellprice = realC;
last_tran = no;
} else {
buyprice = buyprice[1];
sellprice = sellprice[1];
last_tran = last_tran[1];
}
def goodlong = long and buyprice <= sellprice; # // Buying with a profit?
def goodshort = short and sellprice > buyprice; # // Selling with a profit?
def PercVal = if(last_tran, 100 * (realC - buyprice)/buyprice, 100 * (realC - sellprice)/sellprice);
def PercCol = PercVal >= 0;#, color.lime, color.orange);
#//Plot percent label
AddLabel(sigChange, truncate(PercVal,2) + " %", if PercCol then Color.LIGHT_GREEN else Color.PINK);
#/ Alternative 2: Plot buy/sell signals as plotshape without price indication. No max.
def buytxt = truncate(buyprice,8);
def selltxt = truncate(sellprice,8);
plot priceUp = if Arrow and long then low else na;
plot priceDn = if Arrow and short then high else na;
priceUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
priceDn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
AddChartBubble(bubble and long, low,if showPriceValue then "B\n$"+buytxt else "B", if goodlong then Color.GREEN else Color.YELLOW, no);
AddChartBubble(bubble and short, high,if showPriceValue then "S\n$"+selltxt else "S", if goodshort then Color.RED else Color.YELLOW);
#-- END of CODE