Dan*_*iel 4 tradingview-api pine-script
我想知道我们是否可以使用脚本绘制过去的交易。我基本上想访问我的执行历史记录,并以我执行这些操作的价格绘制线条,以便清楚地了解我的盈利和亏损交易。这在松树中可能吗?例如,从我跟踪我的交易的 Excel 开始,如下所示:数据
我会在图表中得到类似的内容:示例
如果可能的话,脚本可以直接从我的经纪人那里获取数据,但如果没有,我总是可以如上所述在 Excel 中记录我的所有交易。
现在我们只能用箭头在 Tradingview 中显示执行情况,并且箭头位于柱的上方或下方。但我们无法从他们那里得到确切的信息。我想要一个脚本来显示执行情况,就像我们在 MT4 中看到的那样:Ninjatrader
此代码中包含的示例交易是TSLA
.
您可以让 Excel 生成类似于f_trade(true, 02, 12, 2020, 557, 03, 12, 2020, 596)
您的交易的字符串,并将其粘贴到脚本中。
//@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)
Run Code Online (Sandbox Code Playgroud)