【问题标题】:How to enter/exit strategy on Fibonacci levels?如何在斐波那契水平上进入/退出策略?
【发布时间】:2020-09-26 17:34:07
【问题描述】:

我无法制定以fibEntryvalue 进行交易的策略。该值绘制在图表上,就在应该在的位置。退出策略有点问题。

我试过了:

strategy.entry(id="EL", when=fibEntry)

crossEntry = cross(close, fibEntry[1])
crossEntrySince = barssince(crossEntry) < 5

if crossEntry
    strategy.entry(id="Long", long=true)

代码如下:

//@version=4

strategy(title="First baby study / long position / trade set up #2 ", shorttitle="F.B.S.:long #2", overlay=true)


//$$$$$$$$$$$$$$$$$$$$$$$ Step 1. Highs and Lows $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


pHigh = pivothigh(1, 1)
pLow = pivotlow(1, 1)


//$$$$$$$$$$$$$$$$$$$$$$$$$ Step 2. Fibonacci $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

//$$$$$$$$$$$$$$$$$$$$$$$ Step 2.1 Fibonacci Top $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


float fibTop = na

if pHigh > pHigh[2] or pHigh > pHigh[3] or pHigh > pHigh[4] or pHigh > pHigh[5]

  or pHigh > pHigh[6] or pHigh > pHigh[7] or pHigh > pHigh[8] or pHigh > pHigh[9]
  

   if close[1] > open[1]
   
       fibTop := close[1]
   
   if open[1] > close[1]
   
       fibTop := open[1]


//$$$$$$$$$$$$$$$$$$$$$$ Step 2.2. Fibonacci Bottom $$$$$$$$$$$$$$$$$$$$$$$$$$$$


float fibBottom = na

if pLow > pLow[2] or pLow > pLow[3] or pLow > pLow[4] or pLow > pLow[5]

  or pLow > pLow[6] or pLow > pLow[7] or pLow > pLow[8] or pLow > pLow[9]
  
  or pLow > pLow[10] or pLow > pLow[11] or pLow > pLow[12] or pLow > pLow[13] 
   
  or pLow > pLow[14] or pLow > pLow[15]
   
   if close[1] > open[1]
       
       fibBottom := open[1]
       
   if open[1] > close[1]
       
       fibBottom := close[1]


//$$$$$$$$$$$$$$$$$$$ Step 2.3. Fibonacci 0.272 - Entry Levl $$$$$$$$$$$$$$$$$$$


var float fibEntry = na

if barstate.isconfirmed and fibBottom[1] 
   fibEntry := fibTop - 0.272 * (fibTop - fibBottom[1])
else
   if barstate.isconfirmed and fibBottom[2] 
       fibEntry := fibTop - 0.272 * (fibTop - fibBottom[2])
   else
       if barstate.isconfirmed and fibBottom[3] 
           fibEntry := fibTop - 0.272 * (fibTop - fibBottom[3])
        
 
//$$$$$$$$$$$$$$$ Step 2.4. Fibbonacci -0.272 - Take Profit Level $$$$$$$$$$$$$$


var float fibProfit = na

if barstate.isconfirmed and fibBottom[1]
   fibProfit := (fibTop - fibBottom[1]) * 0.272 + fibTop
else
   if barstate.isconfirmed and fibBottom[2]
       fibProfit := (fibTop - fibBottom[2]) * 0.272 + fibTop    
   else
       if barstate.isconfirmed and fibBottom[3]
           fibProfit := (fibTop - fibBottom[3]) * 0.272 + fibTop
     

//$$$$$$$$$$$$$$$$$ Step 2.5. Fibonacci 0.786 - Stop Loss Level $$$$$$$$$$$$$$$$


var float fibSL = na

if barstate.isconfirmed and fibBottom[1] 
   fibSL := fibTop - 0.786 * (fibTop - fibBottom[1])
else
   if barstate.isconfirmed and fibBottom[2] 
       fibSL := fibTop - 0.786 * (fibTop - fibBottom[2])
   else
       if barstate.isconfirmed and fibBottom[3] 
           fibSL := fibTop - 0.786 * (fibTop - fibBottom[3])
       
