holidays_1980to1990Library "holidays_1980to1990"
This dataset is part of my "MarketHolidays" library. The datasets were split into different libraries to overcome compiling limitations, streamline the process of removing specific time frames if not needed, and to enhance code execution speed. The timestamps are generated using a custom Python script that employs the 'pandas_market_calendars' library. To build your own set of arrays, you can find the script and instructions at github.com
holidays(_country)
Parameters:
_country (string)
Göstergeler ve stratejiler
holidays_1970to1980Library "holidays_1970to1980"
This dataset is part of my "MarketHolidays" library. The datasets were split into different libraries to overcome compiling limitations, streamline the process of removing specific time frames if not needed, and to enhance code execution speed. The timestamps are generated using a custom Python script that employs the 'pandas_market_calendars' library. To build your own set of arrays, you can find the script and instructions at github.com
holidays(_country)
Parameters:
_country (string)
holidays_1962to1970Library "holidays_1962to1970"
This dataset is part of my "MarketHolidays" library. The datasets were split into different libraries to overcome compiling limitations, streamline the process of removing specific time frames if not needed, and to enhance code execution speed. The timestamps are generated using a custom Python script that employs the 'pandas_market_calendars' library. To build your own set of arrays, you can find the script and instructions at github.com
holidays(_country)
Parameters:
_country (string)
NetLiquidityLibraryMacFLibrary "NetLiquidityLibraryMacF"
The Net Liquidity Library provides daily values for net liquidity. Net liquidity is measured as Fed Balance Sheet - Treasury General Account - Reverse Repo. Time series for each individual component included too.
get_net_liquidity(component)
Gets the Net Liquidity time series for the last 250 trading days. Dates that are not present are represented as na.
Parameters:
component (simple string) : The component of the Net Liquidity function to return. Possible values: 'fed', 'tga', and 'rrp'. (`Net Liquidity` is returned if no argument is supplied).
Returns: The Net Liquidity time series or a component of the Net Liquidity function.
TableUtilsLibrary "TableUtils"
Collection of table utility functions
getTablePositionByLabel(tablePosition)
Return the position for the table by given label. Useful if you want to control the position of the table with a pine script input selector that use a more human friendly labels
Parameters:
tablePosition (string) : (string) Label of the table position. Possible values are: "Top left", "Top right", "Bottom left", "Bottom right", "Middle left", "Middle right"
Returns: the position for the table by given label. Useful if you want to control the position of the table with a pine script input selector that use a more human friendly labels
chrono_utilsLibrary "chrono_utils"
📝 Description
Collection of objects and common functions that are related to datetime windows session days and time ranges. The main purpose of this library is to handle time-related functionality and make it easy to reason about a future bar checking if it will be part of a predefined session and/or inside a datetime window. All existing session functionality I found in the documentation e.g. "not na(time(timeframe, session, timezone))" are not suitable for strategy scripts, since the execution of the orders is delayed by one bar, due to the script execution happening at the bar close. Moreover, a history operator with a negative value that looks forward is not allowed in any pinescript expression. So, a prediction for the next bar using the bars_back argument of "time()"" and "time_close()" was necessary. Thus, I created this library to overcome this small but very important limitation. In the meantime, I added useful functionality to handle session-based behavior. An interesting utility that emerged from this development is data anomaly detection where a comparison between the prediction and the actual value is happening. If those two values are different then a data inconsistency happens between the prediction bar and the actual bar (probably due to a holiday, half session day, a timezone change etc..)
🤔 How to Guide
To use the functionality this library provides in your script you have to import it first!
Copy the import statement of the latest release by pressing the copy button below and then paste it into your script. Give a short name to this library so you can refer to it later on. The import statement should look like this:
import jason5480/chrono_utils/2 as chr
To check if a future bar will be inside a window first of all you have to initialize a DateTimeWindow object.
A code example is the following:
var dateTimeWindow = chr.DateTimeWindow.new().init(fromDateTime = timestamp('01 Jan 2023 00:00'), toDateTime = timestamp('01 Jan 2024 00:00'))
Then you have to "ask" the dateTimeWindow if the future bar defined by an offset (default is 1 that corresponds th the next bar), will be inside that window:
// Filter bars outside of the datetime window
bool dateFilterApproval = dateTimeWindow.is_bar_included()
You can visualize the result by drawing the background of the bars that are outside the given window:
bgcolor(color = dateFilterApproval ? na : color.new(color.fuchsia, 90), offset = 1, title = 'Datetime Window Filter')
In the same way, you can "ask" the Session if the future bar defined by an offset it will be inside that session.
First of all, you should initialize a Session object.
A code example is the following:
var sess = chr.Session.new().from_sess_string(sess = '0800-1700:23456', refTimezone = 'UTC')
Then check if the given bar defined by the offset (default is 1 that corresponds th the next bar), will be inside the session like that:
// Filter bars outside the sessions
bool sessionFilterApproval = view.sess.is_bar_included()
You can visualize the result by drawing the background of the bars that are outside the given session:
bgcolor(color = sessionFilterApproval ? na : color.new(color.red, 90), offset = 1, title = 'Session Filter')
In case you want to visualize multiple session ranges you can create a SessionView object like that:
var view = SessionView.new().init(SessionDays.new().from_sess_string('2345'), array.from(SessionTimeRange.new().from_sess_string('0800-1600'), SessionTimeRange.new().from_sess_string('1300-2200')), array.from('London', 'New York'), array.from(color.blue, color.orange))
and then call the draw method of the SessionView object like that:
view.draw()
🏋️♂️ Please refer to the "EXAMPLE DATETIME WINDOW FILTER" and "EXAMPLE SESSION FILTER" regions of the script for more advanced code examples of how to utilize the full potential of this library, including user input settings and advanced visualization!
⚠️ Caveats
As I mentioned in the description there are some cases that the prediction of the next bar is not accurate. A wrong prediction will affect the outcome of the filtering. The main reasons this could happen are the following:
Public holidays when the market is closed
Half trading days usually before public holidays
Change in the daylight saving time (DST)
A data anomaly of the chart, where there are missing and/or inconsistent data.
A bug in this library (Please report by PM sending the symbol, timeframe, and settings)
Special thanks to @robbatt and @skinra for the constructive feedback 🏆. Without them, the exposed API of this library would be very lengthy and complicated to use. Thanks to them, now the user of this library will be able to get the most, with only a few lines of code!
TouchedLibrary "Touched"
Breakout(zone, lookback)
Tells if the zone has been broken on the current bar.
Parameters:
zone (Zone) : The definitiin of the zone.
lookback (int) : How many bars to look back.
Returns: a Touch object that tells if the zone has been broken up or down.
FalseBreakout(zone, lookback)
Tells if the zone has a false breakout on the current bar.
Parameters:
zone (Zone) : The definitiin of the zone.
lookback (int) : How many bars to look back.
Returns: a Touch object that tells if the zone has had a false breakout up or down.
Retest(zone, lookback)
Tells if the zone has been retested on the current bar.
Parameters:
zone (Zone) : The definitiin of the zone.
lookback (int) : How many bars to look back.
Returns: a Touch object that tells if the zone has been retested up or down.
Zone
Fields:
High (series__float)
Low (series__float)
BaseTime (series__integer)
Touch
Fields:
Up (series__bool)
Down (series__bool)
ETFFinderLibLibrary "ETFFinderLib"
TODO: add library description here
etf_search_ticker(ticker)
searches the entire ETF library by ticker and identifies which ETFs hold a specific tickers.
Parameters:
ticker (string)
Returns: returns 2 arrays, holding_array (string array) and compo_array(float array)
etf_search_sectors(sector)
searches the entire ETF library by sector and pulls by desired sector
Parameters:
sector (string)
Returns: returns 2 arrays, sector_array (string array) and composition array (float array)
A_Traders_Edge__LibraryLibrary "A_Traders_Edge__Library"
- A Trader's Edge (ATE)_Library was created to assist in constructing Market Overview Scanners (MOS)
LabelLocation(_firstLocation)
This function is used when there's a desire to print an assets ALERT LABELS at a set location on the scale that will
NOT change throughout the progression of the script. This is created so that if a lot of alerts are triggered, they
will stay relatively visible and not overlap each other. Ex. If you set your '_firstLocation' parameter as 1, since
there are a max of 40 assets that can be scanned, the 1st asset's location is assigned the value in the '_firstLocation' parameter,
the 2nd asset's location is the (1st asset's location+1)...and so on. If your first location is set to 81 then
the 1st asset is 81 and 2nd is 82 and so on until the 40th location = 120(in this particular example).
Parameters:
_firstLocation (simple int) : (simple int)
Optional(starts at 1 if no parameter added).
Location that you want the first asset to print its label if is triggered to do so.
ie. loc2=loc1+1, loc3=loc2+1, etc.
Returns: Returns 40 output variables each being a different location to print the labels so that an asset is asssigned to
a particular location on the scale. Regardless of if you have the maximum amount of assets being screened (40 max), this
function will output 40 locations… So there needs to be 40 variables assigned in the tuple in this function. What I
mean by that is you need to have 40 output location variables within your tuple (ie. between the ' ') regarless of
if your scanning 40 assets or not. If you only have 20 assets in your scripts input settings, then only the first 20
variables within the ' ' Will be assigned to a value location and the other 20 will be assigned 'NA', but their
variables still need to be present in the tuple.
SeparateTickerids(_string)
You must form this single tickerID input string exactly as described in the scripts info panel (little gray 'i' that
is circled at the end of the settings in the settings/input panel that you can hover your cursor over this 'i' to read the
details of that particular input). IF the string is formed correctly then it will break up this single string parameter into
a total of 40 separate strings which will be all of the tickerIDs that the script is using in your MO scanner.
Parameters:
_string (simple string) : (string)
A maximum of 40 Tickers (ALL joined as 1 string for the input parameter) that is formulated EXACTLY as described
within the tooltips of the TickerID inputs in my MOS Scanner scripts:
assets = input.text_area(tIDset1, title="TickerID (MUST READ TOOLTIP)", tooltip="Accepts 40 TICKERID's for each
copy of the script on the chart. TEXT FORMATTING RULES FOR TICKERID'S:
(1) To exclude the EXCHANGE NAME in the Labels, de-select the next input option.
(2) MUST have a space (' ') AFTER each TickerID.
(3) Capitalization in the Labels will match cap of these TickerID's.
(4) If your asset has a BaseCurrency & QuoteCurrency (ie. ADAUSDT ) BUT you ONLY want Labels
to show BaseCurrency(ie.'ADA'), include a FORWARD SLASH ('/') between the Base & Quote (ie.'ADA/USDT')", display=display.none)
Returns: Returns 40 output variables of the different strings of TickerID's (ie. you need to output 40 variables within the
tuple ' ' regardless of if you were scanning using all possible (40) assets or not.
If your scanning for less than 40 assets, then once the variables are assigned to all of the tickerIDs, the rest
of the 40 variables in the tuple will be assigned "NA".
TickeridForLabelsAndSecurity(_includeExchange, _ticker)
This function accepts the TickerID Name as its parameter and produces a single string that will be used in all of your labels.
Parameters:
_includeExchange (simple bool) : (bool)
Optional(if parameter not included in function it defaults to false ).
Used to determine if the Exchange name will be included in all labels/triggers/alerts.
_ticker (simple string) : (string)
For this parameter, input the varible named '_coin' from your 'f_main()' function for this parameter. It is the raw
Ticker ID name that will be processed.
Returns: ( )
Returns 2 output variables:
1st ('_securityTickerid') is to be used in the 'request.security()' function as this string will contain everything
TV needs to pull the correct assets data.
2nd ('lblTicker') is to be used in all of the labels in your MOS as it will only contain what you want your labels
to show as determined by how the tickerID is formulated in the MOS's input.
InvalidTID(_tablePosition, _stackVertical, _close, _securityTickerid, _invalidArray)
This is to add a table in the middle right of your chart that prints all the TickerID's that were either not formulated
correctly in the '_source' input or that is not a valid symbol and should be changed.
Parameters:
_tablePosition (simple string) : (string)
Optional(if parameter not included, it defaults to position.middle_right). Location on the chart you want the table printed.
Possible strings include: position.top_center, position.top_left, position.top_right, position.middle_center,
position.middle_left, position.middle_right, position.bottom_center, position.bottom_left, position.bottom_right.
_stackVertical (simple bool) : (bool)
Optional(if parameter not included, it defaults to true). All of the assets that are counted as INVALID will be
created in a list. If you want this list to be prited as a column then input 'true' here.
_close (float) : (float)
If you want them printed as a single row then input 'false' here.
This should be the closing value of each of the assets being tested to determine in the TickerID is valid or not.
_securityTickerid (string) : (string)
Throughout the entire charts updates, if a '_close' value is never regestered then the logic counts the asset as INVALID.
This will be the 1st TickerID varible (named _securityTickerid) outputted from the tuple of the TickeridForLabels()
function above this one.
_invalidArray (string ) : (array string)
Input the array from the original script that houses all of the invalidArray strings.
Returns: (na)
Returns a table with the screened assets Invalid TickerID's. Table draws automatically if any are Invalid, thus,
no output variable to deal with.
LabelSizes(_barCnt, _lblSzRfrnce)
This function sizes your Alert Trigger Labels according to the amount of Printed Bars the chart has printed within
a set time period, while also keeping in mind the smallest relative reference size you input in the 'lblSzRfrnceInput'
parameter of this function. A HIGHER % of Printed Bars(aka...more trades occurring for that asset on the exchange),
the LARGER the Name Label will print, potentially showing you the better opportunities on the exchange to avoid
exchange manipulation liquidations.
*** SHOULD NOT be used as size of labels that are your asset Name Labels next to each asset's Line Plot...
if your MOS includes these as you want these to be the same size for every asset so the larger ones dont cover the
smaller ones if the plots are all close to each other ***
Parameters:
_barCnt (float) : (float)
Get the 1st variable('barCnt') from the 'PrintedBarCount' function's tuple and input it as this functions 1st input
parameter which will directly affect the size of the 2nd output variable ('alertTrigLabel') outputted by this function.
_lblSzRfrnce (string) : (string)
Optional(if parameter not included, it defaults to size.small). This will be the size of the 1st variable outputted
by this function ('assetNameLabel') BUT also affects the 2nd variable outputted by this function.
Returns: ( )
Returns 2 variables:
1st output variable ('AssetNameLabel') is assigned to the size of the 'lblSzRfrnceInput' parameter.
2nd output variable('alertTrigLabel') can be of variying sizes depending on the 'barCnt' parameter...BUT the smallest
size possible for the 2nd output variable ('alertTrigLabel') will be the size set in the 'lblSzRfrnceInput' parameter.
AssetColor()
This function is used to assign 40 different colors to 40 variables to be used for the different labels/plots.
Returns: Returns 40 output variables each with a different color assigned to them to be used in your plots & labels.
Regardless of if you have the maximum amount of assets your scanning(40 max) or less,
this function will assign 40 colors to 40 variables that you have between the ' '.
PrintedBarCount(_time, _barCntLength, _barCntPercentMin)
The Printed BarCount Filter looks back a User Defined amount of minutes and calculates the % of bars that have printed
out of the TOTAL amount of bars that COULD HAVE been printed within the same amount of time.
Parameters:
_time (int) : (int)
The time associated with the chart of the particular asset that is being screened at that point.
_barCntLength (int) : (int)
The amount of time (IN MINUTES) that you want the logic to look back at to calculate the % of bars that have actually
printed in the span of time you input into this parameter.
_barCntPercentMin (int) : (int)
The minimum % of Printed Bars of the asset being screened has to be GREATER than the value set in this parameter
for the output variable 'bc_gtg' to be true.
Returns: ( )
Returns 2 outputs:
1st is the % of Printed Bars that have printed within the within the span of time you input in the '_barCntLength' parameter.
2nd is true/false according to if the Printed BarCount % is above the threshold that you input into the '_barCntPercentMin' parameter.
RCI(_rciLength, _source, _interval)
You will see me using this a lot. DEFINITELY my favorite oscillator to utilize for SO many different things from
timing entries/exits to determining trends.Calculation of this indicator based on Spearmans Correlation.
Parameters:
_rciLength (int) : (int)
Amount of bars back to use in RCI calculations.
_source (float) : (float)
Source to use in RCI calculations (can use ANY source series. Ie, open,close,high,low,etc).
_interval (int) : (int)
Optional(if parameter not included, it defaults to 3). RCI calculation groups bars by this amount and then will.
rank these groups of bars.
Returns: (float)
Returns a single RCI value that will oscillates between -100 and +100.
RCIAVG(firstLength, _amtBtLengths, _rciSMAlen, _source, _interval)
20 RCI's are averaged together to get this RCI Avg (Rank Correlation Index Average). Each RCI (of the 20 total RCI)
has a progressively LARGER Lookback Length. Though the RCI Lengths are not individually adjustable,
there are 2 factors that ARE:
(1) the Lookback Length of the 1st RCI and
(2) the amount of values between one RCI's Lookback Length and the next.
*** If you set 'firstLength' to it's default of 200 and '_amtBtLengths' to it's default of 120 (aka AMOUNT BETWEEN LENGTHS=120)...
then RCI_2 Length=320, RCI_3 Length=440, RCI_4 Length=560, and so on.
Parameters:
firstLength (int) : (int)
Optional(if parameter is not included when the function is called, then it defaults to 200).
This parameter is the Lookback Length for the 1st RCI used in the RCI Avg.
_amtBtLengths (int) : (int)
Optional(if parameter not included when the function is called, then it defaults to 120).
This parameter is the value amount between each of the progressively larger lengths used for the 20 RCI's that
are averaged in the RCI Avg.
***** BEWARE ***** Too large of a value here will cause the calc to look back too far, causing an error(thus the value must be lowered)
_rciSMAlen (int) : (int)
Unlike the Single RCI Function, this function smooths out the end result using an SMA with a length value that is this parameter.
_source (float) : (float)
Source to use in RCI calculations (can use ANY source series. Ie, open,close,high,low,etc).
_interval (int) : (int)
Optional(if parameter not included, it defaults to 3). Within the RCI calculation, bars next to each other are grouped together
and then these groups are Ranked against each other. This parameter is the number of adjacent bars that are grouped together.
Returns: (float)
Returns a single RCI value that is the Avg of many RCI values that will oscillate between -100 and +100.
PercentChange(_startingValue, _endingValue)
This is a quick function to calculate how much % change has occurred between the '_startingValue' and the '_endingValue'
that you input into the function.
Parameters:
_startingValue (float) : (float)
The source value to START the % change calculation from.
_endingValue (float) : (float)
The source value to END the % change caluclation from.
Returns: Returns a single output being the % value between 0-100 (with trailing numbers behind a decimal). If you want only
a certain amount of numbers behind the decimal, this function needs to be put within a formatting function to do so.
Rescale(_source, _oldMin, _oldMax, _newMin, _newMax)
Rescales series with a known '_oldMin' & '_oldMax'. Use this when the scale of the '_source' to
rescale is known (bounded).
Parameters:
_source (float) : (float)
Source to be normalized.
_oldMin (int) : (float)
The known minimum of the '_source'.
_oldMax (int) : (float)
The known maximum of the '_source'.
_newMin (int) : (float)
What you want the NEW minimum of the '_source' to be.
_newMax (int) : (float)
What you want the NEW maximum of the '_source' to be.
Returns: Outputs your previously bounded '_source', but now the value will only move between the '_newMin' and '_newMax'
values you set in the variables.
Normalize_Historical(_source, _minimumLvl, _maximumLvl)
Normalizes '_source' that has a previously unknown min/max(unbounded) determining the max & min of the '_source'
FROM THE ENTIRE CHARTS HISTORY. ]
Parameters:
_source (float) : (float)
Source to be normalized.
_minimumLvl (int) : (float)
The Lower Boundary Level.
_maximumLvl (int) : (float)
The Upper Boundary Level.
Returns: Returns your same '_source', but now the value will MOSTLY stay between the minimum and maximum values you set in the
'_minimumLvl' and '_maximumLvl' variables (ie. if the source you input is an RSI...the output is the same RSI value but
instead of moving between 0-100 it will move between the maxand min you set).
Normailize_Local(_source, _length, _minimumLvl, _maximumLvl)
Normalizes series with previously unknown min/max(unbounded). Much like the Normalize_Historical function above this one,
but rather than using the Highest/Lowest Values within the ENTIRE charts history, this on looks for the Highest/Lowest
values of '_source' within the last ___ bars (set by user as/in the '_length' parameter. ]
Parameters:
_source (float) : (float)
Source to be normalized.
_length (int) : (float)
The amount of bars to look back to determine the highest/lowest '_source' value.
_minimumLvl (int) : (float)
The Lower Boundary Level.
_maximumLvl (int) : (float)
The Upper Boundary Level.
Returns: Returns a single output variable being the previously unbounded '_source' that is now normalized and bound between
the values used for '_minimumLvl'/'_maximumLvl' of the '_source' within the user defined lookback period.
ETFHoldingsLibLibrary "ETFHoldingsLib"
spy_get()
: pulls SPY ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
qqq_get()
: pulls QQQ ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
arkk_get()
: pulls ARKK ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
xle_get()
: pulls XLE ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
brk_get()
: pulls BRK ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
ita_get()
: pulls ITA ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
iwm_get()
: pulls IWM ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
xlf_get()
: pulls XLF ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
xlv_get()
: pulls XLV ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
vnq_get()
: pulls VNQ ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
xbi_get()
: pulls XBI ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
blcr_get()
: pulls BLCR ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
vgt_get()
: pulls VGT ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
vwo_get()
: pulls VWO ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
vig_get()
: pulls VIG ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
vug_get()
: pulls VUG ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
vtv_get()
: pulls VTV ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
vea_get()
: pulls VEA ETF data
Returns: : tickers held (string array), percent ticker holding (float array), sectors (string array), percent secture positioning (float array)
lib_mathLibrary "lib_math"
a collection of functions calculating without history operator to avoid max_bars_back errors
mean(value, reset)
Parameters:
value (float) : series to track
reset (bool) : flag to reset tracking
@return returns average/mean of value since last reset
vwap(value, reset)
Parameters:
value (float) : series to track
reset (bool) : flag to reset tracking
@return returns vwap of value and volume since last reset
variance(value, reset)
Parameters:
value (float) : series to track
reset (bool) : flag to reset tracking
@return returns variance of value since last reset
trend(value, reset)
Parameters:
value (float) : series to track
reset (bool) : flag to reset tracking
@return where slope is the trend direction, correlation is a measurement for how well the values fit to the trendline (positive means ), stddev is how far the values deviate from the trend, x1 would be the time where reset is true and x2 would be the current time
DBTVLibrary "DBTV"
entry_message(password, percent, leverage, margin_mode, kis_number)
Create a entry message for POABOT
Parameters:
password (string) : (string) The password of your bot.
percent (float) : (float) The percent for entry based on your wallet balance.
leverage (int) : (int) The leverage of entry. If not set, your levereage doesn't change.
margin_mode (string) : (string) The margin mode for trade(only for OKX). "cross" or "isolated"
kis_number (int) : (int) The number of koreainvestment account. Default 1
Returns: (string) A json formatted string for webhook message.
StrategyDashboardLibrary ”StrategyDashboard”
Hey, everybody!
I haven’t done anything here for a long time, I need to get better ^^.
In my strategies, so far private, but not about that, I constantly use dashboards, which clearly show how my strategy is working out.
Of course, you can also find a number of these parameters in the standard strategy window, but I prefer to display everything on the screen, rather than digging through a bunch of boxes and dropdowns.
At the moment I am using 2 dashboards, which I would like to share with you.
1. monthly(isShow)
this is a dashboard with the breakdown of profit by month in per cent. That is, it displays how much percentage you made or lost in a particular month, as well as for the year as a whole.
Parameters:
isShow (bool) - determine allowance to display or not.
2. total(isShow)
The second dashboard displays more of the standard strategy information, but in a table format. Information from the series “number of consecutive losers, number of consecutive wins, amount of earnings per day, etc.”.
Parameters:
isShow (bool) - determine allowance to display or not.
Since I prefer the dark theme of the interface, now they are adapted to it, but in the near future for general convenience I will add the ability to adapt to light.
The same goes for the colour scheme, now it is adapted to the one I use in my strategies (because the library with more is made by cutting these dashboards from my strategies), but will also make customisable part.
If you have any wishes, feel free to write in the comments, maybe I can implement and add them in the next versions.
SessionVolumeProfileLibrary "SessionVolumeProfile"
Analyzes price & volume during regular trading hours to provide a session volume profile analysis. The primary goal of this library is to provide the developer with three values: the value area high, low and the point of control. The library also provides methods for rendering the value areas and histograms. To learn more about this library and how you can use it, click on the website link in my profile where you will find a blog post with detailed information.
debug(vp, position)
Helper function to write some information about the supplied SVP object to the screen in a table.
Parameters:
vp (Object) : The SVP object to debug
position (string) : The position.* to place the table. Defaults to position.bottom_center
getLowerTimeframe()
Depending on the timeframe of the chart, determines a lower timeframe to grab volume data from for the analysis
Returns: The timeframe string to fetch volume for
get(volumeProfile, lowerTimeframeHigh, lowerTimeframeLow, lowerTimeframeVolume)
Populated the provided SessionVolumeProfile object with vp data on the session.
Parameters:
volumeProfile (Object) : The SessionVolumeProfile object to populate
lowerTimeframeHigh (float ) : The lower timeframe high values
lowerTimeframeLow (float ) : The lower timeframe low values
lowerTimeframeVolume (float ) : The lower timeframe volume values
drawPriorValueAreas(todaySessionVolumeProfile, extendYesterdayOverToday, showLabels, labelSize, pocColor, pocStyle, pocWidth, vahlColor, vahlStyle, vahlWidth, vaColor)
Given a SessionVolumeProfile Object, will render the historical value areas for that object.
Parameters:
todaySessionVolumeProfile (Object) : The SessionVolumeProfile Object to draw
extendYesterdayOverToday (bool) : Defaults to true
showLabels (bool) : Defaults to true
labelSize (string) : Defaults to size.small
pocColor (color) : Defaults to #e500a4
pocStyle (string) : Defaults to line.style_solid
pocWidth (int) : Defaults to 1
vahlColor (color) : The color of the value area high/low lines. Defaults to #1592e6
vahlStyle (string) : The style of the value area high/low lines. Defaults to line.style_solid
vahlWidth (int) : The width of the value area high/low lines. Defaults to 1
vaColor (color) : The color of the value area background. Defaults to #00bbf911)
drawHistogram(volumeProfile, bgColor, showVolumeOnHistogram)
Given a SessionVolumeProfile object, will render the histogram for that object.
Parameters:
volumeProfile (Object) : The SessionVolumeProfile object to draw
bgColor (color) : The baseline color to use for the histogram. Defaults to #00bbf9
showVolumeOnHistogram (bool) : Show the volume amount on the histogram bars. Defaults to false.
Object
Fields:
numberOfRows (series__integer)
valueAreaCoverage (series__integer)
trackDevelopingVa (series__bool)
valueAreaHigh (series__float)
pointOfControl (series__float)
valueAreaLow (series__float)
startTime (series__integer)
endTime (series__integer)
dayHigh (series__float)
dayLow (series__float)
step (series__float)
pointOfControlLevel (series__integer)
valueAreaHighLevel (series__integer)
valueAreaLowLevel (series__integer)
volumeRows (array__float)
priceLevelRows (array__float)
ltfSessionHighs (array__float)
ltfSessionLows (array__float)
ltfSessionVols (array__float)
Debugging Made EasyMake debugging easier during development in Pinescript by displaying debug variables on the chart. No need for explanations, you devs know how to use it :)
Library "debug"
label(_output, _delete, _position)
- Debug label
Parameters:
_output (string) : Label output string
_delete (bool) : Delete all labels and only show the last one
_position (string)
label(_output, _delete, _position)
Parameters:
_output (float)
_delete (bool)
_position (string)
label(_output, _delete, _position)
Parameters:
_output (int)
_delete (bool)
_position (string)
label(_output, _delete, _position)
Parameters:
_output (bool)
_delete (bool)
_position (string)
label_last(_output, _delete, _position)
- Debug label on last bar
Parameters:
_output (string) : Label output string
_delete (bool) : Delete all labels and only show the last one
_position (string)
label_last(_output, _delete, _position)
Parameters:
_output (float)
_delete (bool)
_position (string)
label_last(_output, _delete, _position)
Parameters:
_output (int)
_delete (bool)
_position (string)
label_last(_output, _delete, _position)
Parameters:
_output (bool)
_delete (bool)
_position (string)
label_array(_items, _max, _reversed)
- Debug label on last bar.
Parameters:
_items (float ) : array of items.
_max (int) : Maximum items to display
_reversed (bool) : Show reversed array
label_array(_items, _max, _reversed)
Parameters:
_items (string )
_max (int)
_reversed (bool)
label_array(_items, _max, _reversed)
Parameters:
_items (int )
_max (int)
_reversed (bool)
array(_items, _max, _reversed)
- Debug label on last bar.
Parameters:
_items (float ) : array of items.
_max (int) : Maximum items to display
_reversed (bool) : Show reversed array
error(_message, _display)
- Debug error message.
Parameters:
_message (string) : Label output string
_display (bool) : Toggle to show hide the message.
error(_message, _display)
Parameters:
_message (float)
_display (bool)
error(_message, _display)
Parameters:
_message (int)
_display (bool)
warning(_message, _display)
- Debug warning message.
Parameters:
_message (string) : Label output string
_display (bool) : Toggle to show hide the message.
warning(_message, _display)
Parameters:
_message (float)
_display (bool)
warning(_message, _display)
Parameters:
_message (int)
_display (bool)
info(_message, _display)
- Debug info message.
Parameters:
_message (string) : Label output string
_display (bool) : Toggle to show hide the message.
info(_message, _display)
Parameters:
_message (float)
_display (bool)
info(_message, _display)
Parameters:
_message (int)
_display (bool)
Statistics TableStrategy Statistics
This library will add a table with statistics from your strategy. With this library, you won't have to switch to your strategy tester tab to view your results and positions.
Usage:
You can choose whether to set the table by input fields by adding the below code to your strategy or replace the parameters with the ones you would like to use manually.
// Statistics table options.
statistics_table_enabled = input.string(title='Show a table with statistics', defval='YES', options= , group='STATISTICS')
statistics_table_position = input.string(title='Position', defval='RIGHT', options= , group='STATISTICS')
statistics_table_margin = input.int(title='Table Margin', defval=10, minval=0, maxval=100, step=1, group='STATISTICS')
statistics_table_transparency = input.int(title='Cell Transparency', defval=20, minval=1, maxval=100, step=1, group='STATISTICS')
statistics_table_text_color = input.color(title='Text Color', defval=color.new(color.white, 0), group='STATISTICS')
statistics_table_title_cell_color = input.color(title='Title Cell Color', defval=color.new(color.gray, 80), group='STATISTICS')
statistics_table_cell_color = input.color(title='Cell Color', defval=color.new(color.purple, 0), group='STATISTICS')
// Statistics table init.
statistics.table(strategy.initial_capital, close, statistics_table_enabled, statistics_table_position, statistics_table_margin, statistics_table_transparency, statistics_table_text_color, statistics_table_title_cell_color, statistics_table_cell_color)
Sample:
If you are interested in the strategy used for this statistics table, you can browse the strategies on my profile.
Price - TP/SLPrices
With this library, you can easily manage prices such as stop loss, take profit, calculate differences, prices from a lower timeframe, and get the order size and commission from the strategy properties tab.
Note that the order size and commission only work with strategies!
Usage
Take Profit & Stop Loss
var bool open_trade = false
open_trade := strategy.position_size != 0
bars_since_opened = strategy.opentrades > 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) + 1 : 0
// ############################################################
// # TAKE PROFIT
// ############################################################
take_profit = input.string(title='Take Profit', defval='OFF', options= , group='TAKE PROFIT')
take_profit_percentage = input.float(title='Take Profit (% or X)', defval=0, minval=0, step=0.1, group='TAKE PROFIT')
take_profit_bars = input.int(title='Take Profit Bars', defval=0, minval=0, step=1, group='TAKE PROFIT')
take_profit_indication = input.string(title='Take Profit Plot', defval='OFF', options= , group='TAKE PROFIT')
take_profit_color = input.color(title='Take Profit Color', defval=#26A69A, group='TAKE PROFIT')
take_profit_price = math.round_to_mintick(strategy.position_avg_price)
take_profit_plot = plot(take_profit == 'ON' and take_profit_indication == 'ON' and open_trade and bars_since_opened >= take_profit_bars and take_profit_percentage > 0 and nz(take_profit_price) ? take_profit_price : na, color=take_profit_color, style=plot.style_linebr, linewidth=1, title='TP', offset=0)
// ############################################################
// # STOP LOSS
// ############################################################
stop_loss = input.string(title='Stop Loss', defval='OFF', options= , group='STOP LOSS')
stop_loss_percentage = input.float(title='Stop Loss (% or X)', defval=0, minval=0, step=0.1, group='STOP LOSS')
stop_loss_bars = input.int(title='Stop Loss Bars', defval=0, minval=0, step=1, group='STOP LOSS')
stop_loss_indication = input.string(title='Stop Loss Plot', defval='OFF', options= , group='STOP LOSS')
stop_loss_color = input.color(title='Stop Loss Color', defval=#FF5252, group='STOP LOSS')
stop_loss_price = math.round_to_mintick(strategy.position_avg_price)
stop_loss_plot = plot(stop_loss == 'ON' and stop_loss_indication == 'ON' and open_trade and bars_since_opened >= stop_loss_bars and stop_loss_percentage > 0 and nz(stop_loss_price) ? stop_loss_price : na, color=stop_loss_color, style=plot.style_linebr, linewidth=1, title='SL', offset=0)
// ############################################################
// # STRATEGY
// ############################################################
var limit_price = 0.0
var stop_price = 0.0
limit_price := take_profit == 'ON' ? price.take_profit_price(take_profit_price, take_profit_percentage, take_profit_bars, bars_since_opened) : na
stop_price := stop_loss == 'ON' ? price.stop_loss_price(stop_loss_price, stop_loss_percentage, stop_loss_bars, bars_since_opened) : na
strategy.exit(id='TP/SL', comment='TP/SL', from_entry='LONG', limit=limit_price, stop=stop_price)
Calculate difference between 2 prices:
price.difference(close, close )
Get last price from lower timeframe:
price.ltf(request.security_lower_tf(ticker, '1', close))
Get the order size from the properties tab:
price.order_size()
Get the commission from the properties tab.
price.commission()
Margin/Leverage CalculationMargin
This library calculates margin liquidation prices and quantities for long and short positions in your strategies.
Usage example
// ############################################################
// # INVESTMENT SETTINGS / INPUT
// ############################################################
// Get the investment capital from the properties tab of the strategy settings.
investment_capital = strategy.initial_capital
// Get the leverage from the properties tab of the strategy settings.
// The leverage is calculated from the order size for example: (300% = x3 leverage)
investment_leverage = margin.leverage()
// The maintainance rate and amount.
investment_leverage_maintenance_rate = input.float(title='Maintanance Rate (%)', defval=default_investment_leverage_maintenance_rate, minval=0, maxval=100, step=0.1, tooltip=tt_investment_leverage_maintenance_rate, group='MARGIN') / 100
investment_leverage_maintenance_amount = input.float(title='Maintanance Amount (%)', defval=default_investment_leverage_maintenance_amount, minval=0, maxval=100, step=0.1, tooltip=tt_investment_leverage_maintenance_amount, group='MARGIN')
// ############################################################
// # LIQUIDATION PRICES
// ############################################################
leverage_liquidation_price_long = 0.0
leverage_liquidation_price_long := na(leverage_liquidation_price_long ) ? na : leverage_liquidation_price_long
leverage_liquidation_price_short = 0.0
leverage_liquidation_price_short := na(leverage_liquidation_price_short ) ? na : leverage_liquidation_price_short
leverage_liquidation_price_long := margin.liquidation_price_long(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)
leverage_liquidation_price_short := margin.liquidation_price_short(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)
Get the qty for margin long or short position.
margin.qty_long(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)
margin.qty_short(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)
Get the price and qty for margin long or short position.
= margin.qty_long(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)
= margin.qty_short(investment_capital, strategy.position_avg_price, investment_leverage, investment_leverage_maintenance_rate, investment_leverage_maintenance_amount)
Backtest Strategy Optimizer AdapterBacktest Strategy Optimizer Adapter
With this library, you will be able to run one or multiple backtests with different variables (combinations). For example, you can run 100 backtests of Supertrend at once with an increment factor of 0.1. This way, you can easily fetch the most profitable settings and apply them to your strategy.
To get a better understanding of the code, you can check the code below.
Single backtest results
= backtest.results(date_start, date_end, long_entry, long_exit, take_profit_percentage, stop_loss_percentage, atr_length, initial_capital, order_size, commission)
Add backtest results to a table
backtest.table(initial_capital, profit_and_loss, open_balance, winrate, entries, exits, wins, losses, backtest_table_position, backtest_table_margin, backtest_table_transparency, backtest_table_cell_color, backtest_table_title_cell_color, backtest_table_text_color)
Backtest result without chart labels
= backtest.run(date_start, date_end, long_entry, long_exit, take_profit_percentage, stop_loss_percentage, atr_length, initial_capital, order_size, commission)
Backtest result profit
profit = backtest.profit(date_start, date_end, long_entry, long_exit, take_profit_percentage, stop_loss_percentage, atr_length, initial_capital, order_size, commission)
Backtest result winrate
winrate = backtest.winrate(date_start, date_end, long_entry, long_exit, take_profit_percentage, stop_loss_percentage, atr_length, initial_capital, order_size, commission)
Start Date
You can set the start date either by using a timestamp or a number that refers to the number of bars back.
Stop Loss / Take Profit Issue
Unfortunately, I did not manage to achieve 100% accuracy for the take profit and stop loss. The original TradingView backtest can stop at the correct position within a bar using the strategy.exit stop and limit variables. However, it seems unachievable with a crossunder/crossover function in PineScript unless it is calculated on every tick (which would make the backtesting results invalid). So far, I have not found a workaround, and I would be grateful if someone could solve this issue, if it is even possible. If you have any solutions or fixes, please let me know!
Multiple Backtest Results / Optimizer
You can run multiple backtests in a single strategy or indicator, but there are certain requirements for placing the correct code in the right way. To view examples of running multiple backtests, you can refer to the links provided in the updates I posted below. In the samples I have also explained how you can auto-generate code for your backtest strategy.
map_custom_value_usefullLibrary "map_custom_value_usefull"
makes it possible to create:
1.map with array value:
for this purpose need:
1.create map with arrays type value
2.put your array in this map, overloaded put method itself will put the array based on the type into the required field
3.next you can get this array with help standard get function, which will determine which field you need to get.(But because of this, only arrays of the same type can be used in one map)
2.map with map value:
for this purpose need:
1.create map with maps type value
2.put your other map in how value in your based map, need you need to put it in the field corresponding to your map type
3.next you can get this map with help standard get function.You need to specify a special field name here, because the get function cannot be overloaded without additional variables(
map_custom_value_fullLibrary "map_custom_value_full"
makes it possible to create:
1.map with array value:
for this purpose need:
1.create map with arrays type value
2.put your array in this map, overloaded put method itself will put the array based on the type into the required field
3.next you can get this array with help standard get function, by specifying the type field of your array
2.map with map value:
for this purpose need:
1.create map with maps type value
2.put your other map in how value in your based map, need you need to put it in the field corresponding to your map type
3.next you can get this map with help standard get function, by specifying the type field of your array
3.maps with value in array with maps:
for this purpose need:
1.create map with arrays type value
2.put as value maps_arrays fild with array from maps_arrays type fild which should already contain map of the type you need (there are not all map type fields here you can add a map of the required type by adding a corresponding field of map_arrays type.)
3.next you can get this array from map with help standard get function, by specifying the type field of your array
TableLibrary "Table"
This library provides an easy way to convert arrays and matrixes of data into tables. There are a few different implementations of each function so you can get more or less control over the appearance of the tables. The basic rule of thumb is that all matrix rows must have the same number of columns, and if you are providing multiple arrays/matrixes to specify additional colors (background/text), they must have the same number of rows/columns as the data array. Finally, you do have the option of spanning cells across rows or columns with some special syntax in the data cell. Look at the examples to see how the arrays and matrixes need to be built before they can be used by the functions.
floatArrayToCellArray(floatArray)
Helper function that converts a float array to a Cell array so it can be rendered with the fromArray function
Parameters:
floatArray (float ) : (array) the float array to convert to a Cell array.
Returns: array The Cell array to return.
stringArrayToCellArray(stringArray)
Helper function that converts a string array to a Cell array so it can be rendered with the fromArray function
Parameters:
stringArray (string ) : (array) the array to convert to a Cell array.
Returns: array The Cell array to return.
floatMatrixToCellMatrix(floatMatrix)
Helper function that converts a float matrix to a Cell matrix so it can be rendered with the fromMatrix function
Parameters:
floatMatrix (matrix) : (matrix) the float matrix to convert to a string matrix.
Returns: matrix The Cell matrix to render.
stringMatrixToCellMatrix(stringMatrix)
Helper function that converts a string matrix to a Cell matrix so it can be rendered with the fromMatrix function
Parameters:
stringMatrix (matrix) : (matrix) the string matrix to convert to a Cell matrix.
Returns: matrix The Cell matrix to return.
fromMatrix(CellMatrix, position, verticalOffset, transposeTable, textSize, borderWidth, tableNumRows, blankCellText)
Takes a CellMatrix and renders it as a table.
Parameters:
CellMatrix (matrix) : (matrix) The Cells to be rendered in a table
position (string) : (string) Optional. The position of the table. Defaults to position.top_right
verticalOffset (int) : (int) Optional. The vertical offset of the table from the top or bottom of the chart. Defaults to 0.
transposeTable (bool) : (bool) Optional. Will transpose all of the data in the matrices before rendering. Defaults to false.
textSize (string) : (string) Optional. The size of text to render in the table. Defaults to size.small.
borderWidth (int) : (int) Optional. The width of the border between table cells. Defaults to 2.
tableNumRows (int) : (int) Optional. The number of rows in the table. Not required, defaults to the number of rows in the provided matrix. If your matrix will have a variable number of rows, you must provide the max number of rows or the function will error when it attempts to set a cell value on a row that the table hadn't accounted for when it was defined.
blankCellText (string) : (string) Optional. Text to use cells when adding blank rows for vertical offsetting.
fromMatrix(dataMatrix, position, verticalOffset, transposeTable, textSize, borderWidth, tableNumRows, blankCellText)
Renders a float matrix as a table.
Parameters:
dataMatrix (matrix) : (matrix_float) The data to be rendered in a table
position (string) : (string) Optional. The position of the table. Defaults to position.top_right
verticalOffset (int) : (int) Optional. The vertical offset of the table from the top or bottom of the chart. Defaults to 0.
transposeTable (bool) : (bool) Optional. Will transpose all of the data in the matrices before rendering. Defaults to false.
textSize (string) : (string) Optional. The size of text to render in the table. Defaults to size.small.
borderWidth (int) : (int) Optional. The width of the border between table cells. Defaults to 2.
tableNumRows (int) : (int) Optional. The number of rows in the table. Not required, defaults to the number of rows in the provided matrix. If your matrix will have a variable number of rows, you must provide the max number of rows or the function will error when it attempts to set a cell value on a row that the table hadn't accounted for when it was defined.
blankCellText (string) : (string) Optional. Text to use cells when adding blank rows for vertical offsetting.
fromMatrix(dataMatrix, position, verticalOffset, transposeTable, textSize, borderWidth, tableNumRows, blankCellText)
Renders a string matrix as a table.
Parameters:
dataMatrix (matrix) : (matrix_string) The data to be rendered in a table
position (string) : (string) Optional. The position of the table. Defaults to position.top_right
verticalOffset (int) : (int) Optional. The vertical offset of the table from the top or bottom of the chart. Defaults to 0.
transposeTable (bool) : (bool) Optional. Will transpose all of the data in the matrices before rendering. Defaults to false.
textSize (string) : (string) Optional. The size of text to render in the table. Defaults to size.small.
borderWidth (int) : (int) Optional. The width of the border between table cells. Defaults to 2.
tableNumRows (int) : (int) Optional. The number of rows in the table. Not required, defaults to the number of rows in the provided matrix. If your matrix will have a variable number of rows, you must provide the max number of rows or the function will error when it attempts to set a cell value on a row that the table hadn't accounted for when it was defined.
blankCellText (string) : (string) Optional. Text to use cells when adding blank rows for vertical offsetting.
fromArray(dataArray, position, verticalOffset, transposeTable, textSize, borderWidth, blankCellText)
Renders a Cell array as a table.
Parameters:
dataArray (Cell ) : (array) The data to be rendered in a table
position (string) : (string) Optional. The position of the table. Defaults to position.top_right
verticalOffset (int) : (int) Optional. The vertical offset of the table from the top or bottom of the chart. Defaults to 0.
transposeTable (bool) : (bool) Optional. Will transpose all of the data in the matrices before rendering. Defaults to false.
textSize (string) : (string) Optional. The size of text to render in the table. Defaults to size.small.
borderWidth (int) : (int) Optional. The width of the border between table cells. Defaults to 2.
blankCellText (string) : (string) Optional. Text to use cells when adding blank rows for vertical offsetting.
fromArray(dataArray, position, verticalOffset, transposeTable, textSize, borderWidth, blankCellText)
Renders a string array as a table.
Parameters:
dataArray (string ) : (array_string) The data to be rendered in a table
position (string) : (string) Optional. The position of the table. Defaults to position.top_right
verticalOffset (int) : (int) Optional. The vertical offset of the table from the top or bottom of the chart. Defaults to 0.
transposeTable (bool) : (bool) Optional. Will transpose all of the data in the matrices before rendering. Defaults to false.
textSize (string) : (string) Optional. The size of text to render in the table. Defaults to size.small.
borderWidth (int) : (int) Optional. The width of the border between table cells. Defaults to 2.
blankCellText (string) : (string) Optional. Text to use cells when adding blank rows for vertical offsetting.
fromArray(dataArray, position, verticalOffset, transposeTable, textSize, borderWidth, blankCellText)
Renders a float array as a table.
Parameters:
dataArray (float ) : (array_float) The data to be rendered in a table
position (string) : (string) Optional. The position of the table. Defaults to position.top_right
verticalOffset (int) : (int) Optional. The vertical offset of the table from the top or bottom of the chart. Defaults to 0.
transposeTable (bool) : (bool) Optional. Will transpose all of the data in the matrices before rendering. Defaults to false.
textSize (string) : (string) Optional. The size of text to render in the table. Defaults to size.small.
borderWidth (int) : (int) Optional. The width of the border between table cells. Defaults to 2.
blankCellText (string) : (string) Optional. Text to use cells when adding blank rows for vertical offsetting.
debug(message, position)
Renders a debug message in a table at the desired location on screen.
Parameters:
message (string) : (string) The message to render.
position (string) : (string) Optional. The position of the debug message. Defaults to position.middle_right.
Cell
Type for each cell's content and appearance
Fields:
content (series string)
bgColor (series color)
textColor (series color)
align (series string)
colspan (series int)
rowspan (series int)
UtilsLibrary "Utils"
A collection of convenience and helper functions for indicator and library authors on TradingView
formatNumber(num)
My version of format number that doesn't have so many decimal places...
Parameters:
num (float) : (float) the number to be formatted
Returns: (string) The formatted number
getDateString(timestamp)
Convenience function returns timestamp in yyyy/MM/dd format.
Parameters:
timestamp (int) : (int) The timestamp to stringify
Returns: (int) The date string
getDateTimeString(timestamp)
Convenience function returns timestamp in yyyy/MM/dd hh:mm format.
Parameters:
timestamp (int) : (int) The timestamp to stringify
Returns: (int) The date string
getInsideBarCount()
Gets the number of inside bars for the current chart. Can also be passed to request.security to get the same for different timeframes.
Returns: (int) The # of inside bars on the chart right now.
getLabelStyleFromString(styleString, acceptGivenIfNoMatch)
Tradingview doesn't give you a nice way to put the label styles into a dropdown for configuration settings. So, I specify them in the following format: . This function takes care of converting those custom strings back to the ones expected by tradingview scripts.
Parameters:
styleString (string)
acceptGivenIfNoMatch (bool) : (bool) If no match for styleString is found and this is true, the function will return styleString, otherwise it will return tradingview's preferred default
Returns: (string) The string expected by tradingview functions
getTime(hourNumber, minuteNumber)
Given an hour number and minute number, adds them together and returns the sum. To be used by getLevelBetweenTimes when fetching specific price levels during a time window on the day.
Parameters:
hourNumber (int) : (int) The hour number
minuteNumber (int) : (int) The minute number
Returns: (int) The sum of all the minutes
getHighAndLowBetweenTimes(start, end)
Given a start and end time, returns the high or low price during that time window.
Parameters:
start (int) : The timestamp to start with (# of seconds)
end (int) : The timestamp to end with (# of seconds)
Returns: (float) The high or low value
getPremarketHighsAndLows()
Returns an expression that can be used by request.security to fetch the premarket high & low levels in a tuple.
Returns: (tuple)
getAfterHoursHighsAndLows()
Returns an expression that can be used by request.security to fetch the after hours high & low levels in a tuple.
Returns: (tuple)
getOvernightHighsAndLows()
Returns an expression that can be used by request.security to fetch the overnight high & low levels in a tuple.
Returns: (tuple)
getNonRthHighsAndLows()
Returns an expression that can be used by request.security to fetch the high & low levels for premarket, after hours and overnight in a tuple.
Returns: (tuple)
getLineStyleFromString(styleString, acceptGivenIfNoMatch)
Tradingview doesn't give you a nice way to put the line styles into a dropdown for configuration settings. So, I specify them in the following format: . This function takes care of converting those custom strings back to the ones expected by tradingview scripts.
Parameters:
styleString (string) : (string) Plain english (or TV Standard) version of the style string
acceptGivenIfNoMatch (bool) : (bool) If no match for styleString is found and this is true, the function will return styleString, otherwise it will return tradingview's preferred default
Returns: (string) The string expected by tradingview functions
getPercentFromPrice(price)
Get the % the current price is away from the given price.
Parameters:
price (float)
Returns: (float) The % the current price is away from the given price.
getPositionFromString(position)
Tradingview doesn't give you a nice way to put the positions into a dropdown for configuration settings. So, I specify them in the following format: . This function takes care of converting those custom strings back to the ones expected by tradingview scripts.
Parameters:
position (string) : (string) Plain english position string
Returns: (string) The string expected by tradingview functions
getTimeframeOfChart()
Get the timeframe of the current chart for display
Returns: (string) The string of the current chart timeframe
getTimeNowPlusOffset(candleOffset)
Helper function for drawings that use xloc.bar_time to help you know the time offset if you want to place the end of the drawing out into the future. This determines the time-size of one candle and then returns a time n candleOffsets into the future.
Parameters:
candleOffset (int) : (int) The number of items to find singular/plural for.
Returns: (int) The future time
getVolumeBetweenTimes(start, end)
Given a start and end time, returns the sum of all volume across bars during that time window.
Parameters:
start (int) : The timestamp to start with (# of seconds)
end (int) : The timestamp to end with (# of seconds)
Returns: (float) The volume
isToday()
Returns true if the current bar occurs on today's date.
Returns: (bool) True if current bar is today
padLabelString(labelText, labelStyle)
Pads a label string so that it appears properly in or not in a label. When label.style_none is used, this will make sure it is left-aligned instead of center-aligned. When any other type is used, it adds a single space to the right so there is padding against the right end of the label.
Parameters:
labelText (string) : (string) The string to be padded
labelStyle (string) : (string) The style of the label being padded for.
Returns: (string) The padded string
plural(num, singular, plural)
Helps format a string for plural/singular. By default, if you only provide num, it will just return "s" for plural and nothing for singular (eg. plural(numberOfCats)). But you can optionally specify the full singular/plural words for more complicated nomenclature (eg. plural(numberOfBenches, 'bench', 'benches'))
Parameters:
num (int) : (int) The number of items to find singular/plural for.
singular (string) : (string) The string to return if num is singular. Defaults to an empty string.
plural (string) : (string) The string to return if num is plural. Defaults to 's' so you can just add 's' to the end of a word.
Returns: (string) The singular or plural provided strings depending on the num provided.
timeframeInSeconds(timeframe)
Get the # of seconds in a given timeframe. Tradingview's timeframe.in_seconds() expects a simple string, and we often need to use series string, so this is an alternative to get you the value you need.
Parameters:
timeframe (string)
Returns: (int) The number of secondsof that timeframe
timeframeToString(tf)
Convert a timeframe string to a consistent standard.
Parameters:
tf (string) : (string) The timeframe string to convert
Returns: (string) The standard format for the string, or the unchanged value if it is unknown.