But this is only part of the code.😁
Since you've decided to share your developments, I suggest you attach an .mq4 file with the full code so that anyone (even non-programmers) can download and test it.
This is the main part of the code (the “Strategy”) that I explained in the image “Capturing smaller countertrend in a bigger trending market.” The EA is not completed and is still under development. I can only continue working on the EA when I know I am doing it correctly. I backtested this code and I feel something is missing — it’s not taking trades the way I want, as I described in my MS Paint diagram.
Can you help me fix this based on the given logic? I will share the .mq4 file once it is ready and I have some backtest results.
This is the main part of the code (the “Strategy”) that I explained in the image “Capturing smaller countertrend in a bigger trending market.” The EA is not completed and is still under development. I can only continue working on the EA when I know I am doing it correctly. I backtested this code and I feel something is missing — it’s not taking trades the way I want, as I described in my MS Paint diagram.
Ok, as you wish, I don’t insist. I just thought the people most enthusiastic about testing your code might be the ones who can't develop it themselves.
Can you help me fix this based on the given logic?
Unfortunately, I don't have time right now. But if you provide the rules for closing orders, then perhaps I will share my own implementation in a few days (although I make no promises).
if you provide the rules for closing orders
Closing with ATR (or) BollingerBand middle/opposite band (or) RSI Opposite Closing. - Any one is true, it will close the position (3 Ways to close)
ATR Code :
extern string _ATR_TradeExit_ = "***ATR TP/SL/Trailing***"; extern int ATR_Period = 14; extern int RiskToReward_TP = 1; extern int RiskToReward_SL = 1; fATRValue = iATR(NULL, PERIOD_CURRENT, ATR_Period, 0); TakeProfit = fATRValue * RiskToReward_TP; StopLoss = fATRValue * RiskToReward_SL;
RSI Closing (Opp direction): In counter-trend Buy generally open when RSI is Oversold, so it will close buy position when RSI is Overbought.
for(int i = OrdersTotal() - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if((OrderMagicNumber() == MagicNumber) && (OrderSymbol() == Symbol())) { string RSIClosingLevel = (OrderType() == OP_BUY) ? 70 : 30; double RSI_Value = iRSI(NULL, CandlePeriod, 14, PRICE_CLOSE, 0); if((OrderType() == OP_BUY && RSI_Value >= RSIClosingLevel) || (OrderType() == OP_SELL && RSI_Value <= RSIClosingLevel)) { ResetLastError(); if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE)) Print(__FUNCTION__, " => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError()); else Print(__FUNCTION__, " => Order ", OrderTicket(), " Successfully Closed"); } } } }
BB Middle Band or Opposite band: When middle band is touched or Opposite band
enum BBClosing_enum {MiddleBand, BBBands}; extern BBClosing_enum BollingerBands_Closing = BBBands; double BB_Upper_Resistance, BB_Lower_Support, BB_Middle; for(int i = OrdersTotal() - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if((OrderMagicNumber() == MagicNumber) && (OrderSymbol() == Symbol())) { if(BollingerBands_Closing == BBBands) { BB_Upper_Resistance = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_UPPER, 0); BB_Lower_Support = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_LOWER, 0); } if(BollingerBands_Closing == MiddleBand) BB_Middle = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_MAIN, 0); if((OrderType() == OP_BUY && ((BollingerBands_Closing == BBBands && Bid >= BB_Upper_Resistance) || (BollingerBands_Closing == MiddleBand && Bid >= BB_Middle))) || (OrderType() == OP_SELL && ((BollingerBands_Closing == BBBands && Bid <= BB_Lower_Support) || (BollingerBands_Closing == MiddleBand && Bid <= BB_Middle)))) { ResetLastError(); if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3, clrNONE)) Print(__FUNCTION__, " => Order ", OrderTicket(), " Failed to Close. Error: ", GetLastError()); else Print(__FUNCTION__, " => Order ", OrderTicket(), " Successfully Closed"); } } } }
Ok, as you wish, I don’t insist. I just thought the people most enthusiastic about testing your code might be the ones who can't develop it themselves.
As the code is not fully completed and can have many logical error/bugs. i will share when at-least i have some development. and peoples are free to test my code. Maybe it takes more few more days to complete it like adding many option like trailing, signal strength, etc.
I will share my own implementation in a few days (although I make no promises).
Thanks, i just need some guidance.
Thanks, i just need some guidance.
I'm afraid my code is unlikely to be useful to anyone (for study). Because I am sick with OOP; once I started writing in this paradigm, I can no longer write any other way.
So my implementation would be code that just works, but that few people can understand.
I have some developments for MT4 EA, but none of my code is publicly available. Considering that MT4 is going to die soon (I hope), I would like to share some example of my code just as a keepsake. I myself am not going to develop for MT4 anymore.
Considering that MT4 is going to die soon (I hope)
Really?. lot of codes, indicator and EA are still in MT4.
I started writing in this paradigm, I can no longer write any other way.
I do not want you to write whole code. just you can provide the logical in code or algorithm (if mine is wrong) and i can write based on algorithm.
//Buy Trade if((RSI_Value < 30) && Bid < BB_Lower_Support && Bid < BB_MiddleBand && Bid > iMovingAverage && (iMovingAverage < BB_Lower_Support || iMovingAverage > BB_Upper_Resistance) && CandleGreen_S0) { //Buy Order }
Why use the Bid and indicator values from the current bar? You'll get a flickering signal, which requires more complex logic to avoid re-openings.
It would be much simpler to look at the Close[1] and indicators from the previous bar. That is, trade only formed signals.
Why use the Bid and indicator values from the current bar? You'll get a flickering signal, which requires more complex logic to avoid re-openings.
It would be much simpler to look at the Close[1] and indicators from the previous bar. That is, trade only formed signals.
Presumably, the OP intends to enter and exit within formation of the current bar. This is ok as long as the OP has some position limiting code. As you've already alluded, the entire code is not posted.
For example, the OP's ATR based dynamic stops are described as:
extern string _ATR_TradeExit_ = "***ATR TP/SL/Trailing***";
If the OP intends to get back into the market after getting stopped out, and the entry conditions recur after some time has passed, so be it.
Presumably, the OP intends to enter and exit within formation of the current bar. This is ok as long as the OP has some position limiting code. As you've already alluded, the entire code is not posted.
I don't like these types of things because they're inconvenient to test.😁
- If the formed indicator values are used to generate the signal, you can check the correct operation by simply viewing the chart after the test is completed.
- If you take unformed indicator values from the current bar, then to check the correct operation, you will have to pause the testing (manually or programmatically) at the moment of each signal to see the indicator values.
Moreover, if you take the indicator values from the current bar, you will not be able to find the last signal on the chart if necessary. That is you won't be able to analyze historical signals from past bars (this would require tick history and more complex algorithms). Knowing the last signal is often necessary for the reliable operation of the advisor. Imagine that in the middle of a trend (with open orders), you migrate an advisor to a VPS. This is an example of why a reliable EA should be able to understand what's happening at any moment by analyzing only the chart, without saving the state to files or any other such stuff.
That's why I ask right away: are you sure you want the current bar and all the headaches that come with it?
Why use the Bid and indicator values from the current bar? You'll get a flickering signal, which requires more complex logic to avoid re-openings.
It would be much simpler to look at the Close[1] and indicators from the previous bar. That is, trade only formed signals.
Thanks, your correct, using current candle make lot of noise and false signal. So, apart from this there is no error with the logic?
Something like this with Shift 1 :
double RSI_Value = iRSI(NULL, PERIOD_CURRENT, 14, PRICE_CLOSE, 1); double BB_Upper_Resistance = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_UPPER, 1); double BB_Lower_Support = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_LOWER, 1); double BB_MiddleBand = iBands(NULL, PERIOD_CURRENT, 20, 2.0, 0, PRICE_CLOSE, MODE_MAIN, 1); double iMovingAverage = iMA(NULL, PERIOD_CURRENT, 200, 0, MODE_EMA, PRICE_CLOSE, 1); double ClosePrice = iClose(NULL, PERIOD_CURRENT, 1); bool CandleGreen = iClose(NULL, PERIOD_CURRENT, 1) > iOpen(NULL, PERIOD_CURRENT, 1); //Buy Trade if((RSI_Value < 30) && ClosePrice < BB_Lower_Support && ClosePrice < BB_MiddleBand && ClosePrice > iMovingAverage && (iMovingAverage < BB_Lower_Support || iMovingAverage > BB_Upper_Resistance) && CandleGreen) { //Buy Order } //Sell Trade if((RSI_Value > 70) && ClosePrice > BB_Upper_Resistance && ClosePrice > BB_MiddleBand && ClosePrice < iMovingAverage && (iMovingAverage < BB_Lower_Support || iMovingAverage > BB_Upper_Resistance) && !CandleGreen) { //Sell Order }
Or more strict condition like this :
// 1. SETUP: Check Shift 2 (The candle BEFORE the last closed one) double BB_Lower_S2 = iBands(NULL, 0, 20, 2.0, 0, PRICE_CLOSE, MODE_LOWER, 2); double BB_Upper_S2 = iBands(NULL, 0, 20, 2.0, 0, PRICE_CLOSE, MODE_UPPER, 2); double RSI_S2 = iRSI(NULL, 0, 14, PRICE_CLOSE, 2); double Close_S2 = iClose(NULL, 0, 2); // 2. TRIGGER & FILTER: Check Shift 1 (The most recent closed candle) double MA_S1 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, 1); double BB_Lower_S1 = iBands(NULL, 0, 20, 2.0, 0, PRICE_CLOSE, MODE_LOWER, 1); double BB_Upper_S1 = iBands(NULL, 0, 20, 2.0, 0, PRICE_CLOSE, MODE_UPPER, 1); double Close_S1 = iClose(NULL, 0, 1); double Open_S1 = iOpen(NULL, 0, 1); bool CandleGreen_S1 = Close_S1 > Open_S1; // --- BUY LOGIC --- if( // A. CounterTrend-Small Trend (Shift 2) Close_S2 < BB_Lower_S2 && // Price was below the band (Support line) RSI_S2 < 30 && // RSI was oversold // B. Main Trend (Shift 1) Close_S1 > MA_S1 && // Price is above 200 EMA (Up-Trend) // C. Band FILTER (Shift 1) // Checks if MA is not inside Band (Strong Uptrend Structure) (MA_S1 < BB_Lower_S1 || MA_S1 > BB_Upper_S1) && // D. The Trigger (Shift 1) CandleGreen_S1 // Green Reversal Candle Confirmation ) { // OPEN BUY ORDER } // --- SELL LOGIC --- if( // A. CounterTrend-Small Trend (Shift 2) Close_S2 > BB_Upper_S2 && // Price was above the band (Resistance Line) RSI_S2 > 70 && // RSI was overbought // B. Main Trend (Shift 1) Close_S1 < MA_S1 && // Price is below 200 EMA (Down-Trend) // C. Band FILTER (Shift 1) // Checks if MA is not inside Band (Strong Downtrend Structure) (MA_S1 < BB_Lower_S1 || MA_S1 > BB_Upper_S1) // D. The Trigger (Shift 1) !CandleGreen_S1 // Red Reversal Candle Confirmation ) { // OPEN SELL ORDER }
And what is your Opinion on my logic behind OrderClosing/Order exit?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello,
In a trending market, I try to take trades during small counter-trend moves. For example, in an uptrend, the market moves down, then goes back up strongly, then pulls back slightly, and then goes up again. During these smaller pullbacks (counter-trend moves), I take buy trades in the direction of the larger trend. The same logic applies in a downtrend for sell trades.
I have created an MQL EA based on this logic. I need your help to verify and validate whether my code is correct. If there are any issues, please suggest how to fix them. Here is my EA code:
Here is basic diagram.
Code :