简体   繁体   English

止损和利润目标占入场价格的百分比

[英]Stop loss and profit target as percentage of entry price

In Pine, how can I set up automatic exit if the price goes 5% over or under the price I bought it for?在 Pine 中,如果价格高于或低于我购买的价格 5%,我该如何设置自动退出?

strategy.order("order ID", strategy.long, 1)
strategy.exit("Exit ID", from_entry="ID", ?)

What would I put by the ?我会放什么? to make it limit exit at 5% over/under?使其限制退出在 5% 以上/以下?

Use strategy.position_avg_price to get your average price in the position and than calculate your take profit and stop loss accordingly.使用strategy.position_avg_price获取头寸的平均价格,然后相应地计算您的止盈和止损。

Here is a simple example for a long position.这是一个多头头寸的简单示例。

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

tp_per = input.float(2.5, "Take profit %") * 0.01
sl_per = input.float(0.5, "Stop loss %") * 0.01

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

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

long_tp_price = strategy.position_avg_price * (1 + tp_per)
long_sl_price = strategy.position_avg_price * (1 - sl_per)

if (strategy.position_size > 0)
    strategy.exit("Long Exit", "Long", stop=long_sl_price, limit=long_tp_price)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM