Your question is very common and has been asked many times. Please do a search in the Forum, CodeBase and Articles.
Here is one post of mine ... https://www.mql5.com/en/forum/390589#comment_28231377
But there are many other posts with different approaches and there are even articles dedicated to the subject. So, please do a search and study them
Hello Guys , i want to Create a personal panel for trading buy but I have a problem... im new in Programming and The problem I have now is that : i want Calculate Position Size with My account Balance and my risk buy i dont know how to do that ...
My stoploss is not fixed and I consider it according to the length of the previous candle(candleback)
What do you mean here ?
For example, in the candleback variable, I enter the number 3, so the stop loss should be determined according to the length of the low to high of the previous 3 candles.
Yes, I want the risk that is taken for the trade to be according to the size of the previous candles. That is, if I enter 1% risk, I will risk 1% of my account balance for the size of this candle. I don't know how to do this...
I see , try this function , if it returns 0.0 it means there is an error
#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){ //reset the error ResetLastError(); //error collector int errors=0; //get tick value for one lot for asset double tvol=(double)SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE); //collect errors if(GetLastError()!=0){errors+=GetLastError();ResetLastError();} //get min and max lot double minlot=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN); //collect errors if(GetLastError()!=0){errors+=GetLastError();ResetLastError();} double maxlot=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX); //collect errors if(GetLastError()!=0){errors+=GetLastError();ResetLastError();} //lot step double lotstep=(double)SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP); //collect errors if(GetLastError()!=0){errors+=GetLastError();ResetLastError();} //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() { }
if(GetLastError()!=0){errors+=GetLastError();ResetLastError();}You read last error, set it to zero, checked if it was non-zero, added zero, and set it to zero again.
GetLastError - Checkup - MQL4 Reference
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.
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
It says in the docs (of mql5) that calling the GetLastError() does not reset it .
Its not the same in mql4 ?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello Guys , i want to Create a personal panel for trading buy but I have a problem... im new in Programming and The problem I have now is that : i want Calculate Position Size with My account Balance and my risk buy i dont know how to do that ...
My stoploss is not fixed and I consider it according to the length of the previous candle(candleback)