Publishing in CodeBase - page 4

 
Osmar Sandoval Espinosa #:
Here is the code attached!

For me, this code fails validation before the test on M1 is run.


 

Osmar Sandoval Espinosa #:
Here is the code attached!

               if(CheckMoneyForTrade(_Symbol, minLot, ORDER_TYPE_BUY))  // Checking if there is enough money
                 {
                  double tp = rangeHigh + (rangeHigh -rangeLow);        // calculating the tp for the buy: rangehigh + size of the range
                  trade.Buy(minLot, _Symbol, 0, rangeLow, tp);          // entering a buy position
                  isTrade = false;                                      // deactivating the possibility to trade (only 1 trade a day)
                 }
               else
                 {
                  Print("Not enough margin for Buy trade");
                  isTrade = false;                                      // deactivating the possibility to trade (only 1 trade a day)
                 }

There are two potential causes of the invalid stops error visible to the naked eye:

  1. You do not normalize the price of SL/TP.
  2. You don't check the stop level.

You should learn how to properly normalize the price and how to check the stop level before placing a SL/TP.

[edit] By stop level I mean SYMBOL_TRADE_STOPS_LEVEL

 
Vladislav Boyko #:

There are two potential causes of the invalid stops error visible to the naked eye:

  1. You do not normalize the price of SL/TP.
  2. You don't check the stop level.
You should learn how to properly normalize the price and how to check the stop level before placing a SL/TP.

This version should have those corrections: 

 
Osmar Sandoval Espinosa #:

This version should have those corrections: 

double tp = rangeHigh + (rangeHigh - rangeLow);             // calculating the tp for the buy: rangehigh + size of the range

There is still no SYMBOL_TRADE_STOPS_LEVEL check and you should normalize the tp variable.

Furthermore, NormalizeDouble isn't the best way to normalize prices. Although it might even be sufficient for the validator (I haven't tested it), if you're committed to writing robust code, see the post below.

Forum on trading, automated trading systems and testing trading strategies

How To Manage 0.5 or 0.05 Price Steps (Tick Value, Tick Size) in order to avoid Error 130

Fernando Carreiro, 2018.01.04 12:13

You must NOT use NormalizeDouble(). You must use the Tick-Size to correctly set the price values. This has been discussed on the forum many times, so do a search but the most common post you will see goes something like this ...

In essence, make sure that your price quotes, are properly aligned to the Tick size (see following examples).

...
double tickSize = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
...
double normalised_price = round( price / tick_size ) * tick_size;
...
// Or use a function
double Round2Ticksize( double price )
{
   double tick_size = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE );
   return( round( price / tick_size ) * tick_size );
}

You may also benefit from reading this: https://www.mql5.com/en/forum/223705#comment_6279080