简体   繁体   English

如何设置止损和止盈?

[英]How Can I Set SL and TP?

i have strategy for entry and my stop loss is low of before candle.我有入场策略,我的止损在蜡烛之前很低。 and my tp is R/R 3 that it set according to Stoploss.我的 tp 是根据止损设置的 R/R 3。

i wrote code but it not work.我写了代码,但它不起作用。

strategy.entry("Long Position",strategy.long,when = LongEntry)
strategy.exit("Exit Long" , from_entry = "Long Position" , stop = low[1] , profit = 3*low[1] )

what is problem?什么是问题?

The problem is, you will be updating your stop loss and take profit prices on every bar as it is written in your example.问题是,您将按照示例中的说明更新每根柱的止损和获利价格。 Because, with every new bar, there will be a new low value.因为,每出现一个新柱,都会有一个新low And with every new bar, your strategy.exit() function will be called.对于每一个新柱,都会调用你的strategy.exit() function。

What you can do is, store the low price at the time of the entry and use it in your strategy.exit() call.您可以做的是,在入场时存储low并在您的strategy.exit()调用中使用它。

var float low_at_entry = na

if (LongEntry)
    low_at_entry := low

strategy.entry("Long Position",strategy.long,when = LongEntry)

if (strategy.position_size > 0)
    strategy.exit("Exit Long" , from_entry = "Long Position" , stop = low_at_entry [1] , profit = 3*low_at_entry [1] )

Note: You need to make sure that your LongEntry is true only once during a trade.注意:您需要确保您的LongEntry在交易期间只为真一次。 Otherwise low_at_entry will be updated after the entry again.否则low_at_entry将在再次进入后更新。

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

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