String type global variable

 

Is it possible to have a global variable of type String? Or must it be a double?


If not, are there any options other than writing to disk?

--

Patabugen

 

general answer is NO, type double as you stated it must be.

files, as you've also stated.

external database interface via #import eg, this may fire up yer gray cells: https://www.mql4.com/search/microsoft%20database

eg: "How to use MQL4 to retrive data outside of the platform?" at https://www.mql5.com/en/forum/112518

IF you only want to pass around data (in your case type string) in a one chart sandboxed environment such as between an applied EA and a run as required script, you could consider using objects.

eg: EA (as design requires or each time it's entered) can check for a specific named object of type OBJ_LABEL (can hold text in it's description via ObjectSetText())

the 'check' is to determine if the object exists. Analogy to using global vars as a semaphore mechanism for 2 or more programs eg: single user at a time resource access.

IF object exists THEN the script must've been run and it's telling me

(only me - as objects are a 'per chart' item, similar to local vars in a function() and only two programs will be involved in this example ie, excluding indicator programs applied to main window or any sub windows)

that it has data for me, in this case in the object's description which EA can get at via ObjectDescription()

NOTE: although description is of type string, any stuff can be in it, yes? eg, 1.12345 and/or ABC ...

and... EA must remember to ObjectDelete() when it has consumed this comms from the script else next time it is entered it 'sees' it again, and again, ...

Further, the script could be sitting there in a waited/sleep poll loop on this object 'until' it does NOT exist and then eg: if(ObjectFind(sName) == -1) return;

Or indeed, commence phase.N of a series of sequentially ordered comms with the EA, perhaps passing other snippets of data - which eg, been given to the script via it's extern inputs on start up. Ramble on, but lots of things could be done.

Even the idea, I read on some other post about a designer that did not want to use F7 on an EA (maybe it was in a while(true) loop, cuz F7 not work then) but... he wanted to have mechanism to alter the EA's extern inputs via a script which had an identical set (I'd use a common .mqh for that). Anyway, obviously, the script could now put this updated extern data wherever (as suggested in this post) and the EA would 'see' that was time to update it's extern's.

One simple way would be to have a mutually agreed filename and once EA became aware... it'd read the file and then perhaps delete it and delete whatever comms item used for initiating this transaction eg, ObjectDelete() or GlobalVariableDel() or whatever needed for the data sink type employed.

.

See what I'm getting at?

There is the valid argument that both global vars and objects can be messed with via GUI, being F3 and Ctrl-B respectively.

All depends on your mindset/design/whatever. My attitude is that IF user messes in places that docs strictly stated are out-of-bounds then T.O.U.G.H

I have no sympathies - just like if I scr.wed around with some 'do not touch' resource and everything went pear shaped... is totally MY fault, yes?

.

so in summary:

global variables

files

external database

objects

not forgetting searching articles database for featured goodies to fit your requirements.

.

The resources available on this site are massive - just takes some persistent digging to unearth them!

hth

 

Well, I also needed to persist a string content

I circumvented the lack of it with the idea of using the variable name to keep the content I wanted to persist, with some prefix (I used the prefix "MyVar" for the name of the "string variables" I created)

To get the initial value:

void OnStart()  {

   uint   n=GlobalVariablesTotal();

   datetime max = 0;

   string myVal;

   for(uint i=0;i<n;i++)  {

      string name = GlobalVariableName(i);

      datetime wDt = GlobalVariableTime(name);

      if (wDt>max) {

         if (StringSubstr(name,0,5)=="MyVar") {

            max = wDt;

            myVal = StringSubstr(name,6);

         }

      }

   }

   Print("The persisted string is ",myVal);

Also, you need to update/create the var:

double doesnMatter = 0;

string myValue = "to be persisted forever";

    datetime w = GlobalVariableSet("MyVar " + myValue, doesnMatter);



Reason: