- No Idea. Use the debugger or print out your variables, including _LastError
and prices and find
out why. At the OrderSend.
-
double lots = NormalizeDouble(AccountFreeMargin() * Risk/100 / 1000.0, LotDigits);
Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total to the account.Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage. No SL means you have infinite risk.
- You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
- AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
- Do NOT use TickValue by itself - DeltaPerLot
and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or
whether it is returning a value in the instrument's base currency.
MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19 - You must normalize lots properly and check against min and max.
- You must also check FreeMargin to avoid stop out
Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
else lots=NormalizeDouble(entry_amount,Digits);
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int ORDER_SIZE () { //< 1> //< 2> double MAX_LOTS = SymbolInfoDouble ( SYMBOL , SYMBOL_VOLUME_MAX ) ; //< 3> double MIN_LOTS = SymbolInfoDouble ( SYMBOL , SYMBOL_VOLUME_MIN ) ; //< 4> double LOT_STEP = SymbolInfoDouble ( SYMBOL , SYMBOL_VOLUME_STEP ) ; //< 5> //< 6> double SIZE_LIMIT = ORDER_SIZE_LIMIT () ; //< 7> if ( SIZE_LIMIT < MIN_LOTS ) return NULL ; //< 8> //< 9> int STEPS = (int) MathFloor ( ( SIZE_LIMIT - MIN_LOTS ) / LOT_STEP ) ; //<10> //<11> ORDER_VOLUME = MIN_LOTS + LOT_STEP * STEPS ; //<12> //<13> if ( ORDER_VOLUME > MAX_LOTS ) ORDER_VOLUME = MAX_LOTS ; //<14> //<15> return true ; } //<16>
William Roeder:
Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total to the account.
Thanks for the feedback, i need to digest and understand what you have written as at first glance some went over my head.
In terms of the above, maybe that's where I have also messed up the formula, risk variable as in my syntax was a whole number - e.g. 2% in my case.
double lots = NormalizeDouble(AccountFreeMargin() * Risk/100 / 1000.0, LotDigits);
Ok, using the check volume code from https://www.mql5.com/en/forum/192896, i see the error message:
2020.04.01 09:09:32.854 2020.03.27 20:30:00 EATest AUDNZD,M15: CheckVolume: false Error Message: Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=0.10, the closest correct volume is 0.00
So, what is the difference between SYMBOL_VOLUME_MAX and MODE_MAXLOT? Have I just confused the two and used MODE_MAXLOT incorrectly? At least I am heading in the right direction now. Thanks @William Roeder
@Airat Safin your code is starting to make a little more sense to me now.

- 2017.05.18
- www.mql5.com
AIS1 CODE FRAGMENTS => AS IS
/******************************************************************************/ // AIS1 SUPERROBOT : SAMPLE 5 : AIS1 TRADE MACHINE // //============================================================================// // AIS1_CONTROL : DATA : INPUT 1 //< > //------------------------------------------------------------------------//<--> input string s31="== RISK =========" ; /* ==== RISK MANAGEMENT ======= */ //< 1> input double RISK_EQUITY = 0.99 ; /* RISK LIMIT: FOR DEPOSIT */ //< 2> input double RISK_ORDER = 0.0002 ; /* RISK LIMIT: FOR 1 ORDER */ //< 3> input double ORDER_LOTS = 0.0 ; /* LOTS: CONSTANT ORDER VOLUME */ //< 4> //< 5> double LIMIT_DRAWDOWN = RISK_EQUITY - RISK_ORDER ; //< 6> //< 7> double LIMIT_ACCOUNT = 1.0 - LIMIT_DRAWDOWN ; //< 8> //< 9> //<10> //<11> //<12> //<13> //<14> //<15> //<16> //<17> //============================================================================// // AIS AIRAT SAFIN 2020-03-28 11:35 // /******************************************************************************/
/******************************************************************************/ // AIS1 SUPERROBOT : SAMPLE 5 : AIS1 TRADE MACHINE // //============================================================================// // AIS1_TRADING : DATA 3 : FIELDS OF ORDER TO SEND //< > //------------------------------------------------------------------------//<--> ENUM_ORDER_TYPE ORDER_TYPE = ENUM_ORDER_TYPE (EMPTY) ; //< 1> double ORDER_VOLUME = NULL ; //< 2> double ORDER_TAKE = NULL ; //< 3> double ORDER_STOP = NULL ; //< 4> //< 5> double ORDER_PRICE = NULL ; //< 6> //< 7> //< 8> //< 9> //<10> //<11> //<12> //<13> //<14> //<15> //<16> //<17> //============================================================================// // AIS AIRAT SAFIN 2020-03-24 14:11 // /******************************************************************************/
/******************************************************************************/ // AIS1 SUPERROBOT : SAMPLE 5 : AIS1 TRADE MACHINE // //============================================================================// // AIS1_ORDER : DATA 1 : ORDER SIZE COMPUTING //< > //------------------------------------------------------------------------//<--> double MARGIN_1_LOT = NULL ; //< 1> double POINT_VALUE = NULL ; //< 2> //< 3> int POSITION_POINTS = NULL ; //< 4> int RISK_POINTS = NULL ; //< 5> //< 6> double VAR_LIMIT = NULL ; //< 7> double RISK_POINT_VALUE = NULL ; //< 8> //< 9> double POSITION_LIMIT = NULL ; //<10> //<11> //<12> //<13> //<14> //<15> //<16> //<17> //============================================================================// // AIS AIRAT SAFIN 2020-03-28 19:40 // /******************************************************************************/
/******************************************************************************/ // AIS1 SUPERROBOT : SAMPLE 5 : AIS1 TRADE MACHINE // //============================================================================// // AIS1_ORDER : INTERFACE //< > //------------------------------------------------------------------------//<--> int AIS1_ORDER_RUN () { //< 1> //< 2> if ( ORDER_SIZE () == true ) //< 3> { //< 4> ORDER_FILL () ; //< 5> ORDER_SEND () ; //< 6> } //< 7> //< 8> //< 9> //<10> //<11> //<12> //<13> //<14> //<15> //<16> return true ; } //<17> //============================================================================// // AIS AIRAT SAFIN 2020-03-31 16:02 // /******************************************************************************/
/******************************************************************************/ // AIS1 SUPERROBOT : SAMPLE 5 : AIS1 TRADE MACHINE // //============================================================================// // AIS1_ORDER : FUNCTION 1 //< > //------------------------------------------------------------------------//<--> int ORDER_SIZE () { //< 1> //< 2> if ( COMMAND != OPEN ) { ORDER_VOLUME = POS_VOLUME ; return true ; } //< 3> //< 4> if ( ORDER_LOTS > 0 ) { ORDER_VOLUME = ORDER_LOTS ; return true ; } //< 5> //< 6> ORDER_SIZE_COMPUTE () ; //< 7> //< 8> if ( ORDER_VOLUME > 0 ) return true ; //< 9> else return NULL ; //<10> //<11> //<12> //<13> //<14> //<15> //<16> } //<17> //============================================================================// // AIS AIRAT SAFIN 2020-03-31 16:55 // /******************************************************************************/
/******************************************************************************/ // AIS1 SUPERROBOT : SAMPLE 5 : AIS1 TRADE MACHINE // //============================================================================// // AIS1_ORDER : FUNCTION 2 //< > //------------------------------------------------------------------------//<--> int ORDER_SIZE_COMPUTE () { //< 1> //< 2> double MAX_LOTS = SymbolInfoDouble ( SYMBOL , SYMBOL_VOLUME_MAX ) ; //< 3> double MIN_LOTS = SymbolInfoDouble ( SYMBOL , SYMBOL_VOLUME_MIN ) ; //< 4> double LOT_STEP = SymbolInfoDouble ( SYMBOL , SYMBOL_VOLUME_STEP ) ; //< 5> //< 6> double SIZE_LIMIT = ORDER_SIZE_LIMIT () ; //< 7> if ( SIZE_LIMIT < MIN_LOTS ) ORDER_VOLUME = NULL ; //< 8> //< 9> int STEPS = (int) MathFloor ( ( SIZE_LIMIT - MIN_LOTS ) / LOT_STEP ) ; //<10> //<11> ORDER_VOLUME = MIN_LOTS + LOT_STEP * STEPS ; //<12> //<13> if ( ORDER_VOLUME > MAX_LOTS ) ORDER_VOLUME = MAX_LOTS ; //<14> //<15> //<16> return true ; } //<17> //============================================================================// // AIS AIRAT SAFIN 2020-03-31 16:45 // /******************************************************************************/ // AIS1 SUPERROBOT : SAMPLE 5 : AIS1 TRADE MACHINE // //============================================================================// // AIS1_ORDER : FUNCTION 3 //< > //------------------------------------------------------------------------//<--> double ORDER_SIZE_LIMIT () { //< 1> //< 2> ORDER_SIZE_COMPUTE_NOMINAL_MARGIN () ; //< 3> ORDER_SIZE_COMPUTE_NOMINAL_POINT () ; //< 4> ORDER_SIZE_COMPUTE_ACTUAL_POINT () ; //< 5> ORDER_SIZE_COMPUTE_POSITION_LIMIT () ; //< 6> //< 7> return POSITION_LIMIT / MARGIN_1_LOT ; //< 8> //< 9> //<10> //<11> //<12> //<13> //<14> //<15> //<16> } //<17> //============================================================================// // AIS AIRAT SAFIN 2020-03-28 19:40 // /******************************************************************************/ // AIS1 SUPERROBOT : SAMPLE 5 : AIS1 TRADE MACHINE // //============================================================================// // AIS1_ORDER : FUNCTION 4 //< > //------------------------------------------------------------------------//<--> double ORDER_SIZE_COMPUTE_NOMINAL_MARGIN () { //< 1> //< 2> return OrderCalcMargin ( ORDER_TYPE , SYMBOL , 1 , ORDER_PRICE , //< 3> MARGIN_1_LOT ) ; //< 4> //< 5> /* MARGIN IN ACCOUNT CURRENCY FOR NOMINAL LOT SIZE 1 LOT */ //< 6> //< 7> //< 8> //< 9> //<10> //<11> //<12> //<13> //<14> //<15> //<16> } //<17> //============================================================================// // AIS AIRAT SAFIN 2020-03-28 19:40 // /******************************************************************************/ // AIS1 SUPERROBOT : SAMPLE 5 : AIS1 TRADE MACHINE // //============================================================================// // AIS1_ORDER : FUNCTION 5 //< > //------------------------------------------------------------------------//<--> int ORDER_SIZE_COMPUTE_NOMINAL_POINT () { //< 1> //< 2> double TICK_POINTS = SymbolInfoDouble //< 3> ( SYMBOL , SYMBOL_TRADE_TICK_SIZE ) ; //< 4> double TICK_VALUE = SymbolInfoDouble //< 5> ( SYMBOL , SYMBOL_TRADE_TICK_VALUE ) ; //< 6> //< 7> //< 8> POINT_VALUE = POINT * TICK_VALUE / TICK_POINTS ; //< 9> //<10> //<11> /* COST OF 1 POINT IN ACCOUNT CURRENCY FOR NOMINAL LOT SIZE 1 LOT */ //<12> //<13> //<14> //<15> //<16> return true ; } //<17> //============================================================================// // AIS AIRAT SAFIN 2020-03-28 19:40 // /******************************************************************************/ // AIS1 SUPERROBOT : SAMPLE 5 : AIS1 TRADE MACHINE // //============================================================================// // AIS1_ORDER : FUNCTION 6 //< > //------------------------------------------------------------------------//<--> int ORDER_SIZE_COMPUTE_ACTUAL_POINT () { //< 1> //< 2> // DISTANCE IN POINTS THAT GIVES PROFIT OR LOSS FOR OPEN POSITION //< 3> // EQUAL TO MARGIN FOR THIS POSITION //< 4> POSITION_POINTS = int ( MathRound ( MARGIN_1_LOT //< 5> / POINT_VALUE ) ) ; //< 6> // STOP DISTANCE IN POINTS //< 7> RISK_POINTS = int ( MathRound ( ORDER_RISK //< 8> / POINT ) ) ; //< 9> // RISK VALUE FOR 1 ORDER IN ACCOUNT CURRENCY //<10> VAR_LIMIT = AccountInfoDouble ( ACCOUNT_EQUITY ) //<11> * RISK_ORDER ; //<12> //<13> // COST OF 1 POINT OF STOP DISTANCE IN ACCOUNT CURRENCY //<14> RISK_POINT_VALUE = VAR_LIMIT / RISK_POINTS ; //<15> //<16> return true ; } //<17> //============================================================================// // AIS AIRAT SAFIN 2020-03-28 19:40 // /******************************************************************************/ // AIS1 SUPERROBOT : SAMPLE 5 : AIS1 TRADE MACHINE // //============================================================================// // AIS1_ORDER : FUNCTION 7 //< > //------------------------------------------------------------------------//<--> int ORDER_SIZE_COMPUTE_POSITION_LIMIT () { //< 1> //< 2> // MARGIN IN ACCOUNT CURRENCY THAT MATCHES VARLIMIT AND STOP DISTANCE //< 3> //< 4> POSITION_LIMIT = RISK_POINT_VALUE * POSITION_POINTS ; //< 5> //< 6> // MAXIMAL ALLOWED MARGIN CORRECTED BY VARLIMIT SIZE //< 7> // TO PREVENT STOP OUTS //< 8> //< 9> double MARGIN_LIMIT = AccountInfoDouble ( ACCOUNT_MARGIN_FREE ) //<10> - VAR_LIMIT ; //<11> //<12> if ( POSITION_LIMIT > MARGIN_LIMIT ) //<13> POSITION_LIMIT = MARGIN_LIMIT ; //<14> //<15> //<16> return true ; } //<17> //============================================================================// // AIS AIRAT SAFIN 2020-03-28 19:40 // /******************************************************************************/

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have a script which seems to be working but am trying to backtest so that I can improve on the results. But when I try to back test i get the following error:
I've read all the other similar posts and believe that I have the calcs correct (might be an incorrect assumption).
Function called like this:
Actual function (code based on what I've read here):
It seems to be returning all the correct values but is still failing with Error 131: