trailing a number

 

Hello, Greeting, I want to change a number according to the changes in the prices of ask and bid. It means that, for example, for every two point change in each of these two ask and bid prices, my number changes by one point, just like what happens in the trail of a position. Can you tell which part of this code has a problem?


void OnTick()

    {

     X = NormalizeDouble(iClose(_Symbol, 0, 10), _Digits);

     if(Ask + 2 * _Point < X)

         {

          X -= 2 * _Point;

          Print("", Ask);

          Print("1 ===>>>> X: ", X);

         }

     if(Bid - 2 * _Point > X)

         {

          X += 1 * _Point;

          Print("", Bid);

          Print("2 ===>>>> X: ", X);

         }

    }

Documentation on MQL5: Checkup / Point
Documentation on MQL5: Checkup / Point
  • www.mql5.com
Point - Checkup - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
  1. Use the Styler in the Editor (Strl+, or Tools => Styler) to 'beautify' your code.
  2. If you post code please use (and edit your code accordingly) the code button (Alt+S or the code button from the editor header line </>).
  3. It seems this is MT4 code - it has its own thread a little bit lower...
 
I fixed the points you mentioned, can you take a look again?
 
  1. Thank you :)
  2. NormalizeDouble is needless as the difference to 'not normalized' close is a lot less than _Point! Read this about it: https://www.mql5.com/en/forum/390336#comment_28182542
  3. If you want to have a nice printout use DoubleToString(...)!
  4. The prices on the chart and of iClose (iOpen, iHigh,..) are always 'made of' Bid prices.
  5. You'd better use 2.0 *_Point as otherwise _Point could be casted to an integer as 2 is an integer.
  6. Any time OnTick is called X is set to Close[10] so nothing is summed up,read about static variables: https://www.mql5.com/de/docs/basis/variables/static.
  7. Make sure that the indexing of iClose is the way you want it: https://www.mql5.com/en/docs/series/bufferdirection
Double value doesn't show Normalize digits
Double value doesn't show Normalize digits
  • 2022.03.07
  • www.mql5.com
Hello Everyone i used a normalize function for a double value but it doesn't show fixed digits in results...