Download MetaTrader 5

My first Expert Advisor is my code okay?

To add comments, please log in or register
chris bayne
19
chris bayne  

Hi there,

I am new to metatrader and I have created my first Expert Advisor to alert me when a new bar is created. I list my code below as it doesn't appear to be alerting so i must have something wrong. Can any one help?

Also once I have attach the cod eto the chart how do I unattach it?

Hope someone can help.

Chris.

//+------------------------------------------------------------------+
//| Alert_New_Bar.mq4 |
//| Copyright © 2008, Chris Bayne. |
//| http://www.xxxxxx.com |
//| |
//| Function Description: |
//| |
//| Compare the current bar number with the previous bar number |
//| and if the current bar count is greater then we have found a |
//| new bar, so set an Alert |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Chris Bayne."
#property link "http://www.xxxxxx.com"

static int Current_Bar = 0; // Global variable
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init() // Spec. funct. init()
{
Alert ("Funct. init() triggered at start"); // Alert
return(0); // Exit init()
}

//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit() // Spec. funct. deinit()
{
Alert ("Funct. deinit() triggered at exit"); // Alert
return(0); // Exit deinit()
}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start() // Spec. funct. start()
{
if( Current_Bar == 0 || Current_Bar < Bars )
double Price = Bid; // Local variable
Alert("New Bar ",Current_Bar," Price = ",Price); // Alert
Sleep(500); // Delay
PlaySound ("alert2.wav"); // Sound
Current_Bar = Bars; // increment Bar
return(0); // Exit start()
}
//+------------------------------------------------------------------+

Alexander
2441
Alexander  

Use curly brackets:

int start() // Spec. funct. start() 
{
if( Current_Bar == 0 || Current_Bar < Bars )
  {
  double Price = Bid; // Local variable
  Alert("New Bar ",Current_Bar," Price = ",Price); // Alert
  Sleep(500); // Delay
  PlaySound ("alert2.wav"); // Sound
  Current_Bar = Bars; // increment Bar
  }
return(0); // Exit start()
}
To unattach push right button on chart, click Expert Advisor and Remove
chris bayne
19
chris bayne  
Roger wrote >>

Use curly brackets:

To unattach push right button on chart, click Expert Advisor and Remove

Thanks Roger. Changed the code and inserted a test alert which is not been triggered.

int start() // Spec. funct. start()
{
Alert("test message ",Current_Bar); // Alert
if( Current_Bar == 0 || Current_Bar < Bars )
{
double Price = Bid; // Local variable
Alert("New Bar ",Current_Bar," Price = ",Price); // Alert
Sleep(500); // Delay
PlaySound ("alert2.wav"); // Sound
Current_Bar = Bars; // increment Bar
}
return(0); // Exit start()
}

Displays the alert - Alert ("Funct. init() triggered at start"); but nothing else. Any ideas?

Thanks,

Chris

Alexander
2441
Alexander  

