0

I am attempting to plot a list of trades inside Tradingview using pine, ideally utilizing the 'strategy' function so that the Strategy Tester panel will display performance statistics about the trades listed in the code.

I started with Bjorn Mistiaen's answer here: How can I plot my historical trades/executions in Tradingview the same way as in Ninatrader?

but the code does not seem to work as intended. When plugging the code in and viewing his example trades on TSLA, TradingView's strategy tester shows one Entry Long at 12-2, and then never closes the trade even though a trade close is specified. The other trade in his code from 11-27 is never registered. When I replaced some example trades of my own, I am getting duplicate entries returned in the Trade List (such as 5 trades when 3 are specified in the code). I would like to be able to include the hour and minute of the execution as well which I thought could help sort out why that is, but my attempts to add '_enter_h, _enter_min' and '_exit_h, _exit_min' result in no data plotting on the chart at all.

Despite a dozen different implementations, ChatGPT has been also unsurprisingly unsuccessful at arriving at any functional way of plotting these trades, either using 'strategy' or otherwise.

Would greatly appreciate the help if this is possible!

Here is the code from Bjorn's answer for reference:

//@version=4
strategy(title="Trades", overlay=true, backtest_fill_limits_assumption=0, process_orders_on_close=true, pyramiding=0, commission_value=0, slippage=0, calc_on_order_fills=true, close_entries_rule="ANY")

var int[]   enter_ts    = array.new_int(na)
var float[] enter_price = array.new_float(na)
var int[]   exit_ts     = array.new_int(na)
var float[] exit_price  = array.new_float(na)
var int[]   position    = array.new_int(na)

f_trade(_long, _enter_d, _enter_m, _enter_y, _enter_price, _exit_d, _exit_m, _exit_y, _exit_price) =>
    array.push(enter_ts,    timestamp(_enter_y, _enter_m, _enter_d, 0, 0, 0))
    array.push(exit_ts,     timestamp(_exit_y,  _exit_m,  _exit_d,  0, 0, 0))
    array.push(enter_price, _enter_price)
    array.push(exit_price,  _exit_price)
    array.push(position,    _long ? 1 : -1)

if barstate.isfirst
    f_trade(false, 27, 11, 2020, 580, 30, 11, 2020, 560)
    f_trade(true,  02, 12, 2020, 570, 03, 12, 2020, 596)


ts_today    = timestamp(year, month, dayofmonth, 0, 0, 0)

if (array.includes(enter_ts, ts_today)) and strategy.position_size == 0
    idx     = array.indexof(enter_ts, ts_today)
    ts      = array.get(enter_ts, idx)
    price   = array.get(enter_price, idx)
    pos     = array.get(position, idx)
    comm    = (pos ? "Long" : "Short") + " at " + tostring(price, "#.##")

    if pos == 1 
        strategy.entry("L", strategy.long, limit=price, comment=comm)

    else if pos == -1 
        strategy.entry("S", strategy.short, limit=price, comment=comm)

else if (array.includes(exit_ts, ts_today)) and strategy.position_size != 0

    idx     = array.indexof(exit_ts, ts_today)
    ts      = array.get(exit_ts, idx)
    price   = array.get(exit_price, idx)
    pos     = array.get(position, idx)
    comm    = "TP " + (pos ? "Long" : "Short") + " at " + tostring(price, "#.##")

    if strategy.position_size > 0
        strategy.exit("L", limit=price, comment=comm)
    else if strategy.position_size < 0
        strategy.exit("S", limit=price, comment=comm)
    ```
peanut53
  • 1
  • 1

0 Answers0