[imba]lance algo for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
dhJFaZC.png


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
 
Thank you @samer800 . This is helpful. I did notice that it's basically a Donchian Channel's Median line with added fib levels. I'm wondering, in consideration to how Fib strategy works, instead of a breakout, would it make sense to adjust entry as a retracement strategy with break/retest above/below that fib for entry instead of reversal strategy?

Also instead of % profit, would it be possible to adjust profit/targets to use ATR as the basis of TP % and add a breakeven point if price hits 1/1.

If there are any other scripts that are currently doing this, it would be helpful as well.

Thank you.
 
Last edited:
dhJFaZC.png


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

Is there an alert for the Buy and Sell conditions or some type of scan?
 
Last edited by a moderator:
dhJFaZC.png


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
Hi,
Re: last part of this study:
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);

Isnt the 'volL then Color.RED' should be DARK.RED since vol is low there?
 
Hey @samer800 I see your name a lot lol

How would we be able to scan with this indicator?
Sure. At the bottom of the script, change:
def buyCond = BullCon and !BullCon[1];
def sellCond = BearCon and !BearCon[1];
To:
plot buyCond = BullCon and !BullCon[1];
plot sellCond = BearCon and !BearCon[1];

here is the scanner tutorial: https://usethinkscript.com/threads/how-to-use-thinkorswim-stock-hacker-scans.284/

The condition would be:
buyCond is true
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
333 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top