根据绘制的方框创建动态止损和止盈

问题描述 投票:0回答:1

我指的是我创建的另一个问题,但我认为这样设置可能会更好。

(原始代码和问题可以在这里查找:使用范围高点和低点作为止损并将其绘制在图表上

我想实现什么:

如您所见,我在图表上绘制了一个方框。如果我进行多头交易,则应使用箱体的最低点作为止损。止盈应该始终是两倍。进入空头交易时反之亦然。

该方框将始终在上午 5:00 至上午 8:55 之间绘制。

我当前的代码是什么样的:

这是盒子的代码:

    sessionHighPrice := high
    sessionLowPrice  := low
    sessionOpenPrice := open
// Else, during the session, track the highest high and lowest low
else if inSession
    sessionHighPrice := math.max(sessionHighPrice, high)
    sessionLowPrice  := math.min(sessionLowPrice, low)

// STEP 4:
// When a session begins, make a new box for that session
if sessionStart
    sessionBox := box.new(left=bar_index, top=na, right=na, bottom=na,
         border_width=boxBorderSize)

// STEP 5:
// During the session, update that session's existing box
if inSession
    box.set_top(sessionBox, sessionHighPrice)
    box.set_bottom(sessionBox, sessionLowPrice)

    box.set_right(sessionBox, bar_index + 1)

    // See if bar closed higher than session open. When it did, make
    // box green (and use red otherwise).
    if close > sessionOpenPrice
        box.set_bgcolor(sessionBox, upBoxColor)
        box.set_border_color(sessionBox, upBorderColor)
    else
        box.set_bgcolor(sessionBox, downBoxColor)
        box.set_border_color(sessionBox, downBorderColor)

// Save Previous Max & Min
Minpreday = ta.valuewhen(SessionEnd,sessionLowPrice,0)
Maxpreday = ta.valuewhen(SessionEnd,sessionHighPrice,0)



// Look if the close time of the current bar
// falls inside the date range
inDateRange = (time >= timestamp(syminfo.timezone, startYear,
         startMonth, startDate, 0, 0)) and
     (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))

这就是我想要创建动态止损的方式:

Value_entryshort = ta.valuewhen(EntryShort == 1, Minpreday,0)
Value_sllong = ta.valuewhen(EntryLong == 1, Minpreday,0)
Value_slshort = ta.valuewhen(EntryShort == 1, Maxpreday,0)

SL_Long = (Value_entrylong-Value_sllong)*10000
SL_Short = (Value_entryshort-Value_slshort)*10000

其背后的想法是什么:

如果我进入交易,我的想法是保存最小和最大预日(BOX)。如果是这样,我将减去高低点(多头交易),反之亦然作为空头交易。由于我只关注 EURUSD,因此我可以将其乘以 10000 以获得点值。在这种情况下,我可以使用点数作为止损并将其乘以 2 以获得我的止盈点数。不幸的是它不起作用,我不知道为什么。

如果有人知道如何解决它,我会非常高兴。

pine-script pine-script-v5 trading algorithmic-trading
1个回答
0
投票
SL_Long = 0.
Tp_Long = 0.
SL_Long := box.get_bottom(sessionBox)
Tp_Long := strategy.position_avg_price + ((strategy.position_avg_price - SL_Long) * 2)
© www.soinside.com 2019 - 2024. All rights reserved.