analytics_tablesLibrary "analytics_tables"
📝 Description
This library provides the implementation of several performance-related statistics and metrics, presented in the form of tables.
The metrics shown in the afforementioned tables where developed during the past years of my in-depth analalysis of various strategies in an atempt to reason about the performance of each strategy.
The visualization and some statistics where inspired by the existing implementations of the "Seasonality" script, and the performance matrix implementations of @QuantNomad and @ZenAndTheArtOfTrading scripts.
While this library is meant to be used by my strategy framework "Template Trailing Strategy (Backtester)" script, I wrapped it in a library hoping this can be usefull for other community strategy scripts that will be released in the future.
🤔 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/analytics_tables/1 as ant
There are three types of tables provided by this library in the initial release. The stats table the metrics table and the seasonality table.
Each one shows different kinds of performance statistics.
The table UDT shall be initialized once using the `init()` method.
They can be updated using the `update()` method where the updated data UDT object shall be passed.
The data UDT can also initialized and get updated on demend depending on the use case
A code example for the StatsTable is the following:
var ant.StatsData statsData = ant.StatsData.new()
statsData.update(SideStats.new(), SideStats.new(), 0)
if (barstate.islastconfirmedhistory or (barstate.isrealtime and barstate.isconfirmed))
var statsTable = ant.StatsTable.new().init(ant.getTablePos('TOP', 'RIGHT'))
statsTable.update(statsData)
A code example for the MetricsTable is the following:
var ant.StatsData statsData = ant.StatsData.new()
statsData.update(ant.SideStats.new(), ant.SideStats.new(), 0)
if (barstate.islastconfirmedhistory or (barstate.isrealtime and barstate.isconfirmed))
var metricsTable = ant.MetricsTable.new().init(ant.getTablePos('BOTTOM', 'RIGHT'))
metricsTable.update(statsData, 10)
A code example for the SeasonalityTable is the following:
var ant.SeasonalData seasonalData = ant.SeasonalData.new().init(Seasonality.monthOfYear)
seasonalData.update()
if (barstate.islastconfirmedhistory or (barstate.isrealtime and barstate.isconfirmed))
var seasonalTable = ant.SeasonalTable.new().init(seasonalData, ant.getTablePos('BOTTOM', 'LEFT'))
seasonalTable.update(seasonalData)
🏋️♂️ Please refer to the "EXAMPLE" regions of the script for more advanced and up to date code examples!
Special thanks to @Mrcrbw for the proposal to develop this library and @DCNeu for the constructive feedback 🏆.
getTablePos(ypos, xpos)
Get table position compatible string
Parameters:
ypos (simple string) : The position on y axise
xpos (simple string) : The position on x axise
Returns: The position to be passed to the table
method init(this, pos, height, width, positiveTxtColor, negativeTxtColor, neutralTxtColor, positiveBgColor, negativeBgColor, neutralBgColor)
Initialize the stats table object with the given colors in the given position
Namespace types: StatsTable
Parameters:
this (StatsTable) : The stats table object
pos (simple string) : The table position string
height (simple float) : The height of the table as a percentage of the charts height. By default, 0 auto-adjusts the height based on the text inside the cells
width (simple float) : The width of the table as a percentage of the charts height. By default, 0 auto-adjusts the width based on the text inside the cells
positiveTxtColor (simple color) : The text color when positive
negativeTxtColor (simple color) : The text color when negative
neutralTxtColor (simple color) : The text color when neutral
positiveBgColor (simple color) : The background color with transparency when positive
negativeBgColor (simple color) : The background color with transparency when negative
neutralBgColor (simple color) : The background color with transparency when neutral
method init(this, pos, height, width, neutralBgColor)
Initialize the metrics table object with the given colors in the given position
Namespace types: MetricsTable
Parameters:
this (MetricsTable) : The metrics table object
pos (simple string) : The table position string
height (simple float) : The height of the table as a percentage of the charts height. By default, 0 auto-adjusts the height based on the text inside the cells
width (simple float) : The width of the table as a percentage of the charts width. By default, 0 auto-adjusts the width based on the text inside the cells
neutralBgColor (simple color) : The background color with transparency when neutral
method init(this, seas)
Initialize the seasonal data
Namespace types: SeasonalData
Parameters:
this (SeasonalData) : The seasonal data object
seas (simple Seasonality) : The seasonality of the matrix data
method init(this, data, pos, maxNumOfYears, height, width, extended, neutralTxtColor, neutralBgColor)
Initialize the seasonal table object with the given colors in the given position
Namespace types: SeasonalTable
Parameters:
this (SeasonalTable) : The seasonal table object
data (SeasonalData) : The seasonality data of the table
pos (simple string) : The table position string
maxNumOfYears (simple int) : The maximum number of years that fit into the table
height (simple float) : The height of the table as a percentage of the charts height. By default, 0 auto-adjusts the height based on the text inside the cells
width (simple float) : The width of the table as a percentage of the charts width. By default, 0 auto-adjusts the width based on the text inside the cells
extended (simple bool) : The seasonal table with extended columns for performance
neutralTxtColor (simple color) : The text color when neutral
neutralBgColor (simple color) : The background color with transparency when neutral
method update(this, wins, losses, numOfInconclusiveExits)
Update the strategy info data of the strategy
Namespace types: StatsData
Parameters:
this (StatsData) : The strategy statistics object
wins (SideStats)
losses (SideStats)
numOfInconclusiveExits (int) : The number of inconclusive trades
method update(this, stats, positiveTxtColor, negativeTxtColor, negativeBgColor, neutralBgColor)
Update the stats table object with the given data
Namespace types: StatsTable
Parameters:
this (StatsTable) : The stats table object
stats (StatsData) : The stats data to update the table
positiveTxtColor (simple color) : The text color when positive
negativeTxtColor (simple color) : The text color when negative
negativeBgColor (simple color) : The background color with transparency when negative
neutralBgColor (simple color) : The background color with transparency when neutral
method update(this, stats, buyAndHoldPerc, positiveTxtColor, negativeTxtColor, positiveBgColor, negativeBgColor)
Update the metrics table object with the given data
Namespace types: MetricsTable
Parameters:
this (MetricsTable) : The metrics table object
stats (StatsData) : The stats data to update the table
buyAndHoldPerc (float) : The buy and hold percetage
positiveTxtColor (simple color) : The text color when positive
negativeTxtColor (simple color) : The text color when negative
positiveBgColor (simple color) : The background color with transparency when positive
negativeBgColor (simple color) : The background color with transparency when negative
method update(this)
Update the seasonal data based on the season and eon timeframe
Namespace types: SeasonalData
Parameters:
this (SeasonalData) : The seasonal data object
method update(this, data, positiveTxtColor, negativeTxtColor, neutralTxtColor, positiveBgColor, negativeBgColor, neutralBgColor, timeBgColor)
Update the seasonal table object with the given data
Namespace types: SeasonalTable
Parameters:
this (SeasonalTable) : The seasonal table object
data (SeasonalData) : The seasonal cell data to update the table
positiveTxtColor (simple color) : The text color when positive
negativeTxtColor (simple color) : The text color when negative
neutralTxtColor (simple color) : The text color when neutral
positiveBgColor (simple color) : The background color with transparency when positive
negativeBgColor (simple color) : The background color with transparency when negative
neutralBgColor (simple color) : The background color with transparency when neutral
timeBgColor (simple color) : The background color of the time gradient
SideStats
Object that represents the strategy statistics data of one side win or lose
Fields:
numOf (series int)
sumFreeProfit (series float)
freeProfitStDev (series float)
sumProfit (series float)
profitStDev (series float)
sumGain (series float)
gainStDev (series float)
avgQuantityPerc (series float)
avgCapitalRiskPerc (series float)
avgTPExecutedCount (series float)
avgRiskRewardRatio (series float)
maxStreak (series int)
StatsTable
Object that represents the stats table
Fields:
table (series table) : The actual table
rows (series int) : The number of rows of the table
columns (series int) : The number of columns of the table
StatsData
Object that represents the statistics data of the strategy
Fields:
wins (SideStats)
losses (SideStats)
numOfInconclusiveExits (series int)
avgFreeProfitStr (series string)
freeProfitStDevStr (series string)
lossFreeProfitStDevStr (series string)
avgProfitStr (series string)
profitStDevStr (series string)
lossProfitStDevStr (series string)
avgQuantityStr (series string)
MetricsTable
Object that represents the metrics table
Fields:
table (series table) : The actual table
rows (series int) : The number of rows of the table
columns (series int) : The number of columns of the table
SeasonalData
Object that represents the seasonal table dynamic data
Fields:
seasonality (series Seasonality)
eonToMatrixRow (map)
numOfEons (series int)
mostRecentMatrixRow (series int)
balances (matrix)
returnPercs (matrix)
maxDDs (matrix)
eonReturnPercs (array)
eonCAGRs (array)
eonMaxDDs (array)
SeasonalTable
Object that represents the seasonal table
Fields:
table (series table) : The actual table
headRows (series int) : The number of head rows of the table
headColumns (series int) : The number of head columns of the table
eonRows (series int) : The number of eon rows of the table
seasonColumns (series int) : The number of season columns of the table
statsRows (series int)
statsColumns (series int) : The number of stats columns of the table
rows (series int) : The number of rows of the table
columns (series int) : The number of columns of the table
extended (series bool) : Whether the table has additional performance statistics
Profitloss
Average SL% Calculator with TP Levels by GorkiAverage Stop Loss And Take Profit Calculator For Futures Trading by Gorki
Description
The "Average SL% Calculator with TP Levels" script, is a simple tool for traders to plan the trades and check how much loss they are going to receive if they run this strategy. This script calculates the average price of up to four entry points, determines the percentage distance to the stop-loss level, and provides potential loss information based on margin and leverage. Additionally, it includes multiple take-profit levels to help traders systematically capture profits. Visual elements such as horizontal lines and labels make it easy to monitor key levels directly on the chart.
Why To Use This Indicator?
Risk Management: Automatically calculates the percentage distance to the stop-loss level, helping you to understand potential losses.
Profit Optimization: Supports up to four take-profit levels, enabling a structured approach to capturing gains.
Visual Clarity: Displays key levels and important information directly on the chart for easy monitoring.
Alerts: Generates alerts when the price crosses specified levels, ensuring you never miss critical trading signals.
How to Use the Script
Add the Script to Your Chart: Apply the script to your TradingView chart.
Set Input Values: Entry Points: Define up to four limit prices (LIMIT 1, LIMIT 2, LIMIT 3, LIMIT 4).
Stop-Loss: Set your stop-loss price (STOP LOSS).
Take Profits: Specify up to four take-profit levels (Take Profit 1, Take Profit 2, Take Profit 3, Take Profit 4).
Leverage: Input your leverage factor.
Margin: Enter the total margin amount for the trade.
View Calculations: The script will calculate the average entry price, the percentage distance to the stop-loss, and the potential loss based on margin and leverage.
Monitor Levels: Horizontal lines and labels will appear on the chart, showing entry points, stop-loss, and take-profit levels.
Set Alerts: Alerts will trigger when the price crosses your defined levels, helping you to take timely action.
Calculation Details
Average Price: Calculated as the mean of the specified limit prices.
Distance to Stop-Loss: Determined as the percentage difference between the average price and the stop-loss level.
Leveraged Distance: The stop-loss distance percentage multiplied by the leverage factor.
Potential Loss: Calculated by applying the leveraged distance percentage to the margin amount.
Take Profit Percentages: The percentage difference between the average price and each take-profit level.
This comprehensive indicator is a must-have for any trader looking to manage risks effectively while maximizing potential profits. Happy trading!
ATR profit and loss linesWhat is ATR?
Taking a candlestick, the following 3 transactions are calculated:
1-The difference between the high of the day and the low of the day
2-The difference between today's high and yesterday's close
3-The difference between today's low and yesterday's close
Atr takes the average of these 14-day candlesticks after making their calculations and it predicts how high or low a candle can go and these give us support and resistance helps with points
If you have noticed a rise in your chart and have no idea how high it will go, you can use Atr profit and loss lines.
The red zone is the stop point, the blue zones are the snow zones.
Must be used with macd. macd is validator.
There is an increase in your chart, you opened the atr profit and loss lines upwards and if macd gives you an increase, it is recommended that you enter the trade at that time. It is recommended to increase your loss line 1 step in the direction of profit every 2 profit breaks on atr profit and loss lines.
ATR Nedir?
Bir mum barı ele alınarak şu 3 işlem hesaplanır:
1-Günün yükseği ile günün düşüğü farkı
2-Günün yükseği ile dünün kapanışının farkı
3-Günün düşüğü ile dünkü kapanışın farkı
ATR ise 14 günlük bu mum barlarının hesaplarını yaptıktan sonra ortalamasını alır ve bir mumum ne kadar yükselip düşebileceği konusunda tahmin verir ve bunlar bize destek ve direnç noktaları konusunda yardımcı olur
Eğer grafiğinizde bir yükseliş farketmişseniz ne kadar yükseleceği konusunda fikriniz yoksa Atr kar zarar çizgilerini kullanabilirsiniz.
Kırmızı bölge durdurma noktası,mavi bölgeler kar bölgeleridir.
Macd ile birlikte kullanılmalıdır.macd doğrulayıcıdır.
Grafiğinizde yükseliş var,atr kar zarar çizgilerini yukarı yönlü açtınız ve macd size yükseliş veriyorsa işte o sırada işleme girmeniz tavsiye edilir.atr kar zarar çizgilerinde her 2 kar kırılımında bir zarar çizginizi kar yönünde 1 kademe arttırmanız önerilir
[TH] IMF(In Market Forever)If you want to be in market forever , Set Entry/Profit/Loss in advance.
This strategy is a profit and loss strategy indicating P&L Ratio on chart.
You can use webhook for auto-trade using my opensource webhook server(TradingHook).
Manual
add to chart
set entry price using mouse click on your chart
set profit price using mouse click on your chart
set loss price using mouse click on your chart
set nums of trades(default 1)
set alert for webhook if you have TradingHook webhook server
P/L panelThis is not a indicator or strategy.
I thought of having a table showing running profit or loss on chart from a specific price.
I tried to put the same in code and ended up with this code.
This is a table showing the running profit or loss from a manually specified price and quantity.
when you add the code, This table asks us to input the entry price and quantity.
It will calculate the running profit or loss with respect to running price and puts that in the table.
We will have to input two things.
1.) entry price: the price at which a position(long/short) is taken.
2.) Quantity: A +value need to be entered for Long position and -value for short position.
code detects whether its a long position or short position based on the quantity info.
for example if a LONG position is taken at a price 60 of 100 quantity,
then in price we need to enter 60
and in quantity 100 (+ve value)
for SHORT position at a price of 60 of 100 quantity,
in price we need to enter 60
and in quantity -100 (-ve value)
once the table is added to the chart.
Just double click on the table, it will open the settings tab and we can provide new inputs price/quantity/position.
positioning of table is optional and all possible positioning options are provided.
Advise further improvements required if any in this code.
This piece of code can be used along with any indicator.
For which we may need to use valuewhen() additionally.
Try it yourself and ping me if required.