Wrongs in close of trades in MQL4

43 Views Asked by At

I created this code to Open trade if conditions true and close them if conditions true But he only Open trade and not close them.

This is my code



extern double lotSize = 0.02;

extern double stopLoss = 50;

extern double takeProfit = 100;

extern double targetProfit = 1;

datetime tradeOpenTime = 0;

int slippage = 3;

// دالة لفتح صفقة شراء

void OnTick() {

//void OpenBuyOrder() {

if (true) {

if (TimeCurrent() > tradeOpenTime + PeriodSeconds()) {

if (TimeMinute(TimeCurrent()) == 0) {

if ((Close[1] < Open[1]) && (Close[2] < Open[2]) && (Close[2] < Open[2])) {

OrderSend(Symbol(), OP_BUY, lotSize, MarketInfo(Symbol(), MODE_ASK), slippage, 0, 0, "", 0, clrNONE);

tradeOpenTime = TimeCurrent();

}

}

}

}

//}

// دالة لشروط فتح صفقة بيع

//void OpenSellOrder() {

if (true) {

if (TimeCurrent() > tradeOpenTime + PeriodSeconds()) {

if (TimeMinute(TimeCurrent()) == 0) {

if ((Close[1] > Open[1]) && (Close[2] > Open[2]) && (Close[2] > Open[2])) {

OrderSend(Symbol(), OP_SELL, lotSize, MarketInfo(Symbol(), MODE_BID), slippage, 0, 0, "", 0, clrNONE);

tradeOpenTime = TimeCurrent();

}

}

}

}

//}

// دالة لإغلاق صفقات شراء وبيع بشروط معينة

//void CloseOrdersBasedOnConditions() {

if (OrdersTotal() == 1) {

//if (true) {

if (TimeCurrent() > tradeOpenTime + PeriodSeconds()) {

if (TimeMinute(TimeCurrent()) == 0) {

if ((Close[1] > Open[1]) && (OrderType() == OP_BUY)) {

OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 2, clrNONE);

} else if ((Close[1] < Open[1]) && (OrderType() == OP_SELL)) {

OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 2, clrNONE);

}

}

}

}

//}

// دالة لإغلاق جميع الصفقات إذا كان الربح الإجمالي يساوي القيمة المحددة

//void CloseAllOrdersIfProfitReached() {

else if (OrdersTotal() > 1) {

//if (true) {

double totalProfit = 0;

for (int i = OrdersTotal() - 1; i >= 0; i--) {

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

totalProfit += OrderProfit();

}

}

if (totalProfit >= targetProfit) {

for (int x = OrdersTotal() - 1; x >= 0; x--) {

if (OrderSelect(x, SELECT_BY_POS, MODE_TRADES)) {

OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 2, clrNONE);

}

}

}

}

//}

}

I have written this code to open trades when the conditions are met. If the conditions for buying are met, it opens a buy trade, and if the conditions for selling are met, it opens a sell trade. Additionally, there are conditions for closing a trade when a specific condition is met. There's also a condition to close trades when the total profit reaches a certain dollar amount. However, my problem is that this code only opens trades and doesn't close them when the conditions are met.

2

There are 2 best solutions below

2
Mark SdS On

In your CloseAllOrdersIfProfitReached you're iterating open trades and close them at MarketInfo(OrderSymbol(), MODE_BID) which is BUY related.

Better close them at OrderClosePrice() which is direction neutral.

0
PaulB On

The appears to be multiple errors in your code and approach. I'd suggest the following revisions:

extern double lotSize = 0.02;
extern double stopLoss = 50;
extern double takeProfit = 100;
extern double targetProfit = 1;
datetime timebar, tradeOpenTime=0;
int slippage = 30;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
//---
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{

   //CloseAllOrdersIfProfitReached
   if(OrdersTotal()>1)
   {
      double totalProfit = 0;
      for(int i=OrdersTotal()-1; i>=0; i--) if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) totalProfit+=OrderProfit();
      if(totalProfit>=targetProfit)
      {
         for(int x=OrdersTotal()-1; x>=0; x--)
         {
            if(OrderSelect(x, SELECT_BY_POS, MODE_TRADES))
            {
               if(OrderType()==OP_BUY) bool res=OrderClose(OrderTicket(), OrderLots(), Bid, slippage, clrNONE);
               else bool res=OrderClose(OrderTicket(), OrderLots(), Ask, 50, clrNONE);
            }
         }
      }
   }

   if(timebar==Time[0]) return;

   //CloseOrdersBasedOnConditions
   for(int x=OrdersTotal()-1; x>=0; x--)
   {
      if(OrderSelect(x, SELECT_BY_POS, MODE_TRADES))
      {
         if(Close[1]>Open[1] && OrderType()==OP_BUY) bool res=OrderClose(OrderTicket(), OrderLots(), Bid, 2, clrNONE);
         else if(Close[1]<Open[1] && OrderType()==OP_SELL) bool res=OrderClose(OrderTicket(), OrderLots(), Ask, 2, clrNONE);
      }
   }

   //OpenBuyOrder
   if(Close[1]<Open[1] && Close[2]<Open[2])
   {
      bool res=OrderSend(Symbol(), OP_BUY, lotSize, Ask, slippage, 0, 0, "", 0, clrNONE);
      tradeOpenTime = TimeCurrent();
   }

   //OpenSellOrder
   if(Close[1]>Open[1] && Close[2]>Open[2])
   {
      bool res=OrderSend(Symbol(), OP_SELL, lotSize, Bid, slippage, 0, 0, "", 0, clrNONE);
      tradeOpenTime = TimeCurrent();
   }

timebar=Time[0];
return;
}