Script example

 
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
 
look at my script "CloseOnChart" in code base.
 
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. |
//|                                        https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "https://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);
}
//+------------------------------------------------------------------+
 
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
 
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. |
//|                                        https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "https://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);
}
//+------------------------------------------------------------------+
 
What the build do You use?
 
stringo wrote:
What the build do You use?

build 198
 
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
 
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
 
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
 
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.
Reason: