//+------------------------------------------------------------------+ // Improved Trend…
- User
//+------------------------------------------------------------------+ //| Improved Trend Signal with Filters and Debugging | //+------------------------------------------------------------------+ #property indicator_chart_window #property indicator_buffers 2 #property indicator_color1 Lime #property indicator_color2 Red
// Input parameters input int FastMAPeriod = 9; // Fast moving average period input int SlowMAPeriod = 21; // Slow moving average period input int ATRPeriod = 14; // ATR period for volatility filter input double MinATR = 20; // Minimum ATR value for signal validity input int VolumeThreshold = 1000; // Volume threshold for filter input int HigherTimeframeMAPeriod = 50; // Higher timeframe MA period for trend filter
// Indicator buffers double BuySignal[]; double SellSignal[];
// Initialization int OnInit() { SetIndexBuffer(0, BuySignal); SetIndexBuffer(1, SellSignal);
SetIndexStyle(0, DRAW_ARROW, EMPTY, 2); SetIndexArrow(0, 233); // Up arrow for buy signal SetIndexStyle(1, DRAW_ARROW, EMPTY, 2); SetIndexArrow(1, 234); // Down arrow for sell signal return(INIT_SUCCEEDED);}
// Calculate signals int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) {
int start = prev_calculated > 0 ? prev_calculated - 1 : SlowMAPeriod; for (int i = start; i < rates_total; i++) { // Calculate moving averages double fastMA = iMA(NULL, 0, FastMAPeriod, 0, MODE_SMA, PRICE_CLOSE, i); double slowMA = iMA(NULL, 0, SlowMAPeriod, 0, MODE_SMA, PRICE_CLOSE, i); double atr = iATR(NULL, 0, ATRPeriod, i); double higherTimeframeMA = iMA(NULL, PERIOD_H1, HigherTimeframeMAPeriod, 0, MODE_SMA, PRICE_CLOSE, i); // Previous values for crossover check double prevFastMA = iMA(NULL, 0, FastMAPeriod, 0, MODE_SMA, PRICE_CLOSE, i + 1); double prevSlowMA = iMA(NULL, 0, SlowMAPeriod, 0, MODE_SMA, PRICE_CLOSE, i + 1); double prevHigherTimeframeMA = iMA(NULL, PERIOD_H1, HigherTimeframeMAPeriod, 0, MODE_SMA, PRICE_CLOSE, i + 1); // Reset signals BuySignal[i] = EMPTY_VALUE; SellSignal[i] = EMPTY_VALUE; // Volume filter check bool isHighVolume = volume[i] > VolumeThreshold; if (!isHighVolume) Print("Volume filter not met at ", TimeToString(time[i]), " - Volume: ", volume[i]); // ATR filter check bool isHighVolatility = atr > MinATR; if (!isHighVolatility) Print("ATR filter not met at ", TimeToString(time[i]), " - ATR: ", atr); // Higher timeframe trend filter bool isUptrend = higherTimeframeMA > prevHigherTimeframeMA; bool isDowntrend = higherTimeframeMA < prevHigherTimeframeMA; if (!isUptrend && !isDowntrend) Print("Trend filter not met at ", TimeToString(time[i]), " - HigherTimeframeMA: ", higherTimeframeMA, " PrevHigherTimeframeMA: ", prevHigherTimeframeMA); // Buy Signal if (isUptrend && isHighVolume && isHighVolatility && fastMA > slowMA && prevFastMA <= prevSlowMA) { BuySignal[i] = low[i] - 3 * Point; // Place Buy arrow below candle Print("Filtered Buy signal generated at: ", TimeToString(time[i])); } // Sell Signal if (isDowntrend && isHighVolume && isHighVolatility && fastMA < slowMA && prevFastMA >= prevSlowMA) { SellSignal[i] = high[i] + 3 * Point; // Place Sell arrow above candle Print("Filtered Sell signal generated at: ", TimeToString(time[i])); } } return(rates_total); - Assistant