I'm trying to set up a table with a flexible amount of rows depending on the number of trades that happened within a time window. The variable TradeNumber
holds the number of trades. My plan was using a for loop to create the necessary amount of rows:
for i = 1 to TradeNumber
table.cell(statsTable, 0, i, str.tostring(ta.valuewhen(BreakNegative, TradeNumber, i)))
As you can see, I'd like to display the value of TradeNumber
in each row. To obtain this value I'm using ta.valuewhen
with BreakNegative
as the condition and i
as iterator.
I just realized from the error message that i
cannot be used since it's a series int and a simple int is needed for the requested number of the occurrence.
Does anyone here know a workaround that allows me to still use a for loop in order to create the table rows? Or will I have to manually create the rows and insert simple int number for the occurrences of ta.valuewhen
?
Thanks for your input!
For better context see the entire script below:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bimsbrohm
//@version=5
indicator("Backtest Snippet", overlay = true, max_labels_count=500, max_bars_back = 5000)
var float MaxProfit_USD = na
var float MaxProfit_Perc = na
var TradeNumber = 0
var bool InTrade = na
var float EntryPrice = na
var float highest = na
var float lowest = na
var float NumberShares = na
var float TradeLength = na
var string TradeDir = na
var int lookbackCrossUp = na
var int lookbackCrossDown = na
var int barIndexCrossUp = na
var int barIndexCrossDown = na
// Rounding Function
RoundDown(number, decimals) =>
factor = math.pow(10, decimals)
math.floor(number * factor) / factor
// DATE RANGE FILTERING
DateFilter = input.bool(true, '═════════════ Date Range Filtering', group='Date')
i_startTime = input.time(defval=timestamp('01 Jan 2023 09:30 +0000'), title='Start', group='Date')
i_endTime = input.time(defval=timestamp('31 Dec 2023 16:00 +0000'), title='Ende', group='Date')
TradeDateIsAllowed() =>
DateFilter ? time >= i_startTime and time <= i_endTime : true
Equity = 10000
// Entry Signal: MA-Cross
MA1 = ta.ema(close, 10)
MA2 = ta.ema(close, 50)
plot(MA1, color=color.red, linewidth = 3)
plot(MA2, color=color.green, linewidth = 3)
CrossUp = ta.crossover(MA1, MA2) and TradeDateIsAllowed()
CrossDown = ta.crossunder(MA1, MA2) and TradeDateIsAllowed()
// CALCULATIONS
if CrossUp or CrossDown
TradeNumber := TradeNumber + 1
InTrade := true
EntryPrice := close
NumberShares := (Equity/EntryPrice)
if CrossUp
TradeDir := 'Long'
else if CrossDown
TradeDir := 'Short'
if InTrade
lookbackCrossUp := ta.barssince(CrossUp)
lookbackCrossDown := ta.barssince(CrossDown)
barIndexCrossUp := bar_index - lookbackCrossUp
barIndexCrossDown := bar_index - lookbackCrossDown
TradeLength := TradeDir == 'Long' ? lookbackCrossUp : TradeDir == 'Short' ? lookbackCrossDown : na
if lookbackCrossUp > 0
highest := ta.highest(lookbackCrossUp)
else if lookbackCrossDown > 0
lowest := ta.lowest(lookbackCrossDown)
MaxProfit_USD := (highest * NumberShares) - (EntryPrice * NumberShares)
MaxProfit_USD := RoundDown(MaxProfit_USD,2)
MaxProfit_Perc := ((highest - EntryPrice)/EntryPrice) * 100
MaxProfit_Perc := RoundDown(MaxProfit_Perc,1)
BreakNegative = ta.crossunder(close, EntryPrice)
if BreakNegative
InTrade := false
var table statsTable = table.new(position = position.bottom_right, columns = 4, rows = 100, frame_color = color.rgb(0, 0, 0), frame_width = 2, border_color = color.rgb(0, 0, 0), border_width = 1)
// Horizontal headers
table.cell(table_id = statsTable, column = 0, row = 0, text = 'Trade Number', text_halign = text.align_left)
table.cell(table_id = statsTable, column = 1, row = 0, text = 'Trade Length', text_halign = text.align_left)
table.cell(table_id = statsTable, column = 2, row = 0, text = 'Max Profit in %', text_halign = text.align_left)
table.cell(table_id = statsTable, column = 3, row = 0, text = 'Max Profit in USD', text_halign = text.align_left)
for i = 1 to TradeNumber
table.cell(statsTable, 0, i, str.tostring(ta.valuewhen(BreakNegative, TradeNumber, 1)))
table.cell(statsTable, 1, i, str.tostring(ta.valuewhen(BreakNegative, TradeLength, 1)))
table.cell(statsTable, 2, i, str.tostring(ta.valuewhen(BreakNegative, MaxProfit_Perc, 1)))
table.cell(statsTable, 3, i, str.tostring(ta.valuewhen(BreakNegative, MaxProfit_USD, 1)))