繁体   English   中英

根据之前最低低/最高高的止损设置止盈

[英]Set take profit based on stop loss of previous lowest low/highest high

我正在基于几个不同的指标在 PineScript 中编写 Tradingview 策略,并且我想根据止损百分比设置止盈目标,例如; 如果基于之前最低低点的止损<2.5%,那么我想要5倍的TP,但如果之前的最低低点>2.5%,那么我想要4倍的TP。

这是代码的 TP/SL 部分的当前版本,它不起作用。

// ENTRY

strategy.entry("LONG", strategy.long, when=long)
strategy.entry("SHORT", strategy.short, when=short)

// LONG SL & TP

stopPerlong = (close-ta.lowest(low, 10)) / ta.lowest(low, 10)   

takePerlong = if stopPerlong > 0.05 
    stopPerlong * 3
    
else if stopPerlong > 0.025
    stopPerlong * 4

else if stopPerlong < 0.025
    stopPerlong * 5

else
    na
    
// SHORT SL & TP

stopPershort = (close-ta.highest(high, 10)) / ta.highest(high, 10)    

takePershort = if stopPershort < -0.05 
    stopPerlong * 3
    
else if stopPershort < -0.025
    stopPerlong * 4

else if stopPershort > -0.025
    stopPerlong * 5

else
    na
    

// DEPLOYING TP & SL

longStop = strategy.position_avg_price * (1 - stopPerlong)
shortStop = strategy.position_avg_price * (1 + stopPershort)
shortTake = strategy.position_avg_price * (1 - takePershort)
longTake = strategy.position_avg_price * (1 + takePerlong)

if strategy.position_size > 0 
    strategy.exit(id="Close Long", stop=longStop, limit=longTake)
if strategy.position_size < 0 
    strategy.exit(id="Close Short", stop=shortStop, limit=shortTake)

这是我如何在第 5 版中设置止损和获利的快速示例。假设我想在看涨吞没形态上开仓 position,并根据我计算的止损和获利平仓。

声明声明。 我认为只有“close_entries_rule =”ANY”是你应该放置的东西,因为它似乎控制着 strategy.exit() 中的“from_entry”。

strategy(title="Bullish Engulfing Pattern", overlay=true, close_entries_rule = "ANY", precision = 16, initial_capital = 10000 , default_qty_value = 1, default_qty_type = strategy.percent_of_equity)

检测您是否打开了 position。

bool inTrade = strategy.position_size > 0

如果是这样,请计算条形,以便您知道上次条件为真的时间。

int countBars = ta.barssince(inTrade == false)

设置您的止盈。

int crv = 3

获取您的入门价格。

float entry = strategy.position_avg_price

“保存”计算止损所需的值。 ta.barssince 在您的 position 打开时开始计数。 所以你可以参考你想要的蜡烛。

float candleLow = open[countBars]
float atr = ta.atr(14)[countBars]

计算您的止损,然后计算您的获利。 我更喜欢使用 ATR 指标来定义我的止损。 在这种情况下,我在打开 position 之前使用蜡烛的开盘 (candleLow)。

float sl = candleLow - atr
float tp = entry + (crv * (entry - sl[1]))

获取两者的百分比值。

float slInPercent = 1 - ((entry - sl) / entry)
float tpInPercent = 1 + (((entry - sl) / entry) * crv)

打开你的 position

if bullishEngulfing
    strategy.entry(id = "long", direction = strategy.long)

当 position 使用百分比值打开时,设置您的 sl 和 tp 值。

if inTrade == true
    strategy.exit(id = "Exit Long", from_entry="long", stop = strategy.position_avg_price * slInPercent, limit = strategy.position_avg_price * tpInPercent, comment_loss = "Hit by stop loss", comment_profit = "Took profit!")

我希望这能给你一个想法。 最好的祝愿!

暂无
暂无

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

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