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.

파인 라이브러리

트레이딩뷰 정신에 따라 오써는 이 파인 코드를 오픈 소스 라이브러리로 퍼블리쉬하여 당사 커뮤니티의 다른 파인 프로그래머들이 쓸 수 있도록 하였습니다. 오써에게 찬사를! 여러분은 이 라이브러리를 프라이빗 또는 오픈 소스 퍼블리케이션에 쓸 수 있지만 퍼블리케이션에 재사용은 하우스룰을 따릅니다.

면책사항

이 정보와 게시물은 TradingView에서 제공하거나 보증하는 금융, 투자, 거래 또는 기타 유형의 조언이나 권고 사항을 의미하거나 구성하지 않습니다. 자세한 내용은 이용 약관을 참고하세요.

이 라이브러리를 쓰시겠습니까?

텍스트를 클립보드에 카피한 뒤 님의 스크립트에 붙여 넣기.