0% found this document useful (0 votes)
13 views33 pages

BB

This document contains a Pine Script for a trading strategy tool, including definitions for standard and custom monospace fonts. It features various input parameters for trading strategies across different asset classes, including crypto and commodities, along with backtesting suggestions. The script allows for customization of moving averages, leverage, and trade management settings.

Uploaded by

dongmkt100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views33 pages

BB

This document contains a Pine Script for a trading strategy tool, including definitions for standard and custom monospace fonts. It features various input parameters for trading strategies across different asset classes, including crypto and commodities, along with backtesting suggestions. The script allows for customization of moving averages, leverage, and trade management settings.

Uploaded by

dongmkt100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 33

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
https://mozilla.org/MPL/2.0/
// © BBScalper
//@version=5

///////////////////////////////////////////////////////////////////////////////////
///////////////////
///// Monospace fonts ////
//{
//
// Font source : https://fonts.adobe.com/fonts/courier?
mv=affiliate&mv2=red#recommendations-section //
//
//
///////////////////////////////////////////////////////////////////////////////////
///////////////////
///
//// STANDARD PINESCRIPT FONT ////
Pine_std_LC = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"
Pine_std_UC = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
Pine_std_DG = "0,1,2,3,4,5,6,7,8,9"
//
//// CUSTOM MONOSPACE FONT ////
// (a different font can be copy-pasted here, from the listed website or another
source)
//
// Sans-serif Std
std_LC = "𝚊,𝚋,𝚌,𝚍,𝚎,𝚏,𝚐,𝚑,𝚒,𝚓,𝚔,𝚕,𝚖,𝚗,𝚘,𝚙,𝚚,𝚛,𝚜,𝚝,𝚞,𝚟,𝚠,𝚡,𝚢,𝚣"
std_UC = "𝙰,𝙱,𝙲,𝙳,𝙴,𝙵,𝙶,𝙷,𝙸,𝙹,𝙺,𝙻,𝙼,𝙽,𝙾,𝙿,𝚀,𝚁,𝚂,𝚃,𝚄,𝚅,𝚆,𝚇,𝚈,𝚉"
std_DG = "𝟶,𝟷,𝟸,𝟹,𝟺,𝟻,𝟼,𝟽,𝟾,𝟿"
//

= "𝘢,𝘣,𝘤,𝘥,𝘦,𝘧,𝘨,𝘩,𝘪,𝘫,𝘬,𝘭,𝘮,𝘯,𝘰,𝘱,𝘲,𝘳,𝘴,𝘵,𝘶,𝘷,𝘸,𝘹,𝘺,𝘻"
// Sans-serif Std Italic

= "𝘈,𝘉,𝘊,𝘋,𝘌,𝘍,𝘎,𝘏,𝘐,𝘑,𝘒,𝘓,𝘔,𝘕,𝘖,𝘗,𝘘,𝘙,𝘚,𝘛,𝘜,𝘝,𝘞,𝘟,𝘠,𝘡"
std_it_LC

= "𝟢,𝟣,𝟤,𝟥,𝟦,𝟧,𝟨,𝟩,𝟪,𝟫"
std_it_UC
std_it_DG
//

= "𝗮,𝗯,𝗰,𝗱,𝗲,𝗳,𝗴,𝗵,𝗶,𝗷,𝗸,𝗹,𝗺,𝗻,𝗼,𝗽,𝗾,𝗿,𝘀,𝘁,𝘂,𝘃,𝘄,𝘅,𝘆,𝘇"
// Sans-serif Bold

= "𝗔,𝗕,𝗖,𝗗,𝗘,𝗙,𝗚,𝗛,𝗜,𝗝,𝗞,𝗟,𝗠,𝗡,𝗢,𝗣,𝗤,𝗥,𝗦,𝗧,𝗨,𝗩,𝗪,𝗫,𝗬,𝗭"
bld_LC

= "𝟬,𝟭,𝟮,𝟯,𝟰,𝟱,𝟲,𝟳,𝟴,𝟵"
bld_UC
bld_DG
//

= "𝙖,𝙗,𝙘,𝙙,𝙚,𝙛,𝙜,𝙝,𝙞,𝙟,𝙠,𝙡,𝙢,𝙣,𝙤,𝙥,𝙦,𝙧,𝙨,𝙩,𝙪,𝙫,𝙬,𝙭,𝙮,𝙯"
// Sans-serif Bold Italic

= "𝘼,𝘽,𝘾,𝘿,𝙀,𝙁,𝙂,𝙃,𝙄,𝙅,𝙆,𝙇,𝙈,𝙉,𝙊,𝙋,𝙌,𝙍,𝙎,𝙏,𝙐,𝙑,𝙒,𝙓,𝙔,𝙕"
bld_it_LC

= "𝟬,𝟭,𝟮,𝟯,𝟰,𝟱,𝟲,𝟳,𝟴,𝟵"
bld_it_UC
bld_it_DG
//}
///////////////////////////////////////////////////////////////////////////////////
///////////////////

///////////////////////////////////////////////////////////////////////////////////
///////////////////
//// Font arrays ////
//{
// Pine fonts
Pine_font_std_LC = str.split(Pine_std_LC, ",")
Pine_font_std_UC = str.split(Pine_std_UC, ",")
Pine_font_std_DG = str.split(Pine_std_DG, ",")

// Custom fonts
// Standard
Cust_font_std_LC = str.split(std_LC, ",")
Cust_font_std_UC = str.split(std_UC, ",")
Cust_font_std_DG = str.split(std_DG, ",")
// Standard italic
Cust_font_std_it_LC = str.split(std_it_LC, ",")
Cust_font_std_it_UC = str.split(std_it_UC, ",")
Cust_font_std_it_DG = str.split(std_it_DG, ",")
// Bold
Cust_font_bld_LC = str.split(bld_LC, ",")
Cust_font_bld_UC = str.split(bld_UC, ",")
Cust_font_bld_DG = str.split(bld_DG, ",")
// Bold italic
Cust_font_bld_it_LC = str.split(bld_it_LC, ",")
Cust_font_bld_it_UC = str.split(bld_it_UC, ",")
Cust_font_bld_it_DG = str.split(bld_it_DG, ",")
//}

//--------- UNICODE REPLACEMENT FUNCTIONS ----------------------------


// Unicode Character Replace Function
uni_replace_CHAR(_str, _upper, _bold, _italic) =>
_custom_font_LC =
_bold ? _italic ? Cust_font_bld_it_LC : Cust_font_bld_LC :
_italic ? Cust_font_std_it_LC : Cust_font_std_LC
_custom_font_UC =
_bold ? _italic ? Cust_font_bld_it_UC : Cust_font_bld_UC :
_italic ? Cust_font_std_it_UC : Cust_font_std_UC
_new_str = _str
for _i = 0 to array.size(Pine_font_std_LC) - 1
_new_str := str.replace_all(_new_str, array.get(Pine_font_std_LC, _i),
array.get(_upper ? _custom_font_UC :
_custom_font_LC, _i))

_new_str := str.replace_all(_new_str, array.get(Pine_font_std_UC, _i),


array.get(_custom_font_UC, _i))
_new_str

// Unicode Digit Replace Function


uni_replace_DG(_str, _bold, _italic) =>
_custom_font =
_bold ? _italic ? Cust_font_bld_it_DG : Cust_font_bld_DG :
_italic ? Cust_font_std_it_DG : Cust_font_std_DG
_new_str = _str
for _i = 0 to array.size(Pine_font_std_DG) - 1
_new_str := str.replace_all(_new_str, array.get(Pine_font_std_DG, _i),
array.get(_custom_font, _i))
_new_str

// Unicode Global Replace Function


uni_replace(_str, _upper, _bold, _italic) =>
_str2 = uni_replace_DG( _str, _bold, _italic)
_str3 = uni_replace_CHAR(_str2, _upper, _bold, _italic)
_str3
//
-----------------------------------------------------------------------------------
----------------------------------------------------------
//
-----------------------------------------------------------------------------------
----------------------------------------------------------
// Backtest Suggestions:
// Crypto: TON, SUI, ARB + 1H/4H BACKTEST ON SPOT
// Commodities: SILVER, GAS, COPPER
// Indices: S&P500, DAX

//
-----------------------------------------------------------------------------------
----------------------------------------------------------
//
-----------------------------------------------------------------------------------
----------------------------------------------------------

strategy(title="BB Backtest Tool",


shorttitle="BBb Tool",
overlay=true,
pyramiding=0,
slippage=0,
default_qty_type= strategy.fixed,
default_qty_value=0,
initial_capital=10000000,
commission_type=strategy.commission.percent,
commission_value=0.0,
calc_on_order_fills = true,
process_orders_on_close = true,
max_labels_count = 500)

