fractionLibrary "fraction"
Fraction Creation and Basic Operations.
Cracked a tough problem in making this Polarity Agnostic Decimal without a cheating "abs * sign of input".
it's quite fast, however still test for errors before production use.
>> Big Neon Sign on 1/0 value. <<
Int Array (LOC 0/1)..
To/From Decimal(float)
Comparison ( < / == / >)
Add / Sub / Mult / Div
Invert polarity +/-
String output with 2 formats ..
make(_numerator, _denominator, _val)
Parameters:
_numerator : (int) above the line integer ie: ____ of (___ / bottom )
_denominator : (int) below the line integer ie: ____ of (top / ______ )
_val : (int) OPTIONAL (for no real reason including it) integer to multiply
Returns: array where index 0 is Numerator, 1 is Denominator
add(_fraction, _fraction2)
Perform add operation (left adds right onto )
Parameters:
_fraction : (array) left side Fraction Object
_fraction2 : (array) right side Fraction Object
Returns: array where index 0 is Numerator, 1 is Denominator
subtract(_fraction, _fraction2)
Perform subtract operation (left subtracts right from )
Parameters:
_fraction : (array) left side Fraction Object
_fraction2 : (array) right side Fraction Object
Returns: array where index 0 is Numerator, 1 is Denominator
multiply(_fraction, _fraction2)
Perform multiply operation (left multiplies by right )
Parameters:
_fraction : (array) left side Fraction Object
_fraction2 : (array) right side Fraction Object
Returns: array where index 0 is Numerator, 1 is Denominator
divide(_fraction, _fraction2)
Perform divide operation (left divides by right )
Parameters:
_fraction : (array) left side Fraction Object
_fraction2 : (array) right side Fraction Object
Returns: array where index 0 is Numerator, 1 is Denominator
negative(_fraction)
Perform Negative number inversion ie: (-1/2 => 1/2) or (3/5 => -3/5)
Parameters:
_fraction : (array) Fraction Object to invert to/from negative
Returns: array where index 0 is Numerator, 1 is Denominator
isSmaller(_fraction, _fraction2)
Check if first fraction is smaller
Parameters:
_fraction : (array) left side Fraction Object
_fraction2 : (array) right side Fraction Object
Returns: True if smaller, false if bigger
isLarger(_fraction, _fraction2)
Check if first fraction is larger
Parameters:
_fraction : (array) left side Fraction Object
_fraction2 : (array) right side Fraction Object
Returns: True if smaller, false if bigger
isEqual(_fraction, _fraction2)
Check if first fraction is equal
Parameters:
_fraction : (array) left side Fraction Object
_fraction2 : (array) right side Fraction Object
Returns: True if smaller, false if bigger
fromDec(_input, _epsilon, _iterations)
Convert Decimal to Fraction array
note : this is my own Negative Number Capable (tiny speed loss)
adaptation of the fastest algo out there
Exclusive for Tradingview.
Parameters:
_input : (float) Decimal Input
_epsilon : (int) (OPTIONAL) to precision 0's after dec 0.0000 -> epsilon 0's
_iterations : (int) (OPTIONAL) Maximum iterations Till give up
Returns: array where index 0 is Numerator, 1 is Denominator
toDec()
Convert Fraction to Decimal Output
Returns: Float of fration
toString(_fraction)
Create "A/B" or "A and B/C" String Value of Fraction.
Parameters:
_fraction : (array) Fraction Object to invert to/from negative
Returns: String as (-)? A and B/C format
Fraction
FunctionKellyCriterionLibrary "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 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, 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, 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(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, trade, asset or strategy percent returns.
fraction : float , fraction of capital to bet.
initial_capital : float , capital at the start of simulation.
Returns: array, 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.