簡體   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