RicardoSantos

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.
Pine脚本库

本着真正的TradingView精神,作者将此Pine代码以开源脚本库发布,以便我们社区的其他Pine程序员可以重用它。向作者致敬!您可以私下或在其他开源出版物中使用此库,但在出版物中重用此代码受网站规则约束。

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。

想使用这个脚本库吗?

复制以下行并将其粘贴到您的脚本中。