GlobalVariable help....

 

I'd like to create 2 GlobalVariable so that when my EA restarts it knows to use these two variables if they exist

The first is the value of a currency when an order (the first order) was placed, I call this variable center. It is the center value ie. Bid + Ask / 2 = center

The second is count ...Just a simple count that I increase by one with every new order.....until I close them all and the count is reset to zero.

//=======================================================

Will this work to check for Global Variables "center" and "count"....and if found write the values to the variables center and count in my EA?


if (GlobalVariableCheck(center))
if (GlobalVariableCheck(count))
{
center = (GlobalVariableGet(center));
count = (GlobalVariableGet(count));
AskStart=1;
BidStart=1;
}

//==============================================================

Will this (assuming AskStart and BidStart are zero) write the center value to the GlobalVariable "center"


if(AskStart==0)
if(BidStart==0)
{
AskStart = NormalizeDouble((Ask),4);
BidStart=NormalizeDouble((Bid),4);
center=((AskStart+BidStart)/2);
NormalizeDouble((center),4);
Print(center);
AskStart=1;
BidStart=1;
Comment(center);
GlobalVariableSet("center",center);

}


//=====================================================================

Will this write the value of count to GlobalVariable "count"?


tic = -1;
if(TradeLong)
if(center>(Ask+sh1))


if (count==1)
{
while((tic == -1 )&&center>(Ask+sh1))
{
Sleep(4000);
RefreshRates();
tic = OrderSend(Symbol(),OP_BUY,LE1, NormalizeDouble((Ask),4),5,Ask-sl1*Point,0,"",255,0,CLR_NONE);
}

if (tic != -1)
{
count=2;
GlobalVariableSet("count", count);
tic = -1;
}
else
{
return(0);
}


while((tic == -1 )&&center>(Ask+sh1))
{
Sleep(4000);
RefreshRates();
tic = OrderSend(Symbol(),OP_SELL,LE00, NormalizeDouble((Bid),4),5,Bid+sl0*Point,Bid-tp000*Point,"",255,0,CLR_NONE);

}

return(0);

}


//==============================================

Will this Delete GlobalVariable count and center??


if(OrdersTotal()==0)
{

Closenow=False;

count=1;
AskStart=0;
BidStart=0;
center=0;
Hedgeonce=1;
GlobalVariableDel(center);
GlobalVariableDel(count);
}



Thank you for your help and review.....

 
Also check http://www.fx1.net/permvar.php as alternative. this addon can store also strings, does not delete the variables after x days and it can share variables among unlimited terminals. So its systemwide global variable.
 
n8937g:

if (GlobalVariableCheck(center))

I think this should be GlobalVariableCheck("center"). You seem to be using GlobalVariableSet() correctly, but not GlobalVariableCheck(), GlobalVariableGet(), or GlobalVariableDel().


At first glance, there are also some other oddities. For example, NormalizeDouble((center),4) isn't going to do anything. It's going to return the normalized value, but then discard it. I think what you want is center = NormalizeDouble(center,4)



fx1.net wrote >>

Also check http://www.fx1.net/permvar.php as alternative. this addon can store also strings, does not delete the variables after x days and it can share variables among unlimited terminals. So its systemwide global variable.

This is great but, personally, I don't like storing this kind of dynamic data in the registry. It makes backups very tricky. Personally I'd rather store this kind of thing in a file, which can then be replicated to another computer much faster and much more easily. The registry still has many valid uses, but there's a reason why e.g. Microsoft themselves have increasingly moved to storing this kind of data outside of it.


 
JJc, for sure you right but like everything on life you have to weight positives and negatives. You can use a knife to cut or to kill. If you try to use permvar to store >1000 variables you are crazy. All usages <1000 variables makes no difference on performance.If you need a functionality that it store variables systemwide then you can choose it. Noone tells here that you should globally decide for PermVar. Weight it and decide. thats the right way.
 
fx1.net:
If you try to use permvar to store >1000 variables you are crazy. All usages <1000 variables makes no difference on performance.

My concern isn't performance. It's what happens when my server goes down. Where's the backup? If the answer is "in the registry", then the chances are the backup is less up to date than if it had been in a nice small file which I could feasibly have been replicating from the live computer to the backup computer in near-real-time.

 
jjc:

I think this should be GlobalVariableCheck("center"). You seem to be using GlobalVariableSet() correctly, but not GlobalVariableCheck(), GlobalVariableGet(), or GlobalVariableDel().


At first glance, there are also some other oddities. For example, NormalizeDouble((center),4) isn't going to do anything. It's going to return the normalized value, but then discard it. I think what you want is center = NormalizeDouble(center,4)



This is great but, personally, I don't like storing this kind of dynamic data in the registry. It makes backups very tricky. Personally I'd rather store this kind of thing in a file, which can then be replicated to another computer much faster and much more easily. The registry still has many valid uses, but there's a reason why e.g. Microsoft themselves have increasingly moved to storing this kind of data outside of it.


jjc..THANK YOU!! Yes, in testing I found that I needed the the GlobalVariables to be "center" because center didn't work.... It also seems correct that NormalizeDouble((center),4)
doesn't do anything and needs center = NormalizeDouble(center,4)..... I will change that.... The reason I've used the GlobalVariables is so if the chart or EA must

restart the EA will know what the conditions and criteria were before the restart...I had to trade out positions by hand when the chart locked last week....and the EA was removed

Thank you again for your reply!!

 

Hi jjc/fx1....I tested it on a real account and on the restart the EA didn't pick up the old GlobalVariable values of "count" and "center"....

Could the problem be because of the omitted ==true......??

Would I be better off not even checking with GlobalVariablesCheck....and just use If OrdersTotal() is >0..... "Get".."count" and "center".. and to just assume they are there and if they're not just get an error..who cares....are there any other things I'm overlooking?? Do I need to declare / create GV's??



if (OrdersTotal()>0)
if (GlobalVariableCheck("center")==true)
if (GlobalVariableCheck("count")==true)
{
center = (GlobalVariableGet("center"));
count = (GlobalVariableGet("count"));
AskStart=1;
BidStart=1;
}


Do I need to declare/create GlobalVariables?? They do appear in the pull down MetaTrader GlobalVariables menu so I assumed they existed/are created with the command

GlobalVariableSet("count", count);

GlobalVariableSet("center", center);


but do I need something like


double GlobalVariableSet ("center") =center;

double GlobalVariableSet("count") =count;


Thank You...

Reason: