You are on the wrong track lol.
Remember that to determine trend, you need to compare 2 moving averages. A single moving average by itself doesn’t really tell you much. The most common method is to use 2 moving average…a slow one and a fast one, eg 50 & 100. When the fast MA crosses above the the slow, then we have an uptrend and vice versa
The basic task progamatically is to determine when they cross. You do this by assigning 4 MA values to variables.
MA(50) _1 = moving average last bar;
MA(50)_2 = moving average current bar
MA(100)_1 = moving average last bar
MA(100)_2 = moving average current bar
Now we inspect for the crossover which involves comparing where the 2 moving averages on the last bar were relative to each other(above or below)…the we compare them on the current bar to see if that relationship has changed.
if(MA(50)_1<MA(100)_1) && (MA(50)_2>MA(100)_2) then we have BUY
if(MA(50)_1>MA(100)_1) && (MA(50)_2<MA(100)_2) then we have SELL
Of course you could just use MACD…a negative MACD is downtrend and a positive MACD is uptrend
Hope it helps.