
Author Message: - Modified
CONCEPTS
This is a trend indicator. The trend is the 0.5 fibonacci level for a certain period of time.
A trend change occurs when at least one candle closes above the level of 0.236 (for long) or below 0.786 (for short). Also it has massive amout of settings and features more about this below.
With good settings, the indicator works great on any market and any time frame!
CSS:
#// https://www.tradingview.com/v/xW8hYdbL/
#//@IMBA_TRADER
#indicator(title='[IMBA] ALGO', shorttitle='[IMBA] ALGO', overlay=true, max_lines_count = 500,
# Converted by Sam4Cok@Samer800 - 01/2024 - * Modified.
#// MAIN
input colorInTradeBars = no;
input showInfoLabel = yes;
input timeframeType = {Default "Chart", "Custom"};
input customTimeframe = AggregationPeriod.FIFTEEN_MIN;
input show_fibo_Channel = no;
input triggerFiboLevel = {Default "0.236", "0.382"};
input sensitivity = 180; # 'Sensitive'
input show_tp_enty_sl = yes;
input fill_positions = yes;
input risk_percent = 1.0; # "Risk %"
input TradingAmount = 10000; # "Initial deposit"
input sl_percent_input = 0.00; # "SL %"
input tp_percent = 1.00; # "TP"
def na = Double.NaN;
def last = isNaN(close);
def bar_index = BarNumber();
def chart = timeframeType==timeframeType."Chart";
def rsikAmt = risk_percent * TradingAmount / 100;
def tp1_percent = tp_percent / 100;
def sl_percent = sl_percent_input / 100;
def hTF = high(Period = customTimeframe);
def lTF = low(Period = customTimeframe);
def HHtf = highest(hTF, sensitivity);
def LLtf = lowest(lTF, sensitivity);
def HH = highest(high, sensitivity);
def LL = lowest(low, sensitivity);
script VolCal {
input Index = 100;
def Green = Sum(if close > open then Volume else 0, Index);
def Red = Sum(if close < open then Volume else 0, Index);
def Total = Sum(Volume, Index);
def GreenRatio = Green / Total * 100;
def RedRatio = Red / Total * 100;
plot gr = GreenRatio > 55;
plot rd = RedRatio > 55;
}
#-- fibo Cals
def high_line = if chart then HH else HHtf;# Highest(if chart then high else hTF, sensitivity);
def low_line = if chart then LL else LLtf;#Lowest(if chart then low else lTF, sensitivity);
def channel_range = high_line - low_line;
def fibP236 = high_line - channel_range * 0.236;
def fibP382 = high_line - channel_range * 0.382;
def fib_5 = high_line - channel_range * 0.5;
def fibN382 = low_line + channel_range * 0.382;
def fibN236 = low_line + channel_range * 0.236;
def imba_trend_line = fib_5;
plot fibo236 = if show_fibo_Channel then fibP236 else na;
plot fibo382 = if show_fibo_Channel then fibP382 else na;
plot fibo618 = if show_fibo_Channel then fibN382 else na;
plot fibo786 = if show_fibo_Channel then fibN236 else na;
fibo236.SetDefaultColor(Color.DARK_GRAY);
fibo382.SetDefaultColor(Color.DARK_GRAY);
fibo618.SetDefaultColor(Color.DARK_GRAY);
fibo786.SetDefaultColor(Color.DARK_GRAY);
#// CAN LONG/SHORT
def is_long_trend;
def is_short_trend;
def is_long_trend_started;
def is_short_trend_started;
def long236 = close >= fibP236 and !is_long_trend[1];
def short236 = close <= fibN236 and !is_short_trend[1];
def Long382 = close >= fibP382 and !is_long_trend[1];
def Short382 = close <= fibN382 and !is_short_trend[1];
def can_long; def can_short;def upLvl; def dnLvl;
Switch (triggerFiboLevel) {
Case "0.382" :
can_long = Long382;
can_short = Short382;
upLvl = fibP382;
dnLvl = fibN382;
Default :
can_long = Long236;
can_short = Short236;
upLvl = fibP236;
dnLvl = fibN236;
}
if can_long {
is_long_trend = yes;
is_short_trend = no;
is_short_trend_started = no;
is_long_trend_started = if !is_long_trend[1] then yes else no;
} else if can_short {
is_short_trend = yes;
is_long_trend = no;
is_long_trend_started = no;
is_short_trend_started = if !is_short_trend[1] then yes else no;
} else {
is_long_trend = is_long_trend[1];
is_short_trend = is_short_trend[1];
is_short_trend_started = no;
is_long_trend_started = no;
}
def is_trend_change = is_short_trend_started or is_long_trend_started;
def Buy = is_long_trend_started;
def Sell = is_short_trend_started;
def up = is_long_trend[1];
plot TrendLine = if imba_trend_line then imba_trend_line else na;
TrendLine.SetLineWeight(2);
TrendLine.AssignValueColor(if up then Color.GREEN else Color.RED);#, linewidth = 3)
AddCloud(if !fill_positions then na else if up then upLvl else na, dnLvl, Color.DARK_GREEN);
AddCloud(if !fill_positions then na else if up then na else upLvl, dnLvl, Color.DARK_RED);
#//----- { SL Calculation
def TradeisON;
def LongTrade;
def ShortTrade;
def Long = Buy;
def Short = Sell;
def TradeFire = Long or Short;
if Long {
LongTrade = yes;
ShortTrade = no;
} else
if Short {
LongTrade = no;
ShortTrade = yes;
} else {
LongTrade = LongTrade[1];
ShortTrade = ShortTrade[1];
}
def share;
def win;
def los;
def entry;
def profit;
def losses;
def TP;
def SL;
def tpVar = channel_range * 0.382 * (1 + tp1_percent);
def slLong = imba_trend_line * (1 - sl_percent);
def slShort = imba_trend_line * (1 + sl_percent);
if Long {
entry = ohlc4[-1];
share = RoundDown(TradingAmount/entry, 0);
TP = entry + tpVar;
SL = Min(entry - (rsikAmt/share), slLong);
win = if Bar_index < 1 then 0 else win[1];
los = if Bar_index < 1 then 0 else los[1];
profit = if Bar_index < 1 then 0 else profit[1];
losses = if Bar_index < 1 then 0 else losses[1];
TradeisON = yes;
} else
if Short {
entry = ohlc4[-1];
share = RoundDown(TradingAmount/entry, 0);
TP = entry - tpVar;
SL = Min(entry + (rsikAmt/share), slShort);
win = if Bar_index < 1 then 0 else win[1];
los = if Bar_index < 1 then 0 else los[1];
profit = if Bar_index < 1 then 0 else profit[1];
losses = if Bar_index < 1 then 0 else losses[1];
TradeisON = yes;
} else
if LongTrade and TradeisON[1] {
entry = entry[1];
share = share[1];
TP = TP[1];
SL = SL[1];
if Sell[-1] {
TradeisON = no;
profit = if close > entry then profit[1] + AbsValue(close - entry) * share else profit[1];
losses = if close < entry then losses[1] + AbsValue(entry - close) * share else losses[1];
win = if close > entry then win[1] + 1 else win[1];
los = if close < entry then los[1] + 1 else los[1];
} else if high >= TP {
TradeisON = no;
profit = profit[1] + AbsValue(close - entry) * share;
losses = losses[1];
win = win[1] + 1;
los = los[1];
} else if close < SL {
TradeisON = no;
profit = profit[1];
losses = losses[1] + AbsValue(entry - close) * share;
win = win[1];
los = los[1] + 1;
} else {
TradeisON = TradeisON[1];
profit = profit[1];
losses = losses[1];
win = win[1];
los = los[1];}
} else
if ShortTrade and TradeisON[1] {
entry = entry[1];
share = share[1];
TP = TP[1];
SL = SL[1];
if Buy[-1] {
TradeisON = no;
profit = if close < entry then profit[1] + AbsValue(close - entry) * share else profit[1];
losses = if close > entry then losses[1] + AbsValue(entry - close) * share else losses[1];
win = if close < entry then win[1] + 1 else win[1];
los = if close > entry then los[1] + 1 else los[1];
} else if low <= TP {
TradeisON = no;
profit = profit[1] + AbsValue(close - entry) * share;
losses = losses[1];
win = win[1] + 1;
los = los[1];
} else if close > SL {
TradeisON = no;
profit = profit[1];
losses = losses[1] + AbsValue(entry - close) * share;
win = win[1];
los = los[1] + 1;
} else {
TradeisON = TradeisON[1];
profit = profit[1];
losses = losses[1];
win = win[1];
los = los[1];}
} else {
entry = na;
share = na;
win = if Bar_index < 1 then 0 else win[1];
los = if Bar_index < 1 then 0 else los[1];
profit = if Bar_index < 1 then 0 else profit[1];
losses = if Bar_index < 1 then 0 else losses[1];
TradeisON = TradeisON[1];
TP = if TradeisON then TP[1] else na;
SL = if TradeisON then SL[1] else na;
}
def lineUp = show_tp_enty_sl and !last and TP;
def lineDn = show_tp_enty_sl and !last and SL;
def lineEn = show_tp_enty_sl and !last and entry;
plot TakeProfit = if lineUp then TP else na;
plot stopLoss = if lineDn then SL else na;
plot entryLine = if lineEn then entry else na;
TakeProfit.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
stopLoss.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
entryLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TakeProfit.SetDefaultColor(Color.CYAN);
stopLoss.SetDefaultColor(Color.MAGENTA);
entryLine.SetDefaultColor(GetColor(8));
#--BarColor
def BearCon = TradeisON and ShortTrade;
def BullCon = TradeisON and LongTrade;
AssignPriceColor(if !colorInTradeBars then Color.CURRENT else
if BearCon then Color.RED else
if BullCon then Color.GREEN else Color.GRAY);
#-- Backtest
plot TradeEnd = if !TradeisON and TradeisON[1] then
if los > los[1] then SL[1] else TP[1] else na;
TradeEnd.AssignValueColor(if win > win[1] then Color.GREEN else Color.RED);
TradeEnd.SetPaintingStrategy(PaintingStrategy.SQUARES);
#-- Label
def totTrade = win + los;
def winRate = Round(win / totTrade * 100, 0);
def PandL = Round(profit - losses, 2);
AddLabel(showInfoLabel, "Tot Trade: " + totTrade, Color.WHITE);
AddLabel(showInfoLabel, "P/L: $" + PandL,
if PandL > 0 then Color.LIGHT_GREEN else
if PandL < 0 then Color.PINK else Color.GRAY);
AddLabel(showInfoLabel, "WinRate: " + winRate + "%",
if winRate > 50 then Color.GREEN else
if winRate < 50 then Color.RED else Color.GRAY);
AddLabel(showInfoLabel, "Win: " + win, Color.GREEN);
AddLabel(showInfoLabel, "Loss: " + los, Color.RED);
#-- Signals
def volH = VolCal(30).gr;
def volL = VolCal(30).rd;
def buyCond = BullCon and !BullCon[1];
def sellCond = BearCon and !BearCon[1];
#-- Signals
AddChartBubble(buyCond, low, "B" , if volH then Color.GREEN else Color.DARK_GREEN, no);
AddChartBubble(sellCond,high, "S", if volL then Color.RED else Color.DARK_RED);
#-- END of CODE