Use GlobalVariableGet() in an indicator

 
Files:
 
 

Great thank to you

I'm gonna try it

 

Hi

in my EA I try to put

double var_global = GlobalVariableGet(ok_trade,Direction) ; // or

GlobalVariableGet(ok_trade) but I have the message that the variable not defined.

Why?

best regards

 
natsirte:
Hi

in my EA I try to put

double var_global = GlobalVariableGet(ok_trade,Direction) ; // or

GlobalVariableGet(ok_trade) but I have the message that the variable not defined.

Why?

best regards

Use something like this.

string ok_trade;

int direction;

direction=GlobalVariableGet(ok_trade);

 
project1972:
Use something like this.

string ok_trade;

int direction;

direction=GlobalVariableGet(ok_trade);

Also in the init section you shold set the global variable if it was not set by the indicator before the EA try to read it.

void init()

{

if (!GlobalVariableCheck(ok_trade)) GlobalVariableSet(ok_trade,0);

return(0);

}

and in the start function you can read it without errors

direction=GlobalVariableGet(ok_trade);

 

Hi evrybody and thanks for your help

When I try to put

************

void init()

{

if (!GlobalVariableCheck(ok_trade)) GlobalVariableSet(ok_trade,0);

return(0);

}

************

in my EA it returns me :

init already definted and as a different type ; so I put only the condition "if (!GlobalVariableCheck(ok_trade)) GlobalVariableSet(ok_trade,0);

return(0);" in the init{...} ; and it returns me "ok_trade" is not defined;

So I set ok_trade and direction in my EA like this :

string ok_trade;

int direction;

But when I look into to my Alert ; Alert(direction);the result is always O

Help Please!!

This is my indicator and my EA

Thanks

*************MY EA EA_GLOBAL**********************

#define SIGNAL_NONE 0

#define SIGNAL_BUY 1

#define SIGNAL_SELL 2

#define SIGNAL_CLOSEBUY 3

#define SIGNAL_CLOSESELL 4

#property copyright "Expert Advisor Builder"

#property link "http://sufx.core.t3-ism.net/ExpertAdvisorBuilder/"

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = True;

extern double Lots = 1.0;

extern int Slippage = 3;

extern bool UseStopLoss = True;

extern int StopLoss = 30;

extern bool UseTakeProfit = True;

extern int TakeProfit = 60;

extern bool UseTrailingStop = True;

extern int TrailingStop = 30;

int BarCount;

int Current;

bool TickCheck = False;

string ok_trade;

int direction;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init() {

BarCount = Bars;

if (!GlobalVariableCheck(ok_trade)) GlobalVariableSet(ok_trade,0);

return(0);

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit() {

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start() {

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

direction=GlobalVariableGet(ok_trade);

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

//+------------------------------------------------------------------+

//| Variable Begin |

//+------------------------------------------------------------------+

double Buy1_1 = iMA(NULL, 0, 14, 0, MODE_SMA, PRICE_CLOSE, Current + 0);

double Buy1_2 = iMA(NULL, 0, 14, 0, MODE_SMA, PRICE_CLOSE, Current + 1);

double Sell1_1 = iMA(NULL, 0, 14, 0, MODE_SMA, PRICE_CLOSE, Current + 0);

double Sell1_2 = iMA(NULL, 0, 14, 0, MODE_SMA, PRICE_CLOSE, Current + 1);

//+------------------------------------------------------------------+

//| Variable End |

//+------------------------------------------------------------------+

//Check position

bool IsTrade = False;

for (int i = 0; i < Total; i ++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {

IsTrade = True;

if(OrderType() == OP_BUY) {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy) |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| Signal End(Exit Buy) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(UseTrailingStop && TrailingStop > 0) {

if(Bid - OrderOpenPrice() > Point * TrailingStop) {

if(OrderStopLoss() < Bid - Point * TrailingStop) {

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

} else {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Sell) |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| Signal End(Exit Sell) |

//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");

if (!EachTickMode) BarCount = Bars;

IsTrade = False;

continue;

}

//Trailing stop

if(UseTrailingStop && TrailingStop > 0) {

if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

}

}

}

Alert(direction);

//+------------------------------------------------------------------+

//| Signal Begin(Entry) |

//+------------------------------------------------------------------+

if (Buy1_1 > Buy1_2 && Buy1_2>150) Order = SIGNAL_BUY;

if (Sell1_1 < Sell1_2 && Sell1_2<-150) Order = SIGNAL_SELL;

//+------------------------------------------------------------------+

//| Signal End |

//+------------------------------------------------------------------+

//Buy

if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("BUY order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");

} else {

Print("Error opening BUY order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

//Sell

if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(!IsTrade) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("SELL order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");

} else {

Print("Error opening SELL order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

//+------------------------------------------------------------------+

*************************************

Files:
 

Hi everybody

This is the correction. (Thanks to Jerome M !)

Take the indicator 3MACROSS and add in the EA this :

//*******

int start()

{

int recovering_direction = GlobalVariableGet("ok_trade");

}

//***************

// But now I saw that the Indicator is wrong because Order=SiGNAL_BUY never work!!

if (recovering_direction == 1 ) { Order = SIGNAL_BUY; }

if (recovering_direction == 2) { Order = SIGNAL_SELL; }

and if I do that :

if (recovering_direction == 2 ) { Order = SIGNAL_BUY; }

if (recovering_direction == 1) { Order = SIGNAL_SELL; }

Order = SIGNAL_SELL never work!!

Why?????

best regards

 

The indicator

Files:
3macross.mq4  6 kb
 

Any way to create function libraries that use global variables?

Hi,

I have an indicator that I'd like to split up and provide part of the code with the source, so it can be customized to some limited extent. There are other major sections, however, that I can only provide as an .EX4 file (i.e. no source).

I tried splitting up my file and moving functions into a separate file, added the "#property library" command near the top, and tried to Compile it.

Unfortunately, my functions reference a lot of global variables defined in my indicator .mq4 file, outside of both start() {...} and init() {...}.

Do I really need to completely avoid the use of globals used by my library of functions? Must I pass every variable through the function definition and call?

Or is there a way I can get my functions to compile, yet use the global variable values as they are defined in my main program file (and not as they might be defined in the library file just to get it to compile) ?

If I haven't been clear, here's an example. The following compiles

as one continuous file. But if I try to split the file as indicated, I

can't get the library to compile due to undefined global variables. If I

define them in the library, then the main program complains they're

already defined. If I change my function calls to pass the global

variables, I get "expression on global scope not allowed."

//-----------------begin mytest.mq4 file ---------------

string myObjectNames[];

int myObjTotal;

int myFavoriteObjType = 1;

//#include "mytestLib.mt4"; // Or preferably .ex4

int init()

{

return(0);

}

int start()

{

for (int i=0; i < myObjTotal; i++)

{

if (myObjectNames == "ABC") runFunc(myObjectNames );

if (myObjectNames == "ABC") runFuncRedone(myObjectNames, myObjectNames, myObjTotal, myFavoriteObjType);

Print("i= ", i, " myObjectNames= ", myObjectNames );

}

return(0);

}

//-----------------end of mytest.mq4 file ---------------

//-------- begin library functions mytestLib.mq4 ----------

//#property library // Uncomment when compiling just the section below

// If I compile the library portion only without these vars, the Compiler says "variable not defined"

// But if I do define them here, then when I compile mytest.mq4 (the main program), it

// complains, "variable already defined"

//string myObjectNames[];

//int myObjTotal;

//int myFavoriteObjType;

void runFunc(string objName)

{

string newName = StringConcatenate(objName, "2");

ObjectCreate(newName, myFavoriteObjType, 0, TimeCurrent(), 0 );

myObjTotal++; // This is a GLOBAL integer

ArrayResize(myObjectNames, myObjTotal); // This is a GLOBAL string array

myObjectNames[myObjTotal-1] = newName;

}

void runFuncRedone(string objName, string &myObjectNames[], int &myObjTotal, int &myFavoriteObjType)

{

string newName = StringConcatenate(objName, "2");

ObjectCreate(newName, myFavoriteObjType, 0, TimeCurrent(), 0 );

myObjTotal++; // This is a GLOBAL integer

ArrayResize(myObjectNames, myObjTotal); // This is a GLOBAL string array

myObjectNames[myObjTotal-1] = newName;

}

//-------- end of library functions file ----------

As you can see, I tried also:

void runFuncRedone(string objName, string &myObjectNames, int &myObjTotal, int &myFavoriteObjType)

{

...

}

called by: runFundRedone(myObjectNames, myObjectNames, myObjTotal, myFavoriteObjType);

However, the compiler then complains: expression on global scope not allowed. (What expression?? Is this the "&" that I'm using to pass the global by reference, so it can be a two-way street to read/write the global var?)

At this moment I'm stuck. How can I split up my functions yet still use global variables?

Thanks in advance for any help,

Pips4life

Files:
mytest.mq4  2 kb
mytestlib.mq4  2 kb
 

Global Variable Question:

Here is what I want to do:

I have 2 separate mt4 broker terminals open. (Broker A & Broker B)

Broker A has an EA that stores a double value as a global variable.

GlobalVariableSet("Broker_A",Some_Value);[/PHP]

Broker B has an EA that retrieves the global variable stored in Broker A.

[PHP]GlobalVariableGet("Broker_A");

Does anyone know a way to do this?

Reason: