I can’t calculate the probabilities of the price reaching certain levels based on the last X closed trades
The part of the code that handles counting and calculating probabilities.
// Parameters
trade_count = input(20, title="Numero di trade per il calcolo del win rate")
// Variabili per tenere traccia dei trade e per i livelli di take profit e stop loss
var float[] trade_results = array.new_float(trade_count)
var float stop_loss_price = na
var float take_profit_price = na
// Function to update trade results
update_results(result) =>
if array.size(trade_results) == trade_count
array.shift(trade_results)
array.push(trade_results, result)
// Function to calculate the number of winning trades
count_wins(arr) =>
win_count = 0
for i = 0 to array.size(arr) - 1
if array.get(arr, i) > 0
win_count := win_count + 1
win_count
// Identificazione delle posizioni chiuse e se erano long o short
is_pos_closed = (strategy.position_size[1] != 0 and strategy.position_size == 0) or (strategy.position_size[1] > 0 and strategy.position_size < 0) or (strategy.position_size[1] < 0 and strategy.position_size > 0)
was_long = is_pos_closed and strategy.position_size[1] > 0
was_short = is_pos_closed and strategy.position_size[1] < 0
// Update trade results only when the take profit is reached.
if (was_long and high[1] >= longTakeProfitPrice[1])
update_results(1)
else if (was_short and low[1] <= shortTakeProfitPrice[1])
update_results(1)
// Winrate calc
wins = count_wins(trade_results)
win_rate = wins / math.min(array.size(trade_results), trade_count) * 100
// Plot Winrate
plot(win_rate, color=color.green, title="Win Rate")
// Winrate Table
var table probtab = na
if na(probtab)
probtab := table.new(position.bottom_right, 2, 2, border_width=2)
if not na(probtab)
table.cell(probtab, 0, 0, text='Tp1 Long', bgcolor=color.new(color.lime, 0.5), text_size=size.large)
table.cell(probtab, 1, 0, text='Tp1 Short', bgcolor=color.new(color.red, 0.5), text_size=size.large)
table.cell(probtab, 0, 1, text=str.tostring(math.round(win_rate, 2)) + ' %', bgcolor=color.black, text_color=color.white, text_size=size.large)
table.cell(probtab, 1, 1, text=str.tostring(math.round(win_rate, 2)) + ' %', bgcolor=color.black, text_color=color.white, text_size=size.large)
What I tryed and what i want
Through ChatGPT, I tried to code it starting from a random moving average crossover strategy and my other strategies. The table gets plotted, but the probabilities displayed are not correct. Unfortunately, I haven’t studied arrays yet, so I don’t understand where the errors are. However, the structure of the code seems to make sense to me.
I want to plot a table that shows the probabilities, calculated from the last “x” trades, of the price reaching a specific level (takeprofit arget in this case but I want multiple levels, not only tp level) for both short and long positions.
Simone Rossi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.