Pinescript to MQL4

 

Hi,

I'm in the process of translating a TradingView strategy to MQL4 to run as an EA and see if it's actually profitable.

I've written all of the 'skeleton' of the EA, ie. inputs, risk management, position entries, trailing stop etc.

There's just 1 of the entry criteria I'm struggling to translate.


The strategy basically enters buy/sell positions when trendlines are broken. Heres the pinescript segment im struggling with:


// Pivots

ph = pivothigh(high, leftbars, rightbars)

pl = pivotlow(low, leftbars, rightbars)


phv1 = valuewhen(ph, high[rightbars], 0)

phb1 = valuewhen(ph, bar_index[rightbars], 0)

phv2 = valuewhen(ph, high[rightbars], 1)

phb2 = valuewhen(ph, bar_index[rightbars], 1)


plv1 = valuewhen(pl, low[rightbars], 0)

plb1 = valuewhen(pl, bar_index[rightbars], 0)

plv2 = valuewhen(pl, low[rightbars], 1)

plb2 = valuewhen(pl, bar_index[rightbars], 1)


// TRENDLINE CODE

// --------------

get_slope(x1,x2,y1,y2)=>

    m = (y2-y1)/(x2-x1)

 

get_y_intercept(m, x1, y1)=>

    b=y1-m*x1


get_y(m, b, ts)=>

    Y = m * ts + b


int   res_x1 = na

float res_y1 = na

int   res_x2 = na

float res_y2 = na


int   sup_x1 = na

float sup_y1 = na

int   sup_x2 = na

float sup_y2 = na


// Resistance

res_x1 := ph ? phb1 : res_x1[1]

res_y1 := ph ? phv1 : res_y1[1]

res_x2 := ph ? phb2 : res_x2[1]

res_y2 := ph ? phv2 : res_y2[1]


res_m = get_slope(res_x1,res_x2,res_y1,res_y2)

res_b = get_y_intercept(res_m, res_x1, res_y1)

res_y = get_y(res_m, res_b, bar_index)


// Support

sup_x1 := pl ? plb1 : sup_x1[1]

sup_y1 := pl ? plv1 : sup_y1[1]

sup_x2 := pl ? plb2 : sup_x2[1]

sup_y2 := pl ? plv2 : sup_y2[1]


sup_m = get_slope(sup_x1,sup_x2,sup_y1,sup_y2)

sup_b = get_y_intercept(sup_m, sup_x1, sup_y1)

sup_y = get_y(sup_m, sup_b, bar_index)


// Breaks

long_break = crossunder(close, res_y)

short_break = crossover(close, sup_y)


Any help would be greatly appreciated!

Reason: