Need Help with Alternating Buy/Sell Strategy EA

 

Hello everyone,

I am working on an EA that follows an Alternating Buy/Sell strategy based on price movements, but I have encountered an issue where it keeps opening consecutive Sell orders instead of alternating between Buy and Sell.

Strategy Requirements:

  1. The EA starts with a manual Buy order (0.1 lot).
  2. Take Profit (TP) = $4, Stop Loss (SL) = $6 (Same for all trades).
  3. If the price drops $2 from the initial buy price, the EA should place a Sell order (0.25 lot).
  4. If the price returns to the initial price (+$2 from the sell entry), the EA should place another Buy order (0.35 lot).
  5. The process should continue in an alternating sequence: Buy → Sell → Buy → Sell, using increasing lot sizes from predefined arrays.
  6. If any order hits TP or SL, the EA should close all positions and reset.
  7. If the price doesn’t reach TP but returns to the initial price, the EA should place the next order with the next lot size.

Current Issues:

✅ The EA opens orders correctly, places TP and SL correctly, and opens a Sell order when the price drops $2 as expected.
❌ The problem is that after opening a Sell order, the EA keeps opening consecutive Sell orders instead of alternating back to Buy.
❌ It does not detect when the price has returned to the initial level to place the next Buy order.

I have tried debugging the isBuyActive flag and adjusting conditions in CheckForNewOrders() , but I can’t seem to fix the issue.


CODE IS ATTACHED 


I would really appreciate any insights or fixes to ensure the EA correctly follows the Buy → Sell → Buy → Sell pattern.

Thanks in advance!

 
Subhan Ali:

Hello everyone,

I am working on an EA that follows an Alternating Buy/Sell strategy based on price movements, but I have encountered an issue where it keeps opening consecutive Sell orders instead of alternating between Buy and Sell.

Strategy Requirements:

  1. The EA starts with a manual Buy order (0.1 lot).
  2. Take Profit (TP) = $4, Stop Loss (SL) = $6 (Same for all trades).
  3. If the price drops $2 from the initial buy price, the EA should place a Sell order (0.25 lot).
  4. If the price returns to the initial price (+$2 from the sell entry), the EA should place another Buy order (0.35 lot).
  5. The process should continue in an alternating sequence: Buy → Sell → Buy → Sell, using increasing lot sizes from predefined arrays.
  6. If any order hits TP or SL, the EA should close all positions and reset.
  7. If the price doesn’t reach TP but returns to the initial price, the EA should place the next order with the next lot size.

Current Issues:

✅ The EA opens orders correctly, places TP and SL correctly, and opens a Sell order when the price drops $2 as expected.
❌ The problem is that after opening a Sell order, the EA keeps opening consecutive Sell orders instead of alternating back to Buy.
❌ It does not detect when the price has returned to the initial level to place the next Buy order.

I have tried debugging the isBuyActive flag and adjusting conditions in CheckForNewOrders() , but I can’t seem to fix the issue.


CODE IS ATTACHED 


I would really appreciate any insights or fixes to ensure the EA correctly follows the Buy → Sell → Buy → Sell pattern.

Thanks in advance!

Traders and coders are working for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members on this forum.

Freelance section of the forum should be used in most of the cases.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2025.03.13
  • www.mql5.com
The largest freelance service with MQL5 application developers