How to enter/exit strategy on Fibonacci levels?

Viewed 272

I can't make the strategy to enter the trade on the fibEntryvalue. The value is plotted on the chart, right where is supposed to be. It is some problem with exiting the strategy.

I have tried:

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

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

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

Here is the code:

//@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) ```

1 Answers

I think the problem is that you declare your variables fibTop and fibBottom without the var keyword. This causes them to reinitialize to na on each bar.
The result was that most of the time, your fibEntry was also na, causing the crossEntry to be false.

I've re-written your script (with proper indentation, should be 4 spaces or 1 tab, you had 3 spaces) and put your variables on top of the script.
I've changed the 2 mentiond variables to be declared with var.
Does this work for you?

//@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) ```

I've also changed the code to use the rather new if-else constuct instead of nested if then else.

Also, instead of using

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

You could eliminate the if by simply using

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