pls help me to Calculate Position Size with Balance. - page 2

 

Ah , yeah it sets it to zero

int OnInit()
  {
//---
  while(!EventSetMillisecondTimer(44)){
       Sleep(44);
       }
//---
   return(INIT_SUCCEEDED);
  }
void OnTimer(){
  EventKillTimer();
  ResetLastError();
  int errors=0;
  double tick=SymbolInfoDouble("NICKELBACK",SYMBOL_ASK);
  Print(GetLastError());
  Print(GetLastError());
  ExpertRemove();
  }

Thanks @William Roeder

Here is the updated code @aliasgari524

#property copyright "forum thread"
#property link      "https://www.mql5.com/en/forum/448801"
#property version   "1.00"
#property strict
input int bar_offset=1;//bar offset to acquire size 
input double risk=1;//risk %

double get_lot_by_bar_size_and_risk(int _bar,double _risk){
//error collector
  int errors=0;
//get tick value for one lot for asset
  double tvol=(double)SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);
  //collect errors 
    errors+=GetLastError();
//get min and max lot
  double minlot=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
  //collect errors 
    errors+=GetLastError();
  double maxlot=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);
  //collect errors 
    errors+=GetLastError();
//lot step
  double lotstep=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
  //collect errors
    errors+=GetLastError();
//if no errors 
  if(errors==0){
  //get size of bar 
    if(_bar<Bars){
    double size_in_ticks=(High[_bar]-Low[_bar])/_Point;
    double amount_to_lose=(AccountBalance()/100.0)*_risk;
    //so you are intending to lose the above amount in the above ticks
    //   you can calculate the target value per tick based on the above 
    double tick_value_target=amount_to_lose/size_in_ticks;
    //we know the value of 1 tick if we had a trade of 1 lot
      //so if we divide the target tick value by the one lot tick value we get the lots
    double lot=tick_value_target/tvol;
    //check if within limits 
      if(lotstep>0.0){
      int steps=(int)MathFloor(lot/lotstep);
      lot=((double)steps)*lotstep;
      }
    if(lot<minlot){lot=minlot;}
    if(lot>maxlot){lot=maxlot;}
    return(lot);
    }
  }
return(0.0);
}
int OnInit()
  {
//---
  //don't call oninit 
    while(!EventSetMillisecondTimer(44)){
    Print("Setting timer please wait");
    Sleep(44);
    } 
//---
   return(INIT_SUCCEEDED);
  }

void OnTimer(){
EventKillTimer();
double lot=get_lot_by_bar_size_and_risk(bar_offset,risk);
Print("Lot to use ("+DoubleToString(lot,2)+")");
ExpertRemove();
}
void OnDeinit(const int reason)
  {
  }
void OnTick()
  {
  }

 
William Roeder #:
You read last error, set it to zero, checked if it was non-zero, added zero, and set it to zero again.

Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error.

Thank you bro

 
Lorentzos Roussos #:

Ah , yeah it sets it to zero

Thanks @William Roeder

Here is the updated code @aliasgari524

  thank you, you always help me...