|
|||||||
| Register in Forex TSD! | |
|
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time). Click here to register and get more information |
|
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Need help with coding an EA
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! |
|
|||
|
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(NUL L,Timeframe,"ELine",1,0)) strDirCurrent="long"; if(iCustom(NULL,Timeframe,"ELine",0,0)<iCustom(NUL L,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,"EABuy Order",16384,0,Green); if(strDirCurrent=="short" && strDirPrevious=="long") OrderSend(Symbol(),OP_SELL,Lots,Bid,3,50,200,"EASe llOrder",16384,0,Red); strDirPrevious=strDirCurrent; } // the end. Any help is very much welcome !! Thanks Eric |
|
|||
|
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. Code:
/*
* 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 <stdlib.mqh>
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()));
}
}
}
}
}
Last edited by cardio : 01-17-2006 at 04:26 PM. Reason: Reposted proper code, Update function isclosing1() |
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Coding help please | mj_bolt | Indicators - Metatrader 4 | 2 | 05-07-2007 10:39 AM |
| A Little help coding please??? | Aaragorn | Expert Advisors - Metatrader 4 | 1 | 10-28-2006 07:01 PM |
| Help with Coding Please | hoosain | Questions | 1 | 08-01-2006 05:36 PM |
| EA Coding Help! | WAW | Questions | 0 | 05-03-2006 05:12 AM |
| Coding Help | jerrymar | Indicators - Metatrader 4 | 1 | 03-23-2006 09:20 AM |