4

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.

Rigth now we can show executions in tradingview only with arrows, and the arrows are abobe or below bars. But we can not get exact information from them. i woud like to have an script that shows executions the same way we can see them in MT4: Ninjatrader

Thanks in advace,

Daniel

Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42
Daniel
  • 41
  • 3

1 Answers1

6

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)
Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42