Pinescript: Probability Table for Reaching Specific Price Levels

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.

New contributor

Simone Rossi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật