// Optimized Trend Detection Script
// Developed by ChatGPT

// Variables for MA calculation
double MA1, MA2;
int n1, n2;
int i;

// Variables for trend direction
double Trend;
string TrendDirection;

// User inputs for MA periods and calculations
n1 = Input("MA Period 1", 10, 2, 100);
n2 = Input("MA Period 2", 50, 2, 100);

// Loop through all available bars to calculate MA values
for (i = 0; i < Bars; i++) {
    MA1 = iMA(NULL, 0, n1, 0, MODE_SMA, PRICE_CLOSE, i);
    MA2 = iMA(NULL, 0, n2, 0, MODE_SMA, PRICE_CLOSE, i);
}

// Calculate trend direction based on MA values
if (MA1 > MA2) {
    Trend = 1;
    TrendDirection = "UP";
} else if (MA1 < MA2) {
    Trend = -1;
    TrendDirection = "DOWN";
} else {
    Trend = 0;
    TrendDirection = "SIDEWAYS";
}

// Plot trend direction on the chart
Plot(Trend, "Trend", TrendDirection == "UP" ? RGB(0, 255, 0) : TrendDirection == "DOWN" ? RGB(255, 0, 0) : RGB(255, 255, 0), PLOT_ARROW);
