使用贸易条目值获取指标-Pine脚本 [英] Get indicator with trade entry value - Pine Script

查看:18
本文介绍了使用贸易条目值获取指标-Pine脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在交易进入时设定2ATR的止损。使用Pine脚本,我设置了2ATR的止损,但不是在交易进入时,但Pine脚本将ATR值更新为最后关闭的蜡烛。首选版本是v4是PS的v5。

谢谢

atr = atr(14)

if EntryShortCondiction1 and EntryShortCondiction2
    strategy.entry("Short", false, 100)
    
ShortStop = (strategy.position_avg_price + atr*2)
ShortProfit = (strategy.position_avg_price - atr*2)

if strategy.position_size<0
    strategy.exit(id = "Short", stop=ShortStop, limit=ShortProfit)

推荐答案

可以使用strategy.opentrades.entry_price变量获取入门价格。

举个例子:

//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)

_atr = ta.atr(14)

longCondition = ta.crossover(ta.sma(close, 14), ta.sma(close, 28))

if (longCondition)
    strategy.entry("Long", strategy.long)

lastEntryPrice = strategy.opentrades.entry_price(strategy.opentrades - 1)

var float tp = na
var float sl = na

if (strategy.position_size[1] != strategy.position_size)
    tp := lastEntryPrice + (_atr * 2)
    sl := lastEntryPrice - (_atr * 2)

inTrade = strategy.position_size > 0

plot(inTrade ? tp : na, color=color.green, style=plot.style_circles)
plot(inTrade ? sl : na, color=color.red, style=plot.style_circles)
plot(lastEntryPrice)

strategy.exit("Short", "Long", stop=sl, limit=tp)

所以,入场价是55334,19,ATR当时是2725,88。获利=55334,19+(2*2725,88)=60785,95,这似乎是正确的。

这篇关于使用贸易条目值获取指标-Pine脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