Close All Position on end of the month / starting of the month

 

Is there any possible to create an EA to close all the positions no matter profit or loss on every end of the month or 1st day of the month?
Please help. 

 
Of course, it's possible. Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum

 
William Roeder:
Of course, it's possible. Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum

Hi William, thanks for the reply.
I'm actually found this EA that can close all the positions on specific YYMMDDHHMM.
However it will keep closing all the positions after the YYMMDDHHMM.

Can you help adding some code to stop the EA from working after the position closed?
It can be auto stop, or input specific YYMMDDHHMM to stop the EA from continue working.

Files:
closeattime.mq4  15 kb
 
Kelvin Lim:

Hi William, thanks for the reply.
I'm actually found this EA that can close all the positions on specific YYMMDDHHMM.
However it will keep closing all the positions after the YYMMDDHHMM.

Can you help adding some code to stop the EA from working after the position closed?
It can be auto stop, or input specific YYMMDDHHMM to stop the EA from continue working.

I am looking for something similar, but a logic more like: 

Do not trade x days before the end of the month and y days after the start of the month. 

I am looking at the Basics for Programming Time but still have not found the key to code the logic. 

If you do, let me know. 

 
YThi MJ:

I am looking for something similar, but a logic more like: 

Do not trade x days before the end of the month and y days after the start of the month. 

I am looking at the Basics for Programming Time but still have not found the key to code the logic. 

If you do, let me know. 

ExpertRemove(); // this will remove expert from chart once certain conditions are met

and about day of the month you can check the following link:

https://docs.mql4.com/dateandtime/day
Day - Date and Time - MQL4 Reference
Day - Date and Time - MQL4 Reference
  • docs.mql4.com
Day - Date and Time - MQL4 Reference
 
Nikolay Georgiev:

and about day of the month you can check the following link:

https://docs.mql4.com/dateandtime/day

Hahaha, I do not want to remove the expert from the chart. I want it to not enter at the end or at the beginning of the month. I will check you links, still it does not say how to obtain a clear view of the days of the month. Like Feb, that it does not have a common number of days. Thanks a lot

 
YThi MJ:

Hahaha, I do not want to remove the expert from the chart. I want it to not enter at the end or at the beginning of the month. I will check you links, still it does not say how to obtain a clear view of the days of the month. Like Feb, that it does not have a common number of days. Thanks a lot

check this one too you will need to add more if or switch operations :)

https://docs.mql4.com/dateandtime/month

Month - Date and Time - MQL4 Reference
Month - Date and Time - MQL4 Reference
  • docs.mql4.com
Month - Date and Time - MQL4 Reference
 
Kelvin Lim:

Is there any possible to create an EA to close all the positions no matter profit or loss on every end of the month or 1st day of the month?
Please help. 

You can simply try this out.

//+------------------------------------------------------------------+
//|                                                         aaaa.mq4 |
//|                                 Copyright 2020, Haskaya yazilim. |
//|                                   https://www.haskayayazilim.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Haskaya yazilim."
#property link      "https://www.haskayayazilim.net"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   datetime Cur01 = TimeMonth(TimeCurrent());
   
   HepsiniKapat(Cur01);
   
   
      
  }
//+------------------------------------------------------------------+

int HepsiniKapat(datetime GlnDateTime)
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    bool tsd=OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
   if(TimeMonth(OrderOpenTime())<GlnDateTime)
      {
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

      //Close pending orders
      
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
      
    }
    }
    if(result == false)
    {
       if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}
 

I have managed to code it inside the OnTick by going month by month and skipping the Leap year:


  datetime LocalTime=TimeCurrent();
      MqlDateTime tm;
      TimeToStruct(LocalTime,tm);
      int DayOfWeek = tm.day_of_week;
      int MonthOfYear = tm.mon;
      int DayOfMonth = tm.day;


      //+------------------------------------------------------------------+
      //|           NORMAL_SIGNAL                                          |
      //+------------------------------------------------------------------+
      if((!Open_After)||((Open_After)&&(((DayOfMonth>=1+DD)&&(DayOfMonth<=31-DD)&&(MonthOfYear==1))||((DayOfMonth>=1+DD)&&(DayOfMonth<=28-DD)&&(MonthOfYear==2))||((DayOfMonth>=1+DD)&&(DayOfMonth<=31-DD)&&(MonthOfYear==3))||((DayOfMonth>=1+DD)&&(DayOfMonth<=30-DD)&&(MonthOfYear==4))||((DayOfMonth>=1+DD)&&(DayOfMonth<=31-DD)&&(MonthOfYear==5))||((DayOfMonth>=1+DD)&&(DayOfMonth<=30-DD)&&(MonthOfYear==6))||((DayOfMonth>=1+DD)&&(DayOfMonth<=31-DD)&&(MonthOfYear==7))||((DayOfMonth>=1+DD)&&(DayOfMonth<=31-DD)&&(MonthOfYear==8))||((DayOfMonth>=1+DD)&&(DayOfMonth<=30-DD)&&(MonthOfYear==9))||((DayOfMonth>=1+DD)&&(DayOfMonth<=31-DD)&&(MonthOfYear==10))||((DayOfMonth>=1+DD)&&(DayOfMonth<=30-DD)&&(MonthOfYear==11))||((DayOfMonth>=1+DD)&&(DayOfMonth<=31-DD)&&(MonthOfYear==12)))))
         if(Ind_Method==NORMAL_SIGNAL)
            if((AO_Main)||(MFI_Main)||(SAR_Main)||(BB_Main)||(BBXS_Main)||(ATR_Main)||(STO_Main)||(RSI_Main)||(MA_Main)||(BBB_Main)||((BBBB_Main)&&(MA_Main)))
               if(((DayOfWeek==1)&&(Monday==true))||((DayOfWeek==2)&&(Tuesday==true))||((DayOfWeek==3)&&(Wednesday==true))||((DayOfWeek==4)&&(Thursday==true))||((DayOfWeek==5)&&(Friday==true))||((DayOfWeek==6)&&(Saturday==true))||((DayOfWeek==0)&&(Sunday==true)))
                  if((ActiveBetweenHours)&&((TimeCurrent())>=StringToTime(HourToOpen))&&((TimeCurrent())<StringToTime(HourNotToOpen)))
                     if(CountTrades()<1)
                        if(OrdersHistoryTotal()<2)
                           if(iVolume(NULL,TimeFrame41,1)>MinVol)
                              if((ATR>ATR1)&&(RSIb>RSIb1)&&(ADb>ADb1)&&(AO<1)&&(AOi<1)&&(AO2<1)&&(AOi2<1)&&(MAb1>MAb2)&&(MAb1>MAb11)&&(MAb2>MAb21)&&(Open_Buy)&&(BBb1<BBCLb)&&(BBb1>BBOPb)&&(BBb2<BBCLb1)&&(BBb2>BBOPb1)&&(MAb1>MAb2)&&(MAb1>BBBb1)&&(MAb2>BBBb2)&&(pos1pre>0)&&(pos1pre1>0)&&(Ask>PSARb1)&&(Ask>PSARb2)&&(STO8B<StoBuy)&&(STOb8>STOb18)&&(STOb8>STO8pr))
                                {
                                 int open= OrderSend(Symbol(),OP_BUY,Lot(),Ask,Slip,slb,tpb,CommentBuy,Magic,0,Blue);
                                }
                              else
                                 if((ATR>ATR1)&&(RSIs<RSIs1)&&(ADs<ADs1)&&(AO>0)&&(AOi>0)&&(AO2>0)&&(AOi2>0)&&(MAs1<MAs2)&&(MAs1<MAs11)&&(MAs2<MAs21)&&(Open_Sell)&&(pos2cur<0)&&(BBs1>BBCLs)&&(BBs1<BBOPs)&&(BBs2>BBCLs1)&&(BBs2<BBOPs1)&&(MAs1<MAs2)&&(MAs1<BBBs1)&&(MAs2<BBBs2)&&(pos2cur1<0)&&(Bid<PSARs1)&&(Bid<PSARs2)&&(STO18S>StoSell)&&(STOs8<STOs18)&&(STOs18<STO18pr))
                                   {
                                    int open= OrderSend(Symbol(),OP_SELL,Lot(),Bid,Slip,sls,tps,CommentSell,Magic,0,Red);
                                   }
 
穆罕默德·巴斯特姆 # :

您可以簡單地嘗試一下。

Can you write a everyday version? thanks you! 

Reason: