Library "RiskManagement" This library keeps your money in check, and is used for testing and later on webhook-applications too. It has four volatility functions and two of them can be used to calculate a Stop-Loss, like Average True Range. It also can calculate Position Size, and the Risk Reward Ratio. But those calculations don't take leverage into account.
position_size(portfolio, risk, entry, stop_loss, use_leverage, qty_as_integer) This function calculates the definite amount of contracts/shares/units you should use to buy or sell. This value can used by `strategy.entry(qty)` for example. Parameters: portfolio (float): This is the total amount of the currency you own, and is also used by strategy.initial_capital, for example. The amount is needed to calculate the maximum risk you are willing to take per trade. risk (float): This is the percentage of your Portfolio you willing to loose on a single trade. Possible values are between 0.1 and 100%. Same usecase with strategy(default_qty_type=strategy.percent_of_equity,default_qty_value=100), except its calculation the risk only. entry (float): This is the limit-/market-price for the investment. In other words: The price per contract/share/unit you willing to buy or sell. stop_loss (float): This is the limit-/market-price when to exit the trade, to minimize your losses. use_leverage (bool): This value is optional. When not used or when set to false then this function will let you invest your portfolio at max. qty_as_integer (bool): This value is optional. When set to true this function will return a value used with integers. The largest integer less than or equal to the given number. Because some Broker/Exchanges let you trade hole contracts/shares/units only. Returns: float
position_size_currency(portfolio, risk, entry, stop_loss) This function calculates the definite amount of currency you should use when going long or short. Parameters: portfolio (float): This is the total amount of the currency you own, and is also used by strategy.initial_capital, for example. The amount is needed to calculate the maximum risk you are willing to take per trade. risk (float): This is the percentage of your Portfolio you willing to loose on a single trade. For example: 1 is 100% and 0,01 is 1%. Default amount is 0.02 (2%). entry (float): This is the limit-/market-price for the current investment. In other words: The price per contract/share/units you willing to buy or sell. stop_loss (float): This is the limit-/market-price when to exit the trade, to minimize your losses. Returns: float
rrr(entry, stop_loss, take_profit) This function calculates the Risk Reward Ratio. Common values are between 1.5 and 2.0 and you should not go lower except for very few special cases. Parameters: entry (float): This is the limit-/market-price for the investment. In other words: The price per contract/share/unit you willing to buy or sell. stop_loss (float): This is the limit-/market-price when to exit the trade, to minimize your losses. take_profit (float): This is the limit-/market-price when to take profits. Returns: float
change_in_price(length) This function calculates the difference between price now and close price of the candle 'n' bars before that. If prices are very volatile but closed where they began, then this method would show zero volatility. Over many calculations, this method returns a reasonable measure of volatility, but will always be lower than those using the highs and lows. Parameters: length (int): The length is needed to determine how many candles/bars back should take into account. Returns: float
maximum_price_fluctuation(length) This function measures volatility over most recent candles, which could be used as an estimate of risk. It may also be effective as the basis for a stop-loss or take-profit, like the ATR but it ignores the frequency of directional changes within the time interval. In other words: The difference between the highest high and lowest low over 'n' bars. Parameters: length (int): The length is needed to determine how many candles/bars back should take into account. Returns: float
absolute_price_changes(length) This function measures volatility over most recent close prices. This is excellent for comparing volatility. It includes both frequency and magnitude. In other words: Sum of differences between second to last close price and last close price as absolute value for 'n' bars. Parameters: length (int): The length is needed to determine how many candles/bars back should take into account. Returns: float
annualized_volatility(length) This function measures volatility over most recent close prices. Its the standard deviation of close over the past 'n' periods, times the square root of the number of periods in a year. Parameters: length (int): The length is needed to determine how many candles/bars back should take into account. Returns: float
In pieno spirito TradingView, l'autore ha pubblicato questo codice Pine come libreria open-source in modo che altri programmatori Pine della nostra comunità possano riutilizzarlo. Un saluto all'autore! È possibile utilizzare questa libreria privatamente o in altre pubblicazioni open-source, ma il riutilizzo di questo codice in una pubblicazione è regolato dal nostro Regolamento.
Le informazioni ed i contenuti pubblicati non costituiscono in alcun modo una sollecitazione ad investire o ad operare nei mercati finanziari. Non sono inoltre fornite o supportate da TradingView. Maggiori dettagli nelle Condizioni d'uso.