• Join us

Calling ATR Period for SL TP Calculation

To add comments, please log in or register
chuchu777
213
chuchu777  

If you have this in your code how can you call 'Period' to calculate SL TP or say MA which relies on this ATR value? ATR is dynamic and I want to capture the value with every tick for the next dynamic calculation. Thanks 

void OnTick()
{
//---
double atr = (iATR(NULL, _Period, Ind3Param0, 1) * 100);
double atrValue = NormalizeDouble(atr,0);
int    Period    =  atrValue; 
   
   }
   
chuchu777
213
chuchu777  
when I try calling it I get atrValue undeclared identifier. 
William Roeder
20339
William Roeder  
int    Period    =  atrValue;
Period is a function. Rename your variable.
          Checkup / Period - Reference on algorithmic/automated trading language for MetaTrader 5
chuchu777
213
chuchu777  
Did that, 
int    TakeProfit         =  atrValue * 3.5 * 10;
int    StopLoss           =  atrValue * 2 * 10 ;
int    Period_1    =  atrValue; 
doing this comes up with atrValue undeclared identifier if calling it from outside of void OnTick() 
William Roeder
20339
William Roeder  
chuchu777 if calling it from outside of void OnTick() 
  1. Of course it does. You defined atrValue inside OnTick.
  2. Global and static variables work exactly the same way in MT4/MT5/C/C++.
    1. They are initialized once on program load.
    2. They don't update unless you assign to them.
    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5 (or MT4 with strict which you should always use.)

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - Inflation - MQL4 programming forum
chuchu777
213
chuchu777  
William Roeder:
  1. Of course it does. You defined atrValue inside OnTick.
  2. Global and static variables work exactly the same way in MT4/MT5/C/C++.
    1. They are initialized once on program load.
    2. They don't update unless you assign to them.
    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5 (or MT4 with strict which you should always use.)

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - Inflation - MQL4 programming forum

Understood. What would be the method or function to capture ATR value on the fly and apply to something like SL/TP ?

William Roeder
20339
William Roeder  
double atr = (iATR(NULL, _Period, Ind3Param0, 1)
To add comments, please log in or register