// ------------------------------------------
// GLOBAL INPUTS
// ------------------------------------------
asset = input.string(title="Strategies", defval = " ### Dev Mode ### ", options=[
" ### Dev Mode ### ",
" ",
" ===== Crypto ===== ",
" ",
"_____ 30 min _____",
"BTCUSDT.P 30m | Binance - v1.4",
"ETHUSDT.P 30m | Binance - v1.5",
" ",
" ===== Commodities ===== ",
" ",
" ____ 15 min ____ ",
"GOLD 15m (US$/OZ) | CFD TVC - v1.3",
"SILVER 15m (US$/OZ) | CFD TVC - v1.0",

" ",
" ____ 30 min ____ ",
"USOIL 30m (WTI CRUDE OIL) | CFD TVC - v1.1"
])
ma_choice = input.string(title="MA Choice", defval = "EMA", options=["EMA", "HMA",
"RMA", "SMA", "VWAP", "VWMA", "WMA"])
ma_period = input.int(title='MA Period', defval=300, minval=0, maxval=10000)
leverage = input.int(title="Leverage", defval=20, minval=1, maxval=125)
renorm_coeff_comm = input.float(title="Renormalization", defval=1.0, minval=1.0,
maxval=20.0)
prob_stats_number = input.int(title='Number Trades StatistBB', defval=50, minval=1,
maxval=150)

martingale_input = input.float(title = "Martingale multiplier", defval = 2, group =


"Averaging Data")
avg_perc_input = input.int(title = "Avg Order distance ‰", defval = 15, group =
"Averaging Data")
number_averaging = input.int(title = "Number Averagin Orders", defval = 3, group =
"Averaging Data")
perc_avoid_avg_long_level = input.int(title = "TP Level Avoid Avg Long", defval =
4, group = "Averaging Data", minval = 1, maxval = 8)
perc_avoid_avg_short_level = input.int(title = "TP Level Avoid Avg Short", defval
= 4, group = "Averaging Data", minval = 1, maxval = 8)

deposit = input.int(title = "Deposit", defval = 10000, minval = 1, group =


"Earnings")
margin_perc = input.int(title = "Margin Percentage", defval = 10, tooltip =
"Percentage of deposit to be invested", group = "Earnings") / 100

num_cum1 = input.int(title='Number Trades Cumulative 1', defval=50, minval=1,


maxval=200, group = "Cumulative Input")
num_cum2 = input.int(title='Number Trades Cumulative 2', defval=75, minval=1,
maxval=200, group = "Cumulative Input")
num_cum3 = input.int(title='Number Trades Cumulative 3', defval=100, minval=1,
maxval=200, group = "Cumulative Input")
num_cum4 = input.int(title='Number Trades Cumulative 4', defval=150, minval=1,
maxval=200, group = "Cumulative Input")

// ------------------------------------------
// LONG INPUTS
// ------------------------------------------
std_deviation_period_long = input.int(title='Stdev Period', defval=14, minval=1,
maxval=1000, group="Long")
std_deviation_coefficient_long = input.float(title='Stdev Coeff', defval=0.5,
minval=0, maxval=100, step = 0.1, group="Long")
atr_period_long = input.int(title='ATR Period', defval=14, minval=1, maxval=1000,
group="Long")
atr_parameter_long = input.float(title='ATR Coeff', defval=0.5, minval=0.0,
maxval=100, step = 0.1, group = "Long")
long_entry_ma_distance_perc = input.float(title='MA Delta %', defval=0.5,
minval=0.0, maxval=100.0, step = 0.1, group="Long")
long_stop_price_perc = input.float(title="Stop Loss %", defval=4.5, minval=0,
maxval=100, group="Long")

// ------------------------------------------
// SHORT INPUTS
// ------------------------------------------
std_deviation_period_short = input.int(title='Stdev Period', defval=14, minval=1,
maxval=1000, group="Short")
std_deviation_coefficient_short = input.float(title='Stdev Coeff', defval=0.5,
minval=0, maxval=100, step = 0.1, group="Short")
atr_period_short = input.int(title='ATR Period', defval=14, minval=0, maxval=1000,
group="Short")
atr_parameter_short = input.float(title='ATR Coeff', defval=0.5, minval=0.0,
maxval=100, step = 0.1, group = "Short")
short_entry_ma_distance_perc = input.float(title='MA Delta %', defval=0.5,
minval=0.0, maxval=100.0, step = 0.1, group="Short")
short_stop_price_perc = input.float(title="Stop Loss %", defval=4.5, minval=0,
maxval=100, group="Short")

table_txt_bg_color = input.color(color.new(color.gray, 80), "Text Background


Color", inline = "2", group = "Table Parameters")
table_txt_color = input.color( color.new(color.white, 25), "Text Color", inline =
"2", group = "Table Parameters")
table_txt_size = input.string("auto", "Text Size", inline = "2", options = ["auto",
"tiny", "small", "normal", "large", "huge"], group = "Table Parameters")

// ---------------------------------------------------
// CRYPTO
// ---------------------------------------------------
if asset == "ETHUSDT.P 30m | Binance - v1.5"
ma_period := 1
ma_choice := "VWAP"

std_deviation_period_long := 14
std_deviation_coefficient_long := 1
atr_period_long := 14
atr_parameter_long := 1.7
long_entry_ma_distance_perc := 1.7
long_stop_price_perc := 4.5

std_deviation_period_short := 14
std_deviation_coefficient_short := 1.5
atr_period_short := 14
atr_parameter_short := 1.7
short_entry_ma_distance_perc := 1.7
short_stop_price_perc := 4.5

if asset == "BTCUSDT.P 30m | Binance - v1.4"


ma_period := 1
ma_choice := "VWAP"

std_deviation_period_long := 14
std_deviation_coefficient_long := 1
atr_period_long := 14
atr_parameter_long := 1.8
long_entry_ma_distance_perc := 1.8
long_stop_price_perc := 4.5

std_deviation_period_short := 10
std_deviation_coefficient_short := 0.8
atr_period_short := 14
atr_parameter_short := 1.6
short_entry_ma_distance_perc := 1.4
short_stop_price_perc := 4.5

// ---------------------------------------------------
// COMMODITIES
// ---------------------------------------------------

if asset == "GOLD 15m (US$/OZ) | CFD TVC - v1.3"


renorm_coeff_comm := 5
ma_period := 250
ma_choice := "HMA"

std_deviation_period_long := 10
std_deviation_coefficient_long := 0.2
atr_period_long := 14
atr_parameter_long := 0.5
long_entry_ma_distance_perc := 0.5
long_stop_price_perc := 4.5

std_deviation_period_short := 14
std_deviation_coefficient_short := 0.4
atr_period_short := 14
atr_parameter_short := 0.4
short_entry_ma_distance_perc := 0.4
short_stop_price_perc := 4.5

if asset == "SILVER 15m (US$/OZ) | CFD TVC - v1.0"


renorm_coeff_comm := 5
ma_period := 250
ma_choice := "EMA"

std_deviation_period_long := 14
std_deviation_coefficient_long := 0.3
atr_period_long := 14
atr_parameter_long := 0.5
long_entry_ma_distance_perc := 0.5
long_stop_price_perc := 4.5

std_deviation_period_short := 14
std_deviation_coefficient_short := 0.5
atr_period_short := 14
atr_parameter_short := 0.5
short_entry_ma_distance_perc := 0.5
short_stop_price_perc := 4.5

if asset == "USOIL 30m (WTI CRUDE OIL) | CFD TVC - v1.1"


renorm_coeff_comm := 2
ma_period := 300
ma_choice := "HMA"

std_deviation_period_long := 11
std_deviation_coefficient_long := 1
atr_period_long := 14
atr_parameter_long := 1.5
long_entry_ma_distance_perc := 1.5
long_stop_price_perc := 4.5

std_deviation_period_short := 11
std_deviation_coefficient_short := 1
atr_period_short := 14
atr_parameter_short := 1.5
short_entry_ma_distance_perc := 1.5
short_stop_price_perc := 4.5
// ------------------------------------------
// GLOBAL CALCULATIONS
// ------------------------------------------
// Asset Precision
var float normalization = na
var int precision = 0

if barstate.isfirst
for i = 0 to 10
normalization := syminfo.mintick * math.pow(10, i)
if normalization == 1
precision := i

// Redefinition Global Parameters


var float a = na
var float m = na

a := avg_perc_input / (1000 * renorm_coeff_comm)


m := martingale_input

// TP Functions
tp_function_price(choice, entry, tp, renorm) =>
level = choice == "long" ? entry * (1 + 0.001 * tp/renorm) : choice ==
"short" ? entry * (1 - 0.001 * tp/renorm) : na
math.round(level, precision)

level_to_perc(n) =>
j = 1
k = 2
l = 3

if n <= 8 and n >= 1


for i = 1 to n
j := k
k := l
l := j + k
else
l := 0

math.round((l * leverage / 10) / renorm_coeff_comm)

level_to_price(trade_type, effective_entry, tp_level) =>


var float perc = na
var float price_level = na

perc := level_to_perc(tp_level)
price_level := tp_function_price(trade_type, effective_entry, perc * 10 /
leverage, renorm_coeff_comm)
price_level

// General MA Choice
var float ma_value = 0.0

ma_value := switch
ma_choice == "EMA" => ta.ema(close, ma_period)
ma_choice == "HMA" => ta.hma(close, ma_period)
ma_choice == "RMA" => ta.rma(close, ma_period)
ma_choice == "SMA" => ta.sma(close, ma_period)
ma_choice == "VWAP" => ta.vwap(close)
ma_choice == "VWMA" => ta.vwma(close, ma_period)
ma_choice == "WMA"=> ta.wma(close, ma_period)

// Avoid Averaging
var float perc_avoid_avg_long = na
var float perc_avoid_avg_short = na

perc_avoid_avg_long := level_to_perc(perc_avoid_avg_long_level)
perc_avoid_avg_short := level_to_perc(perc_avoid_avg_short_level)

// ------------------------------------------
// SPECIFIC CALCULATIONS
// ------------------------------------------
// Long
var float stdev_long = na
var float stdev_long_value = na
var float atr_long_value = na
var float long_entry_ma_value = na

stdev_long := ta.stdev(close, std_deviation_period_long)


stdev_long_value := stdev_long * std_deviation_coefficient_long
atr_long_value := ta.atr(atr_period_long) * atr_parameter_long
long_entry_ma_value := ma_value * (1 + (long_entry_ma_distance_perc / 100)) +
atr_long_value + stdev_long_value

var float long_entry_price = na


var float effective_entry_long = na
var float tp1_price_long = na
var float tp2_price_long = na
var float tp3_price_long = na
var float tp4_price_long = na
var float tp5_price_long = na
var float tp6_price_long = na
var float tp7_price_long = na
var float tp8_price_long = na
var float long_stop_price = na

long_entry_price := math.round(long_entry_ma_value, precision)


effective_entry_long := strategy.position_size <= 0 ? long_entry_price :
strategy.position_avg_price // P.S. Con un matching
perfetto non servirebbe tutta la questione di ricalcolo con gli exact_peak_profit e
neanche la creazione di alert ad hoc ma si potrebbero
tp1_price_long := tp_function_price("long", effective_entry_long , 5,
renorm_coeff_comm) // usare le funzioni
di alert negli strategy orders vari.
tp2_price_long := tp_function_price("long", effective_entry_long , 8,
renorm_coeff_comm)
tp3_price_long := tp_function_price("long", effective_entry_long , 13,
renorm_coeff_comm)
tp4_price_long := tp_function_price("long", effective_entry_long , 21,
renorm_coeff_comm)
tp5_price_long := tp_function_price("long", effective_entry_long , 34,
renorm_coeff_comm)
tp6_price_long := tp_function_price("long", effective_entry_long , 55,
renorm_coeff_comm)
tp7_price_long := tp_function_price("long", effective_entry_long , 89,
renorm_coeff_comm)
tp8_price_long := tp_function_price("long", effective_entry_long , 144,
renorm_coeff_comm)
long_stop_price := tp_function_price("long", effective_entry_long , -
long_stop_price_perc * 10, renorm_coeff_comm)

// Short
var float stdev_short = na
var float stdev_short_value = na
var float atr_short_value = na
var float short_entry_ma_value = na

stdev_short := ta.stdev(close, std_deviation_period_short)


stdev_short_value := stdev_short * std_deviation_coefficient_short
atr_short_value := ta.atr(atr_period_short) * atr_parameter_short
short_entry_ma_value := ma_value * (1 - (short_entry_ma_distance_perc / 100)) -
atr_short_value - stdev_short_value

var float short_entry_price = na


var float effective_entry_short = na
var float tp1_price_short = na
var float tp2_price_short = na
var float tp3_price_short = na
var float tp4_price_short = na
var float tp5_price_short = na
var float tp6_price_short = na
var float tp7_price_short = na
var float tp8_price_short = na
var float short_stop_price = na

short_entry_price := math.round(short_entry_ma_value, precision)


effective_entry_short := strategy.position_size >= 0 ? short_entry_price :
strategy.position_avg_price
tp1_price_short := tp_function_price("short", effective_entry_short , 5,
renorm_coeff_comm)
tp2_price_short := tp_function_price("short", effective_entry_short , 8,
renorm_coeff_comm)
tp3_price_short := tp_function_price("short", effective_entry_short , 13,
renorm_coeff_comm)
tp4_price_short := tp_function_price("short", effective_entry_short , 21,
renorm_coeff_comm)
tp5_price_short := tp_function_price("short", effective_entry_short , 34,
renorm_coeff_comm)
tp6_price_short := tp_function_price("short", effective_entry_short , 55,
renorm_coeff_comm)
tp7_price_short := tp_function_price("short", effective_entry_short , 89,
renorm_coeff_comm)
tp8_price_short := tp_function_price("short", effective_entry_short , 144,
renorm_coeff_comm)
short_stop_price := tp_function_price("short", effective_entry_short , -
short_stop_price_perc * 10, renorm_coeff_comm)

//
-----------------------------------------------------------------------------------
--------------------------------------------------------------------

// Useful Conditions
var bool first_long_candle = na
var bool first_short_candle = na
var bool second_long_candle = na
var bool second_short_candle = na
var bool bulk_long_cond = na
var bool bulk_short_cond = na
var bool bulk_long_increased_cond = na
var bool bulk_short_increased_cond = na
var bool long_closed_cond = na
var bool short_closed_cond = na
var bool next_candle_after_long_closed_cond = na
var bool next_candle_after_short_closed_cond = na

// Functions in the model are not "na"


trade_existence = not na(long_entry_price) and not na(short_entry_price) and (low <
long_entry_price and high > short_entry_price)

// First candle trades


first_short_candle := ta.crossunder(strategy.position_size, 0)
first_long_candle := ta.crossover(strategy.position_size, 0)

// Second candle trades


second_short_candle := strategy.position_size[2] >= 0 and strategy.position_size[1]
< 0 and strategy.position_size < 0 // Optimization requires to
avoid barssince functions
second_long_candle := strategy.position_size[2] <= 0 and strategy.position_size[1]
> 0 and strategy.position_size > 0

// Bulk trade position


bulk_long_cond := strategy.position_size[1] > 0 and strategy.position_size > 0
bulk_short_cond := strategy.position_size[1] < 0 and strategy.position_size < 0

// Candle where the position is incremented


bulk_long_increased_cond := bulk_long_cond and strategy.position_size >
strategy.position_size[1]
bulk_short_increased_cond := bulk_short_cond and strategy.position_size <
strategy.position_size[1]

// Candle where the trade closes


long_closed_cond := strategy.position_size[1] > 0 and strategy.position_size <= 0
short_closed_cond := strategy.position_size[1] < 0 and strategy.position_size >= 0

// Next candle after a trade closes


next_candle_after_long_closed_cond := strategy.position_size[2] > 0 and
strategy.position_size[1] <= 0 and strategy.position_size <= 0
next_candle_after_short_closed_cond := strategy.position_size[2] < 0 and
strategy.position_size[1] >= 0 and strategy.position_size >= 0

//
-----------------------------------------------------------------------------------
---------------------------------------------------

// Broker Emulator Conditions


ohlc_cond = (math.abs(high - open) <= math.abs(low - open))
// Open -> High -> Low -> Close
olhc_cond = (math.abs(high - open) >= math.abs(low - open))
// Open -> Low -> High -> Close
ohlc_prev_cond = (math.abs(high[1] - open[1]) <= math.abs(low[1] - open[1]))
olhc_prev_cond = (math.abs(high[1] - open[1]) >= math.abs(low[1] - open[1]))

// Long
// Check Avg order first bar
long_check_current_bar_exec(level) =>
var bool result = na
result := ((ohlc_cond and low <= level) or (olhc_cond and level >= close)) and
level <= high
result

// Check Avg order other bars


long_peak_broker_emul() =>
var float peak = na
peak := ohlc_cond ? close : olhc_cond ? high : na
peak

// Short
// Check Avg order first bar
short_check_current_bar_exec(level) =>
var bool result = na
result := ((ohlc_cond and level <= close) or (olhc_cond and high >= level)) and
level >= low
result

// Check Avg order other bars


short_peak_broker_emul() =>
var float peak = na
peak := ohlc_cond ? low : olhc_cond ? close : na
peak

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////
// TRADE DETAILS
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////

// Perc Function
delta_perc_func(x_f,x_i) =>
r = ((x_f - x_i)/x_i) * 100 * leverage
r

var int pyramiding_counter_long = 0


var int pyramiding_counter_short = 0

var float ath = 0.0


var float peak_profit_long = 0.0
var string peak_profit_string_long = " "
var label positionMaxLabel_long = na
var float draw_long = 0.0
var float draw_perc_long = 0.0

var float atl = 0.0


var float peak_profit_short = 0.0
var string peak_profit_string_short = " "
var label positionMaxLabel_short = na
var float draw_short = 0.0
var float draw_perc_short = 0.0

// Reset
if next_candle_after_long_closed_cond // We
wait 1 candle because we need to store the peak into the array and compute the
statistBB
peak_profit_long := 0
if next_candle_after_short_closed_cond
peak_profit_short := 0

// Long
if first_long_candle
ath := high
peak_profit_long := math.round(delta_perc_func(ath, long_entry_price[1]), 2)
// We have long_entry[1] because label is placed when next candle forms
peak_profit_string_long := str.tostring(peak_profit_long)
positionMaxLabel_long := label.new(bar_index , ath, text ="Maximum Profit " +
peak_profit_string_long + "%",
textcolor = color.white, textalign = text.align_center, color = color.rgb(70,
130, 255, 30), style = label.style_label_down)

draw_long := close
draw_perc_long := math.round(delta_perc_func(draw_long, long_entry_price[1]),
2) < 0 ? math.round(delta_perc_func(draw_long, long_entry_price[1]), 2) : 0

else if strategy.position_size > 0


if not bulk_long_increased_cond and (delta_perc_func(high,
strategy.position_avg_price) >= peak_profit_long)
ath := high
peak_profit_long := math.round(delta_perc_func(ath,
strategy.position_avg_price), 2)
peak_profit_string_long := str.tostring(peak_profit_long)

label.set_x(positionMaxLabel_long, bar_index)
label.set_y(positionMaxLabel_long, ath)
label.set_text(positionMaxLabel_long, "Maximum Profit " +
peak_profit_string_long + "%")

else if bulk_long_increased_cond and (delta_perc_func(long_peak_broker_emul(),


strategy.position_avg_price) >= peak_profit_long) // If averaging is
performed, we recalculate the peak profit
ath := long_peak_broker_emul()
// with the updated entry depending on the shape of the candle
peak_profit_long := math.round(delta_perc_func(ath,
strategy.position_avg_price), 2)
peak_profit_string_long := str.tostring(peak_profit_long)

label.set_x(positionMaxLabel_long, bar_index)
label.set_y(positionMaxLabel_long, ath)
label.set_text(positionMaxLabel_long, "Maximum Profit " +
peak_profit_string_long + "%")

if delta_perc_func(low, strategy.position_avg_price) <= draw_perc_long and


peak_profit_long <= perc_avoid_avg_long
draw_long := low
draw_perc_long := math.round(delta_perc_func(draw_long,
strategy.position_avg_price), 2)

// Short
if first_short_candle
atl := low
peak_profit_short := math.round(-delta_perc_func(atl, short_entry_price[1]), 2)
peak_profit_string_short := str.tostring(peak_profit_short)
positionMaxLabel_short := label.new(bar_index , atl, text ="Maximum Profit " +
peak_profit_string_short + "%",
textcolor = color.white, textalign = text.align_center, color = color.rgb(70,
130, 255, 30), style = label.style_label_up)

draw_short := close
draw_perc_short := math.round(-delta_perc_func(draw_short,
short_entry_price[1]), 2) < 0 ? math.round(-delta_perc_func(draw_short,
short_entry_price[1]), 2) : 0

else if strategy.position_size < 0


if not bulk_short_increased_cond and (-delta_perc_func(low,
strategy.position_avg_price) >= peak_profit_short)
atl := low
peak_profit_short := math.round(-delta_perc_func(atl,
strategy.position_avg_price), 2)
peak_profit_string_short := str.tostring(peak_profit_short)

label.set_x(positionMaxLabel_short, bar_index)
label.set_y(positionMaxLabel_short, atl)
label.set_text(positionMaxLabel_short, "Maximum Profit " +
peak_profit_string_short+ "%")

else if bulk_short_increased_cond and (-


delta_perc_func(short_peak_broker_emul(), strategy.position_avg_price) >=
peak_profit_short)
atl := short_peak_broker_emul()
peak_profit_short := math.round(-delta_perc_func(atl,
strategy.position_avg_price), 2)
peak_profit_string_short := str.tostring(peak_profit_short)

label.set_x(positionMaxLabel_short, bar_index)
label.set_y(positionMaxLabel_short, atl)
label.set_text(positionMaxLabel_short, "Maximum Profit " +
peak_profit_string_short+ "%")

if (-delta_perc_func(high, strategy.position_avg_price)) <= draw_perc_short and


peak_profit_short <= perc_avoid_avg_short
draw_short := high
draw_perc_short := -delta_perc_func(draw_short,
strategy.position_avg_price)
// ------------------------------------------
// STRATEGY ORDERS
// ------------------------------------------
// Contracts Calculations
var float entry_price_contract_long = na
var float entry_price_contract_short = na
var float quantity_contracts_long = na
var float quantity_contracts_short = na

entry_price_contract_long := strategy.position_size <= 0 ? long_entry_price[1] :


strategy.position_avg_price
entry_price_contract_short := strategy.position_size >= 0 ? short_entry_price[1] :
strategy.position_avg_price
quantity_contracts_long := ((deposit * margin_perc *
leverage)/entry_price_contract_long) / math.pow(1+m, number_averaging)
quantity_contracts_short := ((deposit * margin_perc *
leverage)/entry_price_contract_short) / math.pow(1+m, number_averaging)

//
-----------------------------------------------------------------------------------
------

// -----------------------------------------
// -------------- LONG ---------------------
// -----------------------------------------
var int long_array_size = na
long_array_size := number_averaging + 1

var array_long_entries = array.new_float(long_array_size)


var array_long_contracts_entries = array.new_float(long_array_size)
var array_long_avg_active =array.new_bool(long_array_size, true)

// ENTRY
if strategy.position_size <= 0 and trade_existence
strategy.entry("long", strategy.long, qty = quantity_contracts_long, stop =
long_entry_price, comment = "Long", disable_alert = true)

// AVERAGING
if first_long_candle and number_averaging != 0
for i = 0 to number_averaging

// All Long Entries are inserted in the array (long_entry_price is the


first element)
array.set(array_long_entries, i,
i == 0 ? long_entry_price[1] : math.round(long_entry_price[1] * (1-a) *
math.pow((1+m * (1-a))/(1+m), i - 1), precision))

// All Entry Contracts are inserted as well


array.set(array_long_contracts_entries, i, i == 0 ? 1 : i == 1 ? m *
math.abs(nz(strategy.position_size)) :
long_check_current_bar_exec(array.get(array_long_entries, i - 1)) and
not long_check_current_bar_exec(array.get(array_long_entries, i)) ? m *
math.abs(nz(strategy.position_size)) :
array.get(array_long_contracts_entries, i - 1) *(1+m))

if i > 0
strategy.order("Avg_Long " + str.tostring(i), strategy.long,
qty = array.get(array_long_contracts_entries, i),
limit = array.get(array_long_entries, i),
stop = i == 1 ? array.get(array_long_entries, 0) :
long_check_current_bar_exec(array.get(array_long_entries, i - 1)) and
not long_check_current_bar_exec(array.get(array_long_entries, i)) ? na
:
array.get(array_long_entries, i - 1),
disable_alert = true
)

// Averaging Counting
if number_averaging != 0 and peak_profit_long < perc_avoid_avg_long

// First Candle
if first_long_candle
for i = 1 to number_averaging
if long_check_current_bar_exec(array.get(array_long_entries, i))
pyramiding_counter_long := i

// Second Candle
if second_long_candle and pyramiding_counter_long > 0
for i = 1 to pyramiding_counter_long //
The strategy recalculations after the filling of an Avg order during the first bar
causes the algo to place again
strategy.cancel("Avg_Long " + str.tostring(i)) //
the orders already executed. For this reason we remove them at the beginning of the
second candle

// Bulk Candles
if bulk_long_increased_cond
pyramiding_counter_long := math.round(pyramiding_counter_long[1] +
math.log(strategy.position_size/strategy.position_size[1])/math.log(1+m))

// Last Candle
if long_closed_cond
for i = 1 to number_averaging
if (math.round(low, precision) <= array.get(array_long_entries, i)) and
(short_entry_price[1] < array.get(array_long_entries, i))
pyramiding_counter_long := i

// Removing Undesired Avg Orders


if ta.crossover(peak_profit_long, perc_avoid_avg_long-0.01) or number_averaging ==
0 or first_short_candle // Crossover is when source_1 >
source_2, but we can have source_1 == source_2
for i = 1 to number_averaging
// on the previous candle, thus we remove 0.01 so that we have the true crossover
strategy.cancel("Avg_Long " + str.tostring(i))

// Removing orders below the next short entry and replacing them in case the two
values flip again
if bulk_long_cond and peak_profit_long < perc_avoid_avg_long and
pyramiding_counter_long < number_averaging
for j = pyramiding_counter_long + 1 to number_averaging
if short_entry_price[1] >= array.get(array_long_entries, j)
array.set(array_long_avg_active, j, false)
strategy.cancel("Avg_Long " + str.tostring(j))

