EA code to close opposite positions
Hello everyone. I started learning mql5 just last week, I've come a long way though. However, I'm having trouble writing codes to close buy orders when sell signal is received, and vice versa. Is anyone kind enough to help me out?
- [ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4.
- ORDERCLOSEBY Function
- need help identify last order
Nelson Wanyama :
Hello everyone. I started learning mql5 just last week, I've come a long way though. However, I'm having trouble writing codes to close buy orders when sell signal is received, and vice versa. Is anyone kind enough to help me out?
Hello everyone. I started learning mql5 just last week, I've come a long way though. However, I'm having trouble writing codes to close buy orders when sell signal is received, and vice versa. Is anyone kind enough to help me out?
Example: code "Puria method 2", input parameter "Close opposite" -> variable InpCloseOpposite.
Nelson Wanyama:
Hello everyone. I started learning mql5 just last week, I've come a long way though. However, I'm having trouble writing codes to close buy orders when sell signal is received, and vice versa. Is anyone kind enough to help me out?
Hello everyone. I started learning mql5 just last week, I've come a long way though. However, I'm having trouble writing codes to close buy orders when sell signal is received, and vice versa. Is anyone kind enough to help me out?
ClosePositions() function:
it's fully functional, you can use it directly.
for the actual operation results confirmation, you need to check the OnTradeTransaction() or OnTrade() event handler.
//+------------------------------------------------------------------+ input int MaxAcceptableSlippagePoints; //Global Variables int Magic = 1234; double Ask=0,Bid=0; MqlTick NewTick; ENUM_ORDER_TYPE_FILLING MarketFillingType; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnInit() { Print("SYMBOL_FILLING_FOK: ",(bool)((int)(SymbolInfoInteger(_Symbol,SYMBOL_FILLING_MODE)&1)==1)," SYMBOL_FILLING_IOC: ",(bool)((int)(SymbolInfoInteger(_Symbol,SYMBOL_FILLING_MODE)&2)==2)); MarketFillingType=ORDER_FILLING_RETURN; if(SymbolInfoInteger(_Symbol,SYMBOL_FILLING_MODE)==SYMBOL_FILLING_FOK) MarketFillingType=ORDER_FILLING_FOK; else if(SymbolInfoInteger(_Symbol,SYMBOL_FILLING_MODE)==SYMBOL_FILLING_IOC) MarketFillingType=ORDER_FILLING_IOC; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { if ( ClosePositions(0) ) Print("All Positions Closed."); //Any value other than 1 and -1 to close All Positions //or: if ( ClosePositions(1) ) Print("All Buy Positions Closed."); //or: if ( ClosePositions(-1) ) Print("All Sell Positions Closed."); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ bool ClosePositions(char type) { if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED)) { Print("error: Trading is forbidden !"); return(false); } if(!TerminalInfoInteger(TERMINAL_CONNECTED)) { Print("error: Connection to trade server lost !"); return(false); } MqlTradeRequest TradeRequest; MqlTradeResult TradeResult; bool success=false; int failed=0; ENUM_POSITION_TYPE positiontype; long positionticket; double positionvolume; if(SymbolInfoTick(_Symbol,NewTick)) { Ask=NewTick.ask; Bid=NewTick.bid; } int currentpositions=PositionsTotal(); for(int i=currentpositions-1; i>=0; i--) { positionticket=(long)PositionGetTicket(i); if(PositionGetString(POSITION_SYMBOL)!=_Symbol || PositionGetInteger(POSITION_MAGIC)!=Magic) continue; positionvolume=PositionGetDouble(POSITION_VOLUME); positiontype=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE); ZeroMemory(TradeRequest); ZeroMemory(TradeResult); TradeRequest.action=TRADE_ACTION_DEAL; TradeRequest.magic=Magic; TradeRequest.symbol=_Symbol; TradeRequest.position=positionticket; TradeRequest.volume=positionvolume; TradeRequest.type_filling=MarketFillingType; TradeRequest.comment=IntegerToString(Magic); if(positiontype==POSITION_TYPE_BUY) { if(type==-1) continue; else TradeRequest.type=ORDER_TYPE_SELL; } if(positiontype==POSITION_TYPE_SELL) { if(type==1) continue; else TradeRequest.type=ORDER_TYPE_BUY; } if((ENUM_SYMBOL_TRADE_EXECUTION)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_EXEMODE)==SYMBOL_TRADE_EXECUTION_REQUEST || (ENUM_SYMBOL_TRADE_EXECUTION)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_EXEMODE)==SYMBOL_TRADE_EXECUTION_INSTANT) { TradeRequest.deviation=MaxAcceptableSlippagePoints; if(positiontype==POSITION_TYPE_BUY) TradeRequest.price=NormalizeDouble(Bid,_Digits); if(positiontype==POSITION_TYPE_SELL) TradeRequest.price=NormalizeDouble(Ask,_Digits); } ResetLastError(); success=OrderSend(TradeRequest,TradeResult); if(!success) failed++; } if(failed==0) return(true); return(false); } //+------------------------------------------------------------------+

You are missing trading opportunities:
- Free trading apps
- Free Forex VPS for 24 hours
- 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