strategy_helpers

Library "strategy_helpers"
Provides tools for calculating risk metrics across different types of trading strategies including equities, futures, and forex. Functions allow for precise control over risk management by calculating the number of units or contracts to trade, the value at risk, and the total position value based on entry prices, stop levels, and desired risk percentage. Additional utilities include automatic risk calculation based on asset type, leverage level calculations, and determination of liquidation levels for leveraged trades.
calculate_risk(entry, stop_level, stop_range, capital, risk_percent, trade_direction, whole_number_buy)
Calculates risk metrics for equity trades based on entry, stop level, and risk percent
Parameters:
entry (float): The price at which the position is entered. Use close if you arent adding to a position. Use the original entry price if you are adding to a position.
stop_level (float): The price level where the stop loss is placed
stop_range (float): The price range from entry to stop level
capital (float): The total capital available for trading
risk_percent (float): The percentage of capital risked on the trade. 100% is represented by 100.
trade_direction (bool): True for long trades, false for short trades
whole_number_buy (bool): True to adjust the quantity to whole numbers
Returns: A tuple containing the number of units to trade, the value at risk, and the total value of the position: [number_of_underlying, value_of_risk, value_of_position]
calculate_risk_futures(risk_capital, stop_range)
Calculates risk metrics for futures trades based on the risk capital and stop range
Parameters:
risk_capital (float): The capital allocated for the trade
stop_range (float): The price range from entry to stop level
Returns: A tuple containing the number of contracts to trade, the value at risk, and the total value of the position: [contracts, value_of_risk, value_of_position]
calculate_risk_forex(entry, stop_level, stop_range, capital, risk_percent, trade_direction)
Calculates risk metrics for forex trades based on entry, stop level, and risk percent
Parameters:
entry (float): The price at which the position is entered. Use close if you arent adding to a position. Use the original entry price if you are adding to a position.
stop_level (float): The price level where the stop loss is placed
stop_range (float): The price range from entry to stop level
capital (float): The total capital available for trading
risk_percent (float): The percentage of capital risked on the trade. 100% is represented by 100.
trade_direction (bool): True for long trades, false for short trades
Returns: A tuple containing the number of lots to trade, the value at risk, and the total value of the position: [number_of_underlying, value_of_risk, value_of_position]
calculate_risk_auto(entry, stop_level, stop_range, capital, risk_percent, trade_direction, whole_number_buy)
Automatically selects the risk calculation method based on the asset type and calculates risk metrics
Parameters:
entry (float): The price at which the position is entered. Use close if you arent adding to a position. Use the original entry price if you are adding to a position.
stop_level (float): The price level where the stop loss is placed
stop_range (float): The price range from entry to stop level
capital (float): The total capital available for trading
risk_percent (float): The percentage of capital risked on the trade. 100% is represented by 100.
trade_direction (bool): True for long trades, false for short trades
whole_number_buy (bool): True to adjust the quantity to whole numbers, applicable only for non-futures and non-forex trades
Returns: A tuple containing the number of units or contracts to trade, the value at risk, and the total value of the position: [number_of_underlying, value_of_risk, value_of_position]
leverage_level(account_equity, position_value)
Calculates the leverage level used based on account equity and position value
Parameters:
account_equity (float): Total equity in the trading account
position_value (float): Total value of the position taken
Returns: The leverage level used in the trade
calculate_liquidation_level(entry, leverage, trade_direction, maintenance_margine)
Calculates the liquidation price level for a leveraged trade
Parameters:
entry (float): The price at which the position is entered
leverage (float): The leverage level used in the trade
trade_direction (bool): True for long trades, false for short trades
maintenance_margine (float): The maintenance margin requirement, expressed as a percentage
Returns: The price level at which the position would be liquidated, or na if leverage is zero
Pine Bibliothek
Ganz im Sinne von TradingView hat dieser Autor seinen/ihren Pine Code als Open-Source-Bibliothek veröffentlicht. Auf diese Weise können nun auch andere Pine-Programmierer aus unserer Community den Code verwenden. Vielen Dank an den Autor! Sie können diese Bibliothek privat oder in anderen Open-Source-Veröffentlichungen verwenden. Die Nutzung dieses Codes in einer Veröffentlichung wird in unseren Hausregeln reguliert.
Haftungsausschluss
Pine Bibliothek
Ganz im Sinne von TradingView hat dieser Autor seinen/ihren Pine Code als Open-Source-Bibliothek veröffentlicht. Auf diese Weise können nun auch andere Pine-Programmierer aus unserer Community den Code verwenden. Vielen Dank an den Autor! Sie können diese Bibliothek privat oder in anderen Open-Source-Veröffentlichungen verwenden. Die Nutzung dieses Codes in einer Veröffentlichung wird in unseren Hausregeln reguliert.