MQL4 Learning - page 60

 

Easy MA EA

I put together this EA but when I try to use the Stragegy Tester with it, it never buys or sells or anything. Just wondering if someone can point out where I went wrong. Thanks. I sat there forever trying to figure it out, and another set of eyes might see what I did wrong.

CODE:

====================

#define SIGNAL_NONE 0

#define SIGNAL_BUY 1

#define SIGNAL_SELL 2

#define SIGNAL_CLOSEBUY 3

#define SIGNAL_CLOSESELL 4

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = True;

extern double Lots = 1;

extern int Slippage = 5;

extern bool UseStopLoss = True;

extern int StopLoss = 30;

extern bool UseTakeProfit = True;

extern int TakeProfit = 100;

extern bool UseTrailingStop = True;

extern int TrailingStop = 30;

int BarCount;

int Current;

bool TickCheck = False;

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

//| expert initialization function |

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

int init() {

BarCount = Bars;

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;

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

Total = OrdersTotal();

Order = SIGNAL_NONE;

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

//| Variable Begin |

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

double Var1 = iMA(NULL, PERIOD_H1, 10, 1, MODE_EMA, PRICE_OPEN, Current + 0);

double Buy1_1 = iMA(NULL, PERIOD_H1, 10, 1, MODE_EMA, PRICE_CLOSE, Current + 0);

double Sell1_1 = iMA(NULL, PERIOD_H1, 10, 1, MODE_EMA, PRICE_CLOSE, Current + 0);

double CloseBuy1_1 = iMA(NULL, PERIOD_H1, 10, 1, MODE_EMA, PRICE_CLOSE, Current + 0);

double CloseSell1_1 = iMA(NULL, PERIOD_H1, 10, 1, MODE_EMA, PRICE_CLOSE, Current + 0);

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

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

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

if (False) Order = SIGNAL_CLOSEBUY;

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

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

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

if (False) Order = SIGNAL_CLOSESELL;

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

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

}

}

}

}

}

}

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

//| Signal Begin(Entry) |

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

if (False) Order = SIGNAL_BUY;

if (False) 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);

}

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

=================================

END CODE

 

I think that are wrong expressions:

if (False) Order = SIGNAL_BUY; if (False) Order = SIGNAL_SELL;
 

Error opening SELL order : (0) no error

Hello,

I have asked this question in the Universal MA cross thread here:

https://www.mql5.com/en/forum/general

but have not recieved any reply.

Maybe this is a general error with many EAs so maybe someone here can answer my question:

-------------------

If this has been addressed before, please disregard (I haven't finished reading the whole thread).

I've been using this EA for a while now, live and demo, and I often get this type of error message (read backwards). Does anyone know what this means and/or how to correct it?

Many thanks

2009.02.10 07:00:11 universalMACrossEA2 GBPJPY,H1: Error opening SELL order : (0) no error

2009.02.10 07:00:11 stdlib GBPJPY,H1: loaded successfully

2009.02.10 07:00:07 universalMACrossEA2 GBPCHF,H1: Error opening SELL order : (0) no error

2009.02.10 07:00:07 stdlib GBPCHF,H1: loaded successfully

2009.02.10 07:00:07 universalMACrossEA2 GBPCHF,H1: close #21528761 buy 0.10 GBPCHF at 1.6613 sl: 1.6408 at price 1.7149

2009.02.10 07:00:00 universalMACrossEA2 GBPJPY,H1: Alert: Moving Average Cross DOWN !

2009.02.10 06:59:59 universalMACrossEA2 GBPCHF,H1: Alert: Moving Average Cross DOWN !

 

Create an EA listening to the alerts of an indicator

Hi all,

I'm wondering if is possible to create an EA that open positions listening to the alerts of an indicator.

I' ve a good indicator, when a trend is going on, makes an alert like this :

2009.02.13 15:30:00 EMA Crossover Indicator USDJPY,M15: Alert: EMA Cross Trend going Up on USDJPY 15, 2/15 Periods

The EA has to intercept the alert and open the position, in this case buy USDJPY.

Many thanks.

Netmastro

 

Looking for the Worst EAs

I'm working on some trading research that could turn some miserable systems into big winners. If anyone has an EA that is a CONSISTENT loser in a 15-min to 4-hr environment and can share it with me, I would appreciate it. Just post your EA (it must work properly but be a loser) and I will apply my recovery strategy to it.

This is not a joke (though it sounds like it), and I'm not trying to scam anyone. I have no financial motivation for doing this, except for my personal trading. If I benefit from it, I will share the results with you individually in return for your contribution.

 

That's just about every EA on this site.

 

Chart Based Stop Orders for EA

Is it possible to script into my EA stop buys and stop losses based on previous bar high or lows. When my buy signal is generated, I would like the system to place a stop buy above the previous bar, and when the order is filled I would like a stop loss below the previous bar. I built my EA using EA builder so I do not know really how to program this stuff. It sems to me that most of the EA's have a set TP or SL. To further complicate the process I would like the Stop orders to take into consideration the high or low and the spread factored into it or the trading rage based ojn the fill price for the Buy or SEll order. Any help would be very much appreciated.

 

Array Problem.

I have created a custom indicator that utilizes an array. Though I am having problems viewing indicator results as the first bar is being created. In other words as the first bar is being created there is a delay of one bar before my indicator shows the calculated results. Can someone help me to determine how to get my indicator to show results as the first bar is being created?

//--------------------------------------------------------------------

// Volume Oscillator.mq4

//

//--------------------------------------------------------------------

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_color1 Lime

#property indicator_color2 Red

#property indicator_maximum 1.5

#property indicator_minimum 0.5

double Buf_0[500];

double Buf_1[500];

//--------------------------------------------------------------------

int init()

{

IndicatorBuffers(2);

//--------------------------------------------------------------------

SetIndexBuffer(0,Buf_0);

SetIndexStyle (0,DRAW_HISTOGRAM);

//--------------------------------------------------------------------

//SetIndexBuffer(1,Buf_1);

//SetIndexStyle (1,DRAW_LINE,STYLE_SOLID,1);

//--------------------------------------------------------------------

return;

}

//--------------------------------------------------------------------

int start()

{

int i,

Counted_bars;

//--------------------------------------------------------------------

Counted_bars=IndicatorCounted();

i=Bars-Counted_bars+1;

double vol[500];

double FastMA[500];

double SlowMA[500];

double Indicator[500];

int limit=ArraySize(vol);

ArraySetAsSeries(vol,true);

ArraySetAsSeries(FastMA,true);

ArraySetAsSeries(SlowMA,true);

ArraySetAsSeries(Indicator,true);

ArrayInitialize(Buf_0,0);

ArrayInitialize(Buf_1,0);

for(i=0; i<limit; i++)

vol=iVolume(NULL,0,i);

for(i=0; i<limit; i++)

FastMA=iMAOnArray(vol,0,7,0,MODE_SMA,i);

for(i=0; i<limit; i++)

SlowMA=iMAOnArray(vol,0,14,0,MODE_SMA,i);

for(i=0; i<limit; i++)

{

Indicator=(FastMA/SlowMA);

ArrayCopy(Buf_0,Indicator,0,0,0);

}

return(0);

}

//-------------------------------------------------------------------

 

Sorry, please dismiss.

 

To forexsurfr

replace

Indicator=(FastMA/SlowMA);

ArrayCopy(Buf_0,Indicator,0,0,0);[/CODE]

to

[CODE]if(SlowMA!=0)Indicator=(FastMA/SlowMA);else Indicator=1;

ArrayCopy(Buf_0,Indicator,0,0,WHOLE_ARRAY);
Reason: