Forex



Go Back   Forex Trading > Programming > MetaTrader
Forex Forum Register More recent Calendar Advertising Others Help






Register
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.
See more

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2007, 09:28 PM
Junior Member
 
Join Date: Oct 2006
Posts: 25
natsirte is on a distinguished road
Use GlobalVariableGet() in an indicator

Hi

I try to use the function GlobalVariableSet() in the indicator ,for remove my data type "ok_trade" an use it in an EA with GlobalVariableGet()

But is not working .Where my error ? and How can I use GlobalVariableGet() in the EA


Help please





this is the code of my indicator:

******************INDICATOR 3MACROSS****************************************** *****

// This is Not Tested , Use At Your Own Risk !

//+--------------------------------------------------------------------------+
//| 3 MA Cross w_Alert v2.mq4 |
//| Copyright © 2005, Jason Robinson (jnrtrading) |
//| http://www.jnrtading.co.uk |
//| 3 ma conversion and Alert , David Honeywell , transport.david@gmail.com |
//| MetaTrader_Experts_and_Indicators : MetaTrader_Experts_and_Indicators |
//+--------------------------------------------------------------------------+

/*
+-------------------------------------------------------------------------------+
| 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 . |
+-------------------------------------------------------------------------------+
*/

#property copyright "Copyright © 2005, Jason Robinson (jnrtrading)"
#property link "http://www.jnrtrading.co.uk"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Coral

double CrossUp[];
double CrossDown[];
double prevtime;
double Range, AvgRange;
double fasterMAnow, fasterMAprevious, fasterMAafter;
double mediumMAnow, mediumMAprevious, mediumMAafter;
double slowerMAnow, slowerMAprevious, slowerMAafter;

extern int FasterMA = 5;
extern int FasterShift = -5;
extern int FasterMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma

extern int MediumMA = 20;
extern int MediumShift = -5;
extern int MediumMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma

extern int SlowerMA = 34;
extern int SlowerShift = 0;
extern int SlowerMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma

extern int SoundAlert = 1; // 0 = disabled

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 233);
SetIndexBuffer(0, CrossUp);
SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 234);
SetIndexBuffer(1, CrossDown);


//----
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;

fasterMAnow = iMA(NULL, 0, FasterMA, FasterShift, FasterMode, PRICE_CLOSE, i+1);
fasterMAprevious = iMA(NULL, 0, FasterMA, FasterShift, FasterMode, PRICE_CLOSE, i+2);
fasterMAafter = iMA(NULL, 0, FasterMA, FasterShift, FasterMode, PRICE_CLOSE, i-1);

mediumMAnow = iMA(NULL, 0, MediumMA, MediumShift, MediumMode, PRICE_CLOSE, i+1);
mediumMAprevious = iMA(NULL, 0, MediumMA, MediumShift, MediumMode, PRICE_CLOSE, i+2);
mediumMAafter = iMA(NULL, 0, MediumMA, MediumShift, MediumMode, PRICE_CLOSE, i-1);

slowerMAnow = iMA(NULL, 0, SlowerMA, SlowerShift, SlowerMode, PRICE_CLOSE, i+1);
slowerMAprevious = iMA(NULL, 0, SlowerMA, SlowerShift, SlowerMode, PRICE_CLOSE, i+2);
slowerMAafter = iMA(NULL, 0, SlowerMA, SlowerShift, SlowerMode, PRICE_CLOSE, i-1);

if ((fasterMAnow > slowerMAnow &&
fasterMAprevious <= slowerMAprevious &&
fasterMAafter > slowerMAafter &&
mediumMAnow > slowerMAnow )
||
(fasterMAnow > slowerMAnow &&
mediumMAnow > slowerMAnow &&
mediumMAprevious <= slowerMAprevious &&
mediumMAafter > slowerMAafter ))
{
CrossUp[i] = Low[i] - Range*0.5;
ok_trade="Long";


}

if ((fasterMAnow < slowerMAnow &&
fasterMAprevious >= slowerMAprevious &&
fasterMAafter < slowerMAafter &&
mediumMAnow < slowerMAnow )
||
(fasterMAnow < slowerMAnow &&
mediumMAnow < slowerMAnow &&
mediumMAprevious >= slowerMAprevious &&
mediumMAafter < slowerMAafter ))
{
CrossDown[i] = High[i] + Range*0.5;
ok_trade="Short";
}

}

if ((CrossUp[0] > 2000) && (CrossDown[0] > 2000)) { prevtime = 0; }

if ((CrossUp[0] == Low[0] - Range*0.5) && (prevtime != Time[0]) && (SoundAlert != 0))
{
prevtime = Time[0];
Alert(Symbol()," 3 MA Cross Up @ Hour ",Hour()," Minute ",Minute());
}

if ((CrossDown[0] == High[0] + Range*0.5) && (prevtime != Time[0]) && (SoundAlert != 0))
{
prevtime = Time[0];
Alert(Symbol()," 3 MA Cross Down @ Hour ",Hour()," Minute ",Minute());
}

// Alert(ok_trade);
GlobalVariableSet("ok_trade",0);
return(0);
}


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



best regards
Attached Files
File Type: mq4 3 MA Cross.mq4 (6.1 KB, 22 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #2 (permalink)  
Old 11-27-2007, 12:32 AM
project1972's Avatar
Senior Member
 
Join Date: May 2006
Location: Fl, Usa
Posts: 277
project1972 is on a distinguished road
Quote:
Originally Posted by natsirte View Post
Hi

I try to use the function GlobalVariableSet() in the indicator ,for remove my data type "ok_trade" an use it in an EA with GlobalVariableGet()

But is not working .Where my error ? and How can I use GlobalVariableGet() in the EA


Help please
I don't know if your indicator will work but this is the correct way of using global variables.

You will get 1 for longs, 2 for short or 0 not cross


Code:
******************INDICATOR 3MACROSS****************************************** *****

// This is Not Tested , Use At Your Own Risk !

//+--------------------------------------------------------------------------+
//| 3 MA Cross w_Alert v2.mq4 |
//| Copyright © 2005, Jason Robinson (jnrtrading) |
//| http://www.jnrtading.co.uk |
//| 3 ma conversion and Alert , David Honeywell , transport.david@gmail.com |
//| MetaTrader_Experts_and_Indicators : MetaTrader_Experts_and_Indicators |
//+--------------------------------------------------------------------------+

/*
+-------------------------------------------------------------------------------+
| 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 . |
+-------------------------------------------------------------------------------+
*/ 

#property copyright "Copyright © 2005, Jason Robinson (jnrtrading)"
#property link "http://www.jnrtrading.co.uk"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Coral

double CrossUp[];
double CrossDown[];
double prevtime;
double Range, AvgRange;
double fasterMAnow, fasterMAprevious, fasterMAafter;
double mediumMAnow, mediumMAprevious, mediumMAafter;
double slowerMAnow, slowerMAprevious, slowerMAafter;

extern int FasterMA = 5;
extern int FasterShift = -5;
extern int FasterMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma

extern int MediumMA = 20;
extern int MediumShift = -5;
extern int MediumMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma

extern int SlowerMA = 34;
extern int SlowerShift = 0;
extern int SlowerMode = 1; // 0 = sma, 1 = ema, 2 = smma, 3 = lwma

extern int SoundAlert = 1; // 0 = disabled

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 233);
SetIndexBuffer(0, CrossUp);
SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 234);
SetIndexBuffer(1, CrossDown);


//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//---- 

//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{

string ok_trade;
int Direction=0;


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;

fasterMAnow = iMA(NULL, 0, FasterMA, FasterShift, FasterMode, PRICE_CLOSE, i+1);
fasterMAprevious = iMA(NULL, 0, FasterMA, FasterShift, FasterMode, PRICE_CLOSE, i+2);
fasterMAafter = iMA(NULL, 0, FasterMA, FasterShift, FasterMode, PRICE_CLOSE, i-1);

mediumMAnow = iMA(NULL, 0, MediumMA, MediumShift, MediumMode, PRICE_CLOSE, i+1);
mediumMAprevious = iMA(NULL, 0, MediumMA, MediumShift, MediumMode, PRICE_CLOSE, i+2);
mediumMAafter = iMA(NULL, 0, MediumMA, MediumShift, MediumMode, PRICE_CLOSE, i-1);

slowerMAnow = iMA(NULL, 0, SlowerMA, SlowerShift, SlowerMode, PRICE_CLOSE, i+1);
slowerMAprevious = iMA(NULL, 0, SlowerMA, SlowerShift, SlowerMode, PRICE_CLOSE, i+2);
slowerMAafter = iMA(NULL, 0, SlowerMA, SlowerShift, SlowerMode, PRICE_CLOSE, i-1);

if ((fasterMAnow > slowerMAnow &&
fasterMAprevious <= slowerMAprevious &&
fasterMAafter > slowerMAafter &&
mediumMAnow > slowerMAnow )
||
(fasterMAnow > slowerMAnow &&
mediumMAnow > slowerMAnow &&
mediumMAprevious <= slowerMAprevious &&
mediumMAafter > slowerMAafter ))
{
CrossUp[i] = Low[i] - Range*0.5;
Direction=1;


}

if ((fasterMAnow < slowerMAnow &&
fasterMAprevious >= slowerMAprevious &&
fasterMAafter < slowerMAafter &&
mediumMAnow < slowerMAnow )
||
(fasterMAnow < slowerMAnow &&
mediumMAnow < slowerMAnow &&
mediumMAprevious >= slowerMAprevious &&
mediumMAafter < slowerMAafter ))
{
CrossDown[i] = High[i] + Range*0.5;
Direction=2;
}

}

if ((CrossUp[0] > 2000) && (CrossDown[0] > 2000)) { prevtime = 0; }

if ((CrossUp[0] == Low[0] - Range*0.5) && (prevtime != Time[0]) && (SoundAlert != 0))
{
prevtime = Time[0];
Alert(Symbol()," 3 MA Cross Up @ Hour ",Hour()," Minute ",Minute());
}

if ((CrossDown[0] == High[0] + Range*0.5) && (prevtime != Time[0]) && (SoundAlert != 0))
{
prevtime = Time[0];
Alert(Symbol()," 3 MA Cross Down @ Hour ",Hour()," Minute ",Minute());
}

// Alert(ok_trade);
GlobalVariableSet(ok_trade,Direction);
return(0);
}
__________________
I have not failed. I've just found 10,000 ways that won't work.
Thomas Alva Edison
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #3 (permalink)  
Old 11-27-2007, 12:53 AM
Junior Member
 
Join Date: Oct 2006
Posts: 25
natsirte is on a distinguished road
Great thank to you

I'm gonna try it
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #4 (permalink)  
Old 11-27-2007, 02:07 AM
Junior Member
 
Join Date: Oct 2006
Posts: 25
natsirte is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #5 (permalink)  
Old 11-27-2007, 02:15 AM
project1972's Avatar
Senior Member
 
Join Date: May 2006
Location: Fl, Usa
Posts: 277
project1972 is on a distinguished road
Quote:
Originally Posted by natsirte View Post
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);
__________________
I have not failed. I've just found 10,000 ways that won't work.
Thomas Alva Edison
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #6 (permalink)  
Old 11-27-2007, 02:25 AM
project1972's Avatar
Senior Member
 
Join Date: May 2006
Location: Fl, Usa
Posts: 277
project1972 is on a distinguished road
Quote:
Originally Posted by project1972 View Post
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);
__________________
I have not failed. I've just found 10,000 ways that won't work.
Thomas Alva Edison
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #7 (permalink)  
Old 11-27-2007, 02:56 PM
Junior Member
 
Join Date: Oct 2006
Posts: 25
natsirte is on a distinguished road
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);
}
//+------------------------------------------------------------------+


*************************************
Attached Files
File Type: mq4 3 MA Cross_GLOBAL.mq4 (5.0 KB, 20 views)
File Type: mq4 EA_GLOBAL.mq4 (9.5 KB, 21 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #8 (permalink)  
Old 11-29-2007, 11:34 PM
Junior Member
 
Join Date: Oct 2006
Posts: 25
natsirte is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #9 (permalink)  
Old 11-29-2007, 11:35 PM
Junior Member
 
Join Date: Oct 2006
Posts: 25
natsirte is on a distinguished road
The indicator
Attached Files
File Type: mq4 3MACross.mq4 (5.0 KB, 14 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #10 (permalink)  
Old 09-03-2008, 12:33 AM
Junior Member
 
Join Date: Apr 2007
Posts: 7
pips4life is on a distinguished road
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[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?

Thanks in advance for any help,
Pips4life
Attached Files
File Type: mq4 mytest.mq4 (1.8 KB, 1 views)
File Type: mq4 mytestLib.mq4 (1.8 KB, 1 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
Reply

Bookmarks

Tags
GlobalVariableGet


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump


All times are GMT. The time now is 10:18 AM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.