量化金融小初探
用C#访问IFind的简单例子:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Runtime.Serialization; //using Newtonsoft.Json; using System.Text.RegularExpressions; using System.Threading; namespace ConsoleiFind { class Program { /* * D:\iFinDRelease\iFinD\bin\x86\:该目录是用户本地的iFinD安装目录且修复的位数是32位,如果是64位则目录为D:\iFinDRelease\iFinD\bin\x64\ * FTDataInterface.dll为接口文件,该文件是32位的,如果是64位,则文件名称为FTDataInterface_x64.dll */ //当前FTDataInterface.dll或者FTDataInterface_x64.dll文件的获取可以通过修复C++接口得到:【iFinD终端-工具-数据接口-C++修复文件】 const string strPath = @"D:\\金融\\DataInterface_free_Windows\\bin\\x64\\"; const string strDllFileName = "FTDataInterface_x64.dll"; const string strDllFile = strPath + strDllFileName; static void Main(string[] args) { /*登录函数 */ string strUserName = ""; string strPassWord = ""; int nRet = THS_iFinDLogin(strUserName, strPassWord); string strOut = "Login result:" + nRet; Console.WriteLine(strOut); _event = new AutoResetEvent(false); ThreadStart threadStart = new ThreadStart(testSample); Thread thread = new Thread(threadStart); thread.Start(); _event.WaitOne(); //登出函数 nRet = THS_iFinDLogout(); strOut = "Logout result:" + nRet; Console.WriteLine(strOut); Console.ReadLine(); } private static AutoResetEvent _event; public static void testSample() { //高频序列函数 HighFrequenceSequenceSample(); //实时行情函数 RealtimeQuotesSample(); //历史行情函数 HistoryQuotesSample(); //基础数据函数 BasicDataSample(); //日期序列函数 DateSequenceSample(); //数据池函数 DataPoolSample(); //EDB数据请求函数 EDBQuerySample(); //数据使用量统计函数 DataStatisticsSample(); //错误信息查询函数 GetErrorInfoSample(); //日期请求函数 DateQuerySample(); //日期偏移函数 DateOffsetSample(); //日期统计函数 DateCountSample(); Thread.Sleep(3000); _event.Set(); } static void HighFrequenceSequenceSample() { string strOut; //请求高频数据 传递5个字符串参数,返回数据 /*高频序列函数格式为THS_HighFrequenceSequence('thsCodes','indicators','params','startTime','endTime') * thsCodes不可以为空,且支持多个输入,当有多个thsCodes则用英文半角逗号分隔,如thsCode1,thsCode2,thsCode3 * indicators不可以为空,且支持多个输入,当有多个indicators则用英文半角分号分隔,如indicator1;indicator2;indicator3 * params不可以为空,且支持多个输入,当使用默认的参数时可以使用'default'表示,当用户只对其中某个指标设定而其他参数保持默认时,只需要输入设定的参数即可,如'Interval:5' * startDate的日期输入格式为YYYY-MM-DD HH:MM:SS * endDate的日期输入格式为YYYY-MM-DD HH:MM:SS * THS_HighFrequenceSequence('thsCode1,thsCode2,thsCode3','indicator1;indicator2;indicator3','param1,param2,param3','startTime','endTime') */ strOut = THS_HighFrequenceSequence("600000.SH,300033.SZ", "open;high", "CPS:1,Interval:5,Fill:Previous", "2016-09-01 09:30:00", "2016-09-01 15:30:00"); //高频序列函数 Console.WriteLine("高频序列函数"); Console.WriteLine(strOut); } static void RealtimeQuotesSample() { string strOut; //请求实时数据 /*实时行情函数的格式为THS_RealtimeQuotes('thsCodes','indicators','params') * thsCodes不可以为空,且支持多个输入,当有多个thsCodes则用英文半角逗号分隔,如thsCode1,thsCode2,thsCode3 * indicators不可以为空,且支持多个输入,当有多个indicators则用英文半角分号分隔,如indicator1;indicator2;indicator3 * params不可以为空 * THS_RealtimeQuotes('thsCode1,thsCode2,thsCode3','indicator1;indicator2;indicator3','param1,param2,param3') */ strOut = THS_RealtimeQuotes("600000.SH,300033.SZ", "open;close;new;avg;change;price;volume", "pricetype:2"); // 实时行情函数 Console.WriteLine("实时行情函数"); Console.WriteLine(strOut); } static void HistoryQuotesSample() { string strout; //请求历史行情 /* *通过历史行情函数获取同花顺(300033.SZ)从2016-08-23到2016-11-23的开高低收数据 *历史行情函数格式为THS_HistoryQuotes('thsCodes','indicators','params','startDate','endDate') *thsCodes不可以为空,且支持多个输入,当有多个thsCodes则用英文半角逗号分隔,如thsCode1,thsCode2,thsCode3 *indicators不可以为空,且支持多个输入,当有多个indicators则用英文半角分号分隔,如indicator1;indicator2;indicator3 *params不可以为空,且支持多个输入,当使用默认的参数时可以使用'default'表示,当用户只对其中某个指标设定而其他参数保持默认时,只需要输入设定的参数即可,如'period:W' *startDate的日期输入格式为YYYY-MM-DD *endDate的日期输入格式为YYYY-MM-DD *THS_HistoryQuotes('thsCode1,thsCode2,thsCode3','indicator1;indicator2;indicator3','param1,param2,param3','startDate','endDate') */ strout = THS_HistoryQuotes("300033.SZ,600000.SH", "open;high;low;close", "period:D,pricetype:1,rptcategory:0,fqdate:1900-01-01,hb:YSHB", "2016-11-09", "2016-12-08"); //历史行情函数 Console.WriteLine("历史行情函数"); Console.WriteLine(strout); } static void BasicDataSample() { string strOut; //请求基础数据 /*基础数据THS_BasicData('thsCodes','function','params');支持多证券单指标输入 * thsCodes不可以为空,且支持多个输入,当有多个thsCodes则用英文半角逗号分隔,如thsCode1,thsCode2,thsCode3 * function不可以为空,且当前只支持单个function,目前函数名称可以在【iFinD终端-工具-数据接口-指标函数查询工具】查看 * params可以为空,也可以有多个,当有多个params时则用英文半角逗号分隔,如param1,param2,param3 * THS_BasicData('thsCode1,thsCode2,thsCode3','function','param1,param2,param3') */ strOut = THS_BasicData("600000.SH,300033.SZ", "ths_mgsyeps_jb_stock", "2016-09-30"); //基础数据函数 Console.WriteLine("基础数据函数"); Console.WriteLine(strOut); } static void DateSequenceSample() { string strOut; //请求日期序列 /*日期系列函数格式为THS_DateSequence('thsCodes','indicators','params','startDate','endDate') * thsCodes不可以为空,且支持多个输入,当有多个thsCodes则用英文半角逗号分隔,如thsCode1,thsCode2,thsCode3 * indicators不可以为空,且支持多个输入,当有多个indicators则用英文半角分号分隔,如indicator1;indicator2;indicator3 * params不可以为空,且支持多个输入,当使用默认的参数时可以使用'default'表示,当用户只对其中某个指标设定而其他参数保持默认时,只需要输入设定的参数即可,如'Interval:M' * startDate的日期输入格式为YYYY-MM-DD * endDate的日期输入格式为YYYY-MM-DD * 日期序列函数格式为THS_DateSequence('thsCode1,thsCode2,thsCode3','indicator1;indicator2;indicator3','param1,param2,param3','startDate','endDate') */ strOut = THS_DateSequence("600000.SH,300033.SZ", "open;BBI=BBI_day1:4,BBI_day2:7,BBI_day3:13,BBI_day4:25;close=close_fq_type:101", "CPS:1,Interval:W,Fill:Previous,Days:Workdays", "2014-12-18 09:30:00", "2015-01-22 15:30:00"); //日期序列函数 Console.WriteLine("日期序列函数"); Console.WriteLine(strOut); } static void DataPoolSample() { string strOut; //请求数据池 /* * 数据池函数格式为THS_DataPool('modelName','inputParams','outputParams') * modelName不可以为空,且一次只能输入一个 * inputParams用英文半角分号隔开,如inputParam1;inputParam2;inputParam3 * outputParams用英文半角冒号赋值,用英文半角逗号分隔,Y表示该字段输出,N表示该字段不输出,如果不写则默认为Y,如outputParam1:Y,outputParam2:Y,outputParam3:N * THS_DataPool('modelName','inputParam1;inputParam2;inputParam3','outputParam1,outputParam2,outputParams3') * 【001005260】是板块ID,目前板块ID可以在【iFinD终端-工具-数据接口-板块ID查询工具】查看 */ strOut = THS_DataPool("block", "2016-12-19;001005260", "date:Y,security_name:Y,thscode:Y"); //数据池函数 Console.WriteLine("数据池函数"); Console.WriteLine(strOut); } static void EDBQuerySample() { string strout; //请求EDB宏观经济数据 /*EDB数据请求函数格式为THS_EDBQuery('indicatorIDs','startDate','endDate') * indicatorIDs不可以为空,支持多个ID输入。指标ID可以在【iFinD终端-工具-数据接口】中的指标ID查询工具查看 * startDate的日期输入格式为YYYY-MM-DD * endDate的日期输入格式为YYYY-MM-DD */ strout = THS_EDBQuery("M002945620", "2000-01-01", "2017-01-01"); //EDB数据请求函数 Console.WriteLine("EDB数据请求函数"); Console.WriteLine(strout); } static void DataStatisticsSample() { string strout; //用户数据使用量查询函数 /* * 数据使用量查询函数,用于用户查询自身账号的数据使用量,其中行情数据是15000万条/周,基础数据是500万条/周,EDB数据是500条/周; * 通过高频序列函数、历史行情函数和实时行情函数获取的数据统称为行情数据; * 通过基础数据函数、日期序列函数和数据池函数获取的数据统称为基础数据; * 通过EDB数据请求函数获取的数据统称为EDB数据。 */ strout = THS_DataStatistics(); //数据使用量统计函数 Console.WriteLine("数据使用量统计函数"); Console.WriteLine(strout); } static void GetErrorInfoSample() { string strout; //错误代码信息查询 /*错误信息查询函数,对于函数执行后的errorcode进行查询,了解错误信息 * value的值不可以为空,并且value的值必须是枚举出的错误值 */ strout = THS_GetErrorInfo(-1); //错误信息查询函数 Console.WriteLine("错误信息查询函数"); Console.WriteLine(strout); } static void DateQuerySample() { string strout; //方便用户在其自定义的时间区间内查询交易所的交易日历 /* * 交易日期/日历日期查询函数 * 日期查询函数的格式是THS_DateQuery('exchange','dateType:value,period:value,dateFormat:value','startDate','endDate') * exchange不可以为空 * dateType,period,dateFormat的值也不可以为空 * startDate的日期输入格式为YYYY-MM-DD * endDate的日期输入格式为YYYY-MM-DD */ strout = THS_DateQuery("SSE", "dateType:trade,period:D,dateFormat:0", "2016-07-21", "2017-08-21"); //日期请求函数 Console.WriteLine("日期请求函数"); Console.WriteLine(strout); } static void DateOffsetSample() { string strout; //根据用户指定一个日期和偏移量查询到相应的交易日期 /* * 根据指定日期和偏移量找到相应的日期 * 日期偏移函数的格式是THS_DateQuery('exchange','dateType:value,period:value,dateFormat:value','date') * exchange不可以为空 * dateType,period,dateFormat的值也不可以为空 * date的日期输入格式为YYYY-MM-DD */ strout = THS_DateOffset("SSE", "dateType:trade,period:W,offset:-10,dateFormat:0", "2017-08-21"); //日期偏移函数 Console.WriteLine("日期偏移函数"); Console.WriteLine(strout); } static void DateCountSample() { string strout; //方便用户统一在其自定义的时间区间内查询交易日期的总数 /* * 统计指定时间区间和日期类型中的日期数量 * 日期查询函数的格式是THS_DateCount('exchange','dateType:value,period:value,dateFormat:value','startDate','endDate') * exchange不可以为空 * dateType,period,dateFormat的值也不可以为空 * startDate的日期输入格式为YYYY-MM-DD * endDate的日期输入格式为YYYY-MM-DD */ strout = THS_DateCount("SSE", "dateType:trade,period:D,dateFormat:0", "2016-07-21", "2017-08-21"); //日期统计函数 Console.WriteLine("日期统计函数"); Console.WriteLine(strout); } /********************************************************************************************************************************************************************/ [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] public static extern int FT_ifinDLogin(string strUserName, string strPassWord, int nType); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] public static extern int FT_ifinDLogout(); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern IntPtr FTQueryTHS_SynDateCountC(string ExchangeCode, //交易所 string Param, //参数 string startDate, //函数需要的参数 string endDate, out char* pret ); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern IntPtr FTQueryTHS_SynDateOffsetC(string ExchangeCode, //交易所 string Param, //参数 string endDate, out char* pret ); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern IntPtr FTQueryTHS_SynDateQueryC(string ExchangeCode, //交易所 string Param, //参数 string startDate, //函数需要的参数 string endDate, out char* pret ); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern int FTQueryTHS_SynDataPoolC(string DataPoolName, //数据池 string jsonIndicator, // string jsonParam, //函数需要的参数 out char* pret ); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern int FTQueryTHS_SynDateSeriesC(string THSCodes, //同花顺代码 string jsonIndicator, //指标及指标需要的参数 string jsonParam, //函数需要的参数 string begintime, string endtime, out char* pret ); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern int FTQueryTHS_SynBasicDataC(string THSCodes, //同花顺代码 string jsonIndicator, //指标 string jsonParam, //函数需要的参数 out char* pret ); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern int FTQueryTHS_SynHisQuoteC(string THSCodes, //同花顺代码 string jsonIndicator, //指标及指标需要的参数 string jsonParam, //函数需要的参数 string begintime, string endtime, out char* pret ); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern int FTQueryTHS_SynRTByJsonC(string THSCodes, //同花顺代码 string jsonIndicator, //指标及指标需要的参数 string jsonParam, //函数需要的参数 out char* pret ); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern int FTQueryTHS_SynHFQByJsonC(string THSCodes, string jsonIndicator, string jsonParam, string begintime, string endtime, out char* pret); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern int FTQueryTHS_SynEDBQueryC(string jsonIndicator, string begintime, string endtime, out char* pret); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern int FTQueryTHS_SynGetErrorInfoC(int errorcode, out char* pret); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern IntPtr FTQuery_SynDataStasticsC(out int errorcode, void* reserve1, void* reserve2, void* reserve3); [DllImport(strDllFile, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] unsafe public static extern void DeleteBuffer(char* pret); /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //封装的函数命令 直接按照以下封装接口调用即可 static int THS_iFinDLogin(string UserAcount, string Password) { int nRet = FT_ifinDLogin(UserAcount, Password, 0); return nRet; } static int THS_iFinDLogout() { int nRet = FT_ifinDLogout(); return nRet; } unsafe static string THS_HighFrequenceSequence(string THSCodes, string jsonIndicator, string jsonParam, string begintime, string endtime)//高频序列新版接口 { string strOut; char* Pret; FTQueryTHS_SynHFQByJsonC(THSCodes, jsonIndicator, jsonParam, begintime, endtime, out Pret); IntPtr data = new IntPtr(Pret); strOut = Marshal.PtrToStringUni(data); DeleteBuffer(Pret); return strOut; } unsafe static string THS_RealtimeQuotes(string THSCodes, //同花顺代码 string jsonIndicator, //指标及指标需要的参数 string jsonParam //函数需要的参数 )//实时行情新版接口 { string strOut; char* Pret; FTQueryTHS_SynRTByJsonC(THSCodes, jsonIndicator, jsonParam, out Pret); IntPtr data = new IntPtr(Pret); strOut = Marshal.PtrToStringUni(data); DeleteBuffer(Pret); return strOut; } unsafe static string THS_HistoryQuotes(string THSCodes, //同花顺代码 string jsonIndicator, //指标及指标需要的参数 string jsonParam, //函数需要的参数 string begintime, string endtime )//历史行情新版接口 { string strOut; char* Pret; FTQueryTHS_SynHisQuoteC(THSCodes, jsonIndicator, jsonParam, begintime, endtime, out Pret); IntPtr data = new IntPtr(Pret); strOut = Marshal.PtrToStringUni(data); DeleteBuffer(Pret); return strOut; } unsafe static string THS_BasicData(string THSCodes, //同花顺代码 string jsonIndicator, //指标 string jsonParam //函数需要的参数 )//基础数据新版接口 { string strOut; char* Pret; FTQueryTHS_SynBasicDataC(THSCodes, jsonIndicator, jsonParam, out Pret); IntPtr data = new IntPtr(Pret); strOut = Marshal.PtrToStringUni(data); DeleteBuffer(Pret); return strOut; } unsafe static string THS_DateSequence(string THSCodes, //同花顺代码 string jsonIndicator, //指标及指标需要的参数 string jsonParam, //函数需要的参数 string begintime, string endtime)//日期序列新版接口 { string strOut; char* Pret; FTQueryTHS_SynDateSeriesC(THSCodes, jsonIndicator, jsonParam, begintime, endtime, out Pret); IntPtr data = new IntPtr(Pret); strOut = Marshal.PtrToStringUni(data); DeleteBuffer(Pret); return strOut; } unsafe static string THS_DataPool(string DataPoolName, //数据池 string jsonIndicator, // string jsonParam //函数需要的参数 )//数据池新版接口 { string strOut; char* Pret; FTQueryTHS_SynDataPoolC(DataPoolName, jsonIndicator, jsonParam, out Pret); IntPtr data = new IntPtr(Pret); strOut = Marshal.PtrToStringUni(data); DeleteBuffer(Pret); return strOut; } unsafe static string THS_DateQuery(string ExchangeCode, //交易所 string Param, //参数 string startDate, //函数需要的参数 string endDate)//日期查询新版接口 { string strOut; char* Pret; FTQueryTHS_SynDateQueryC(ExchangeCode, Param, startDate, endDate, out Pret); IntPtr data = new IntPtr(Pret); strOut = Marshal.PtrToStringUni(data); DeleteBuffer(Pret); return strOut; } unsafe static string THS_DateOffset(string ExchangeCode, //交易所 string Param, //参数 string endDate)//日期便宜新版接口 { string strOut; char* Pret; FTQueryTHS_SynDateOffsetC(ExchangeCode, Param, endDate, out Pret); IntPtr data = new IntPtr(Pret); strOut = Marshal.PtrToStringUni(data); DeleteBuffer(Pret); return strOut; } unsafe static string THS_DateCount(string ExchangeCode, //交易所 string Param, //参数 string startDate, //函数需要的参数 string endDate)//日期数量新版接口 { string strOut; char* Pret; FTQueryTHS_SynDateCountC(ExchangeCode, Param, startDate, endDate, out Pret); IntPtr data = new IntPtr(Pret); strOut = Marshal.PtrToStringUni(data); DeleteBuffer(Pret); return strOut; } unsafe static string THS_EDBQuery(string jsonIndicator, string begintime, string endtime ) { string strOut; char* Pret; FTQueryTHS_SynEDBQueryC(jsonIndicator, begintime, endtime, out Pret); IntPtr data = new IntPtr(Pret); strOut = Marshal.PtrToStringUni(data); DeleteBuffer(Pret); return strOut; } unsafe static string THS_DataStatistics() { string strOut; int code; strOut = Marshal.PtrToStringUni(FTQuery_SynDataStasticsC(out code, null, null, null)); return strOut; } unsafe static string THS_GetErrorInfo(int code) { string strOut; char* Pret; FTQueryTHS_SynGetErrorInfoC(code, out Pret); IntPtr data = new IntPtr(Pret); strOut = Marshal.PtrToStringUni(data); DeleteBuffer(Pret); return strOut; } } }
,Best Wish 不负年华