金融数据类——外汇,CFD

外汇交易一般都在MetaTrader 4,简称MT4上进行,MT4提供了一种编程语言类似C/C++,叫MQL4,可以直接挂到对应的图表上,接收数据。MT4上的脚本也有很多类型,实时抓取数据的脚本叫EA脚本。

主要思路就是用cmd命令自动打开MT4,然后使用自动化脚本把写好的抓取数据脚本attach到行情图上,就可以开始抓数据了,收到数据之后实时写到mysql中,完成实时数据的抓取。抓到的都是tick data,也就是毫秒级的数据。如果需要时间段的数据,比如5分钟,4小时,1天的数据,恐怕得自己在后台去算。

 

下面看一下具体过程:

首先,cmd里写打开MT4软件的命令

 

 
  1. @echo off

  2.  
  3. set mt4path=D:\MetaTrader 4 at FOREX.com\

  4.  
  5. "%mt4path%terminal.exe" "%mt4path%config\wscn\realtime.txt"


然后,我们看一下realtime这个配置文件

 

Profile=AllCharts

Template=
Expert=
ExpertParameters=
Script=AutoApplyEAToAllCharts
ScriptParameters

上面的Profile代表打开MT4的时候,自动打开的空间文件,其实就是默认打开哪些行情图表。然后Script的意思是打开MT4以后自动运行这个脚本。

 

接着看AutoApplyEAToAllCharts脚本主要干什么

 

 
  1. string EA_SCRIPT_NAME = "insertMySql";

  2. string properties[1000][2],currency[];

  3.  
  4. int start()

  5. {

  6.  
  7. readPropertyFile(properties,getRealtimeConfigFilePath());

  8. getFetchRealTimeCharts(properties,currency);

  9.  
  10. Sleep(60000);

  11.  
  12. for(int i=0;i<ArraySize(currency);i++){

  13. string cur = StringTrimRight(StringTrimLeft(currency[i]));

  14. //Print("cur",cur);

  15. if(cur == "")continue;

  16. int hwnd = Window.getHandle(cur);

  17. Window.activate(hwnd);

  18. //Window.maximize(hwnd);

  19. EA.startEA(hwnd,EA_SCRIPT_NAME,true);

  20. }

  21.  
  22. return(0);

  23. }

上面的代码意思主要功能就是把抓取数据的脚本具体attach到行情图表窗口上去,因为每个行情图表都是一个小窗口。

 

最后看insertMySql脚本,主要功能都在这里面实现

 

 
  1. int init()

  2. {

  3. readPropertyFile(properties,getRealtimeConfigFilePath());

  4. strTableName = getValueByKey(properties,Symbol());

  5. strNewDataTableName = getValueByKey(properties,"newdatatable");

  6.  
  7. connectDB();

  8.  
  9. return(0);

  10. }

  11.  
  12. void connectDB(){

  13. string host,user,pwd,dbName,port;

  14. int socket = 0,client = 0;

  15. bool goodConnect = false;

  16.  
  17. host = getValueByKey(properties,"host");

  18. user = getValueByKey(properties,"username");

  19. pwd = getValueByKey(properties,"password");

  20. dbName = getValueByKey(properties,"database");

  21. port = getValueByKey(properties,"port");

  22.  
  23. goodConnect = mysqlInit(dbConnectId, host, user, pwd, dbName, port, socket, client);

  24.  
  25. if ( !goodConnect ) {

  26. Print("connect DB failed!");

  27. return (1); // bad connect

  28. }

  29.  
  30. }

  31.  
  32. int deinit()

  33. {

  34. mysqlDeinit(dbConnectId);

  35. return(0);

  36. }

  37.  
  38. int start()

  39. {

  40.  
  41. string strDateTime = TimeToStr(TimeCurrent(),TIME_DATE | TIME_SECONDS);

  42. string strOpen = DoubleToStr(iOpen(Symbol(), 0, 0), MarketInfo(Symbol(), MODE_DIGITS));

  43. string strHigh = DoubleToStr(iHigh(Symbol(), 0, 0), MarketInfo(Symbol(), MODE_DIGITS));

  44. string strLow = DoubleToStr(iLow(Symbol(), 0, 0), MarketInfo(Symbol(), MODE_DIGITS));

  45. string strClose = DoubleToStr(iClose(Symbol(), 0, 0), MarketInfo(Symbol(), MODE_DIGITS));

  46. string strVolume = iVolume(Symbol(), NULL, 0);

  47. string strBid = DoubleToStr(Bid, MarketInfo(Symbol(), MODE_DIGITS));

  48. string strAsk = DoubleToStr(Ask, MarketInfo(Symbol(), MODE_DIGITS));

  49.  
  50.  
  51. string timeZone = StringTrimRight(StringTrimLeft(getValueByKey(properties,"timeZone")));

  52. int adjustTime = TimeGMT();

  53. if(timeZone != ""){

  54. adjustTime += StrToInteger(timeZone) * 60 * 60 * 1000;

  55. }

  56.  
  57. Comment(TimeCurrent()+" ", adjustTime+" ",

  58. strBid+" ",

  59. strAsk+" ",

  60. strHigh+" ",

  61. strLow+" ",

  62. strVolume);

  63.  
  64. string insertQuery = createInsertQuery(strTableName,Symbol(),adjustTime,Bid,Ask,

  65. iHigh(Symbol(), 0, 0),iLow(Symbol(), 0, 0),iVolume(Symbol(), NULL, 0));

  66. mysqlQuery(dbConnectId, insertQuery);

  67.  
  68. string updateQuery = createUpdateQuery(strNewDataTableName,Symbol(),adjustTime,Bid,Ask,

  69. iHigh(Symbol(), 0, 0),iLow(Symbol(), 0, 0),iVolume(Symbol(), NULL, 0));

  70. mysqlQuery(dbConnectId, updateQuery);

  71.  
  72. return(0);

  73. }

上面的代码首先连接mysql DB,然后在start函数里面接收tick data,也就是说行情每跳动一次,就会调用一次start方法,这是MT4脚本提供的回调。然后在start方法里,把tick data插入到历史数据表中,接着更新实时行情数据表。历史表和实时数据表是分开的。

 

上面的过程只是个大概程序,具体代码比这个多多了,这里只是提供个思路,感兴趣的,有需要的,在联系我。

 

=========================================

这里说一下,不同的外汇供应商提供的数据种类不一样,比如嘉盛提供的外汇种类可能比福汇多,而每家供应商都会对MT4做定制,如果想抓的比较全,需要打开不同供应商的MT4软件,然后执行上面的过程。

 

所谓的CFD意思是价差合约,背后没有实际的金融资产做支撑,这种玩意在美国是没法交易的,在欧洲可以交易。它往往追踪股指,比如标普500,纳纳斯达克100指数,但是数值不完全一样,因为CFD是5x24小时交易,而对应的股指只有在每天固定的时间交易

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值