Order closing and opening very quickly unintendedly in MQL4 when using multiple criteria with 'OR' operator

Order closing and opening very quickly unintendedly in MQL4 when using multiple criteria with 'OR' operator

Manage alerts

Loading saved threads...

Rahul2304 · External communityPost link
External question — Stack Overflow Stack Exchange Author: Rahul2304 Original post: https://stackoverflow.com/questions/69027162 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I am trying to close my trades based on 2 separate types of criteria, Regardless of what the profit is, close the trade after 1020 seconds. If my import value changes from -1 to 1 or vice versa, close it regardless of any other variables. So far, my code works fine with 1 criteria, however when I add 2 if statements, MT4 does this thing where it opens and closes the trades very quickly, within every half a second, it doesn't look like its working properly. If somebody could let me know what's making it do it, I would be grateful. My code is below, //Global Variables //int Value = ReadCSVFile and get cell value(Either -1 or 1) int Ticket; int TicketBuy; datetime SwitchBuy datetime SwitchSell void OnTick() { //Open Trades based on Value from CSV and if No other buy orders open if(Value == 1){ if(getBuyOrderCount(Symbol(),magic) < 1){ Ticket == OrderSend(NULL,OP_BUY,0.01,Ask,2,0,0,NULL,magic,0,clrAliceBlue); SwitchBuy = TimeCurrent(); }} if(Value == (-1)){ if(SellOrderCount(Symbol(),magic) < 1){ TicketSell = OrderSend(NULL,OP_SELL,0.01,Bid,2,0,0,NULL,magic,0,clrAliceBlue); SwitchSell = TimeCurrent(); }} //Close Trades based on value in CSV file changing if(Ticket > 0){ if(Value == (-1)||(TimeCurrent()>(SwitchBuy+60))){ CloseAllTradesBuy(); Ticket = 0; Alert("Buy Trade Closed Because Probabilities Changed to -1!"); } } if(TicketSell > 0){ if(Value == 1||(TimeCurrent()>(SwitchBuy+60))){ CloseAllTradesSell(); TicketSell = 0; Alert("Sell Trade Closed Because Probabilities Changed to 1!"); } } //Second Independent Criteria if((SwitchBuy+1020) < TimeCurrent()){ if(OrderType() == OP_BUY){ CloseAllTradesBuy(); } } if((SwitchSell+1020) < TimeCurrent()){ if(OrderType() == OP_SELL){ CloseAllTradesSell(); } } } //Functions to Close Trades double CloseAllTradesBuy(){ for (int i =OrdersTotal(); i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS)==true) if(OrderType()==OP_BUY) if (OrderSymbol()==Symbol()) { OrderClose(Ticket,OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,clrRed); } } } double CloseAllTradesSell(){ for (int i =OrdersTotal(); i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS)==true) if(OrderType()==OP_SELL) if (OrderSymbol()==Symbol()) { OrderClose(TicketSell,OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,clrRed); } } }
Quote
Report
waddah attar · External communityPost link
External answer — Stack Overflow Stack Exchange Author: waddah attar Original post: https://stackoverflow.com/a/69124883 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. change this code of close orders : //Functions to Close Trades double CloseAllTradesBuy(){ for (int i =OrdersTotal()-1; i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS) && OrderType()==OP_BUY && OrderSymbol()==Symbol()) { OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,clrRed); } } } double CloseAllTradesSell(){ for (int i =OrdersTotal()-1; i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS) && OrderType()==OP_SELL && OrderSymbol()==Symbol()) { OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,clrRed); } } }
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Rahul2304 Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/69027162 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I am trying to close my trades based on 2 separate types of criteria, Regardless of what the profit is, close the trade after 1020 seconds. If my import value changes from -1 to 1 or vice versa, close it regardless of any other variables. So far, my code works fine with 1 criteria, however when I add 2 if statements, MT4 does this thing where it opens and closes the trades very quickly, within every half a second, it doesn't look like its working properly. If somebody could let me know what's making it do it, I would be grateful. My code is below, //Global Variables //int Value = ReadCSVFile and get cell value(Either -1 or 1) int Ticket; int TicketBuy; datetime SwitchBuy datetime SwitchSell void OnTick() { //Open Trades based on Value from CSV and if No other buy orders open if(Value == 1){ if(getBuyOrderCount(Symbol(),magic) < 1){ Ticket == OrderSend(NULL,OP_BUY,0.01,Ask,2,0,0,NULL,magic,0,clrAliceBlue); SwitchBuy = TimeCurrent(); }} if(Value == (-1)){ if(SellOrderCount(Symbol(),magic) < 1){ TicketSell = OrderSend(NULL,OP_SELL,0.01,Bid,2,0,0,NULL,magic,0,clrAliceBlue); SwitchSell = TimeCurrent(); }} //Close Trades based on value in CSV file changing if(Ticket > 0){ if(Value == (-1)||(TimeCurrent()>(SwitchBuy+60))){ CloseAllTradesBuy(); Ticket = 0; Alert("Buy Trade Closed Because Probabilities Changed to -1!"); } } if(TicketSell > 0){ if(Value == 1||(TimeCurrent()>(SwitchBuy+60))){ CloseAllTradesSell(); TicketSell = 0; Alert("Sell Trade Closed Because Probabilities Changed to 1!"); } } //Second Independent Criteria if((SwitchBuy+1020) < TimeCurrent()){ if(OrderType() == OP_BUY){ CloseAllTradesBuy(); } } if((SwitchSell+1020) < TimeCurrent()){ if(OrderType() == OP_SELL){ CloseAllTradesSell(); } } } //Functions to Close Trades double CloseAllTradesBuy(){ for (int i =OrdersTotal(); i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS)==true) if(OrderType()==OP_BUY) if (OrderSymbol()==Symbol()) { OrderClose(Ticket,OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,clrRed); } } } double CloseAllTradesSell(){ for (int i =OrdersTotal(); i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS)==true) if(OrderType()==OP_SELL) if (OrderSymbol()==Symbol()) { OrderClose(TicketSell,OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,clrRed); } } }

Cancel quote

Checking account access…