下载MetaTrader 5

求助:MT4可任意自定义周期的指标“realtime”不能用了

要添加评论,请登录注册
您习惯使用智能手机进行交易吗?MetaTrader 5移动版最适合您!
maihj
43
maihj 2014.08.04 10:41 

      以前一直能用的,自从MT4更新了之后,这个指标就运行不了了,编译的时候没有错误,但有一个警告,有代码高手给看看吗?

 

源码如下:

 extern double  Version = 1.3;             // code version

extern string  BuildInfo = "2005.12.04 by wfy05@talkforex.com";

extern int     PeriodMultiplier = 2;      // new period multiplier factor

extern int     UpdateInterval = 0;        // update interval in milliseconds, zero means update real-time.

extern bool    Enabled = true;

extern bool    Debug = false;


int      FileHandle = -1;

int      NewPeriod = 0;


#define  CHART_CMD_UPDATE_DATA            33324


void DebugMsg(string msg)

{

   if (Debug) Alert(msg);

}


int init()

{

   NewPeriod = Period() * PeriodMultiplier;

   if (OpenHistoryFile() < 0) return (-1);

   WriteHistoryHeader();

   WriteHistoryFile(Bars-1, true);

   UpdateChartWindow();

   return (0);

}


void deinit()

{

   //Close file handle

   if(FileHandle >=  0) { 

      FileClose(FileHandle); 

      FileHandle = -1; 

   }

}



int OpenHistoryFile()

{

   FileHandle = FileOpenHistory(Symbol()+NewPeriod+".hst", FILE_BIN|FILE_WRITE);

   if(FileHandle < 0) return(-1);

   return (0);

}


int WriteHistoryHeader()

{

   string c_copyright;

   int    i_digits = Digits;

   int    i_unused[13] = {0};

   int    version = 400;   


   if (FileHandle < 0) return (-1);

   c_copyright = "(C)opyright 2003, MetaQuotes Software Corp.";

   FileWriteInteger(FileHandle, version, LONG_VALUE);

   FileWriteString(FileHandle, c_copyright, 64);

   FileWriteString(FileHandle, Symbol(), 12);

   FileWriteInteger(FileHandle, NewPeriod, LONG_VALUE);

   FileWriteInteger(FileHandle, i_digits, LONG_VALUE);

   FileWriteInteger(FileHandle, 0, LONG_VALUE);       //timesign

   FileWriteInteger(FileHandle, 0, LONG_VALUE);       //last_sync

   FileWriteArray(FileHandle, i_unused, 0, ArraySize(i_unused));

   return (0);

}



int WriteHistoryFile(int start_pos, bool init = false)

{

   static int last_fpos, i_time;

   static double d_open, d_low, d_high, d_close, d_volume;


   

   int i, ps;

      

   if (FileHandle < 0) return (-1);

   // normalize open time

   ps = NewPeriod * 60;   

   i_time = Time[start_pos]/ps;

   i_time *=  ps;

   if (init) {

         //first time, init data

         d_open = Open[start_pos];

         d_low = Low[start_pos];

         d_high = High[start_pos];

         d_close = Close[start_pos];

         d_volume = Volume[start_pos];                           

         i = start_pos - 1;

   } else {

         i = start_pos;

         FileSeek(FileHandle,last_fpos,SEEK_SET);

   }

   if (i < 0) return (-1);


   int cnt = 0;

   int LastBarTime;

   //processing bars

   while (i >= 0) {

      LastBarTime = Time[i];


      //a new bar

      if (LastBarTime >=  i_time+ps) {

         //write the bar data

         FileWriteInteger(FileHandle, i_time, LONG_VALUE);

         FileWriteDouble(FileHandle, d_open, DOUBLE_VALUE);

         FileWriteDouble(FileHandle, d_low, DOUBLE_VALUE);

         FileWriteDouble(FileHandle, d_high, DOUBLE_VALUE);

         FileWriteDouble(FileHandle, d_close, DOUBLE_VALUE);

         FileWriteDouble(FileHandle, d_volume, DOUBLE_VALUE);

         cnt++;

         i_time = LastBarTime/ps;

         i_time *= ps;

         d_open = Open[i];

         d_low = Low[i];

         d_high = High[i];

         d_close = Close[i];

         d_volume = Volume[i];

      } else {

         //no new bar

         d_volume +=  Volume[i];

         if (Low[i]<d_low) d_low = Low[i];

         if (High[i]>d_high) d_high = High[i];

         d_close = Close[i];      

      }

      i--;

   }

   

   //record last_fpos before writing last bar.

   last_fpos = FileTell(FileHandle);

   //write last bar's data

   FileWriteInteger(FileHandle, i_time, LONG_VALUE);

   FileWriteDouble(FileHandle, d_open, DOUBLE_VALUE);

   FileWriteDouble(FileHandle, d_low, DOUBLE_VALUE);

   FileWriteDouble(FileHandle, d_high, DOUBLE_VALUE);

   FileWriteDouble(FileHandle, d_close, DOUBLE_VALUE);

   FileWriteDouble(FileHandle, d_volume, DOUBLE_VALUE);

   cnt++;

   d_volume -=  Volume[0];


   //flush the data writen

   FileFlush(FileHandle);

   return (cnt);

}