2008.12.08 16:03:21 2008.12.05 22:54 test EURUSD,H1: Alert: Funct. deinit() triggered at exit
2008.12.08 16:03:21 2008.12.05 22:00 test EURUSD,H1: Alert: New Bar 1022 Price = 1.2717
2008.12.08 16:03:21 2008.12.05 21:00 test EURUSD,H1: Alert: New Bar 1021 Price = 1.269
2008.12.08 16:03:21 2008.12.05 20:00 test EURUSD,H1: Alert: New Bar 1020 Price = 1.2641
2008.12.08 16:03:21 2008.12.05 19:00 test EURUSD,H1: Alert: New Bar 1019 Price = 1.2652
2008.12.08 16:03:21 2008.12.05 18:00 test EURUSD,H1: Alert: New Bar 1018 Price = 1.2654
2008.12.08 16:03:21 2008.12.05 17:00 test EURUSD,H1: Alert: New Bar 1017 Price = 1.2675
2008.12.08 16:03:21 2008.12.05 16:00 test EURUSD,H1: Alert: New Bar 1016 Price = 1.2678
2008.12.08 16:03:21 2008.12.05 15:00 test EURUSD,H1: Alert: New Bar 1015 Price = 1.2666
2008.12.08 16:03:21 2008.12.05 14:00 test EURUSD,H1: Alert: New Bar 1014 Price = 1.2686
2008.12.08 16:03:21 2008.12.05 13:00 test EURUSD,H1: Alert: New Bar 1013 Price = 1.2687
2008.12.08 16:03:21 2008.12.05 12:00 test EURUSD,H1: Alert: New Bar 1012 Price = 1.2723
2008.12.08 16:03:21 2008.12.05 11:00 test EURUSD,H1: Alert: New Bar 1011 Price = 1.2758
2008.12.08 16:03:21 2008.12.05 10:00 test EURUSD,H1: Alert: New Bar 1010 Price = 1.2774
2008.12.08 16:03:21 2008.12.05 09:00 test EURUSD,H1: Alert: New Bar 1009 Price = 1.2765
2008.12.08 16:03:21 2008.12.05 08:00 test EURUSD,H1: Alert: New Bar 1008 Price = 1.278
2008.12.08 16:03:21 2008.12.05 07:00 test EURUSD,H1: Alert: New Bar 1007 Price = 1.2753
2008.12.08 16:03:21 2008.12.05 06:00 test EURUSD,H1: Alert: New Bar 1006 Price = 1.2746
2008.12.08 16:03:21 2008.12.05 05:00 test EURUSD,H1: Alert: New Bar 1005 Price = 1.2767
2008.12.08 16:03:21 2008.12.05 04:00 test EURUSD,H1: Alert: New Bar 1004 Price = 1.2758
2008.12.08 16:03:21 2008.12.05 03:00 test EURUSD,H1: Alert: New Bar 1003 Price = 1.2757
2008.12.08 16:03:21 2008.12.05 02:00 test EURUSD,H1: Alert: New Bar 1002 Price = 1.2739
2008.12.08 16:03:21 2008.12.05 01:00 test EURUSD,H1: Alert: New Bar 1001 Price = 1.2774
2008.12.08 16:03:21 2008.12.05 00:00 test EURUSD,H1: Alert: New Bar 0 Price = 1.2772
2008.12.08 16:03:21 2008.12.05 00:00 test EURUSD,H1: Alert: Funct. init() triggered at start
2008.12.08 16:03:21 test started for testing
2008.12.08 16:03:16 test: loaded successfully

I don't know, it works properly in my terminal. Try to restart yours.

chris bayne
19
chris bayne  
Roger wrote >>

2008.12.08 16:03:21 2008.12.05 22:54 test EURUSD,H1: Alert: Funct. deinit() triggered at exit
2008.12.08 16:03:21 2008.12.05 22:00 test EURUSD,H1: Alert: New Bar 1022 Price = 1.2717
2008.12.08 16:03:21 2008.12.05 21:00 test EURUSD,H1: Alert: New Bar 1021 Price = 1.269
2008.12.08 16:03:21 2008.12.05 20:00 test EURUSD,H1: Alert: New Bar 1020 Price = 1.2641
2008.12.08 16:03:21 2008.12.05 19:00 test EURUSD,H1: Alert: New Bar 1019 Price = 1.2652
2008.12.08 16:03:21 2008.12.05 18:00 test EURUSD,H1: Alert: New Bar 1018 Price = 1.2654
2008.12.08 16:03:21 2008.12.05 17:00 test EURUSD,H1: Alert: New Bar 1017 Price = 1.2675
2008.12.08 16:03:21 2008.12.05 16:00 test EURUSD,H1: Alert: New Bar 1016 Price = 1.2678
2008.12.08 16:03:21 2008.12.05 15:00 test EURUSD,H1: Alert: New Bar 1015 Price = 1.2666
2008.12.08 16:03:21 2008.12.05 14:00 test EURUSD,H1: Alert: New Bar 1014 Price = 1.2686
2008.12.08 16:03:21 2008.12.05 13:00 test EURUSD,H1: Alert: New Bar 1013 Price = 1.2687
2008.12.08 16:03:21 2008.12.05 12:00 test EURUSD,H1: Alert: New Bar 1012 Price = 1.2723
2008.12.08 16:03:21 2008.12.05 11:00 test EURUSD,H1: Alert: New Bar 1011 Price = 1.2758
2008.12.08 16:03:21 2008.12.05 10:00 test EURUSD,H1: Alert: New Bar 1010 Price = 1.2774
2008.12.08 16:03:21 2008.12.05 09:00 test EURUSD,H1: Alert: New Bar 1009 Price = 1.2765
2008.12.08 16:03:21 2008.12.05 08:00 test EURUSD,H1: Alert: New Bar 1008 Price = 1.278
2008.12.08 16:03:21 2008.12.05 07:00 test EURUSD,H1: Alert: New Bar 1007 Price = 1.2753
2008.12.08 16:03:21 2008.12.05 06:00 test EURUSD,H1: Alert: New Bar 1006 Price = 1.2746
2008.12.08 16:03:21 2008.12.05 05:00 test EURUSD,H1: Alert: New Bar 1005 Price = 1.2767
2008.12.08 16:03:21 2008.12.05 04:00 test EURUSD,H1: Alert: New Bar 1004 Price = 1.2758
2008.12.08 16:03:21 2008.12.05 03:00 test EURUSD,H1: Alert: New Bar 1003 Price = 1.2757
2008.12.08 16:03:21 2008.12.05 02:00 test EURUSD,H1: Alert: New Bar 1002 Price = 1.2739
2008.12.08 16:03:21 2008.12.05 01:00 test EURUSD,H1: Alert: New Bar 1001 Price = 1.2774
2008.12.08 16:03:21 2008.12.05 00:00 test EURUSD,H1: Alert: New Bar 0 Price = 1.2772
2008.12.08 16:03:21 2008.12.05 00:00 test EURUSD,H1: Alert: Funct. init() triggered at start
2008.12.08 16:03:21 test started for testing
2008.12.08 16:03:16 test: loaded successfully

