松树脚本策略Stoploss和Take -Profit问题

发布于 2025-01-19 10:52:06 字数 1216 浏览 0 评论 0原文

我尝试编写带有止损和止盈的策略。信号栏之后,宣布入场(蓝线)、止损(红线)和止盈(绿线)。

问题一:建仓时触发止损。即使价格没有触及红线。我做错了什么? (比较 Imagelink) 1

在此处输入图像描述

问题 2:当仓位平仓后,在下一根柱线进入新仓位。即使价格高于入场价(蓝线)。这些购买不应该发生。 (比较 Imagelink) 2

在此处输入图像描述

这是我的代码:

//Stoploss + Take profit
SL = input.float(0.5, step=0.1) 
TP = input.float(2.5, step=0.1)

buyprice = BB_high
longstop = BB_high - (BB_high - BB_low)*SL
longprofit = BB_high + (BB_high - BB_low)/2 * TP

plot (longstop, color=color.red, linewidth=2)
plot (longprofit, color=color.green, linewidth=2)

//Position entry + exit (stoploss or takeprofit)
strategy.entry("Buy", strategy.long, stop=buyprice) 

if strategy.position_size >0
    strategy.exit (id="Long SL", from_entry="Buy", limit=longstop)
    strategy.exit (id="Long TP", from_entry="Buy", stop=longprofit)

I tried to code a strategy with Stoploss and Takeprofit. After the signal bar, the entry (blue Line), stoploss (red line) and takeprofit (green line) are declared.

Problem 1: When a position is entered, the stoploss is triggered. Even if the red line is not touched by the price. What am i doing wrong? (compare Imagelink) 1

enter image description here

Problem 2: When a position is closed, a new position is entered at the next bar. Even if the price is higher than the entry price (blue line). These buys should not happen. (compare Imagelink) 2

enter image description here

Here my Code:

//Stoploss + Take profit
SL = input.float(0.5, step=0.1) 
TP = input.float(2.5, step=0.1)

buyprice = BB_high
longstop = BB_high - (BB_high - BB_low)*SL
longprofit = BB_high + (BB_high - BB_low)/2 * TP

plot (longstop, color=color.red, linewidth=2)
plot (longprofit, color=color.green, linewidth=2)

//Position entry + exit (stoploss or takeprofit)
strategy.entry("Buy", strategy.long, stop=buyprice) 

if strategy.position_size >0
    strategy.exit (id="Long SL", from_entry="Buy", limit=longstop)
    strategy.exit (id="Long TP", from_entry="Buy", stop=longprofit)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的味道 2025-01-26 10:52:06

使用限制以获取利润和停止以进行停止损失。你把那些混在一起了。

strategy.exit (id="Long SL", from_entry="Buy", stop=longstop)
strategy.exit (id="Long TP", from_entry="Buy", limit=longprofit)

Use limit for take profit and stop for stop loss. You got those mixed up.

strategy.exit (id="Long SL", from_entry="Buy", stop=longstop)
strategy.exit (id="Long TP", from_entry="Buy", limit=longprofit)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文