//$$$$$$$$$$$$$$$$$$$$$$$$$$ Step 3. Enter Trade $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


crossEntry = cross(close, fibEntry)

crossEntrySince = barssince(crossEntry) < 5

if crossEntrySince
   strategy.entry(id="Long", long=true)


//$$$$$$$$$$$$$$$$$$$$$$$$$$$ Step 4. Take Profit $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

// crossProfit = cross(high, fibProfit)
// if barssince(fibProfit) < 5
//     strategy.exit(id="Long")


//$$$$$$$$$$$$$$$$$$$$$$$$$$$$ Step 5. Stop Loss $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


crossSL = cross(low, fibSL)
if barssince(fibSL) < 7
   strategy.close(id="Long")


//$$$$$$$$$$$$$$$$$$$$$$$$$$ Step 6. Plotting $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


plot(fibEntry, transp=50, style=plot.style_circles, linewidth=5, color=#ffe504)
plot(fibProfit, transp=50, style=plot.style_circles, linewidth=5, color=#00ff0a)
plot(fibSL, transp=50, style=plot.style_circles, linewidth=5, color=#00ffe7)
plot(fibTop, color=color.green, offset=-1)
plot(fibBottom, color=color.red, offset=-1)
// plot(pHigh, color=color.green, transp=50,style=plot.style_circles, linewidth=5, offset=-1)
// plot(pLow, color=color.red, transp=50, style=plot.style_circles, linewidth=5, offset=-1) ```

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    我认为问题在于您在声明变量 fibTopfibBottom 时没有使用 var 关键字。这会导致它们在每个柱上重新初始化为 na
    结果是大多数时候,您的fibEntry 也是na,导致crossEntry 成为false

    我已经重写了您的脚本(使用适当的缩进,应该是 4 个空格或 1 个制表符,您有 3 个空格)并将您的变量放在脚本的顶部。
    我已将提到的 2 个变量更改为使用 var 声明。
    这对你有用吗?

    //@version=4
    strategy(title="First baby study / long position / trade set up #2 ", shorttitle="F.B.S.:long #2", overlay=true)
    
    var float fibEntry = na
    var float fibProfit = na
    var float fibSL = na
    
    // float fibTop = na
    // float fibBottom = na
    var float fibTop = na
    var float fibBottom = na
    
    //$$$$$$$$$$$$$$$$$$$$$$$ Step 1. Highs and Lows $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    pHigh = pivothigh(1, 1)
    pLow = pivotlow(1, 1)
    
    //$$$$$$$$$$$$$$$$$$$$$$$$$ Step 2. Fibonacci $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    //$$$$$$$$$$$$$$$$$$$$$$$ Step 2.1 Fibonacci Top $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    if pHigh > pHigh[2] or pHigh > pHigh[3] or pHigh > pHigh[4] or pHigh > pHigh[5] or pHigh > pHigh[6] or pHigh > pHigh[7] or pHigh > pHigh[8] or pHigh > pHigh[9]
        if close[1] > open[1]
            fibTop := close[1]
        if open[1] > close[1]
            fibTop := open[1]
    
    //$$$$$$$$$$$$$$$$$$$$$$ Step 2.2. Fibonacci Bottom $$$$$$$$$$$$$$$$$$$$$$$$$$$$
    if pLow > pLow[2] or pLow > pLow[3] or pLow > pLow[4] or pLow > pLow[5] or pLow > pLow[6] or pLow > pLow[7] or pLow > pLow[8] or pLow > pLow[9] or pLow > pLow[10] or pLow > pLow[11] or pLow > pLow[12] or pLow > pLow[13] or pLow > pLow[14] or pLow > pLow[15]
        if close[1] > open[1]
            fibBottom := open[1]
        if open[1] > close[1]
            fibBottom := close[1]
    
    //$$$$$$$$$$$$$$$$$$$ Step 2.3. Fibonacci 0.272 - Entry Levl $$$$$$$$$$$$$$$$$$$
    
    //if barstate.isconfirmed and fibBottom[1] 
    if fibBottom[1] 
        fibEntry := fibTop - 0.272 * (fibTop - fibBottom[1])
    //else if barstate.isconfirmed and fibBottom[2] 
    else if fibBottom[2] 
        fibEntry := fibTop - 0.272 * (fibTop - fibBottom[2])
    //else if barstate.isconfirmed and fibBottom[3] 
    else if fibBottom[3] 
        fibEntry := fibTop - 0.272 * (fibTop - fibBottom[3])
    
    //$$$$$$$$$$$$$$$ Step 2.4. Fibbonacci -0.272 - Take Profit Level $$$$$$$$$$$$$$
    
    // if barstate.isconfirmed and fibBottom[1]
    if fibBottom[1]
        fibProfit := (fibTop - fibBottom[1]) * 0.272 + fibTop
    // else if barstate.isconfirmed and fibBottom[2]
    else if fibBottom[2]
        fibProfit := (fibTop - fibBottom[2]) * 0.272 + fibTop    
    // else if barstate.isconfirmed and fibBottom[3]
    else if fibBottom[3]
        fibProfit := (fibTop - fibBottom[3]) * 0.272 + fibTop
    
    //$$$$$$$$$$$$$$$$$ Step 2.5. Fibonacci 0.786 - Stop Loss Level $$$$$$$$$$$$$$$$
    
    // if barstate.isconfirmed and fibBottom[1] 
    if fibBottom[1] 
        fibSL := fibTop - 0.786 * (fibTop - fibBottom[1])
    // else if barstate.isconfirmed and fibBottom[2] 
    else if fibBottom[2] 
        fibSL := fibTop - 0.786 * (fibTop - fibBottom[2])
    // else if barstate.isconfirmed and fibBottom[3] 
    else if fibBottom[3] 
        fibSL := fibTop - 0.786 * (fibTop - fibBottom[3])
    
    //$$$$$$$$$$$$$$$$$$$$$$$$$$ Step 3. Enter Trade $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    crossEntry = cross(close, fibEntry)
    crossEntrySince = barssince(crossEntry) < 5
    
    if crossEntrySince
        strategy.entry(id="Long", long=true)
    
    //$$$$$$$$$$$$$$$$$$$$$$$$$$$ Step 4. Take Profit $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    // crossProfit = cross(high, fibProfit)
    // if barssince(fibProfit) < 5
    //     strategy.exit(id="Long")
    
    //$$$$$$$$$$$$$$$$$$$$$$$$$$$$ Step 5. Stop Loss $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    crossSL = cross(low, fibSL)
    if barssince(fibSL) < 7
        strategy.close(id="Long")
    
    //$$$$$$$$$$$$$$$$$$$$$$$$$$ Step 6. Plotting $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
    plot(fibEntry, title="fibEntry", transp=50, style=plot.style_circles, linewidth=5, color=#ffe504)
    plot(fibProfit, title="fibProfit", transp=50, style=plot.style_circles, linewidth=5, color=#00ff0a)
    plot(fibSL, title="fibSL", transp=50, style=plot.style_circles, linewidth=5, color=#00ffe7)
    plot(fibTop, title="fibTop", color=color.green, offset=-1)
    plot(fibBottom, title="fibBottom", color=color.red, offset=-1)
    // plot(pHigh, color=color.green, transp=50,style=plot.style_circles, linewidth=5, offset=-1)
    // plot(pLow, color=color.red, transp=50, style=plot.style_circles, linewidth=5, offset=-1) ```
    

    我还更改了代码以使用相当新的if-else 构造,而不是嵌套的 if then else。

    另外,而不是使用

    if crossEntrySince
        strategy.entry(id="Long", long=true)
    

    您可以通过简单地使用消除if

    strategy.entry(id="Long", long=strategy.long, when=crossEntrySince)
    

    【讨论】:

    • 谢谢你,@BjornMistiaen。不幸的是,它没有帮助。我什至复制并粘贴了您的代码以查看是否存在差异,但结果是一些。就在这一次,我收到了许多虚假条目。并且脚本仍然没有使用fibEntry 作为入口值。感谢 if-else 构造。我不知道。
    猜你喜欢
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 2019-03-25
    • 2015-06-05
    相关资源
    最近更新 更多