How to store last ADX cross signal in a variable in EA in MT4

 

Hi

I am using ADX cross EA. At the moment it buys and sells only when +DI and -DI crosses. Could someone please help as below


If No trades exist
   then place trade based on LAST +Di and -DI cross.


I have tried to store the last cross signal value in "static double" under  void OnTick(void), BUT ITS NOT changing when +DI and-DI crosses next time. And the code I have tried is below. Help please


if (adxSig()>0)
static double lastsignal =adxSig();

  {
    
     if(adxSig()==1)bool BUYS=true;
      {
      Sleep(1000);
      Buy here
     
      if(adxSig()==2)bool SELLS=true;
      Sleep(1000);
      Sell here
 
      }
  
 }

Print(lastsignal);
 
Sudha365:


Do not double post!

I have deleted your other post.

In future please post in the correct section.

I will move this to the MQL4 and MT4 section

 
Keith Watford:

Do not double post!

I have deleted your other post.

In future please post in the correct section.

I will move this to the MQL4 and MT4 section

Sorry, will post future questions under the correct section
 
Sudha365: I have tried to store the last cross signal value in "static double" under  void OnTick(void), BUT ITS NOT changing when +DI and-DI crosses next time. And the code I have tried is below. Help please
static double lastsignal =adxSig();
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 - MQL4 programming forum
 
William Roeder:
Global and static variables work exactly the same way in MT4/MT5/C/C++.
  1. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum
int init(){
   OnInitFunction(); OnInitFunction2 ..
   :
}
TYPE functionVariable; void OnInitFunction(){ functionVariable = 0; }
TYPE Function(){
   if (functionVariable == 0){ functionVariable = ...
Thank you. I have copied and pasted your above code from other question.
Still , it is hard for me to follow or convert this into a workable solution. Could you please let me know how to simplyfy this ?
 
Sudha365 Could you please let me know how to simplyfy this ?
I gave you a template. Only you know what you need. That is a simple as possible without specifics.
  1. MT4: Learn to code it.
    MT5: Begin learning to code it. If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into yours.
  2. or pay (Freelance) someone to code it.
              Hiring to write script - General - MQL5 programming forum
We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help
Reason: