Need help with coding please
Hi
I have now solved this problem. I could compile the EA and tried to backtest it. But unfortunately it does not buy or sell anything !
Apart from this it seems to work, this is the journal:
14:16:18 EL1 inputs: Lots=0.1; Timeframe=240;
14:16:18 ELine GBPUSD,H4: removed
14:16:18 2005.11.14 00:00 ELine GBPUSD,H4: loaded successfully
And this is the EA Code:
//+------------------------------------------------------------------+
//| T1.mq4
//|
//|
//+------------------------------------------------------------------+
extern double Lots = 0.1;
extern int Timeframe = 240;
string strDirCurrent="none";
string strDirPrevious="none";
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
// check for current direction
if(iCustom(NULL,Timeframe,"ELine",0,0)>iCustom(NULL,Timeframe,"ELine",1,0))
strDirCurrent="long";
if(iCustom(NULL,Timeframe,"ELine",0,0)<iCustom(NULL,Timeframe,"ELine",1,0))
strDirCurrent="short";
// compare to previous direction and open a position if there was a change
if(strDirCurrent=="long" && strDirPrevious=="short")
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,50,200,"EABuyOrder",16384,0,Green);
if(strDirCurrent=="short" && strDirPrevious=="long")
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,50,200,"EASellOrder",16384,0,Red);
strDirPrevious=strDirCurrent;
}
// the end.
Any help is very much welcome !!
Thanks
Eric
if you look in the development-course on this board, youll find "My_First_EA", a codind with crossing MAs
I could not get any result testing your EA and I'm very new on EA-Proramming too. Sorry
Thank you balue. The space in NULL ist only from copy and paste, in my code it is written correctly. And it does compile (the second version).
I know the MQL course (even though i'm not through all of it yet).
I have written a similar EA for ADX to test and there it works. Only when i try to use my custom indicator it does not trade..
Please attach your indicator - so we can do backtesting
Hi
Great code - I would love to backtest it - please give us your icustom Eline.
I modified your code extensively.
/*
* Created by SharpDevelop.
* User: CARDIO
* Date: 1/17/2006
* Time: 4:55 AM
*
*Todo: if there is an ope position - close it- then open in opposite direction.
*
*/
//+------------------------------------------------------------------+
//| T1.mq4
//|
//|
//+------------------------------------------------------------------+
#include
extern double Lots = 0.1;
extern int Timeframe = 240;
string strDirCurrent="none";
string strDirPrevious="none";
int cnt, magicEA;
bool isclosing = false;
double slippage = 3;
int init() {
return(0);
}
int deinit() {
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
magicEA = 16384;
// check for current direction
if(iCustom(NULL,Timeframe,"ELine",0,0)>iCustom(NULL,Timeframe,"ELine",1,0))
{
strDirCurrent="long";
}
if(iCustom(NULL,Timeframe,"ELine",0,0)<iCustom(NULL,Timeframe,"ELine",1,0))
{strDirCurrent="short";
}
// compare to previous direction and open a position if there was a change
if(strDirCurrent=="long" && strDirPrevious=="short")
{
//firs close open positions
isclosing = true;
isclosing1();
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,"T1_EA_Buy_Order",16384,0,Green);
return(0);
}
if(strDirCurrent=="short" && strDirPrevious=="long")
{
isclosing = true;
isclosing1();
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,"T1_EA_Sell_Order",16384,0,Red);
return(0);
}
strDirPrevious=strDirCurrent;
return(0);
}
// the end.
void isclosing1(){
//Close all open orders
//todo: get a requote on the prices if error 138 occurs, use refreshrates
//todo: check if the last 3 closes where losers - if so stop the ea
int totalOrders = OrdersTotal();
int numPos = 0;
for(cnt=0; cnt<totalOrders; cnt++) { // scan all orders and positions...
OrderSelect(cnt, SELECT_BY_POS); // the next line will check for ONLY market trades, not entry orders
if(OrderSymbol() == Symbol() && OrderType() <= OP_SELL && OrderMagicNumber() == magicEA) { // only look for this symbol, and only orders from this EA
numPos++;
if(OrderType() == OP_BUY) { // Check for close signal for bought trade
if(isclosing) {
if (OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Violet)) { // Close bought trade
//writetofile("10","Closed buy", OrderTicket());
//prtAlert("Day Trading: Closing BUY order");
} else {
// writetofile("10b","Closed buy fail", ErrorDescription(GetLastError()));
}
}
} else { // Check sold trade for close signal
if(isclosing) {
OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Violet);
//writetofile("10","Closed buy", OrderTicket());
// prtAlert("Day Trading: Closing SELL order");
}else {
//writetofile("10c","Closed sell fail", ErrorDescription(GetLastError()));
}
}
}
}
}
Thanks, now i get this:
2006.01.17 18:59:10 2006.01.12 08:20 EL1: invalid double number as parameter 7 for OrderSend function
2006.01.17 18:59:10 2006.01.12 08:10 EL1: comment for OrderSend function must be a string
2006.01.17 18:59:09 EL1: loaded successfully
and the error repeats about a million times ;-)
but i think this one is easy to fix.
still trying to fix - just hold on I will post now
Still fixing
Fixed - hopefully
You were just missing a take profit (tp) value actually.
Don't u want a tp and a stoploss?
Good luck

- Free trading apps
- Free Forex VPS for 24 hours
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi
i am trying to code a very simple EA but i'm not a programmer and i've never made an EA before.
I took the MACD Sample from MT4 and edited some parts and deleted all the rest that i thought i don't need.
I'm sure my code is really horrible
I would like the EA to work like this:
My custom indicator ELine has two lines that move in steps. Sometimes one is higher than the other, sometimes they are the same.
When one line crosses the other i would like the EA to enter a long position or a short position, depending on which line crosses up.
There is always an open position, except on start of the EA.
When i try to compile it it says "if" - semicolon expected (20,7)
Then in the next step it should always close an already existing position when it opens a new one but i couldn't code this so far.
Could somebody please correct the errors in my code and maybe tell me how i can close the positions any time the EA enters a new one?
Thanks a lot!