首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为Fractals添加条件在策略中不起作用

为Fractals添加条件在策略中不起作用
EN

Stack Overflow用户
提问于 2022-07-01 14:04:28
回答 1查看 64关注 0票数 0

我试图通过使用特定的分形来设置策略的停止损失,定义如下:LongSL: ((low[4] > low[3] and low[3] > low[2] and low[2] < low[1] and low[1] < low[0]) and (low[2]>SmaConditionFractal and ADX>30 ) )

ShortSL= ( (high[4] < high[3] and high[3] < high[2] and high[2] > high[1] and high[1] > high[0]) and (high[2]<sma(close,50) and ADX>30 ) )

我测试了简单的移动平均交叉策略,脚本工作在第一步,长时fastEMA>slowEMA (退出时close<last LongSL )和短时fastEMA<slowEMA (退出时close>ShortSL)。

问题是,当现有交易获利或停业时,多笔交易在长/短期退出后立即发生,我不希望有策略来做这些交易(Picture)。

脚本

代码语言:javascript
运行
复制
//@version=4

strategy(" Fractal SMA ADX 2", overlay=true)
           //////////////////////////ADX
//Average Directional Index Calculation (ADX)
lengthADX =input(title="lengthADX", type=input.integer, defval=14)
threshold =  input(title="ADX threshold Exit", type=input.integer, defval=30)
TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0

SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/lengthADX) + TrueRange

SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/lengthADX) + DirectionalMovementPlus

SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/lengthADX) + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
ADX = sma(DX, lengthADX)

//Frctals Above/Below Desired Moving Average 
SmaConditionFractal             =       sma(close, 55)

filteredtopf = ( (high[4] < high[3] and high[3] < high[2] and high[2] > high[1] and high[1] > high[0])    and  (high[2]<SmaConditionFractal  and  ADX>threshold )   ) ? high[2]:na

plotshape(filteredtopf, title='Filtered Top Fractals', style=shape.triangledown,size=size.small, location=location.abovebar, color=color.red,offset=-2)


filteredbotf =((low[4] > low[3] and low[3] > low[2] and low[2] < low[1] and low[1] < low[0])   and  (low[2]>SmaConditionFractal  and  ADX>threshold )   ) ?low[2]:na
plotshape(filteredbotf, title='Filtered Bottom Fractals', style=shape.triangleup, size=size.small,location=location.belowbar, color=color.lime,offset=-2)


// Define a 5-bar fractals greater than sma55 and adx>30

upFractal =  ( (high[4] < high[3] and high[3] < high[2] and high[2] > high[1] and high[1] > high[0])    and  (high[2]<SmaConditionFractal  and  ADX>threshold )   )
dnFractal =((low[4] > low[3] and low[3] > low[2] and low[2] < low[1] and low[1] < low[0])   and  (low[2]>SmaConditionFractal  and  ADX>threshold )   )
// Store the fractal value in a variable

var float holdLastHigh = na
var float holdLastLow = na

// ————— hi/lo to save needs to be indexed.
if upFractal
    holdLastHigh := high[2]
if dnFractal
    holdLastLow := low[2]

    
    //Entry COnditions
fastEMA = ema(close, 14)
slowEMA = ema(close, 28)
longCondition=fastEMA>slowEMA 
shortCondition=fastEMA<slowEMA 



// Set your stop-loss
var  float  stop_long_FB = na
var  float stop_short_FB = na
stop_long_FB :=longCondition and dnFractal?low[2]:stop_long_FB[1]
stop_short_FB := shortCondition and upFractal? high[2]:stop_short_FB[1]

//Strategy Execution
if longCondition
    strategy.entry("long",strategy.long)
    strategy.exit("Stop Loss/TP", "long", stop = stop_long_FB,limit=1000000)

if shortCondition
    strategy.entry("short",strategy.short)
    strategy.exit("Stop Loss/TP", "short", stop = stop_short_FB,limit=0.0)
longexit=crossunder(fastEMA,slowEMA)
if longexit
    strategy.close("long",when=longexit)
``
EN

回答 1

Stack Overflow用户

发布于 2022-07-01 14:49:26

而不是使用

代码语言:javascript
运行
复制
longCondition=fastEMA>slowEMA 
shortCondition=fastEMA<slowEMA 

您可以像下面这样使用交叉

代码语言:javascript
运行
复制
longCondition=crossover(fastEMA,slowEMA)
shortCondition=crossover(slowEMA,fastEMA) 

并且每次更改stoploss的条件来计算它,下面都是完整的代码。

代码语言:javascript
运行
复制
//@version=4

strategy(" Fractal SMA ADX 2", overlay=true)
           //////////////////////////ADX
//Average Directional Index Calculation (ADX)
lengthADX =input(title="lengthADX", type=input.integer, defval=14)
threshold =  input(title="ADX threshold Exit", type=input.integer, defval=30)
TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))
DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0
DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0

SmoothedTrueRange = 0.0
SmoothedTrueRange := nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/lengthADX) + TrueRange

SmoothedDirectionalMovementPlus = 0.0
SmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/lengthADX) + DirectionalMovementPlus

SmoothedDirectionalMovementMinus = 0.0
SmoothedDirectionalMovementMinus := nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/lengthADX) + DirectionalMovementMinus

DIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100
DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100
DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100
ADX = sma(DX, lengthADX)

//Frctals Above/Below Desired Moving Average 
SmaConditionFractal             =       sma(close, 55)

filteredtopf = ( (high[4] < high[3] and high[3] < high[2] and high[2] > high[1] and high[1] > high[0])    and  (high[2]<SmaConditionFractal  and  ADX>threshold )   ) ? high[2]:na

plotshape(filteredtopf, title='Filtered Top Fractals', style=shape.triangledown,size=size.small, location=location.abovebar, color=color.red,offset=-2)


filteredbotf =((low[4] > low[3] and low[3] > low[2] and low[2] < low[1] and low[1] < low[0])   and  (low[2]>SmaConditionFractal  and  ADX>threshold )   ) ?low[2]:na
plotshape(filteredbotf, title='Filtered Bottom Fractals', style=shape.triangleup, size=size.small,location=location.belowbar, color=color.lime,offset=-2)


// Define a 5-bar fractals greater than sma55 and adx>30

upFractal =  ( (high[4] < high[3] and high[3] < high[2] and high[2] > high[1] and high[1] > high[0])    and  (high[2]<SmaConditionFractal  and  ADX>threshold )   )
dnFractal =((low[4] > low[3] and low[3] > low[2] and low[2] < low[1] and low[1] < low[0])   and  (low[2]>SmaConditionFractal  and  ADX>threshold )   )
// Store the fractal value in a variable

var float holdLastHigh = na
var float holdLastLow = na

// ————— hi/lo to save needs to be indexed.
if upFractal
    holdLastHigh := high[2]
if dnFractal
    holdLastLow := low[2]

    
    //Entry COnditions
fastEMA = ema(close, 14)
slowEMA = ema(close, 28)
longCondition=crossover(fastEMA,slowEMA)
shortCondition=crossover(slowEMA,fastEMA)



// Set your stop-loss
var  float  stop_long_FB = na
var  float stop_short_FB = na
stop_long_FB :=fastEMA>slowEMA and dnFractal?low[2]:stop_long_FB[1]
stop_short_FB := fastEMA<slowEMA  and upFractal? high[2]:stop_short_FB[1]

//Strategy Execution
if longCondition
    strategy.entry("long",strategy.long)
if strategy.position_size > 0
    strategy.exit("Stop Loss/TP", "long", stop = stop_long_FB,limit=1000000)

if shortCondition
    strategy.entry("short",strategy.short)
if strategy.position_size < 0
    strategy.exit("Stop Loss/TP", "short", stop = stop_short_FB,limit=0.0)
longexit=crossunder(fastEMA,slowEMA)
if longexit
    strategy.close("long",when=longexit)

这将确保在退出后直到下一个交叉发生时才会获得多个条目。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72830487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档