MQL4 - automated forex trading   /  

Forum

Script example

Back to topics list  | 1 2 To post a new topic, please log in or register

avatar
28
brave0heart 2006.10.15 23:24 
Hi I know that a script is different from an EA or an indicator, etc. But could someone please post an example of a script?
Thanks
Steve
Expert System 'Commentator'. Practical Use of Embedded Indicators in an MQL4 Program

Expert System 'Commentator'. Practical Use of Embedded Indicators in an MQL4 Program

The article describes the use of technical indicators in programming on MQL4.


avatar
597
irusoh1 2006.10.16 03:58 
look at my script "CloseOnChart" in code base.

avatar
28
brave0heart 2006.10.16 04:34 
irusoh1 wrote:
look at my script "CloseOnChart" in code base.
I figured out how to make a script and run one. Here is my example. But my real problem is that I would like to be able run the following on an offline chart. Do you know how to make the following do that? My guess is that the all_minute script of '"Free-of-Holes" Charts' can read an offline chart. After all it must be able to if it can fill in gaps, but how does one go about making a script look at each bar of an offline chart.
thanks
Steve

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
 
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
int x = 1;
while(Bid > 0 && x < 10)
   {   
   Comment(" x = ",x," bid = ", Bid," ask = ",Ask);   
   RefreshRates();
   Sleep(1000);
   x = x + 1;
   }
//----
   return(0);
}
//+------------------------------------------------------------------+


avatar
Moderator
5198
stringo 2006.10.16 17:37 
see Period_converter script. It posts window message to force data refresh

1. Start Period converter
2. open produced data as offline chart
3. change your script

int start()
  {
   double d_Spread=MarketInfo(Symbol(),MODE_SPREAD) * MarketInfo(Symbol(),MODE_POINT);
   double d_Bid,d_Ask;
//----
   while(!IsStopped())
     {
      d_Bid=Close[0];
      d_Ask=d_Bid+d_Spread;
      Comment("bid = ", d_Bid," ask = ",d_Ask);   
      Sleep(100);
      RefreshRates();
     }
//----
   return(0);
}

4. Attach it to offline chart and see comments

avatar
28
brave0heart 2006.10.16 20:47 
stringo, I did as you suggested, but the comments still issue the last bid/ask over and over, they don't start with the earliest piece of historical data.
1) I ran the period coverter 2) I modified the script as you kindly suggested, but adding more of a pause. Here it is: Thanks so much for you help,
Steve

//+------------------------------------------------------------------+
//|                                                         test.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
 
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   double d_Spread=MarketInfo(Symbol(),MODE_SPREAD) * MarketInfo(Symbol(),MODE_POINT);
   double d_Bid,d_Ask;
//----
   while(!IsStopped())
     {
      d_Bid=Close[0];
      d_Ask=d_Bid+d_Spread;
      Comment("bid = ", d_Bid," ask = ",d_Ask);   
      Alert("bid = ", d_Bid," ask = ",d_Ask);
      Sleep(1000);
      RefreshRates();
     }
//----
   return(0);
}
//+------------------------------------------------------------------+


avatar
Moderator
5198
stringo 2006.10.17 12:54 
What the build do You use?

avatar
28
brave0heart 2006.10.17 18:24 
stringo wrote:
What the build do You use?

build 198

avatar
Moderator
5198
stringo 2006.10.17 18:57 
very strange. my updated script

int start()
  {
   double d_Spread=MarketInfo(Symbol(),MODE_SPREAD) * MarketInfo(Symbol(),MODE_POINT);
   double d_Bid,d_Ask;
   bool   refreshed=true;
//----
   while(!IsStopped())
     {
      if(refreshed)
        {
         d_Bid=Close[0];
         d_Ask=d_Bid+d_Spread;
         Comment("bid = ", d_Bid," ask = ",d_Ask);
         Print("bid = ", d_Bid," ask = ",d_Ask);
        }
      Sleep(100);
      refreshed=RefreshRates();
     }
//----
   return(0);
}

and output log
18:46:13 period_converter EURUSD,H1: loaded successfully
18:46:38 period_converter EURUSD,H1: 1534 record(s) written
18:47:42 period_converter EURUSD,H1: Chart window detected
18:47:42 Compiling 'test_script'
18:48:22 test_script EURUSD,H3: loaded successfully
18:48:22 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:22 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:22 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:22 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:22 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:22 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:22 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:22 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:22 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:22 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:23 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:23 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
18:48:23 test_script EURUSD,H3: bid = 1.2538 ask = 1.2541
....
18:49:10 test_script EURUSD,H3: bid = 1.2539 ask = 1.2542
18:49:10 test_script EURUSD,H3: bid = 1.2539 ask = 1.2542
18:49:10 test_script EURUSD,H3: bid = 1.2539 ask = 1.2542
18:49:10 test_script EURUSD,H3: bid = 1.2539 ask = 1.2542
18:49:11 test_script EURUSD,H3: bid = 1.2539 ask = 1.2542
....
I've changed script
....
18:49:11 test_script EURUSD,H3: removed
18:49:12 Compiling 'test_script'
18:49:38 test_script EURUSD,H3: loaded successfully
18:49:38 test_script EURUSD,H3: bid = 1.254 ask = 1.2543
18:49:52 test_script EURUSD,H3: bid = 1.2541 ask = 1.2544
18:49:57 test_script EURUSD,H3: bid = 1.254 ask = 1.2543
18:50:14 test_script EURUSD,H3: bid = 1.2541 ask = 1.2544
18:50:14 test_script EURUSD,H3: bid = 1.254 ask = 1.2543
18:50:31 test_script EURUSD,H3: bid = 1.2539 ask = 1.2542
18:50:43 test_script EURUSD,H3: bid = 1.254 ask = 1.2543
18:51:16 test_script EURUSD,H3: bid = 1.2539 ask = 1.2542
18:51:16 test_script EURUSD,H3: bid = 1.254 ask = 1.2543
18:51:19 test_script EURUSD,H3: bid = 1.2539 ask = 1.2542
18:51:21 test_script EURUSD,H3: bid = 1.254 ask = 1.2543
18:51:35 test_script EURUSD,H3: bid = 1.2541 ask = 1.2544
18:52:30 test_script EURUSD,H3: bid = 1.254 ask = 1.2543
18:52:30 test_script EURUSD,H3: bid = 1.254 ask = 1.2543
18:52:33 test_script EURUSD,H3: bid = 1.2541 ask = 1.2544
18:52:35 test_script EURUSD,H3: bid = 1.254 ask = 1.2543
18:52:39 test_script EURUSD,H3: bid = 1.2541 ask = 1.2544
18:52:49 test_script EURUSD,H3: bid = 1.254 ask = 1.2543
18:52:51 test_script EURUSD,H3: bid = 1.2541 ask = 1.2544
18:53:00 test_script EURUSD,H3: bid = 1.254 ask = 1.2543

avatar
28
brave0heart 2006.10.17 20:10 
I loaded and tried your changed script, but get no output, although the log shows that it was loaded.
09:07:10 history2 EURJPYm,M15: loaded successfully

avatar
Moderator
5198
stringo 2006.10.18 11:29 
What about following lines?
===
18:46:13 period_converter EURUSD,H1: loaded successfully
18:46:38 period_converter EURUSD,H1: 1534 record(s) written
18:47:42 period_converter EURUSD,H1: Chart window detected
===
Period converter must detect a chart opened offline

Test script attached to the offline chart
===
18:48:22 test_script EURUSD,H3: loaded successfully
===
In this case EURUSD,H3

avatar
28
brave0heart 2006.10.18 18:12 
stringo wrote:
What about following lines?
===
18:46:13 period_converter EURUSD,H1: loaded successfully
18:46:38 period_converter EURUSD,H1: 1534 record(s) written
18:47:42 period_converter EURUSD,H1: Chart window detected
===
Period converter must detect a chart opened offline

Test script attached to the offline chart
===
18:48:22 test_script EURUSD,H3: loaded successfully
===
In this case EURUSD,H3
============================
OK. I have an offline H1 chart of the EUR USD loaded. I compiled your script, but it still reads the most current bid/ask. Here is the log.
07:06:58 Compiling 'hist_Test'
07:07:45 hist_Test EURUSDm,H1: loaded successfully
07:07:45 hist_Test EURUSDm,H1: bid = 1.2559 ask = 1.2561
07:07:45 hist_Test EURUSDm,H1: bid = 1.2559 ask = 1.2561

The script has stopped by itself.
Back to topics list   | 1 2  

To add comments, please log in or register