elcounto
Pine Script Rookie
Pine Script Rookie
Posts: 8
Joined: February 5th, 2023

Help with my first pine script

Hey all,

My name is Phil and I've been trading for a little over a year now. I trade futures on the S&P500 e-mini using a prop firm. I've been using a Hull MA indicator and I find it quite useful in my trading. I'm trying to convert it to a strategy but can't get the buy and sell conditions to work. Script is below and any help would be apricated.

//@version=5
strategy(title='Hull MA & Warning Zones & Buy/Sell Arrows', overlay=true)

//inputs
n = input(title='MA period', defval=9) //period
el = input(true, title='Show 1st MA - entry line') //show 1nd HMA entry line
trl = input(false, title='Show 2nd MA - trendline') //show 2nd HMA trendline
pld = input(true, title='Show Downtrend - red line') //Show Downtrend
plu = input(true, title='Show Uptrend - green line') //Show Uptrend
plr = input(true, title='Show Reversing trend - yellow line') //Show trend change
plb = input(true, title='Show Line background - black line') //Show Line background
pla = input(false, title='Show buy/sell arrows') //Show buy/sell arrows
useCurrentRes = input(false, title='Use Current Chart Resolution for 2nd MA') //use custom resolution
resCustom = input.timeframe(title='Timeframe for 2nd MA, Standart - 4h', defval='240') //resolution for 2nd HMA
res = useCurrentRes ? timeframe.period : resCustom

//functions
n2ma = 2 * ta.wma(close, math.round(n / 2))
nma = ta.wma(close, n)
diff = n2ma - nma
sqn = math.round(math.sqrt(n))
n2ma1 = 2 * ta.wma(close[1], math.round(n / 2))
nma1 = ta.wma(close[1], n)
diff1 = n2ma1 - nma1
sqn1 = math.round(math.sqrt(n))
n1 = ta.wma(diff, sqn)
n2 = ta.wma(diff1, sqn)
n3 = n1 - n1 * -1
n4 = n1 + n1

//colors
dif = n1[1] - n3
dif1 = dif > dif[1] and dif[1] > dif[2] ? na : color.lime //uptrend - green
dif3 = n4 - n1[1]
dif2 = dif3 > dif3[1] and dif3[1] > dif3[2] ? na : color.red //downtrend - red
dif4 = (dif > dif[1] and dif[1] > dif[2]) == (dif3 > dif3[1] and dif3[1] > dif3[2]) ? color.yellow : na //trend change - yellow
c = n1 > n2 ? color.lime : color.red //fill red/lime

//periods
outn1 = request.security(syminfo.tickerid, res, n1) //MA custom res
outc = request.security(syminfo.tickerid, res, c) //fill red/lime custom res
outdif = request.security(syminfo.tickerid, res, dif)
outdif1 = request.security(syminfo.tickerid, res, dif1) //uptrend green custom res
outdif2 = request.security(syminfo.tickerid, res, dif2) //downtrend red custom res
outdif3 = request.security(syminfo.tickerid, res, dif3)
outdif4 = request.security(syminfo.tickerid, res, dif4) //trend change yellow custom res

//plots - 2nd line
sma = plot(trl and outn1 ? outn1 : na, color=outdif1, linewidth=25, transp=80) //2nd green
sma2 = plot(trl and outn1 ? outn1 : na, color=outdif2, linewidth=25, transp=80) //2nd red
sma3 = plot(trl and outn1 ? outn1 : na, color=outdif4, linewidth=25, transp=90) //2nd yellow

//plots - 1st line
mab = plot(el and n1 and plb ? n1 : na, color=color.new(color.black, 0), linewidth=4, title='Background line') //black
ma = plot(el and n1 and plu ? n1 : na, color=dif1, linewidth=2, transp=10) //green
ma2 = plot(el and n1 and pld ? n1 : na, color=dif2, linewidth=2, transp=20) //red
ma3 = plot(el and n1 and plr ? n1 : na, color=dif4, linewidth=2, transp=10) //yellow

long = dif < dif[1] and dif[1] >= dif[2] //long condition
short = dif3 < dif3[1] and dif3[1] >= dif3[2] //short condition

//plots - arrows and alerts
longarrow = long == 1 ? 1 : na
shortarrow = short == 1 ? -1 : na
plotarrow(pla and longarrow ? longarrow : na, colorup=color.new(color.aqua, 50))
plotarrow(pla and shortarrow ? shortarrow : na, colordown=color.new(color.red, 50))

// Buy condition
if (c[1] == color.lime)
strategy.entry("Long", strategy.long)

// Sell condition
if (c[1] == color.red)
strategy.close("Long", "Long")

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Help with my first pine script

Hey Phil,

I'll look into it and let you know shortly.

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Help with my first pine script

Interesting approach using colors as the buy and sell condition.

The only thing that needed fixing for it to work was the strategy.entry and strategy.close functions needed to be tabbed in 4 spaces. You also have warning messages on your plot statements which can be fixed with the following syntax:

sma = plot(trl and outn1 ? outn1 : na, color=color.new(outdif1,80), linewidth=25) //2nd green

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Help with my first pine script

If you wanted to differentiate between buy and sell comments on the chart you could use the following:

// Buy condition
if (c[1] == color.lime)
strategy.entry("Long", strategy.long, comment="Buy Long")

// Sell condition
if (c[1] == color.red)
strategy.close("Long", "Sell Long")

elcounto
Pine Script Rookie
Pine Script Rookie
Posts: 8
Joined: February 5th, 2023

Re: Help with my first pine script

Hi Steve,

Thanks for taking the time out to reply. You were spot on with your recommendation. I've ran the script but it's not doing what I expected it to, the buy and sell conditions to align to the buy/sell arrows on the charts. I'm not sure what's gone wrong!
Image

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Help with my first pine script

Hey,

A quick look at the code, shows that you are using different logical checks for the arrows, and different checks for the entries.

If you would like them to match as per the arrows than :
Change the buy and sell to

Code: Select all

// Buy condition
if (long)
    strategy.entry("Long", strategy.long)

// Sell condition
if (short)
    strategy.close("Long", "Long")

elcounto
Pine Script Rookie
Pine Script Rookie
Posts: 8
Joined: February 5th, 2023

Re: Help with my first pine script

Ah great thanks for that it works fine now. Something I've noticed though is that it opens the trade on the start of the next candle. Is there anyway to open a trade when it actually alerts on the candle, or say when it moves a point in the correct direction?

elcounto
Pine Script Rookie
Pine Script Rookie
Posts: 8
Joined: February 5th, 2023

Re: Help with my first pine script

I've flipped the buy and sell conditions for this script and managed to get a 66% win rate from the start of the year which is pretty hot for the future ES. The only issue is if a trade goes against me it goes big time so I'm looking to add a stop lose of around .20% or $375. I've put in the below strat but but it's not working. Any advise would be helpful. Full script is below.

Also the ma, ma2 colors are not plotting on the chart but the ma3 one is, weird.

// Sell condition
if (strategy.position_size > 0 and strategy.equity / strategy.position_avg_price - 1 <= -0.20)
strategy.close_all("Stop Loss")


//@version=5
strategy(title='Hull MA & Warning Zones & Buy/Sell Arrows', initial_capital=2000, overlay=true)


//inputs
startDate = input.int(title="Start Date", defval=1, minval=1, maxval=31)
startMonth = input.int(title="Start Month", defval=2, minval=1, maxval=12)
startYear = input.int(title="Start Year", defval=2023, minval=1800, maxval=2100)

n = input(title='MA period', defval=28) //period
el = input(true, title='Show 1st MA - entry line') //show 1nd HMA entry line
trl = input(false, title='Show 2nd MA - trendline') //show 2nd HMA trendline
pld = input(true, title='Show Downtrend - red line') //Show Downtrend
plu = input(true, title='Show Uptrend - green line') //Show Uptrend
plr = input(true, title='Show Reversing trend - yellow line') //Show trend change
plb = input(true, title='Show Line background - black line') //Show Line background
pla = input(false, title='Show buy/sell arrows') //Show buy/sell arrows
useCurrentRes = input(false, title='Use Current Chart Resolution for 2nd MA') //use custom resolution
resCustom = input.timeframe(title='Timeframe for 2nd MA, Standart - 4h', defval='240') //resolution for 2nd HMA
res = useCurrentRes ? timeframe.period : resCustom

//functions
n2ma = 2 * ta.wma(close, math.round(n / 2))
nma = ta.wma(close, n)
diff = n2ma - nma
sqn = math.round(math.sqrt(n))
n2ma1 = 2 * ta.wma(close[1], math.round(n / 2))
nma1 = ta.wma(close[1], n)
diff1 = n2ma1 - nma1
sqn1 = math.round(math.sqrt(n))
n1 = ta.wma(diff, sqn)
n2 = ta.wma(diff1, sqn)
n3 = n1 - n1 * -1
n4 = n1 + n1

//colors
dif = n1[1] - n3
dif1 = dif > dif[1] and dif[1] > dif[2] ? na : color.lime //uptrend - green
dif3 = n4 - n1[1]
dif2 = dif3 > dif3[1] and dif3[1] > dif3[2] ? na : color.red //downtrend - red
dif4 = (dif > dif[1] and dif[1] > dif[2]) == (dif3 > dif3[1] and dif3[1] > dif3[2]) ? color.yellow : na //trend change - yellow
c = n1 > n2 ? color.lime : color.red //fill red/lime

//periods
outn1 = request.security(syminfo.tickerid, res, n1) //MA custom res
outc = request.security(syminfo.tickerid, res, c) //fill red/lime custom res
outdif = request.security(syminfo.tickerid, res, dif)
outdif1 = request.security(syminfo.tickerid, res, dif1) //uptrend green custom res
outdif2 = request.security(syminfo.tickerid, res, dif2) //downtrend red custom res
outdif3 = request.security(syminfo.tickerid, res, dif3)
outdif4 = request.security(syminfo.tickerid, res, dif4) //trend change yellow custom res

//plots - 2nd line
sma = plot(trl and outn1 ? outn1 : na, color=color.new(outdif1,80), linewidth=25) //2nd green
sma2 = plot(trl and outn1 ? outn1 : na, color=color.new(outdif2,80), linewidth=25) //2nd red
sma3 = plot(trl and outn1 ? outn1 : na, color=color.new(outdif4,80), linewidth=25) //2nd yellow

//plots - 1st line
mab = plot(el and n1 and plb ? n1 : na, color=color.new(color.black, 0), linewidth=4, title='Background line') //black
ma = plot(el and n1 and plu ? n1 : na, color=color.new(dif1,10), linewidth=2) //green
ma2 = plot(el and n1 and pld ? n1 : na, color=color.new(dif2,20), linewidth=2) //red
ma3 = plot(el and n1 and plr ? n1 : na, color=color.new(dif4,10), linewidth=2) //yellow

long = dif < dif[1] and dif[1] >= dif[2] //long condition
short = dif3 < dif3[1] and dif3[1] >= dif3[2] //short condition

//plots - arrows and alerts
longarrow = long == 1 ? 1 : na
shortarrow = short == 1 ? -1 : na
plotarrow(pla and longarrow ? longarrow : na, colorup=color.new(color.aqua, 50))
plotarrow(pla and shortarrow ? shortarrow : na, colordown=color.new(color.red, 50))

// Buy condition
if (short)
strategy.entry("Long", strategy.long, comment="Buy Long")
if (long)
strategy.entry("Short", strategy.short, comment="Buy Short")

// Sell condition
if (strategy.position_size > 0 and strategy.equity / strategy.position_avg_price - 1 <= -0.20)
strategy.close_all("Stop Loss")

if (short)
strategy.close("Short", "Sell Short")
if (long)
strategy.close("Long", "Sell Long")

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Help with my first pine script

Hi Phil,

Firstly your stop loss calculation seems to be incorrect. You have it as strategy.equity / strategy.position_avg_price - 1 <= -0.20. But a stop loss would normally be a specific price that could be a certain number of pips below or above the entry price (depending on direction) or a priced based on % of equity. I can't correlate your formula as a stop level unless I'm missing something.

Also, I find it best to use the strategy.exit statement as it has a stop parameter that exits if the stop_price is hit.

if (short)
strategy.exit(id='Short', from_entry='Short', stop=stop_price)

if (long)
strategy.exit(id='Long', from_entry='Long', stop=stop_price)

I hope this helps!

Return to “Pine Script Q&A”