if array.get(array_long_avg_active, j) == false and short_entry_price[1] <


array.get(array_long_entries, j)
array.set(array_long_avg_active, j, true)
strategy.order("Avg_Long " + str.tostring(j), strategy.long,
qty = array.get(array_long_contracts_entries, j),
limit = array.get(array_long_entries, j),
stop = na,
disable_alert = true
)

// Reset Counters
if second_short_candle or next_candle_after_long_closed_cond or number_averaging ==
0
pyramiding_counter_long := 0
for i = 1 to number_averaging
array.set(array_long_avg_active, i, true)

// EXIT
allow_averaging_cond_long = number_averaging != 0
and pyramiding_counter_long < number_averaging
and array.get(array_long_entries,
pyramiding_counter_long < number_averaging ? pyramiding_counter_long + 1 : 0) >
short_entry_price[1]
and (strategy.position_size > 0 or
strategy.position_size[1] > 0)
and peak_profit_long < perc_avoid_avg_long

if strategy.position_size > 0
strategy.exit("SL_Long", qty_percent = 100, stop = long_stop_price,
disable_alert = true)
strategy.exit("Flip_to_Short", qty_percent = 100, stop = short_entry_price,
disable_alert = true)

if allow_averaging_cond_long
strategy.cancel("SL_Long")
else
strategy.cancel("Flip_to_Short")

//
-----------------------------------------------------------------------------------
--------------------------------------------------------------------

// -----------------------------------------
// -------------- SHORT --------------------
// -----------------------------------------
var int short_array_size = na
short_array_size := number_averaging + 1

var array_short_entries = array.new_float(short_array_size)


var array_short_contracts_entries = array.new_float(short_array_size)
var array_short_avg_active = array.new_bool(short_array_size, true)

// ENTRY
if strategy.position_size >= 0 and trade_existence
strategy.entry("short", strategy.short, qty = quantity_contracts_short, stop =
short_entry_price, comment = "Short", disable_alert = true)

// AVERAGING
if first_short_candle and number_averaging != 0
for i = 0 to number_averaging

// All Long Entries are inserted in the array (long_entry_price is the


first element)
array.set(array_short_entries, i,
i == 0 ? short_entry_price[1] : math.round(short_entry_price[1] * (1+a) *
math.pow((1+m * (1+a))/(1+m), i - 1), precision))

// All Entry Contracts are inserted as well


array.set(array_short_contracts_entries, i, i == 0 ? 1 : i == 1 ? m *
math.abs(strategy.position_size) :
short_check_current_bar_exec(array.get(array_short_entries, i - 1)) and
not short_check_current_bar_exec(array.get(array_short_entries, i)) ? m *
math.abs(strategy.position_size) :
array.get(array_short_contracts_entries, i - 1) *(1+m))

if i > 0
strategy.order("Avg_Short " + str.tostring(i), strategy.short,
qty = array.get(array_short_contracts_entries, i),
limit = array.get(array_short_entries, i),
stop = i == 1 ? array.get(array_short_entries, 0) :
short_check_current_bar_exec(array.get(array_short_entries, i - 1)) and not
short_check_current_bar_exec(array.get(array_short_entries, i)) ? na :
array.get(array_short_entries, i - 1),
disable_alert = true
)

// Averaging Counting
if number_averaging != 0 and peak_profit_short < perc_avoid_avg_short

// First Candle
if first_short_candle
for i = 1 to number_averaging
if short_check_current_bar_exec(array.get(array_short_entries, i))
pyramiding_counter_short := i

// Second Candle
if second_short_candle and pyramiding_counter_short > 0
for i = 1 to pyramiding_counter_short //
The strategy recalculations after the filling of an Avg order during the first bar
causes the algo to place again
strategy.cancel("Avg_Short " + str.tostring(i)) //
the orders already executed. For this reason we remove them at the beginning of the
second candle

// Bulk Candles
if bulk_short_increased_cond
pyramiding_counter_short := math.round(pyramiding_counter_short[1] +
math.log(strategy.position_size/strategy.position_size[1])/math.log(1+m))

// Last Candle
if short_closed_cond
for i = 1 to number_averaging
if (math.round(high, precision) >= array.get(array_short_entries, i))
and (long_entry_price[1] > array.get(array_short_entries, i))
pyramiding_counter_short := i

// Removing Undesired Avg Orders


if ta.crossover(peak_profit_short, perc_avoid_avg_short-0.01) or number_averaging
== 0 or first_long_candle // Crossover is when source_1 > source_2,
but we can have source_1 == source_2
for i = 1 to number_averaging
// on the previous candle, thus we remove 0.01 so that we have the true crossover
strategy.cancel("Avg_Short " + str.tostring(i))

// Removing orders below the next short entry and replacing them in case the two
values flip again
if bulk_short_cond and peak_profit_short < perc_avoid_avg_short and
pyramiding_counter_short > 0 and pyramiding_counter_short < number_averaging
for j = pyramiding_counter_short + 1 to number_averaging
if long_entry_price[1] <= array.get(array_short_entries, j)
array.set(array_short_avg_active, j, false)
strategy.cancel("Avg_Short " + str.tostring(j))

if array.get(array_short_avg_active, j) == false and long_entry_price[1] >


array.get(array_short_entries, j)
array.set(array_short_avg_active, j, true)
strategy.order("Avg_Short " + str.tostring(j), strategy.short,
qty = array.get(array_short_contracts_entries, j),
limit = array.get(array_short_entries, j),
stop = na,
disable_alert = true
)

// Reset Counters
if second_long_candle or next_candle_after_short_closed_cond or number_averaging ==
0
pyramiding_counter_short := 0
for i = 1 to number_averaging
array.set(array_short_avg_active, i, true)

// EXIT
allow_averaging_cond_short = number_averaging != 0
and pyramiding_counter_short < number_averaging
and array.get(array_short_entries,
pyramiding_counter_short < number_averaging ? pyramiding_counter_short + 1 : 0) <
long_entry_price[1]
and (strategy.position_size < 0 or
strategy.position_size[1] < 0)
and peak_profit_short < perc_avoid_avg_short

if strategy.position_size < 0
strategy.exit("SL_Short", qty_percent = 100, stop = short_stop_price,
disable_alert = true)
strategy.exit("Flip_to_Long", qty_percent = 100, stop = long_entry_price,
disable_alert = true)
if allow_averaging_cond_short
strategy.cancel("SL_Short")
else
strategy.cancel("Flip_to_Long")

//
-----------------------------------------------------------------------------------
--------------------------------------------------------------------
//
-----------------------------------------------------------------------------------
--------------------------------------------------------------------
//
-----------------------------------------------------------------------------------
--------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////
// STATISTBB
///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////

// --------------------------------------------------------------
// Functions
// --------------------------------------------------------------
// Generic TP Counting
generic_tp_winrate(tp_perc, N) =>
var max_array = array.new_float()
var int max_array_size = na
var int tp_counter = 0
var float tp_prob = 0.0

if long_closed_cond
array.push(max_array, peak_profit_long)
if short_closed_cond
array.push(max_array, peak_profit_short)

max_array_size := array.size(max_array)

if barstate.islastconfirmedhistory and max_array_size >= N


for i = 1 to N
tp_counter := array.get(max_array, max_array_size - i) >= tp_perc *
leverage / renorm_coeff_comm ? tp_counter + 1 : tp_counter
tp_prob := math.round(tp_counter / N, 2) * 100
tp_prob

// Generic Max Profit Counting


generic_avg_max_profit(N) =>
var max_array = array.new_float()
var int max_array_size = na
var float cumulative_peak = 0.0
var float avg_max = 0.0

if long_closed_cond
array.push(max_array, peak_profit_long)
if short_closed_cond
array.push(max_array, peak_profit_short)

max_array_size := array.size(max_array)

if barstate.islastconfirmedhistory and max_array_size >= N


for i = 1 to N
cumulative_peak += array.get(max_array, max_array_size - i)
avg_max := cumulative_peak / N
math.round(avg_max, 2)

// Generic Drawdown Counting


generic_avg_draw(N) =>
var draw_array = array.new_float()
var int draw_array_size = na
var float cumulative_draw = 0.0
var float avg_draw = 0.0

if long_closed_cond
array.push(draw_array, draw_perc_long)
if short_closed_cond
array.push(draw_array, draw_perc_short)

draw_array_size := array.size(draw_array)

if barstate.islastconfirmedhistory and draw_array_size >= N


for i = 1 to N
cumulative_draw += array.get(draw_array, draw_array_size - i)
avg_draw := cumulative_draw / N
math.round(avg_draw, 2)

// Specific TP Counting
specific_tp_winrate(trade_type, tp_perc, N) =>
var max_array_long = array.new_float()
var max_array_short = array.new_float()
var int ath_array_size = na
var int atl_array_size = na
var int long_tp_counter = 0
var int short_tp_counter = 0
var float tp_prob = 0.0