int UpdateChartWindow()

{

   static int hwnd = 0;


   if(hwnd == 0) {

      //trying to detect the chart window for updating

      hwnd = WindowHandle(Symbol(), NewPeriod);

   }

   if(hwnd!= 0) {

      if (IsDllsAllowed() == false) {

         //DLL calls must be allowed

         DebugMsg("Dll calls must be allowed");

         return (-1);

      }

      if (PostMessageA(hwnd,WM_COMMAND,CHART_CMD_UPDATE_DATA,0) == 0) {

         //PostMessage failed, chart window closed

         hwnd = 0;

      } else {

         //PostMessage succeed

         return (0);

      }

   }

   //window not found or PostMessage failed

   return (-1);

}



/*

int PerfCheck(bool Start)

{

   static int StartTime = 0;

   static int Index = 0;

   

   if (Start) {

      StartTime = GetTickCount();

      Index = 0;

      return (StartTime);

   }

   Index++;

   int diff = GetTickCount() - StartTime;

   Alert("Time used [" + Index + "]: " + diff);

   StartTime = GetTickCount();

   return (diff);

}

*/


static int LastStartTime = 0;

static int LastEndTime = 0;

static int LastBarCount = 0;


int reinit()

{

   deinit();

   init();

   LastStartTime = Time[Bars-1];

   LastEndTime = Time[0];

   LastBarCount = Bars;

}


bool IsDataChanged()

{

   static int LastBars = 0, LastTime = 0, LastVolume = 0;

   static double LastOpen = 0, LastClose = 0, LastHigh = 0, LastLow = 0;

   

   if (LastVolume != Volume[0] || LastBars != Bars || LastTime != Time[0]|| 

      LastClose != Close[0] || LastHigh != High[0] || LastLow != Low[0] || 

      LastOpen != Open[0]) {


      LastBars = Bars;

      LastVolume = Volume[0];

      LastTime = Time[0];

      LastClose = Close[0];

      LastHigh = High[0];

      LastLow = Low[0];

      LastOpen = Open[0];

      return (true);

   }

   return (false);

}

int CheckNewData()

{

   static string LastServer = "";

   

   if (Bars < 2) {

      //the data is not loaded yet.

      DebugMsg("Data not loaded, only " +  Bars + " Bars");

      return (-1);

   }


   string serv = ServerAddress();

   if (serv == "") {

      //no server yet

      DebugMsg("No server connected");

      return (-1);

   }


   //server changed? check this and reinit to prevent wrong data while changing server.

   if (LastServer != serv) {

      DebugMsg("Server changed from " + LastServer + " to " + serv);

      LastServer = serv;

      reinit();

      return (-1);

   }


   if (!IsDataChanged()) {

      //return if no data changed to save resource

      //DebugMsg("No data changed");

      return (-1);

   }


   if (Time[Bars-1] != LastStartTime) {

      DebugMsg("Start time changed, new history loaded or server changed");

      reinit();

      return (-1);

   }

      

   int i, cnt;

   

   //try to find LastEndTime bar, which should be Time[0] or Time[1] usually,

   //so the operation is fast

   for (i = 0; i < Bars; i++) {

      if (Time[i] <= LastEndTime) {

         break;

      }

   }

   

   if (i >= Bars || Time[i] != LastEndTime) {

      DebugMsg("End time " + TimeToStr(LastEndTime) + " not found");

      reinit();

      return (-1);

   }

   

   cnt = Bars - i;

   if (cnt != LastBarCount) {

      DebugMsg("Data loaded, cnt is " + cnt + " LastBarCount is " + LastBarCount);

      reinit();

      return (-1);

   }


   //no new data loaded, return with LastEndTime position.

   LastBarCount = Bars;

   LastEndTime = Time[0];

   return (i);

}


//+------------------------------------------------------------------+

//| program start function                                           |

//+------------------------------------------------------------------+

int start()

{

   static int last_time = 0;


   if (!Enabled) return (0);

         

   //always update or update only after certain interval

   if (UpdateInterval !=  0) {

      int cur_time;

      

      cur_time = GetTickCount();

      if (MathAbs(cur_time - last_time) < UpdateInterval) {

         return (0);

      }

      last_time = cur_time;

   }


   //if (Debug) PerfCheck(true);

   int n = CheckNewData();

   //if (Debug) PerfCheck(false);   

   if (n < 0) return (0);


   //update history file with new data

   WriteHistoryFile(n);

   //refresh chart window

   UpdateChartWindow();

   //if (Debug) PerfCheck(false);

   return(0);

 

 --------------------------------------------------------------

 

编译结果如下: 

 'realtime.mq4' realtime.mq4 1 1

'WinUser32.mqh' WinUser32.mqh 1 1

not all control paths return a value realtime.mq4 299 1

0 error(s), 1 warning(s) 1 2



maihj
43
maihj 2014.08.04 10:44  
 这是指标
附加的文件:
realtime.mq4 14 kb
enbo lu
版主
1947
enbo lu 2014.08.04 15:51  

贴代码请用 SRC 按钮!!

另外,这只是一个警告,告诉你所有的控制路径都应该有返回值,但不影响使用。

int reinit()
{
   deinit();
   init();
   LastStartTime = Time[Bars-1];
   LastEndTime = Time[0];
   LastBarCount = Bars;
}
int 改为 void: 
void reinit()
{
   deinit();
   init();
   LastStartTime = Time[Bars-1];
   LastEndTime = Time[0];
   LastBarCount = Bars;
}
maihj
43
maihj 2014.08.07 19:10  
多谢版主,编译通过了,但运行MT4在指标列表里却仍然找不到这个指标,怎么回事?
enbo lu
版主
1947
enbo lu 2014.08.08 01:14  
maihj:
多谢版主,编译通过了,但运行MT4在指标列表里却仍然找不到这个指标,怎么回事?
放在**\MQL4\Indicators文件夹下,重新编译一下,没有显示的话退出平台在打开试试。
maihj
43
maihj 2014.08.08 06:58  
都试过了,还是不行,在旧版本的MT4可以使用,新版本的就不能运行了
enbo lu
版主
1947
enbo lu 2014.08.08 09:56  
maihj:
都试过了,还是不行,在旧版本的MT4可以使用,新版本的就不能运行了
把日志贴出来看
maihj
43
maihj 2014.08.10 08:15  
luenbo:
把日志贴出来看
maihj
43
maihj 2014.08.10 08:18  
请问版主,还有其他方法能在MT4实现看2,6,8,12小时图吗?除了MT4自带的脚本,那个脚本不好用,最好不用离线图表,就像MT5一样的看
要添加评论,请登录注册