I don't know, it works properly in my terminal. Try to restart yours.

Roger,

I must be doing something stupid as the alert isn't triggering.

I am only getting the strart alert in the dialog box.

int init() // Spec. funct. init()
{
Alert ("Funct. init() triggered at start"); // Alert
return; // Exit init()
}

Not showing on the log either:

17:30:34 MetaTrader 4.00 build 220 started (MetaQuotes Software Corp.)
17:30:35 Custom indicator MV_BigPrice EURUSD,M1: loaded successfully
17:30:35 Expert Alert_New_Bar EURUSD,M1: loaded successfully
17:30:35 Custom indicator MV_BigPrice GBPUSD,M1: loaded successfully
17:30:36 '1121452': login


17:34:31 Expert Alert_New_Bar EURUSD,M1: removed
17:34:31 Custom indicator MV_BigPrice EURUSD,M1: removed
17:34:31 Custom indicator MV_BigPrice GBPUSD,M1: removed
17:34:31 MetaTrader 4.00 build 220 stopped

Is there some setting somewhere that I should be setting?

Hope this makes sense.

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start() // Spec. funct. start()
{
Alert("Start of Alerting routine ",Current_Bar); // Alert
if( Current_Bar == 0 || Current_Bar < Bars )
{
Alert("New Bar Opened - Open Price ", Open[0]); // Alert
Sleep(500); // Delay
PlaySound ("alert2.wav"); // Sound
Current_Bar = Bars; // increment Bar
}
return; // Exit start()
}
//+------------------------------------------------------------------+

Thanks

Chris.

Alexander
2441
Alexander  

Check is it allowed EA in your terminal, it is supposed to be face sad or smile on the top left of your chart.

Put this EA to a M1 chart and test it.

Files:
test.mq4 2 kb
chris bayne
19
chris bayne  
Roger wrote >>

Check is it allowed EA in your terminal, it is supposed to be face sad or smile on the top left of your chart.

Put this EA to a M1 chart and test it.

Roger,

One the top right of the screen there is "test x".

See atached screen.

Sorry Roger for causing you hassle.

regards

Chris.

Alexander
2441
Alexander  

Push the button Expert Advisors

chris bayne
19
chris bayne  
Roger wrote >>

Push the button Expert Advisors

Roger,

That seems to have made things work. Does that button turn the EA on and off?

Sorry to have taken up your time. Didn't know anything about this button.

Once again thanks very much for the help.

Regards

Chris.

Alexander
2441
Alexander  
billywiz wrote >>

Roger,

Does that button turn the EA on and off?

Yes, all of them.

To add comments, please log in or register