Lot size - page 4

 
William Roeder:

Thanks so much william

it works well the backtesting on january using your function

#property copyright "Copyright © 2021"
#property link      "http://www.mql5.com/"
#property description "Author: STEEVE NIRINA"


#define MAGICMA  20131111
//--- Inputs
int Injection;
input double MaximumRisk   =0.02;
input double DecreaseFactor=3;
input int    MovingPeriod  =20;
input int    MovingShift   =0;
input string SL_COMMENT    ="Set sl to TRUE for automated the SL value";
input bool   sl            =true;
input double SL_initial    =500;
string EA_Comment="Steeve_Nirina_EA";
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//--- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
   double ma12;
   double ma4;
   int    res;
   double lots,Lots;
   double SL;
   double TP;
   double ma;
   double closeM5;
   double _sl;
   double InitAccount=GetInitAccount();
   
   
   //END
   
   //TIME FILTER
   string TradeStartTime = "03:30";
   string TradeStopTime = "23:00";
   //END TIME FILTER

//--- 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))
     {
      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;
                   }
      Print("InitAccount: ", DoubleToString(InitAccount));
      Lots=(((GetInitAccount() * 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))
     {
      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;
                   }
      Print("InitAccount: ", DoubleToString(InitAccount));
      Lots=(((GetInitAccount() * MaximumRisk / 100) / (StopLoss)));
      lots=Lots;
      res=OrderSend(Symbol(),OP_BUY,NormalizeLots(lots,Symbol()),Ask,3,SL,TP,EA_Comment,MAGICMA,0,Blue);
      return;
     }
//---
  }

//+------------------------------------------------------------------+
//| OnTick function                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//--- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false)
      return;
//--- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
//---
int maxDuration = 15 * 60; 
   for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
    OrderSelect(pos, SELECT_BY_POS) &&  OrderSymbol()== Symbol())
    {
      int duration = TimeCurrent() - OrderOpenTime();
      if (duration >= maxDuration) OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),3*Point);
    }
//---
  }

//make lots to right format
double NormalizeLots(double _lots,string pair="")
  {
   if(pair=="") pair=Symbol();
   double  lotStep=MarketInfo(pair,MODE_LOTSTEP),
   minLot=MarketInfo(pair,MODE_MINLOT);
   _lots=MathRound(_lots/lotStep)*lotStep;
   if(_lots<MarketInfo(pair,MODE_MINLOT)) _lots=MarketInfo(pair,MODE_MINLOT);
   if(_lots>MarketInfo(pair,MODE_MAXLOT)) _lots=MarketInfo(pair,MODE_MAXLOT);
   return(_lots);
  }

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

        FileHandle = FileOpen(FileName, FILE_READ | FILE_BIN); 
        if (FileHandle != INVALID_HANDLE)
        {
                TmpValue    = FileReadDouble(FileHandle, DOUBLE_VALUE);
                InitAccount = FileReadDouble(FileHandle, DOUBLE_VALUE);
                FileClose(FileHandle);

                LastMonth = TimeMonth((datetime) TmpValue);
        }

        if (isNewMonth())
        {
                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, (double) TimeCurrent());    
                FileWriteDouble(FileHandle, InitAccount);
                FileClose(FileHandle);
        }

        return(InitAccount);
}

bool isNewMonth(){
 static int monthCurrent  = 0; 
 int monthPrevious = monthCurrent; 
 monthCurrent  = Month(); 
 return monthCurrent != monthPrevious; 
 }
 

Hi Steeve,

of course, the function will not work for backtesting (how should it know how the balance was in January?)
Maybe it would help to delete the "MyAccountBalance.dat" file manualy before backtesting. Another solution
maybe to return a fixed value for backtesting. E.g. something like this at the beginn of the GetInitAccount function:


...

íf (IsTesting()) return(10000.0);

...

Best regards,

 
Werner Klehr:

Hi Steeve,

of course, the function will not work for backtesting (how should it know how the balance was in January?)
Maybe it would help to delete the "MyAccountBalance.dat" file manualy before backtesting. Another solution
maybe to return a fixed value for backtesting. E.g. something like this at the beginn of the GetInitAccount function:


Best regards,

I will test it too for more axperience :-)

 
Werner Klehr:

Hi Steeve,

of course, the function will not work for backtesting (how should it know how the balance was in January?)
Maybe it would help to delete the "MyAccountBalance.dat" file manualy before backtesting. Another solution
maybe to return a fixed value for backtesting. E.g. something like this at the beginn of the GetInitAccount function:


Best regards,

work very well 

Thanks for helping

Reason: