VPQuantLib

Misc of math, position size and consolidation detection functions that can be used accross various scripts.
isPercentAboveReference(current, percent, reference, or_equal)
Checks if the current value is bigger (or equal) with the provided percent value to the reference
Parameters:
current (float): - what to check against the reference
percent (float): - what is the percent to check for difference
reference (float): - what to compare against
or_equal (bool): - enables checking for bigger or equal
Returns: true if the current is percent bigger (or equal) to the reference
isPercentBelowReference(current, percent, reference, or_equal)
Checks if the current value is smaller (or equal) with the provided percent value to the reference
Parameters:
current (float): - what to check against the reference
percent (float): - what is the percent to check for difference
reference (float): - what to compare against
or_equal (bool): - enables checking for smaller or equal
Returns: true if the current is percent smaller (or equal) to the reference
isInRange(current, reference, min_percent, max_percent, below)
Checks if the current value is greater/smaller than the reference value within the provided percent range
Parameters:
current (float): - what to check for being in range against the refenence
reference (float): - what to compare against
min_percent (float): - the min percent range border
max_percent (float): - the max percent range border
below (bool): - check if below or above the reference
return true if the current is bigger/smaller than the reference withing the [min, max] percent range provided
GetRiskBasedPositionSize(account_balance, equity_risk_perc, max_loss_per_share)
Calculates and returns the positins size based on risk of the equity
Parameters:
account_balance (float): - total account balance
equity_risk_perc (int): - percent of equity to risk in the trade
max_loss_per_share (float): - maximum loss per share (in currency, not in %) that we're willing to loose (calc based on the entry_price-stop_loss_price)
return number of shares to buy
CheckInRangeConsolidation(consolidation_period, allowed_consolidation_range, ref_high, ref_low, prev_bar_consolidaton, draw_consolidation_lines)
Checks if the current bar is in a consolidation range
Parameters:
consolidation_period (int): - the number of bars to consider for consolidation range calculation
allowed_consolidation_range (int): - the percentage range allowed for the current consolidation range to be considered valid
ref_high (float): - the reference high value to use for consolidation range calculation
ref_low (float): - the reference low value to use for consolidation range calculation
prev_bar_consolidaton (bool)
draw_consolidation_lines (bool): - a boolean indicating if consolidation range lines should be drawn on the chart
return a tuple of three values:
1. _curr_consolidation - a boolean indicating if the current bar is in consolidation range
2. _curr_consolidation_low - the current consolidation low value
3. _curr_consolidation_high - the current consolidation high value
FindBasicConsolidation(loopback_period, consolidation_length, ref_high, ref_low, draw_consolidation_lines)
Finds a basic consolidation areas, looking back 1000 bars to find the pivot of the trend and checks if the current bar is in consolidation area counting the
number of bars that have not broken the consolidation high/low levels
Parameters:
loopback_period (int): - the number of bars to look back to determine the high/low watermark
consolidation_length (int): - minimum number of bars required to establish a consolidation period
ref_high (float): - user input for high (can be based on the bar or wicks)
ref_low (float): - user input for high (can be based on the bar or wicks)
draw_consolidation_lines (bool): - enable/disable drawing of the consolidation lines
Returns: _pivot_point - pivot point
Libreria Pine
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. Complimenti 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.
Declinazione di responsabilità
Libreria Pine
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. Complimenti 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.