基于绘制的方框创建动态止损和止盈

huangapple go评论82阅读模式
英文:

Create dynamic StopLoss and TakeProfit based on a plotted box

问题

以下是您要翻译的内容:

"I'm referring to another question which I have created, but I think it might be better to set it up like this.

(The original Code and Question can be looked up here: https://stackoverflow.com/questions/76860482/use-range-high-low-as-sl-and-plot-it-on-the-chart)

What do I want to implement:
基于绘制的方框创建动态止损和止盈

As you can see here, I'm plotting a box on my chart. If I enter a long trade, the lowest low of the box should be used as a stop loss. The take profit should be always twice as high. Vice versa when entering a short trade.

The box will always be plotted between 5:00am and 8:55am.

What does my current code look like:

That is the code for the box:

    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))

This is how I wanted to create a dynamic Stop Loss:

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

What was the idea behind it:

Idea was to save the Min and Maxpreday (BOX) if im entering a trade. If so, I will subtract high-low (LONG Trade) or vice versa for a Short trade. As I'm only focussing on EURUSD I can multiply it with 10000 to get the amount of pips. In that case I can use amount of pips as a SL and multiply it with 2 to get the amount of pips for my TP. Unfortunately it does not work and I dont know why.

I would be more than happy if someone has an idea on how to solve it.

英文:

I'm referring to another question which I have created, but I think it might be better to set it up like this.

(The original Code and Question can be looked up here: https://stackoverflow.com/questions/76860482/use-range-high-low-as-sl-and-plot-it-on-the-chart)

What do I want to implement:
基于绘制的方框创建动态止损和止盈

As you can see here, I'm plotting a box on my chart. If I enter a long trade, the lowest low of the box should be used as a stop loss. The take profit should be always twice as high. Vice versa when entering a short trade.

The box will always be plotted between 5:00am and 8:55am.

What does my current code look like:

That is the code for the box:

    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))

This is how I wanted to create a dynamic Stop Loss:

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

What was the idea behind it:

Idea was to save the Min and Maxpreday (BOX) if im entering a trade. If so, I will subtract high-low (LONG Trade) or vice versa for a Short trade. As I'm only focussing on EURUSD I can multiply it with 10000 to get the amount of pips. In that case I can use amount of pips as a SL and multiply it with 2 to get the amount of pips for my TP. Unfortunately it does not work and I dont know why.

I would be more than happy if someone has an idea on how to solve it.

答案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)

英文:
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)

huangapple
  • 本文由 发表于 2023年8月9日 18:21:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定