Your code assumes a simplification with positive and negative pips which could lead to issues with detecting profitable positions correctly. Code below fixes Loop integrity and position select: PositionSelectByIndex(i) ensures you're correctly referencing each position in each iteration, plus pip calculation is standardized by SymbolInfoDouble(symbol, SYMBOL_POINT).
#include <Trade\Trade.mqh> // Create an instance of CTrade class CTrade trade; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { // Initialization code EventSetTimer(300); // Set timer to trigger every 5 minutes (300 seconds) return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { // Deinitialization code EventKillTimer(); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { CloseTradesAfterHours(1); } //+------------------------------------------------------------------+ //| Close all positions after the specified hour | //+------------------------------------------------------------------+ void CloseTradesAfterHours(int closeAfterHour) { // Check if current time is after closeAfterHour datetime currentTime = TimeCurrent(); MqlDateTime currentTimeStruct; TimeToStruct(currentTime, currentTimeStruct); int hour = currentTimeStruct.hour; if(hour < closeAfterHour) return; // Loop through all open positions for(int i = PositionsTotal() - 1; i >= 0; i--) { if(PositionSelectByIndex(i)) { double openPrice = PositionGetDouble(POSITION_PRICE_OPEN); double currentPrice, pips; long positionType = PositionGetInteger(POSITION_TYPE); string symbol = PositionGetString(POSITION_SYMBOL); // Get current price based on position type if(positionType == POSITION_TYPE_BUY) { currentPrice = SymbolInfoDouble(symbol, SYMBOL_BID); pips = (currentPrice - openPrice) / SymbolInfoDouble(symbol, SYMBOL_POINT); } else if(positionType == POSITION_TYPE_SELL) { currentPrice = SymbolInfoDouble(symbol, SYMBOL_ASK); pips = (openPrice - currentPrice) / SymbolInfoDouble(symbol, SYMBOL_POINT); } // Close position if pips reach -10 if(pips <= -10) { trade.PositionClose(PositionGetInteger(POSITION_TICKET)); Print("Closed position: ", PositionGetInteger(POSITION_TICKET)); } } } } //+------------------------------------------------------------------+ //| TEST Timer function | //+------------------------------------------------------------------+ void OnTimer() { OpenBuySellOrdersAfterHours(1); } //+------------------------------------------------------------------+ //| Open buy and sell orders after the specified hour | //+------------------------------------------------------------------+ void OpenBuySellOrdersAfterHours(int openAfterHour) { // Check if current time is after openAfterHour datetime currentTime = TimeCurrent(); MqlDateTime currentTimeStruct; TimeToStruct(currentTime, currentTimeStruct); int hour = currentTimeStruct.hour; if(hour < openAfterHour) return; // Open a buy and sell order with no stop loss or take profit string symbol = _Symbol; double lotSize = 0.1; // Example lot size // Open a buy order trade.Buy(lotSize, symbol); // Open a sell order trade.Sell(lotSize, symbol); } //+------------------------------------------------------------------+
Marcel:
// Open a buy order trade.Buy(lotSize, symbol); // Open a sell order trade.Sell(lotSize, symbol);
That ain't an order, and it ain't going to work
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
Hello,
I am trying to build a very simple EA that monitors all open positions and then closes a position if that one reaches 10 pips drawdown ..
I am familiar with MQL5 and have written many codes with it, though I am not a professional programmer .. the EA is simply not closing positions at 10 pips, sometimes 1 sometimes 2 sometimes 4 ..
Can anyone take a quick look at the code to perhaps see where a mistake might be ? I am trading GOLD so the pips are calculated as the difference between open and current price * 10 ..
I appreciate all help and suggestions !