Balance of Power Heikin Ashi Investing Strategy 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © exlux99

//@version=5
strategy(title="Balance of Power Heikin Ashi Swing Strategy")


x_close = request.security(ticker.heikinashi("SPY"), "M", close)
x_open = request.security(ticker.heikinashi("SPY"), "M", open)
x_high =request.security(ticker.heikinashi("SPY"), "M", high)
x_low = request.security(ticker.heikinashi("SPY"), "M", low)

gog = (x_close - x_open) / (x_high - x_low)

out = ta.ema(gog,input.int(252))

plot(out, color = color.white)

hline(0)


length = input(75)
p = input.int(99, 'Percentage', minval=0, maxval=100)
p2 = input.int(1, 'Percentage', minval=0, maxval=100)


pnr = ta.percentile_nearest_rank(out, length, p)
pnr2 = ta.percentile_nearest_rank(out, length, p2)


long =  pnr > pnr[1] and ( out > 0 or (out >-0.35 and out[1] <-0.35))  
short = (out < 0 and ( pnr < pnr[1] and pnr2 < pnr2[1]))  or ( out < 0.6 and out[1] > 0.6)


strategy.entry("long",strategy.long,when=long)
strategy.entry("short",strategy.short,when=short) 

