#property strict input int MA50Period = 50; // Period for the MA50 indicator input int MA100Period = 100; // Period for the MA100 indicator input int ShiftPeriod = 3; // Number of candles to check for rising/falling trend int TrendDirection = 0; // Trend direction: 1 for uptrend, -1 for downtrend, 0 for no trend void OnTick() { double MA50Value = iMA(_Symbol, _Period, MA50Period, 0, MODE_SMA, PRICE_CLOSE, 0); double MA100Value = iMA(_Symbol, _Period, MA100Period, 0, MODE_SMA, PRICE_CLOSE, 0); bool MA50Rising = true; bool MA100Rising = true; for(int i = 1; i <= ShiftPeriod; i++) { double MA50PrevValue = iMA(_Symbol, _Period, MA50Period, 0, MODE_SMA, PRICE_CLOSE, i); double MA100PrevValue = iMA(_Symbol, _Period, MA100Period, 0, MODE_SMA, PRICE_CLOSE, i); if(MA50Value < MA50PrevValue) MA50Rising = false; if(MA100Value < MA100PrevValue) MA100Rising = false; } if(MA50Value > MA100Value && MA50Rising && MA100Rising) TrendDirection = 1; // Uptrend else if(MA50Value < MA100Value && !MA50Rising && !MA100Rising) TrendDirection = -1; // Downtrend else TrendDirection = 0; // No trend // Add your trade logic based on the TrendDirection value }
NOTE: This code assumes (i) that you have included the necessary input variables, and (ii) that you are running it on MT5.
ayaloveschab:
Hello,
I am new to coding and I cant seems to properly identify the trend direction using below codes. Your inputs is greatly appreciated. Thank you
This is the logic:
Trend Direction = 1 (if MA50>MA100 and MA50 last 3 candles is "Rising" and MA100 last 3 candles is "Rising"
Trend Direction = -1 (if MA50<MA100 and MA50 last 3 candles is "Falling" and MA100 last 3 candles is "Falling"
Hello,
I am new to coding and I cant seems to properly identify the trend direction using below codes. Your inputs is greatly appreciated. Thank you
This is the logic:
Trend Direction = 1 (if MA50>MA100 and MA50 last 3 candles is "Rising" and MA100 last 3 candles is "Rising"
Trend Direction = -1 (if MA50<MA100 and MA50 last 3 candles is "Falling" and MA100 last 3 candles is "Falling"
Improperly formatted code removed by moderator
// Define the input parameters here input int MA50_Period = 50; input int MA100_Period = 100; input ENUM_MA_METHOD MA_Method = MODE_SMA; input ENUM_APPLIED_PRICE Fast_MA_ApplyTo = PRICE_CLOSE; input ENUM_APPLIED_PRICE Slow_MA_ApplyTo = PRICE_CLOSE; input int CandlesToCheck = 3; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { return INIT_SUCCEEDED; } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { // Calculate Moving Averages double ma50_array[]; double ma100_array[]; ArraySetAsSeries(ma50_array, true); ArraySetAsSeries(ma100_array, true); // Copy data of MA50 and MA100 into arrays ArrayResize(ma50_array, CandlesToCheck); ArrayResize(ma100_array, CandlesToCheck); int copied_ma50 = CopyBuffer(0, 0, 0, CandlesToCheck, ma50_array); int copied_ma100 = CopyBuffer(1, 0, 0, CandlesToCheck, ma100_array); // Check if data copied successfully if (copied_ma50 != CandlesToCheck || copied_ma100 != CandlesToCheck) { Print("Error copying data. Exiting..."); return; } // Check the direction of the last 3 candles for both MA50 and MA100 here bool ma50_rising = IsRising(ma50_array); bool ma100_rising = IsRising(ma100_array); // Determine the trend direction here int trend_direction = 0; if (ma50_array[0] > ma100_array[0] && ma50_rising && ma100_rising) { trend_direction = 1; } else if (ma50_array[0] < ma100_array[0] && !ma50_rising && !ma100_rising) { trend_direction = -1; } // Print the trend direction if (trend_direction == 1) { Print("Trend Direction: Uptrend"); } else if (trend_direction == -1) { Print("Trend Direction: Downtrend"); } else { Print("No clear trend direction"); } } // Function to check if the last n candles are rising bool IsRising(const double& array[]) { for (int i = 1; i < ArraySize(array); i++) { if (array[i] >= array[i - 1]) { continue; } else { return false; } } return true; }

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I am new to coding and I cant seems to properly identify the trend direction using below codes. Your inputs is greatly appreciated. Thank you
This is the logic:
Trend Direction = 1 (if MA50>MA100 and MA50 last 3 candles is "Rising" and MA100 last 3 candles is "Rising"
Trend Direction = -1 (if MA50<MA100 and MA50 last 3 candles is "Falling" and MA100 last 3 candles is "Falling"