if trade_type == "long"
if long_closed_cond
array.push(max_array_long, peak_profit_long)
ath_array_size := array.size(max_array_long)

if barstate.islastconfirmedhistory and ath_array_size >= N


for i = 1 to N
long_tp_counter := array.get(max_array_long, ath_array_size - i) >=
tp_perc * leverage / renorm_coeff_comm ? long_tp_counter + 1 : long_tp_counter
tp_prob := math.round(long_tp_counter / N, 2) * 100

else if trade_type == "short"


if short_closed_cond
array.push(max_array_short, peak_profit_short)
atl_array_size := array.size(max_array_short)

if barstate.islastconfirmedhistory and atl_array_size >= N


for i = 1 to N
short_tp_counter := array.get(max_array_short, atl_array_size - i)
>= tp_perc * leverage / renorm_coeff_comm ? short_tp_counter + 1 : short_tp_counter
tp_prob := math.round(short_tp_counter / N, 2) * 100
tp_prob

// Specific Max Profit Counting


specific_avg_max_profit(trade_type, N) =>
var max_array_long = array.new_float()
var max_array_short = array.new_float()
var int ath_array_size = na
var int atl_array_size = na
var float cumulative_peak_long = 0.0
var float cumulative_peak_short = 0.0
var float avg_max = 0.0

if trade_type == "long"
if long_closed_cond
array.push(max_array_long, peak_profit_long)
ath_array_size := array.size(max_array_long)

if barstate.islastconfirmedhistory and ath_array_size >= N


for i = 1 to N
cumulative_peak_long += array.get(max_array_long, ath_array_size -
i)
avg_max := cumulative_peak_long / N

else if trade_type == "short"


if short_closed_cond
array.push(max_array_short, peak_profit_short)
atl_array_size := array.size(max_array_short)

if barstate.islastconfirmedhistory and atl_array_size >= N


for i = 1 to N
cumulative_peak_short += array.get(max_array_short, atl_array_size
- i)
avg_max := cumulative_peak_short / N
math.round(avg_max, 2)

// Specific Drawdown Counting


specific_avg_draw(trade_type, N) =>
var draw_array_long = array.new_float()
var draw_array_short = array.new_float()
var int draw_array_size_long = na
var int draw_array_size_short = na
var float cumulative_draw_long = 0.0
var float cumulative_draw_short = 0.0
var float avg_draw = 0.0
if trade_type == "long"
if long_closed_cond
array.push(draw_array_long, draw_perc_long)
draw_array_size_long := array.size(draw_array_long)

if barstate.islastconfirmedhistory and draw_array_size_long >= N


for i = 1 to N
cumulative_draw_long += array.get(draw_array_long,
draw_array_size_long - i)
avg_draw := cumulative_draw_long / N

else if trade_type == "short"


if short_closed_cond
array.push(draw_array_short, draw_perc_short)
draw_array_size_short := array.size(draw_array_short)

if barstate.islastconfirmedhistory and draw_array_size_short >= N


for i = 1 to N
cumulative_draw_short += array.get(draw_array_short,
draw_array_size_short - i)
avg_draw := cumulative_draw_short / N
math.round(avg_draw, 2)

//
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------
//
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------
//
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
--------------------

// ------------------------------------------------------------------
// Cumulative Results
// ------------------------------------------------------------------

N100=generic_avg_max_profit(num_cum4)
N75=generic_avg_max_profit(num_cum3)
N50=generic_avg_max_profit(num_cum2)
N25=generic_avg_max_profit(num_cum1)

D100=generic_avg_draw(num_cum4)
D75=generic_avg_draw(num_cum3)
D50=generic_avg_draw(num_cum2)
D25=generic_avg_draw(num_cum1)

tp1WR_100 = generic_tp_winrate(0.5, num_cum4)


tp2WR_100= generic_tp_winrate(0.8, num_cum4)
tp3WR_100 = generic_tp_winrate(1.3, num_cum4)
tp4WR_100= generic_tp_winrate(2.1, num_cum4)
tp5WR_100 = generic_tp_winrate(3.4, num_cum4)
tp6WR_100= generic_tp_winrate(5.5, num_cum4)
tp7WR_100 = generic_tp_winrate(8.9, num_cum4)
tp8WR_100= generic_tp_winrate(14.4, num_cum4)

tp1WR_75 = generic_tp_winrate(0.5, num_cum3)


tp2WR_75= generic_tp_winrate(0.8, num_cum3)
tp3WR_75 = generic_tp_winrate(1.3, num_cum3)
tp4WR_75= generic_tp_winrate(2.1, num_cum3)
tp5WR_75 = generic_tp_winrate(3.4, num_cum3)
tp6WR_75= generic_tp_winrate(5.5, num_cum3)
tp7WR_75 = generic_tp_winrate(8.9, num_cum3)
tp8WR_75= generic_tp_winrate(14.4, num_cum3)

tp1WR_50 = generic_tp_winrate(0.5, num_cum2)


tp2WR_50= generic_tp_winrate(0.8, num_cum2)
tp3WR_50 = generic_tp_winrate(1.3, num_cum2)
tp4WR_50= generic_tp_winrate(2.1, num_cum2)
tp5WR_50 = generic_tp_winrate(3.4, num_cum2)
tp6WR_50= generic_tp_winrate(5.5, num_cum2)
tp7WR_50 = generic_tp_winrate(8.9, num_cum2)
tp8WR_50= generic_tp_winrate(14.4, num_cum2)

tp1WR_25 = generic_tp_winrate(0.5, num_cum1)


tp2WR_25= generic_tp_winrate(0.8, num_cum1)
tp3WR_25 = generic_tp_winrate(1.3, num_cum1)
tp4WR_25= generic_tp_winrate(2.1, num_cum1)
tp5WR_25 = generic_tp_winrate(3.4, num_cum1)
tp6WR_25= generic_tp_winrate(5.5, num_cum1)
tp7WR_25 = generic_tp_winrate(8.9, num_cum1)
tp8WR_25= generic_tp_winrate(14.4, num_cum1)

///////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////

var float current_entry = 0.0


var float current_tp1_level = 0.0
var float current_tp2_level = 0.0
var float current_tp3_level = 0.0
var float current_tp4_level = 0.0
var float current_tp5_level = 0.0
var float current_tp6_level = 0.0
var float current_tp7_level = 0.0
var float current_tp8_level = 0.0
var float current_stop = 0.0

current_entry := nz(strategy.position_avg_price)
current_tp1_level := strategy.position_size > 0 ? tp1_price_long :
strategy.position_size < 0 ? tp1_price_short : 0.0
current_tp2_level := strategy.position_size > 0 ? tp2_price_long :
strategy.position_size < 0 ? tp2_price_short : 0.0
current_tp3_level := strategy.position_size > 0 ? tp3_price_long :
strategy.position_size < 0 ? tp3_price_short : 0.0
current_tp4_level := strategy.position_size > 0 ? tp4_price_long :
strategy.position_size < 0 ? tp4_price_short : 0.0
current_tp5_level := strategy.position_size > 0 ? tp5_price_long :
strategy.position_size < 0 ? tp5_price_short : 0.0
current_tp6_level := strategy.position_size > 0 ? tp6_price_long :
strategy.position_size < 0 ? tp6_price_short : 0.0
current_tp7_level := strategy.position_size > 0 ? tp7_price_long :
strategy.position_size < 0 ? tp7_price_short : 0.0
current_tp8_level := strategy.position_size > 0 ? tp8_price_long :
strategy.position_size < 0 ? tp8_price_short : 0.0

if strategy.position_size > 0
if pyramiding_counter_long == number_averaging or peak_profit_long >=
perc_avoid_avg_long
current_stop := long_stop_price
else
current_stop := array.get(array_long_entries, pyramiding_counter_long + 1)
else if strategy.position_size < 0
if pyramiding_counter_short == number_averaging or peak_profit_short >=
perc_avoid_avg_short
current_stop := short_stop_price
else
current_stop := array.get(array_short_entries, pyramiding_counter_short +
1)
else
current_stop := 0.0

// Total Trades
var int tot_trades_long = 0
var int tot_trades_short = 0

var int op_index_long = 0


var int op_index_short = 0
var int cl_index_long = 0
var int cl_index_short = 0
var int duration_long = 0
var int duration_short = 0

var int avg_duration_long = 0


var int avg_duration_short = 0

if first_short_candle
tot_trades_short += 1
op_index_short := bar_index
else if first_long_candle
tot_trades_long += 1
op_index_long := bar_index

if strategy.position_size[1] > 0 and strategy.position_size <= 0


duration_long += (bar_index - op_index_long)
avg_duration_long := math.round(duration_long / tot_trades_long)

else if strategy.position_size[1] < 0 and strategy.position_size >= 0


duration_short += (bar_index - op_index_short)
avg_duration_short := math.round(duration_short / tot_trades_short)
// ------------------------------------------------------------------
// TABLE LONG vs SHORT
// ------------------------------------------------------------------

stats_table = table.new(position.middle_right, 4, 20, border_width=1)

//
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------

table.cell(stats_table, 0, 5, text="PROBABILITY TABLES" + "\n" + "(Last " +


str.tostring(prob_stats_number) + " Trades Each)",
width=10, height=7, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=color.new(#BCBCBC, 80), text_color=table_txt_color,
text_size=table_txt_size)
table.cell(stats_table, 1, 5, text="", width=10, height=5,
text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)
table.cell(stats_table, 2, 5, text="", width=10, height=5,
text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)
table.cell(stats_table, 3, 5, text="", width=10, height=5,
text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.merge_cells(stats_table, 0,5,3,5)

//
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------

table.cell(stats_table, 0, 6, text="LONG", width=10, text_valign=text.align_center,


text_halign=text.align_center, bgcolor=color.new(#6ECE44, 80),
text_color=table_txt_color, text_size=table_txt_size)
table.cell(stats_table, 1, 6, text="", width=10, height=5,
text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)
table.cell(stats_table, 2, 6, text="SHORT", width=10,
text_valign=text.align_center, text_halign=text.align_center,
bgcolor=color.new(#E06666, 80), text_color=table_txt_color,
text_size=table_txt_size)
table.cell(stats_table, 3, 6, text="", width=10, height=5,
text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.merge_cells(stats_table, 0,6,1,6)
table.merge_cells(stats_table, 2,6,3,6)
//
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------
// Long/Short Probabilities
//
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------

table.cell(stats_table, 0, 7, text=
"TP 1: " + str.tostring(specific_tp_winrate("long", 0.5,
prob_stats_number)) + "%" + "\n" +
"TP 2: " + str.tostring(specific_tp_winrate("long", 0.8,
prob_stats_number)) + "%" + "\n" +
"TP 3: " + str.tostring(specific_tp_winrate("long", 1.3,
prob_stats_number)) + "%" + "\n" +
"TP 4: " + str.tostring(specific_tp_winrate("long", 2.1,
prob_stats_number)) + "%",
width=10, height=9, text_valign=text.align_center,
text_halign=text.align_center, bgcolor=table_txt_bg_color,
text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 1, 7, text=
"TP 5: " + str.tostring(specific_tp_winrate("long", 3.4,
prob_stats_number)) + "%" + "\n" +
"TP 6: " + str.tostring(specific_tp_winrate("long", 5.5,
prob_stats_number)) + "%" + "\n" +
"TP 7: " + str.tostring(specific_tp_winrate("long", 8.9,
prob_stats_number)) + "%" + "\n" +
"TP 8: " + str.tostring(specific_tp_winrate("long", 14.4,
prob_stats_number)) + "%",
width=10, height=9, text_valign=text.align_center,
text_halign=text.align_center, bgcolor=table_txt_bg_color,
text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 2, 7, text=
"TP 1: " + str.tostring(specific_tp_winrate("short", 0.5,
prob_stats_number)) + "%" + "\n" +
"TP 2: " + str.tostring(specific_tp_winrate("short", 0.8,
prob_stats_number)) + "%" + "\n" +
"TP 3: " + str.tostring(specific_tp_winrate("short", 1.3,
prob_stats_number)) + "%" + "\n" +
"TP 4: " + str.tostring(specific_tp_winrate("short", 2.1,
prob_stats_number)) + "%",
width=10, height=9, text_valign=text.align_center,
text_halign=text.align_center, bgcolor=table_txt_bg_color,
text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 3, 7, text=
"TP 5: " + str.tostring(specific_tp_winrate("short", 3.4,
prob_stats_number)) + "%" + "\n" +
"TP 6: " + str.tostring(specific_tp_winrate("short", 5.5,
prob_stats_number)) + "%" + "\n" +
"TP 7: " + str.tostring(specific_tp_winrate("short", 8.9,
prob_stats_number)) + "%" + "\n" +
"TP 8: " + str.tostring(specific_tp_winrate("short", 14.4,
prob_stats_number)) + "%",
width=10, height=9, text_valign=text.align_center,
text_halign=text.align_center, bgcolor=table_txt_bg_color,
text_color=table_txt_color, text_size=table_txt_size)

// Average Data
table.cell(stats_table, 0, 8, text=
"Avg Max Profit: " + str.tostring(specific_avg_max_profit("long",
prob_stats_number)) + "%" + "\n" +
"Avg Drawdown: " + str.tostring(specific_avg_draw("long", prob_stats_number))
+ "%" + "\n" +
"Avg R/R Factor: " +
str.tostring(math.abs(math.round(specific_avg_max_profit("long", prob_stats_number)
/ specific_avg_draw("long", prob_stats_number),2))),
width=10, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)
table.cell(stats_table, 1, 8, text="", width=10, height=5,
text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 2, 8, text=
"Avg Max Profit: " + str.tostring(specific_avg_max_profit("short",
prob_stats_number)) + "%" + "\n" +
"Avg Drawdown: " + str.tostring(specific_avg_draw("short", prob_stats_number))
+ "%" + "\n" +
"Avg R/R Factor: " +
str.tostring(math.abs(math.round(specific_avg_max_profit("short",
prob_stats_number) / specific_avg_draw("short", prob_stats_number),2))),
width=10, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)
table.cell(stats_table, 3, 8, text="", width=10, height=5,
text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.merge_cells(stats_table, 0,8,1,8)
table.merge_cells(stats_table, 2,8,3,8)

//
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------

table.cell(stats_table, 0, 10, text=


"CUMULATIVE RESULTS" +
"\n(Total Trades Opened: " +
str.tostring(tot_trades_long + tot_trades_short) +
" | L " +
str.tostring(tot_trades_long) +
" - S " +
str.tostring(tot_trades_short) +
")" +
"\n(Total Avg Bars Duration: " +
str.tostring(math.round((avg_duration_long + avg_duration_short)/2)) +
" | L " +
str.tostring(avg_duration_long) +
" - S " +
str.tostring(avg_duration_short) +
")",
width=10, height=9, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=color.new(#BCBCBC, 80), text_color=table_txt_color,
text_size=table_txt_size)
table.cell(stats_table, 1, 10, text="", width=10, height=5,
text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)
table.cell(stats_table, 2, 10, text="", width=10, height=5,
text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)
table.cell(stats_table, 3, 10, text="", width=10, height=5,
text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.merge_cells(stats_table, 0,10,3,10)

//
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------
// Cumulative Probabilities
//
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
---------------

table.cell(stats_table, 0, 12, text=str.tostring(num_cum4) + " TRADES", width=10,


height=4, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)
table.cell(stats_table, 1, 12, text=str.tostring(num_cum3) + " TRADES", width=10,
height=5, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)
table.cell(stats_table, 2, 12, text=str.tostring(num_cum2) + " TRADES", width=10,
height=5, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)
table.cell(stats_table, 3, 12, text=str.tostring(num_cum1) + " TRADES", width=10,
height=5, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 0, 13, text=


"TP 1: " + str.tostring(math.round(tp1WR_100,0)) + "%" + "\n" +
"TP 2: " + str.tostring(math.round(tp2WR_100,0)) + "%" + "\n" +
"TP 3: " + str.tostring(math.round(tp3WR_100,0)) + "%" + "\n" +
"TP 4: " + str.tostring(math.round(tp4WR_100,0)) + "%" + "\n" +
"TP 5: " + str.tostring(math.round(tp5WR_100,0)) + "%" + "\n" +
"TP 6: " + str.tostring(math.round(tp6WR_100,0)) + "%" + "\n" +
"TP 7: " + str.tostring(math.round(tp7WR_100,0)) + "%" + "\n" +
"TP 8: " + str.tostring(math.round(tp8WR_100,0)) + "%",
width=10, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 1, 13, text=


"TP 1: " + str.tostring(math.round(tp1WR_75,0)) + "%" + "\n" +
"TP 2: " + str.tostring(math.round(tp2WR_75,0)) + "%" + "\n" +
"TP 3: " + str.tostring(math.round(tp3WR_75,0)) + "%" + "\n" +
"TP 4: " + str.tostring(math.round(tp4WR_75,0)) + "%" + "\n" +
"TP 5: " + str.tostring(math.round(tp5WR_75,0)) + "%" + "\n" +
"TP 6: " + str.tostring(math.round(tp6WR_75,0)) + "%" + "\n" +
"TP 7: " + str.tostring(math.round(tp7WR_75,0)) + "%" + "\n" +
"TP 8: " + str.tostring(math.round(tp8WR_75,0)) + "%",
width=10, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 2, 13, text=


"TP 1: " + str.tostring(math.round(tp1WR_50,0)) + "%" + "\n" +
"TP 2: " + str.tostring(math.round(tp2WR_50,0)) + "%" + "\n" +
"TP 3: " + str.tostring(math.round(tp3WR_50,0)) + "%" + "\n" +
"TP 4: " + str.tostring(math.round(tp4WR_50,0)) + "%" + "\n" +
"TP 5: " + str.tostring(math.round(tp5WR_50,0)) + "%" + "\n" +
"TP 6: " + str.tostring(math.round(tp6WR_50,0)) + "%" + "\n" +
"TP 7: " + str.tostring(math.round(tp7WR_50,0)) + "%" + "\n" +
"TP 8: " + str.tostring(math.round(tp8WR_50,0)) + "%",
width=10, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 3, 13, text=


"TP 1: " + str.tostring(math.round(tp1WR_25,0)) + "%" + "\n" +
"TP 2: " + str.tostring(math.round(tp2WR_25,0)) + "%" + "\n" +
"TP 3: " + str.tostring(math.round(tp3WR_25,0)) + "%" + "\n" +
"TP 4: " + str.tostring(math.round(tp4WR_25,0)) + "%" + "\n" +
"TP 5: " + str.tostring(math.round(tp5WR_25,0)) + "%" + "\n" +
"TP 6: " + str.tostring(math.round(tp6WR_25,0)) + "%" + "\n" +
"TP 7: " + str.tostring(math.round(tp7WR_25,0)) + "%" + "\n" +
"TP 8: " + str.tostring(math.round(tp8WR_25,0)) + "%",
width=10, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 0, 14, text=


"Avg Peak: " + str.tostring(math.round(N100, 2)) + "%" + "\n" +
"Avg Down: " + str.tostring(math.round(D100, 2)) + "%",
width=10, height=7, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 1, 14, text=


"Avg Peak: " + str.tostring(math.round(N75, 2)) + "%" + "\n" +
"Avg Down: " + str.tostring(math.round(D75, 2)) + "%",
width=10, height=7, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 2, 14, text=


"Avg Peak: " + str.tostring(math.round(N50, 2)) + "%" + "\n" +
"Avg Down: " + str.tostring(math.round(D50, 2)) + "%",
width=10, height=7, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)

table.cell(stats_table, 3, 14, text=


"Avg Peak: " + str.tostring(math.round(N25, 2)) + "%" + "\n" +
"Avg Down: " + str.tostring(math.round(D25, 2)) + "%",
width=10, height=7, text_valign=text.align_center, text_halign=text.align_center,
bgcolor=table_txt_bg_color, text_color=table_txt_color, text_size=table_txt_size)
// ------------------------------------------
// PLOT
// ------------------------------------------

// Avg Conditions Plot


long_avg_plot_cond = strategy.position_size > 0 and number_averaging != 0 and
pyramiding_counter_long < number_averaging and peak_profit_long <
perc_avoid_avg_long
short_avg_plot_cond = strategy.position_size < 0 and number_averaging != 0 and
pyramiding_counter_short < number_averaging and peak_profit_short <
perc_avoid_avg_short

// Averaging Levels
plot(long_avg_plot_cond ? array.get(array_long_entries, pyramiding_counter_long +
1) : na, "Avg Order Long", color = color.rgb(72, 241, 241),
style=plot.style_circles, linewidth = 1)
plot(short_avg_plot_cond ? array.get(array_short_entries,pyramiding_counter_short +
1) : na, "Avg Order Short", color = color.rgb(72, 241, 241),
style=plot.style_circles, linewidth = 1)
plot(strategy.position_size > 0 and not long_avg_plot_cond ? long_stop_price : na,
"SL Long", color= color.red, style = plot.style_circles)
plot(strategy.position_size < 0 and not short_avg_plot_cond ? short_stop_price :
na, "SL Short", color=color.red, style=plot.style_circles)

//Plot Entry Short/Long Points


plot((barstate.islast or barstate.islastconfirmedhistory) and
strategy.position_size <= 0 ? long_entry_price : na,
title = "Next Entry Long", style=plot.style_cross, color=color.green,
linewidth=6, offset=1, show_last=1)
plot((barstate.islast or barstate.islastconfirmedhistory) and
strategy.position_size >= 0 ? short_entry_price : na,
title = "Next Entry Short", style=plot.style_cross, color=color.red,
linewidth=6, offset=1, show_last=1)

// plot(long_entry_price[1], style=plot.style_line, color=color.green)


// plot(short_entry_price[1], style=plot.style_line, color=color.red)

// Entry, SL and Averagins


plot(strategy.position_size != 0 ? strategy.position_avg_price : na, title = "Entry
Price", style=plot.style_circles, color=color.yellow, linewidth=1)

// // BE
// plot(be_activated and be_is_active_long and strategy.position_size > 0 ? p_n :
na, "BE Long", color = color.orange, style = plot.style_circles, linewidth = 3)
// // plot(ts_activated and ts_plot_cond_short and not ts_achieved_long ?
ts_level_price_short : na, "BE Short", color = color.orange, style =
plot.style_circles, linewidth = 3)

// // TS
// plot(ts_activated and ts_plot_cond_long and not ts_achieved_long ?
ts_level_price_long : na, "TS Long", color = color.blue, style =
plot.style_circles, linewidth = 3)
// plot(ts_activated and ts_plot_cond_short and not ts_achieved_short ?
ts_level_price_short : na, "TS Short", color = color.blue, style =
plot.style_circles, linewidth = 3)
// TP Levels
plot(strategy.position_size != 0 ? current_tp1_level : na, title = "TP1",
style=plot.style_circles, color=color.new(color.green, 10), linewidth=1) // plot_4
--> 5° Plot
plot(strategy.position_size != 0 ? current_tp2_level : na, title = "TP2",
style=plot.style_circles, color=color.new(color.green, 10), linewidth=1)
plot(strategy.position_size != 0 ? current_tp3_level : na, title = "TP3",
style=plot.style_circles, color=color.new(color.green, 10), linewidth=1)
plot(strategy.position_size != 0 ? current_tp4_level : na, title = "TP4",
style=plot.style_circles, color=color.new(color.green, 10), linewidth=1)
plot(strategy.position_size != 0 ? current_tp5_level : na, title = "TP5",
style=plot.style_circles, color=color.new(color.green, 10), linewidth=1)
plot(strategy.position_size != 0 ? current_tp6_level : na, title = "TP6",
style=plot.style_circles, color=color.new(color.green, 10), linewidth=1) // plot_9
--> 10° Plot
plot(strategy.position_size != 0 ? current_tp7_level : na, title = "TP7",
style=plot.style_circles, color=color.new(color.green, 10), linewidth=1)
plot(strategy.position_size != 0 ? current_tp8_level : na, title = "TP8",
style=plot.style_circles, color=color.new(color.green, 10), linewidth=1)

// Background Color
backgroundColor = strategy.position_size > 0 ? color.new(color.green, 85) :
strategy.position_size < 0 ? color.new(color.red, 85) : na
bgcolor(backgroundColor)

///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
/////
// TEST ALERT
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
/////
var string closed_long_alert = na
var string closed_short_alert = na
var string avg_long_alert = na
var string avg_short_alert = na

closed_alert_function(trade_type) =>
var string action_text = na
var string peak_string = na
var string final_message = na

action_text := trade_type == "long" ? "Long" : trade_type == "short" ?


"Short" : na
peak_string := trade_type == "long" ? peak_profit_string_long : trade_type ==
"short" ? peak_profit_string_short : na

final_message :=
"/b version: 1.0.0" +
"\nstrategy: " + asset +
"\n" +
"\n" +
"\n——— 🤖 " + uni_replace("BBScalper", false, true, false) + " 🤖 ———" +
"\n\n📋 " + uni_replace("Asset", false, true, false) + ": " + asset +
"\n🗃 " + uni_replace("Action", false, true, false) + ": " + action_text + "
Closed" +
"\n✍ " + uni_replace("Maximum Profit: " + peak_string + "%", false, true,
false)

avg_alert_function(trade_type) =>
var string action_text = na
var string peak_string = na
var string final_message = na

action_text := trade_type == "long" ? "Long" : trade_type == "short" ?


"Short" : na
peak_string := trade_type == "long" ? peak_profit_string_long : trade_type ==
"short" ? peak_profit_string_short : na

final_message :=
"/b version: 1.0.0" +
"\nstrategy: " + asset +
"\n" +
"\n" +
"\n——— 🤖 " + uni_replace("BBScalper", false, true, false) + " 🤖 ———" +
"\n\n📋 " + uni_replace("Asset", false, true, false) + ": " + asset +
"\n🗃 " + uni_replace("Action", false, true, false) + ": " + action_text + "
Averaged" +
"\n✍ " + uni_replace("Current Maximum Profit: " + peak_string + "%", false,
true, false) +
"\n✍ " + uni_replace("Current Avg Entry: " +
str.tostring(strategy.position_avg_price), false, true, false)

closed_long_alert := closed_alert_function("long")
closed_short_alert := closed_alert_function("short")
avg_long_alert := avg_alert_function("long")
avg_short_alert := avg_alert_function("short")

You might also like