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

//@version=5
indicator(title = 'John F. Ehlers - Predictive Moving Average', shorttitle = 'predictive_moving_average', overlay = true, timeframe = '', timeframe_gaps = true)


// input
src = input.source(ohlc4, title = 'Source', group = 'Source', tooltip = 'Determines the source of input data, default to ohlc4')
band = input.float(defval = 2, title = '%Band', group = 'Value', minval = 1, maxval = 100, step = 1, tooltip = 'Determines the % of applied band')
bar_color = input.bool(defval= true, title = 'Bar Color', group = 'Options', tooltip = 'Determines whether to apply bar color, base on crossover or crossunder of predict to trigger')
dynamic_band = input.bool(defval = false, title = 'Price Band', group = 'Options', tooltip = 'Determines whether to display price band (price-corridor)')


// predictive moving average
wma1 = 0.00
wma2 = 0.00
predict = 0.00
trigger = 0.00

wma1 := (7*src+6*src[1]+5*src[2]+4*src[3]+3*src[4]+2*src[5]+src[6])/28
wma2 := (7*wma1+6*wma1[1]+5*wma1[2]+4*wma1[3]+3*wma1[4]+2*wma1[5]+wma1[6])/28

predict := (2*wma1)-wma2
trigger := (4*predict+3*predict[1]+2*predict[2]+predict)/10

series_ = predict > trigger ? predict : trigger
upper = series_*(1+(band/100))
lower = series_*(1-(band/100))


// visualize
// color condition
color_con = predict > trigger and predict > predict[1] ? color.green : color.red 
color_con_ = predict > trigger and predict > predict[1]

// bar color
barcolor(bar_color ? color_con : na)

// line type
plot(series_, color = color_con, style = plot.style_line, linewidth = 2)
p_up = plot(dynamic_band ? upper : na, color = color.new(color.yellow, 70))
p_low = plot(dynamic_band ? lower : na, color = color.new(color.yellow, 70))


// create alert
alertcondition((not color_con_[1] and color_con_), title = 'Entry', message = 'Buy/Long entry point detected')
alertcondition((color_con_[1] and not color_con_), title = 'Close', message = 'Sell/Short entry point detected')


// // strategy test
// percent_sl = input.float(defval = 3, title = 'Stop Loss', group = 'Value', tooltip = 'Determines the stop-loss percentage')
// long_condition = color_con_
// short_condition = not color_con_

// long_sl = 0.00
// long_sl := long_condition ? series_*(1-percent_sl/100) : nz(long_sl[1])

// if long_condition
//     strategy.entry(id = 'long', direction = strategy.long)
// if not long_condition
//     strategy.exit(id = 'exit-long', from_entry = 'long', stop = long_sl)

// plot(strategy.position_size > 0 ? long_sl : na, color = color.gray, style = plot.style_linebr)