Help Working with Class Objects

 

Hi All, first post!

I wanted some help understanding Classes and Objects in MQL4. I have a question around making correct usage of them.

Is it possible to declare a Global Object from a Class as you can do with normal variables? I can create them OnTick and they persist until OnDeinit where I need to make use of them again, but they are not kept in memory for the OnDeinit section.

More Detail:

I have a class called MyOrder, it has a number of properties relating to to the Order, such as Open Price, Stop Loss, Expiry, Magic Number, Signal specific values and so on. Previously I used a simple Array as Double, which can be declared Globally. Because it is global I am able to populate it in the OnStart section from a Bin file, then use it during the OnTick section and finally write it to a Bin file OnDeinit. This is useful for retaining the information when the bot is restarted/modified.

I would like to replicate this but as a class object since working with class objects and methods makes the code much cleaner/easier to read and there is considerably less duplication of code as a result. Without being able to retain it between restarts somehow it is basically useless to me.

I've been reading everything I can on classes but can't seem to find an answer to my problem, any help is appreciated.


Thanks in advance,

Noki

 

Noki0100:

Is it possible to declare a Global Object from a Class as you can do with normal variables? I can create them OnTick and they persist until OnDeinit where I need to make use of them again, but they are not kept in memory for the OnDeinit section.

  1. Of course, exactly the same as any other data type. Global and static variables persist until the code is unloaded.

    OnInit is called at every initialization. The EA is not reloaded at every initialization.

    Initial load
    EA loaded: Globals, status reset. OnInit
    Recompile, parameter changed, account, template Deinit EA reloaded: Globals, status reset. OnInit
    Chart change (symbol, TF.)
    Deinit Not unloaded/reloaded*. OnInit
    Removed, chart close, terminal close, Init failed. Deinit Unloaded.

    * Indicators used to do the same thing, now they are always reloaded.

    Just to be precise, the documentation says: The OnDeinit() function run is restricted to 2.5 seconds

  2. All non-static variables you create in OnTick are deleted when OnTick exits. They can not be seen outside it.
 
William Roeder:
  1. Of course, exactly the same as any other data type. Global and static variables persist until the code is unloaded.
  2. All non-static variables you create in OnTick are deleted when OnTick exits. They can not be seen outside it.

Thanks for the response, I've been looking at this most of the day now. I can see that's it's possible but I'm missing the how to implement part. From my reading I think I am missing the 'constructor' portion, or I am not setting the global object correctly to being with.

To that end I have made a mini-version in a new file to help with debugging. But I am still stuck, would you mind looking at the attached file and prodding my in the right direction please and thank you?

Noki

Files:
 

SOLVED

Turns out it was as simple as moving the class to the beginning of the file ahead of the Global Scope object creation. Or moving it to an include file, again called before the Global scope.

Thanks for the help!

Noki