Product Validation fail MQL5 only. [Invalid Stops] - page 3

 

In order to test it on a hedging account do I just need to change the Account Type under the Common Tab when publishing the EA to the market place? 

 
Tyler James Wanta #:In order to test it on a hedging account do I just need to change the Account Type under the Common Tab when publishing the EA to the market place? 

Yes!

EDIT: I believe you can also set both options, and have it test both.

 

I'm still getting [Invalid Stops] on a hedging account. Any other ideas?

test on EURUSD,H1 (hedging)
 2019.04.05 02:21:30   failed instant buy 2.8 EURUSD at 1.12236 sl: 1.12200 [Invalid stops]
 2019.04.11 03:30:06   failed instant sell 3.1 EURUSD at 1.12732 sl: 1.12763 [Invalid stops]
 2019.04.16 04:21:25   failed instant sell 3.2 EURUSD at 1.12999 sl: 1.13030 [Invalid stops]
 2019.04.18 03:00:02   failed instant sell 2.85 EURUSD at 1.12960 sl: 1.12995 [Invalid stops]
 2019.04.23 02:00:34   failed instant buy 3.1 EURUSD at 1.12607 sl: 1.12575 [Invalid stops]
 2019.04.29 02:39:30   failed instant sell 3.55 EURUSD at 1.11476 sl: 1.11507 [Invalid stops]
 2019.05.03 03:00:42   failed instant sell 2.9 EURUSD at 1.11714 sl: 1.11751 [Invalid stops]
 2019.05.10 02:41:25   failed instant buy 3 EURUSD at 1.12208 sl: 1.12174 [Invalid stops]
 2019.05.21 02:25:59   failed instant buy 2.6 EURUSD at 1.11701 sl: 1.11664 [Invalid stops]
 2019.05.22 02:18:42   failed instant buy 2.8 EURUSD at 1.11654 sl: 1.11620 [Invalid stops]
 2019.05.24 02:20:30   failed instant buy 2.65 EURUSD at 1.11841 sl: 1.11804 [Invalid stops]
 2019.05.27 02:01:34   failed instant buy 3.15 EURUSD at 1.12110 sl: 1.12079 [Invalid stops]
 2019.05.29 02:12:59   failed instant buy 3.05 EURUSD at 1.11662 sl: 1.11630 [Invalid stops]
 2019.05.30 08:49:18   failed instant sell 2.6 EURUSD at 1.11336 sl: 1.11373 [Invalid stops]
strategy tester report 53 total trades

I added code to check the freeze level before closing and that seemed to fix the modification errors so some progress was made. 

 
Tyler James Wanta #: I'm still getting [Invalid Stops] on a hedging account. Any other ideas? I added code to check the freeze level before closing and that seemed to fix the modification errors so some progress was made. 

It seems rather strange that it only seems to fail for "Instant Execution".

I am wondering if it has anything to do with re-quotes and the value you set for your maximum deviation. In other words, slippage!

What value are you using for deviation?

Also, as a test, can you force your stops to widen a bit more?

 

On MQL5, I use the CTrade class to place orders. I use:

 SetDeviationInPoints(0)

 


I added an extra 10 pips to each SL. I'm no longer getting [Invalid Stops] errors but I am getting these errors:

test on EURUSD,M1 (hedging)
 2020.02.03 02:38:40   current account state: Balance: 1.00, Credit: 0.00, Commission: 0.00, Accumulated: 0.00, Assets: 0.00, Liabilities: 0.00, Equity 1.00, Margin: 0.00, FreeMargin: 1.00
 2020.02.03 02:38:40   calculated account state: Assets: 0.00, Liabilities: 0.00, Equity 1.00, Margin: 221.64, FreeMargin: -220.64
 2020.02.03 02:38:40   not enough money [instant sell 0.2 EURUSD at 1.10821 sl: 1.11027]
 2020.02.03 02:38:40   failed instant sell 0.2 EURUSD at 1.10821 sl: 1.11027 [No money]
 2020.02.04 03:47:40   current account state: Balance: 1.00, Credit: 0.00, Commission: 0.00, Accumulated: 0.00, Assets: 0.00, Liabilities: 0.00, Equity 1.00, Margin: 0.00, FreeMargin: 1.00
 2020.02.04 03:47:40   calculated account state: Assets: 0.00, Liabilities: 0.00, Equity 1.00, Margin: 221.29, FreeMargin: -220.29
 2020.02.04 03:47:40   not enough money [instant buy 0.2 EURUSD at 1.10643 sl: 1.10471]
 2020.02.04 03:47:40   failed instant buy 0.2 EURUSD at 1.10643 sl: 1.10471 [No money]
 2020.02.04 04:12:40   current account state: Balance: 1.00, Credit: 0.00, Commission: 0.00, Accumulated: 0.00, Assets: 0.00, Liabilities: 0.00, Equity 1.00, Margin: 0.00, FreeMargin: 1.00
 2020.02.04 04:12:40   calculated account state: Assets: 0.00, Liabilities: 0.00, Equity 1.00, Margin: 221.14, FreeMargin: -220.14
 2020.02.04 04:12:40   not enough money [instant sell 0.2 EURUSD at 1.10568 sl: 1.10730]
 2020.02.04 04:12:40   failed instant sell 0.2 EURUSD at 1.10568 sl: 1.10730 [No money]
 2020.02.05 03:41:40   current account state: Balance: 1.00, Credit: 0.00, Commission: 0.00, Accumulated: 0.00, Assets: 0.00, Liabilities: 0.00, Equity 1.00, Margin: 0.00, FreeMargin: 1.00
 2020.02.05 03:41:40   calculated account state: Assets: 0.00, Liabilities: 0.00, Equity 1.00, Margin: 220.82, FreeMargin: -219.82
 2020.02.05 03:41:40   not enough money [instant sell 0.2 EURUSD at 1.10411 sl: 1.10559]
 2020.02.05 03:41:40   failed instant sell 0.2 EURUSD at 1.10411 sl: 1.10559 [No money]
 2020.02.05 10:22:40   current account state: Balance: 1.00, Credit: 0.00, Commission: 0.00, Accumulated: 0.00, Assets: 0.00, Liabilities: 0.00, Equity 1.00, Margin: 0.00, FreeMargin: 1.00
 2020.02.05 10:22:40   calculated account state: Assets: 0.00, Liabilities: 0.00, Equity 1.00, Margin: 220.93, FreeMargin: -219.93
 2020.02.05 10:22:40   not enough money [instant buy 0.2 EURUSD at 1.10466 sl: 1.10311]
 2020.02.05 10:22:40   failed instant buy 0.2 EURUSD at 1.10466 sl: 1.10311 [No money]
 2020.02.06 02:53:40   current account state: Balance: 1.00, Credit: 0.00, Commission: 0.00, Accumulated: 0.00, Assets: 0.00, Liabilities: 0.00, Equity 1.00, Margin: 0.00, FreeMargin: 1.00
there are no trading operation

I'm always risking 1 percent but it seems like the balance is set to $1? That seems really low for testing. Either way, I don't think these need to be worried about.

 

Tyler James Wanta #: On MQL5, I use the CTrade class to place orders. I use:

I added an extra 10 pips to each SL. I'm no longer getting [Invalid Stops] errors but I am getting these errors:

I'm always risking 1 percent but it seems like the balance is set to $1? That seems really low for testing. Either way, I don't think these need to be worried about.

Given that widening your stop did change things, tells me that slippage was, in part, involved in the issue.

As for the "no money" problem, that tells me that you are not properly adjusting your volume accordingly to both Stop-Loss risk and the Margin requirements.

There are just too many inconsistency. It would be necessary to do a proper and completely analysis of your code to debug the issues.

Since this is for a Market product, I can understand that you would not want to publish the entire code here in public.

 

"Given that widening your stop dis change things, tells me that slippage was, in part, involved in the issue."

Is there a specific way I should be addressing this? If slippage is involved in the issue, should I be allowing for more of it? Just increasing it doesn't seem like an actual solution, just a convenience to pass validation. 

"As for the "no money" problem, that tells me that you are not properly adjusting your volume accordingly to both Stop-Loss risk and the Margin requirements."

I don't think I am taking into account the Margin requirement. I assume you mean, to account for how much margin is left and only use as much as is available? This is all i'm doing for the lot size calculation. 

double EAOrderHelper::GetLotSizeForRiskPercent(string symbol, double stopLossPips, double riskPercent)
{
    double pipValue = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE) * 10 * SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE);

    // since UJ starts with USD, it also involves the current price
    // TODO: update this to catch any other pairs
    if (StringFind(symbol, "JPY") != -1)
    {
        MqlTick currentTick;
        if (!SymbolInfoTick(symbol, currentTick))
        {
            Print("Can't get tick during lot size calculation");
            return 0;
        }

        pipValue = pipValue / currentTick.bid;
    }

    return NormalizeDouble((AccountInfoDouble(ACCOUNT_BALANCE) * riskPercent / 100) / (stopLossPips * pipValue), 2);
}
 
Tyler James Wanta #: Is there a specific way I should be addressing this? If slippage is involved in the issue, should I be allowing for more of it? Just increasing it doesn't seem like an actual solution, just a convenience to pass validation. 

It is difficult to answer because it depends on both your strategy and how your code is implement. Since I don't have access to either, I am unable to offer much advice.

It usually happens when stops are too tight, and their values are too close to normal price deviation and/or average spread. Normally a trader with select a broker and account type appropriate for such a strategy, for example offering "Market Execution" with low spreads.

However, here, the verification process does not allow for that, so you have to design your code with default input values that minimise the issue when tested on "Instant Execution" instead of "Market Execution", or when spreads are higher.

Most probably, you will just need to have default inputs that widen the stop sufficiently to compensate for any slippage, but allowing the user to modify those inputs for tighter stops for actual live trading.

Tyler James Wanta #: I don't think I am taking into account the Margin requirement. I assume you mean, to account for how much margin is left and only use as much as is available? This is all i'm doing for the lot size calculation. 

When calculating your volume, you first calculate it based on maximum allowed risk for the stop-loss size (e.g. 1% risk of balance).

Then you see how much margin is required for that volume, and what percentage is acceptable. (e.g. 10% margin maximum).

And when the minimum allowed volume is beyond the allowed margin requirement, then no order is placed at all.

For MQL5, please read the following ... https://www.mql5.com/en/forum/446211#comment_46501011

For MQL4, your code is unfortunately incorrect. See the following (please note that I am taking a very conservative approach) ... https://www.mql5.com/en/forum/390589#comment_28231377 .

As for combining the risk and margin, my own current code is in library form, so I am unable to provide a current sample, but here is a very old post about it ... https://www.mql5.com/en/forum/148552/page3#comment_3734015

Obviously the last link is very old, and the code is completely outdated and needs to be revised, but it serves only to give you an idea about it. Nowadays, I don't use "pips", nor "points", and every thing is in "ticks" only.


By the way, to quote another person's text, first start your own post and position your cursor where you want to place the quote, then with your mouse, select/highlight the section of the other person's text and then click "reply" in the bottom-right corner of their post.

 
Tyler James Wanta #:

Ok. That makes sense. Thanks for clearing that part up. 


I tried normalizing the price like so:

(I know I should place code instead of images but the code formatting was not working)

but this still isn't working. In fact, it lead to a larger number of [Invalid Stops] errors when running validation. Any thoughts?

Error in your logic:

1. You only check if minstoplos>0.

=You should know that SYMBOL_TRADE_STOPS_LEVEL is not always greater than 0.

=Error [Invalid Stops] when running validation, because SYMBOL_TRADE_STOPS_LEVEL or minstoplos is 0.

2. You have to code what if minstoplos=0.

 
Fernando Carreiro #:
Most probably, you will just need to have default inputs that widen the stop sufficiently to compensate for any slippage, but allowing the user to modify those inputs for tighter stops for actual live trading.

Not ideal but it does work so I guess I'm fine with it.


Fernando Carreiro #:

When calculating your volume, you first calculate it based on maximum allowed risk for the stop-loss size (e.g. 1% risk of balance).

Then you see how much margin is required for that volume, and what percentage is acceptable. (e.g. 10% margin maximum).

And when the minimum allowed volume is beyond the allowed margin requirement, then no order is placed at all.

For MQL5, please read the following ... https://www.mql5.com/en/forum/446211#comment_46501011

I implemented a margin check for both MQl4 and 5 and they both seem to be working properly. Thanks for the resources.


Fernando Carreiro #:

For MQL4, your code is unfortunately incorrect. See the following (please note that I am taking a very conservative approach) ... https://www.mql5.com/en/forum/390589#comment_28231377 .

I compared the results from this approach to what I am currently doing and got the exact same result so I'm going to leave it as it is.


After implementing the margin checks, a bit extra SL padding, and switching the account type to Hedging, all validation passed. Thanks for all the help!