Lot Size being too big for some reason on cryptocurrenies

 

i was having this problem when OptimalLotSize runs it give me a big lot size for btc chart. Instead of a optimal lot size which is somehwere around 0.01-0.05 it gives me a lot size of 84+. So if any one got a answer to this would apricate it alot. 

BBStrategy code:  

//+------------------------------------------------------------------+
//|                                                   BBStrategy.mq4 |
//|                                              Entreprenuervisions |
//|                   https://www.instagram.com/entrepreneurvisions/ |
//+------------------------------------------------------------------+
#property copyright "Entreprenuervisions"
#property link      "https://www.instagram.com/entrepreneurvisions/"
#property version   "1.00"
#property strict
#property show_inputs
#include <customfuction.mqh>

int magicNB = 55555;
int bbPeriod = 20;
int band1Std = 1;
int band2Std = 4;
input double riskPerTrade = 0.01;

int orderID;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Alert("EA Has Started");
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Alert("EA Has Stop");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {//check
//---
   double bbLower1 = iBands(NULL,0,bbPeriod,band1Std,0,PRICE_CLOSE,MODE_LOWER,0);
   double bbUpper1 = iBands(NULL,0,bbPeriod,band1Std,0,PRICE_CLOSE,MODE_UPPER,0);
   double bbMid = iBands(NULL,0,bbPeriod,band1Std,0,PRICE_CLOSE,0,0);
   
   double bbLower2 = iBands(NULL,0,bbPeriod,band2Std,0,PRICE_CLOSE,MODE_LOWER,0);
   double bbUpper2 = iBands(NULL,0,bbPeriod,band2Std,0,PRICE_CLOSE,MODE_UPPER,0);
   
   if(!CheckIfOpenOrdersByMagicNB(magicNB)){
      if(Ask < bbLower1)//buying
      {
         
         Alert("Price is bellow bbLower1, Sending buy order");
         double stopLossPrice = NormalizeDouble(bbLower2,Digits);
         double takeProfitPrice = NormalizeDouble(bbMid,Digits);;
         Alert("Entry Price = " + Ask);
         Alert("Stop Loss Price = " + stopLossPrice);
         Alert("Take Profit Price = " + takeProfitPrice);
         
         double lotSize = OptimalLotSize(riskPerTrade,Ask,stopLossPrice);
         
         Alert("LotSize: "+ lotSize);
         
         //Send buy order
         //OrderSend(NULL,OP_BUY,0.01,Ask,10,stopLossPrice,takeProfitPrice);
         orderID = OrderSend(NULL,OP_BUY,lotSize,Ask,10,stopLossPrice,takeProfitPrice,NULL,magicNB);
         if(orderID < 0) Alert("Order is rejected. Order error: " + GetLastError());
         Alert(orderID);
      }
      else if(Bid > bbUpper1)//shorting
      {
         Alert("Price is above bbUpper1, Sending short order");
         double stopLossPrice = NormalizeDouble(bbUpper2,Digits);
         double takeProfitPrice = NormalizeDouble(bbMid,Digits);
         Alert("Entry Price = " + Bid);
         Alert("Stop Loss Price = " + stopLossPrice);
         Alert("Take Profit Price = " + takeProfitPrice);
          
          double lotSize = OptimalLotSize(riskPerTrade,Bid,stopLossPrice);
          
           Alert("LotSize: "+ lotSize);
           
          //Send short order
          orderID = OrderSend(NULL,OP_SELL,lotSize,Bid,10,stopLossPrice,takeProfitPrice,NULL,magicNB);
          if(orderID < 0) Alert("Order is rejected. Order error: " + GetLastError());
          Alert(orderID);
      }
    }
    else
    {
      Alert("order already open");
      if(OrderSelect(orderID,SELECT_BY_TICKET)== true)
      {
         int orderType = OrderType(); // 0 = long, 1 =short;
         
         /*double currentExitPoint;
         if(orderType == 0)
         {
            currentExitPoint = NormalizeDouble(bbLower2,Digits); 
         }
         else
         {
            currentExitPoint= NormalizeDouble(bbUpper2,Digits);
         }*/
         
         
         
         double currentMidLine = NormalizeDouble(bbMid,Digits);
         
         double TP = OrderTakeProfit();
         
         if(TP != currentMidLine)//|| SL != currentExitPoint
         {
           
            bool ans = OrderModify(orderID,OrderOpenPrice(),OrderStopLoss(),currentMidLine,0);
            if(ans == true)
            {
               Alert("Order Modified: " +orderID);
            }
         }
      }
    }
 }
//+------------------------------------------------------------------+

CustomFunction code:

Save
New
Duplicate & Edit
Just Text
//|                                            CustomFunctions01.mqh |
//|                                                    Mohsen Hassan |
//|                             https://www.MontrealTradingGroup.com |
//+------------------------------------------------------------------+
#property copyright "Mohsen Hassan"
#property link      "https://www.MontrealTradingGroup.com"
#property strict
//+------------------------------------------------------------------+
//| defines                                                          |
//+------------------------------------------------------------------+
// #define MacrosHello   "Hello, world!"
// #define MacrosYear    2010
//+------------------------------------------------------------------+
//| DLL imports                                                      |
//+------------------------------------------------------------------+
// #import "user32.dll"
//   int      SendMessageA(int hWnd,int Msg,int wParam,int lParam);
// #import "my_expert.dll"
//   int      ExpertRecalculate(int wParam,int lParam);
// #import
//+------------------------------------------------------------------+
//| EX5 imports                                                      |
//+------------------------------------------------------------------+
// #import "stdlib.ex5"
//   string ErrorDescription(int error_code);
// #import
//+------------------------------------------------------------------+


double CalculateTakeProfit(bool isLong, double entryPrice, int pips)
{
   double takeProfit;
   if(isLong)
   {
      takeProfit = entryPrice + pips * GetPipValue();
   }
   else
   {
      takeProfit = entryPrice - pips * GetPipValue();
   }
   
   return takeProfit;
}

double CalculateStopLoss(bool isLong, double entryPrice, int pips)
{
   double stopLoss;
   if(isLong)
   {
      stopLoss = entryPrice - pips * GetPipValue();
   }
   else
   {
      stopLoss = entryPrice + pips * GetPipValue();
   }
   return stopLoss;
}




double GetPipValue()
{
   if(_Digits >=4)
   {
      return 0.0001;
   }
   else
   {
      return 0.01;
   }
}


void DayOfWeekAlert()
{

   Alert("");
   
   int dayOfWeek = DayOfWeek();
   
   switch (dayOfWeek)
   {
      case 1 : Alert("We are Monday. Let's try to enter new trades"); break;
      case 2 : Alert("We are tuesday. Let's try to enter new trades or close existing trades");break;
      case 3 : Alert("We are wednesday. Let's try to enter new trades or close existing trades");break;
      case 4 : Alert("We are thursday. Let's try to enter new trades or close existing trades");break;
      case 5 : Alert("We are friday. Close existing trades");break;
      case 6 : Alert("It's the weekend. No Trading.");break;
      case 0 : Alert("It's the weekend. No Trading.");break;
      default : Alert("Error. No such day in the week.");
   }
}


double GetStopLossPrice(bool bIsLongPosition, double entryPrice, int maxLossInPips)
{
   double stopLossPrice;
   if (bIsLongPosition)
   {
      stopLossPrice = entryPrice - maxLossInPips * 0.0001;
   }
   else
   {
      stopLossPrice = entryPrice + maxLossInPips * 0.0001;
   }
   return stopLossPrice;
}


bool IsTradingAllowed()
{
   if(!IsTradeAllowed())
   {
      Print("Expert Advisor is NOT Allowed to Trade. Check AutoTrading.");
      return false;
   }
   
   if(!IsTradeAllowed(Symbol(), TimeCurrent()))
   {
      Print("Trading NOT Allowed for specific Symbol and Time");
      return false;
   }
   
   return true;
}
  
  
double OptimalLotSize(double maxRiskPrc, int maxLossInPips)
{

  double accEquity = AccountEquity();
  Print("accEquity: " + accEquity);
  
  double lotSize = MarketInfo(NULL,MODE_LOTSIZE);
  Print("lotSize: " + lotSize);
  
  double tickValue = MarketInfo(NULL,MODE_TICKVALUE);
  
  if(Digits <= 3)
  {
   tickValue = tickValue /100;
  }
  
  Print("tickValue: " + tickValue);
  
  double maxLossDollar = accEquity * maxRiskPrc;
  Print("maxLossDollar: " + maxLossDollar);
  
  double maxLossInQuoteCurr = maxLossDollar / tickValue;
  Print("maxLossInQuoteCurr: " + maxLossInQuoteCurr);
  
  double optimalLotSize = NormalizeDouble(maxLossInQuoteCurr /(maxLossInPips * GetPipValue())/lotSize,2);
  
  return optimalLotSize;
 
}


double OptimalLotSize(double maxRiskPrc, double entryPrice, double stopLoss)
{
   int maxLossInPips = MathAbs(entryPrice - stopLoss)/GetPipValue();
   return OptimalLotSize(maxRiskPrc,maxLossInPips);
}



bool CheckIfOpenOrdersByMagicNB(int magicNB)
{
   int openOrders = OrdersTotal();
   
   for(int i = 0; i < openOrders; i++)
   {
      if(OrderSelect(i,SELECT_BY_POS)==true)
      {
         if(OrderMagicNumber() == magicNB) 
         {
            return true;
         }  
      }
   }
   return false;
}