PINE LIBRARY

FunctionKellyCriterion

Library "FunctionKellyCriterion"
Kelly criterion methods.
the kelly criterion helps with the decision of how much one should invest in
a asset as long as you know the odds and expected return of said asset.

simplified(win_p, rr)
  simplified version of the kelly criterion formula.
  Parameters:
    win_p: float, probability of winning.
    rr: float, reward to risk rate.
  Returns: float, optimal fraction to risk.
usage:
simplified(0.55, 1.0)

partial(win_p, loss_p, win_rr, loss_rr)
  general form of the kelly criterion formula.
  Parameters:
    win_p: float, probability of the investment returns a positive outcome.
    loss_p: float, probability of the investment returns a negative outcome.
    win_rr: float, reward on a positive outcome.
    loss_rr: float, reward on a negative outcome.
  Returns: float, optimal fraction to risk.
usage:
partial(0.6, 0.4, 0.6, 0.1)

from_returns(returns)
  Calculate the fraction to invest from a array of returns.
  Parameters:
    returns: array<float> trade/asset/strategy returns.
  Returns: float, optimal fraction to risk.
usage:
from_returns(array.from(0.1,0.2,0.1,-0.1,-0.05,0.05))

final_f(fraction, max_expected_loss)
  Final fraction, eg. if fraction is 0.2 and expected max loss is 10%
then you should size your position as 0.2/0.1=2 (leverage, 200% position size).
  Parameters:
    fraction: float, aproximate percent fraction invested.
    max_expected_loss: float, maximum expected percent on a loss (ex 10% = 0.1).
  Returns: float, final fraction to invest.
usage:
final_f(0.2, 0.5)

hpr(fraction, trade, biggest_loss)
  Holding Period Return function
  Parameters:
    fraction: float, aproximate percent fraction invested.
    trade: float, profit or loss in a trade.
    biggest_loss: float, value of the biggest loss on record.
  Returns: float, multiplier of effect on equity so that a win of 5% is 1.05 and loss of 5% is 0.95.
usage:
hpr(fraction=0.05, trade=0.1, biggest_loss=-0.2)

twr(returns, rr, eps)
  Terminal Wealth Relative, returns a multiplier that can be applied
to the initial capital that leadds to the final balance.
  Parameters:
    returns: array<float>, list of trade returns.
    rr: float , reward to risk rate.
    eps: float , minimum resolution to void zero division.
  Returns: float, optimal fraction to invest.
usage:
twr(returns=array.from(0.1,-0.2,0.3), rr=0.6)

ghpr(returns, rr, eps)
  Geometric mean Holding Period Return, represents the average multiple made on the stake.
  Parameters:
    returns: array<float>, list of trade returns.
    rr: float , reward to risk rate.
    eps: float , minimum resolution to void zero division.
  Returns: float, multiplier of effect on equity so that a win of 5% is 1.05 and loss of 5% is 0.95.
usage:
ghpr(returns=array.from(0.1,-0.2,0.3), rr=0.6)

run_coin_simulation(fraction, initial_capital, n_series, n_periods)
  run multiple coin flipping (binary outcome) simulations.
  Parameters:
    fraction: float, fraction of capital to bet.
    initial_capital: float, capital at the start of simulation.
    n_series: int , number of simulation series.
    n_periods: int , number of periods in each simulation series.
  Returns: matrix<float>(n_series, n_periods), matrix with simulation results per row.
usage:
run_coin_simulation(fraction=0.1)

run_asset_simulation(returns, fraction, initial_capital)
  run a simulation over provided returns.
  Parameters:
    returns: array<float>, trade, asset or strategy percent returns.
    fraction: float , fraction of capital to bet.
    initial_capital: float , capital at the start of simulation.
  Returns: array<float>, array with simulation results.
usage:
run_asset_simulation(returns=array.from(0.1,-0.2,0.-3,0.4), fraction=0.1)

strategy_win_probability()
  calculate strategy() current probability of positive outcome in a trade.

strategy_avg_won()
  calculate strategy() current average won on a trade with positive outcome.

strategy_avg_loss()
  calculate strategy() current average lost on a trade with negative outcome.
arraysfractionfractionalalgorithmicfunctionkellykellycriterionstatisticsstrategies

Thư viện Pine

Theo tinh thần TradingView thực sự, tác giả đã xuất bản mã Pine này dưới dạng thư viện nguồn mở để các lập trình viên Pine khác trong cộng đồng của chúng tôi có thể sử dụng lại. Chúc mừng tác giả! Bạn có thể sử dụng thư viện này riêng tư hoặc trong các bài đăng nguồn mở khác. Tuy nhiên, bạn cần sử dụng lại mã này theo Quy tắc nội bộ.

Thông báo miễn trừ trách nhiệm