Lot size

 

I need help for my code plz

I want to increase the lot size every month only

For eg: 

This code should be calculated from the  initial balance of the month

If the initial balance is 1000usd in 1january

                                   1050usd in 2 january

                                   .........

                                  2100usd in 31 january

           So during this month,the AccountBalance() should be always 1000usd because it's the initial balance

For the next month(february), the //Lot code should be actualised,I mean,if the balance become 2200usd,the AccountBalance should be calculated from this new account balance on first of february(AccountBalance()=2200usd) .... and soon as


Lots=(((AccountBalance() * MaximumRisk / 100) / (StopLoss))); //Lot code
//--- SELL 
   ma12=iMA(NULL,PERIOD_M15,12,0,MODE_EMA,PRICE_MEDIAN,1);
   ma4=iMA(NULL,PERIOD_M15,4,0,MODE_EMA,PRICE_TYPICAL,1);//--- sell conditions
   if(TimeCurrent()>StrToTime(TradeStartTime) && TimeCurrent()<StrToTime(TradeStopTime)
     && (ma4<ma12) && CandleSell && !isWhiteHammer01 && !neutralSell && !WarningTradeSell && !WarningTradeSell1)
     {
      ma=iMA(NULL,PERIOD_M15,1,0,MODE_EMA,PRICE_TYPICAL,1);
      closeM5=iClose(Symbol(), PERIOD_M15, 1);
      if(sl==TRUE){
         if(MathRound(closeM5-ma)/Point<=500)
            {_sl=600;}
         else _sl=MathRound(closeM5-ma)/Point;
      double StopLoss=1.5*_sl*Point;
      double TakeProfit=1.5*_sl*Point;
      if (_sl == 0) {SL = 0;} else{SL = Bid + 1.5*_sl*Point;}
      if (_sl == 0){ TP = 0;} else{TP = Bid - 1.5*_sl*Point;}
      }
    if(sl==FALSE) {
                    _sl=SL_initial;
                    Alert(_sl);
                    if (_sl == 0) {SL = 0;} else{SL = Bid + 1.5*_sl*Point;}
                    if (_sl == 0){ TP = 0;} else{TP = Bid - 1.5*_sl*Point;}
      StopLoss=1.5*_sl*Point;
      TakeProfit=1.5*_sl*Point;
                   }
      Lots=(((AccountBalance() * MaximumRisk / 100) / (StopLoss)));
      lots=Lots;
      res=OrderSend(Symbol(),OP_SELL,NormalizeLots(lots,Symbol()),Bid,3,SL,TP,EA_Comment,MAGICMA,0,Red);
      return;
     }
      //--- BUY
   if(TimeCurrent()>StrToTime(TradeStartTime) && TimeCurrent()<StrToTime(TradeStopTime)
     && (ma4>ma12) && CandleBuy && !isBlackHammer01 && !neutralBuy && !WarningTradeBuy && !WarningTradeBuy1)
     {
      ma=iMA(NULL,PERIOD_M15,1,0,MODE_EMA,PRICE_TYPICAL,1);
      closeM5=iClose(Symbol(), PERIOD_M15, 1);
      if(sl==TRUE){
         if(MathRound(ma-closeM5)/Point<=500)
            {_sl=600;}
         else _sl=MathRound(ma-closeM5)/Point;
      StopLoss=1.5*_sl*Point;
      TakeProfit=1.5*_sl*Point;
      Alert(_sl);
      if (_sl == 0) {SL = 0;} else{SL = Ask - 1.5*_sl*Point;}
      if (_sl == 0){ TP = 0;} else{TP = Ask + 1.5*_sl*Point;}
      }
    if(sl==FALSE) {
                    _sl=SL_initial;
                    Alert(_sl);
                    if (_sl == 0) {SL = 0;} else{SL = Ask - 1.5*_sl*Point;}
                    if (_sl == 0){ TP = 0;} else{TP = Ask + 1.5*_sl*Point;}
      StopLoss=1.5*_sl*Point;
      TakeProfit=1.5*_sl*Point;
                   }
      Lots=(((AccountBalance() * MaximumRisk / 100) / (StopLoss)));
      lots=Lots;
      res=OrderSend(Symbol(),OP_BUY,NormalizeLots(lots,Symbol()),Ask,3,SL,TP,EA_Comment,MAGICMA,0,Blue);
      return;
     }
 

Hi,

it looks a little bit difficult, but one way to solve your problem is to save the AccountBalance
in a file on the first day of the month and read the value on the other days out of that file.

Best regards,
 
Werner Klehr:

Hi,

it looks a little bit difficult, but one way to solve your problem is to save the AccountBalance
in a file on the first day of the month and read the value on the other days out of that file.

Best regards,
Can you give a little function to save an information in the file and to read it?plz
 

Hi,

this little function should do what you want:

double GetInitAccount()
{
        int FileHandle;
        string FileName = "MyAccountBalance.dat";
        double InitAccount = 0.0;

        if ((TimeDay(TimeCurrent()) == 1) && (TimeHour(TimeCurrent()) == 6) && (TimeMinute(TimeCurrent()) == 0) && (TimeSeconds(TimeCurrent()) < 5))
        {
                FileHandle = FileOpen(FileName, FILE_WRITE); 
                if (FileHandle == INVALID_HANDLE)
                {
                        Print("FileOpen failed / FileName: " + FileName + " / Error: " + IntegerToString(GetLastError()));
                        return(-1.0);
                }

                InitAccount = AccountBalance();
        
                FileWriteDouble(FileHandle, InitAccount);
                FileClose(FileHandle);
        }
        else
        {
                FileHandle = FileOpen(FileName, FILE_READ); 
                if (FileHandle == INVALID_HANDLE)
                {
                        Print("FileOpen failed / FileName: " + FileName + " / Error: " + IntegerToString(GetLastError()));
                        return(-1.0);
                }
        
                FileReadDouble(FileHandle, InitAccount);
                FileClose(FileHandle);
        }

        return(InitAccount);
}

The file will be saved in the MQL4/Files subfolder.

I hope this will help you.

Best regards,

 
Werner Klehr:

Hi,

this little function should do what you want:

The file will be saved in the MQL4/Files subfolder.

I hope this will help you.

Best regards,

Thanks so much, I will try it now
 

Hi,

i'm sorry, but i think i forgot a little thing: you must use the FILE_BIN Option in the FileOpen statement - the
correct code should look like this:

double GetInitAccount()
{
        int FileHandle;
        string FileName = "MyAccountBalance.dat";
        double InitAccount = 0.0;

        if ((TimeDay(TimeCurrent()) == 1) && (TimeHour(TimeCurrent()) == 6) && (TimeMinute(TimeCurrent()) == 0) && (TimeSeconds(TimeCurrent()) < 5))
        {
                FileHandle = FileOpen(FileName, FILE_WRITE | FILE_BIN); 
                if (FileHandle == INVALID_HANDLE)
                {
                        Print("FileOpen failed / FileName: " + FileName + " / Error: " + IntegerToString(GetLastError()));
                        return(-1.0);
                }

                InitAccount = AccountBalance();
        
                FileWriteDouble(FileHandle, InitAccount);
                FileClose(FileHandle);
        }
        else
        {
                FileHandle = FileOpen(FileName, FILE_READ | FILE_BIN); 
                if (FileHandle == INVALID_HANDLE)
                {
                        Print("FileOpen failed / FileName: " + FileName + " / Error: " + IntegerToString(GetLastError()));
                        return(-1.0);
                }
        
                FileReadDouble(FileHandle, InitAccount);
                FileClose(FileHandle);
        }

        return(InitAccount);
}

Best regards,

 
Werner Klehr:

Hi,

i'm sorry, but i think i forgot a little thing: you must use the FILE_BIN Option in the FileOpen statement - the
correct code should look like this:


Best regards,


Thanks so much sir

 
Werner Klehr:

Hi,

i'm sorry, but i think i forgot a little thing: you must use the FILE_BIN Option in the FileOpen statement - the
correct code should look like this:


Best regards,

The initial balance does not give a value :-( SO the lot did not change

this is my new code

//START FUNCTION

//TIME FILTER
   string TradeStartTime = "03:30";
   string TradeStopTime = "23:00";
   //END TIME FILTER
   double InitAccount;

//--- go trading only for first tiks of new bar
   if(Volume[0]>1) return;

//--- SELL 
   ma12=iMA(NULL,PERIOD_M15,12,0,MODE_EMA,PRICE_MEDIAN,1);
   ma4=iMA(NULL,PERIOD_M15,4,0,MODE_EMA,PRICE_TYPICAL,1);//--- sell conditions
   if(TimeCurrent()>StrToTime(TradeStartTime) && TimeCurrent()<StrToTime(TradeStopTime)
     && (ma4<ma12) && CandleSell && !isWhiteHammer01 && !neutralSell && !WarningTradeSell && !WarningTradeSell1)
     {
      ma=iMA(NULL,PERIOD_M15,1,0,MODE_EMA,PRICE_TYPICAL,1);
      closeM5=iClose(Symbol(), PERIOD_M15, 1);
      if(sl==TRUE){
         if(MathRound(closeM5-ma)/Point<=500)
            {_sl=600;}
         else _sl=MathRound(closeM5-ma)/Point;
      double StopLoss=1.5*_sl*Point;
      double TakeProfit=1*_sl*Point;
      if (_sl == 0) {SL = 0;} else{SL = Bid + 1.5*_sl*Point;}
      if (_sl == 0){ TP = 0;} else{TP = Bid - 1*_sl*Point;}
      }
    if(sl==FALSE) {
                    _sl=SL_initial;
                    if (_sl == 0) {SL = 0;} else{SL = Bid + 1.5*_sl*Point;}
                    if (_sl == 0){ TP = 0;} else{TP = Bid - 1.5*_sl*Point;}
      StopLoss=1.5*_sl*Point;
      TakeProfit=1.5*_sl*Point;
                   }
      GetInitAccount();
      Alert("Initial balance:",InitAccount);   //initial balance 0
      Lots=(((InitAccount * MaximumRisk / 100) / (StopLoss)));
      lots=Lots;
      res=OrderSend(Symbol(),OP_SELL,NormalizeLots(lots,Symbol()),Bid,3,SL,TP,EA_Comment,MAGICMA,0,Red);
      return;
     }
      //--- BUY
   if(TimeCurrent()>StrToTime(TradeStartTime) && TimeCurrent()<StrToTime(TradeStopTime)
     && (ma4>ma12) && CandleBuy && !isBlackHammer01 && !neutralBuy && !WarningTradeBuy && !WarningTradeBuy1)
     {
      ma=iMA(NULL,PERIOD_M15,1,0,MODE_EMA,PRICE_TYPICAL,1);
      closeM5=iClose(Symbol(), PERIOD_M15, 1);
      if(sl==TRUE){
         if(MathRound(ma-closeM5)/Point<=500)
            {_sl=600;}
         else _sl=MathRound(ma-closeM5)/Point;
      StopLoss=1.5*_sl*Point;
      TakeProfit=1.5*_sl*Point;
      Alert(_sl);
      if (_sl == 0) {SL = 0;} else{SL = Ask - 1.5*_sl*Point;}
      if (_sl == 0){ TP = 0;} else{TP = Ask + 1.5*_sl*Point;}
      }
    if(sl==FALSE) {
                    _sl=SL_initial;
                    if (_sl == 0) {SL = 0;} else{SL = Ask - 1.5*_sl*Point;}
                    if (_sl == 0){ TP = 0;} else{TP = Ask + 1.5*_sl*Point;}
      StopLoss=1.5*_sl*Point;
      TakeProfit=1.5*_sl*Point;
                   }
      GetInitAccount();
      Alert("Initial balance:",InitAccount);   //initial balance 0
      Lots=(((InitAccount * MaximumRisk / 100) / (StopLoss)));
      lots=Lots;
      res=OrderSend(Symbol(),OP_BUY,NormalizeLots(lots,Symbol()),Ask,3,SL,TP,EA_Comment,MAGICMA,0,Blue);
      return;
     }
//END START
//EXTERN FUNCTION
double GetInitAccount()
{
        int FileHandle;
        string FileName = "MyAccountBalance.dat";
        double InitAccount = 0.0;

        if ((TimeDay(TimeCurrent()) == 1) && (TimeHour(TimeCurrent()) == 6) && (TimeMinute(TimeCurrent()) == 0) && (TimeSeconds(TimeCurrent()) < 5))
        {
                FileHandle = FileOpen(FileName, FILE_WRITE | FILE_BIN); 
                if (FileHandle == INVALID_HANDLE)
                {
                        Print("FileOpen failed / FileName: " + FileName + " / Error: " + IntegerToString(GetLastError()));
                        return(-1.0);
                }

                InitAccount = AccountBalance();
        
                FileWriteDouble(FileHandle, InitAccount);
                FileClose(FileHandle);
        }
        else
        {
                FileHandle = FileOpen(FileName, FILE_READ | FILE_BIN); 
                if (FileHandle == INVALID_HANDLE)
                {
                        Print("FileOpen failed / FileName: " + FileName + " / Error: " + IntegerToString(GetLastError()));
                        return(-1.0);
                }
        
                FileReadDouble(FileHandle, InitAccount);
                FileClose(FileHandle);
        }

        return(InitAccount);
}
//END EXTERN FUNCTION
Files:
Capture.PNG  72 kb
 
Werner Klehr:

Hi,

i'm sorry, but i think i forgot a little thing: you must use the FILE_BIN Option in the FileOpen statement - the
correct code should look like this:


Best regards,

I tried to Alert the initial balance after reading the write file for verification but it does not give a value, however it should give the balance at (TimeDay(TimeCurrent()) == 4) && (TimeHour(TimeCurrent()) == 6) && (TimeMinute(TimeCurrent()) == 0) && (TimeSeconds(TimeCurrent()) < 5)

int FileHandle;
        string FileName = "MyAccountBalance";
        double InitAccount = 0.0;

        if ((TimeDay(TimeCurrent()) == 4) && (TimeHour(TimeCurrent()) == 6) && (TimeMinute(TimeCurrent()) == 0) && (TimeSeconds(TimeCurrent()) < 5))
        {
                FileHandle = FileOpen(FileName, FILE_WRITE | FILE_BIN); 
                if (FileHandle == INVALID_HANDLE)
                {
                        Print("FileOpen failed / FileName: " + FileName + " / Error: " + IntegerToString(GetLastError()));
                        return(-1.0);
                }

                InitAccount = AccountBalance();
        
                FileWriteDouble(FileHandle, InitAccount);
                FileClose(FileHandle);
        }
        else
        {
                FileHandle = FileOpen(FileName, FILE_READ | FILE_BIN); 
                if (FileHandle == INVALID_HANDLE)
                {
                        Print("FileOpen failed / FileName: " + FileName + " / Error: " + IntegerToString(GetLastError()));
                        return(-1.0);
                }
                FileReadDouble(FileHandle, InitAccount);
                FileClose(FileHandle);
                Alert("Initial balance:",InitAccount);   //initial balance 0
        }
        return(InitAccount);
 
SteeveNi25: at (TimeDay(TimeCurrent()) == 4) && (TimeHour(TimeCurrent()) == 6) && (TimeMinute(TimeCurrent()) == 0) && (TimeSeconds(TimeCurrent()) < 5)

What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart.

 
William Roeder:

What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart.

So it should be like this?

isn't i?

at 

(TimeDay(TimeCurrent()) == 4) && (TimeHour(TimeCurrent()) == 6) && (TimeMinute(TimeCurrent()) == 0) && (TimeSeconds(TimeCurrent()) < 60)

Reason: