Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I plot my historical trades/executions in Tradingview the same way as in Ninjatrader?

I'd like to know if we can draw past trades using a script. I basically want to access my history of executions and draw lines at the price where I did them, to have a clear picture of my winning and losing trades. Is that possible in pine? For instance, starting from an excel where I keep track of my trades like here: data

I would get something like this in the chart: example

If possible the script could get the data directly from my broker, but if not I can always log all my trades in excel as above.

Right now we can show executions in Tradingview only with arrows, and the arrows are above or below bars. But we can not get exact information from them. I would like to have an script that shows executions the same way we can see them in MT4: Ninjatrader

like image 967
Daniel Avatar asked Oct 29 '25 04:10

Daniel


1 Answers

The example trade included in this code is for TSLA.
You can have excel generate strings like f_trade(true, 02, 12, 2020, 557, 03, 12, 2020, 596) for your trades, and paste that into the script.

//@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)
like image 137
Bjorn Mistiaen Avatar answered Nov 01 '25 08:11

Bjorn Mistiaen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!