kungpao
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: June 29th, 2022

Indicator script works, but not strategy

Hello everyone. I have been coding and recoding for days (many hours in), but now I must humbly ask for guidance . Basically there is a trend condition ( shown in blue bars) and the additional entry parameter for the actual entry (white bars). As an indicator the script works and shows many trades, but as a strategy, there is one trade taken and never exited.

Also, I added the "not na()" script that I saw in one of Mathews videos, but that did not fix this either. This started as a Long and Short side strategy, but I have dissected line by line trying to figure what is wrong. Wondering if someone can tell me why this is only opening one trade opening and never exiting?

Many Thanks!

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © omoorea

//@version=5
strategy('AO Trend Entry 2.0', overlay=true)


rainbowMinSet = input.float(title='Min % between Red & Yellow EMA before entry ex. 0.14%',minval=0.10, defval=0.15)

var stopLossLongSaved = 0.0
var stopLossShortSaved = 0.0

rainbowLogicLong = false
rainbowLogicLongEntry = false


//*****************************determining the EMAs for the trend and if rainbow logic applies
//******************************also want to calculate min distance to rule out false entries
rainbowRed = ta.ema(close,3)
rainbowOrange = ta.ema(close,8)
rainbowYellow = ta.ema(close, 13)
rainbowGreen = ta.ema(close,21)

//*******************************added this to test the not na condition Matthew spoke about
rainbowRedValid = not na(rainbowRed)
rainbowOrangeValid = not na(rainbowRed)
rainbowYellowValid = not na(rainbowRed)
rainbowGreenValid = not na(rainbowRed)
allValid = false

rainbowMinLong = 0.0
rainbowMinShort = 0.0


//******trend is valid????********
if rainbowRedValid and rainbowOrangeValid and rainbowYellowValid and rainbowGreenValid
allValid:=true
rainbowMinLong:= (math.abs(rainbowRed - rainbowYellow)/rainbowRed)*100
rainbowMinShort:= (math.abs(rainbowRed - rainbowYellow)/rainbowRed)*100


// **********Long Entry ----Rainbow Logic and Minimum distance

if barstate.isconfirmed and rainbowRed > rainbowOrange and rainbowOrange > rainbowYellow and allValid
rainbowLogicLong:=true
stopLossLongSaved:=rainbowYellow


if rainbowLogicLong and rainbowMinLong >= rainbowMinSet
rainbowLogicLongEntry:=true


if rainbowLogicLongEntry
strategy.entry("long", strategy.long, 100)


strategy.exit("Long Exit", from_entry="Long", stop=stopLossLongSaved)

//***************************************changes barcolor if rainbow logic is true****************************************
barcolor(rainbowLogicLongEntry ? color.white : rainbowLogicLong ? color.blue:rainbowLogicShortEntry ? color.gray : rainbowLogicShort ? color.fuchsia: close > open ? color.green : color.red)

plot(rainbowRed,color=color.red)
plot(rainbowOrange, color=color.orange)
plot(rainbowYellow, color=color.yellow)
plot(rainbowGreen, color=color.green)
plot(stopLossLongSaved, color=color.blue)

Return to “Pine Script Q&A”