Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.
From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.
Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
Elite Section
Get access to private discussions, specialized support, indicators and trading systems reported every week.
Advanced Elite Section
For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
/*
+-------------------------------------------------------------------------------+
| Allows you to enter 3 ma periods and it will then show you and alert you at |
| which point the 2 faster ma's "OPEN" are both above or below the Slowest ma . |
+-------------------------------------------------------------------------------+
*/
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
string ok_trade ="no_trade";
int limit, i, counter;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
for(i = 0; i <= limit; i++)
{
counter=i;
Range=0;
AvgRange=0;
for (counter=i ;counter<=i+9;counter++)
{
AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
}
Range=AvgRange/10;
************
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**********************
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 |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| 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);
}
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."
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[i] == "ABC") runFunc(myObjectNames[i] );
if (myObjectNames[i] == "ABC") runFuncRedone(myObjectNames[i], myObjectNames, myObjTotal, myFavoriteObjType);
Print("i= ", i, " myObjectNames[i]= ", myObjectNames[i] );
}
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[i], 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?