Forex
Google

Go Back   Forex Trading > Discussion Areas > Metatrader 4
Forex Forum Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


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

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-12-2006, 10:39 AM
eric79 eric79 is offline
Senior Member
 
Join Date: Dec 2005
Posts: 217
eric79 is on a distinguished road
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!
Attached Files
File Type: mq4 EL1.mq4 (1.4 KB, 31 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-14-2006, 12:18 PM
eric79 eric79 is offline
Senior Member
 
Join Date: Dec 2005
Posts: 217
eric79 is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-16-2006, 07:36 PM
balue balue is offline
Junior Member
 
Join Date: Jan 2006
Posts: 8
balue is on a distinguished road
Hi,
when i change NUL L in NULL , i dont get any errors from the copilation
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-16-2006, 07:46 PM
balue balue is offline
Junior Member
 
Join Date: Jan 2006
Posts: 8
balue is on a distinguished road
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-16-2006, 07:57 PM
eric79 eric79 is offline
Senior Member
 
Join Date: Dec 2005
Posts: 217
eric79 is on a distinguished road
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..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-17-2006, 10:27 AM
cardio cardio is offline
Senior Member
 
Join Date: Sep 2005
Location: St Louis, MO, USA
Posts: 176
cardio is an unknown quantity at this point
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()
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-17-2006, 04:42 PM
cardio cardio is offline
Senior Member
 
Join Date: Sep 2005
Location: St Louis, MO, USA
Posts: 176
cardio is an unknown quantity at this point
Fixed, I hope

Fixed hassles. Try this
We still have to work on money management etc.
Attached Files
File Type: mq4 EL1.mq4 (3.6 KB, 28 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-17-2006, 04:59 PM
eric79 eric79 is offline
Senior Member
 
Join Date: Dec 2005
Posts: 217
eric79 is on a distinguished road
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-17-2006, 05:36 PM
cardio cardio is offline
Senior Member
 
Join Date: Sep 2005
Location: St Louis, MO, USA
Posts: 176
cardio is an unknown quantity at this point
Try this

I am not testing the code - if you give the indicator - i will test - actually I will test it pretty soon with another indicator. Still hope this helps.
Attached Files
File Type: mq4 EL1.mq4 (4.1 KB, 18 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-17-2006, 05:37 PM
cardio cardio is offline
Senior Member
 
Join Date: Sep 2005
Location: St Louis, MO, USA
Posts: 176
cardio is an unknown quantity at this point
still trying to fix - just hold on I will post now

Still fixing
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


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

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


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


All times are GMT. The time now is 08:39 AM.