public void DoIt() { string strErrorMsg; WindAPI w = new WindAPI(); //登录WFT int nRetCode = w.start(); if (0 != nRetCode)//登录失败 { strErrorMsg = w.getErrorMsg(nRetCode); Console.Write(strErrorMsg); return; } //登录账号 Console.Write("\r\ntlogon……"); WindData wd = w.tlogon("0000", "0", "w081496501", "******", "SHSZ"); string strLogonId = wd.GetLogonId(); wd = w.tquery("Trade"); return; //下单浦发银行 wd = w.torder("600000.SH", "Buy", "12.0", "200", "OrderType=LMT;LogonID=" + strLogonId);//单账户登录可以不指定LogonId=1 //获取下单ID string strRequestID1 = wd.GetOrderRequestID(); Console.WriteLine("RequestID=" + strRequestID1); //下单白云机场 wd = w.torder("600004.SH", "Buy", "12.00", "300", "OrderType=LMT;LogonID=" + strLogonId);//单账户登录可以不指定LogonId=1 //获取下单ID string strRequestID2 = wd.GetOrderRequestID(); //查询 Console.WriteLine("query……"); wd = w.tquery("Order", "RequestID=" + strRequestID1); //获取浦发银行OrderNumber string strOrderNumber = wd.GetOrderNumber(); //浦发银行撤单 Console.WriteLine("cancel……"); wd = w.tcancel(strOrderNumber); //再次查询 Console.WriteLine("query after cancel……"); wd = w.tquery("Order"); string strQueryInfoAfter = WindDataMethod.WindDataToString(wd, "tquery"); Console.Write(strQueryInfoAfter); //登出 Console.WriteLine("tlogout……"); w.tlogout(); //退出WindAPI w.stop(); }
/// <summary> /// 从万德数据库中读取交易日信息数据。 /// </summary> private void GetDataFromWindDataBase() { int theLastDay = 0; if (tradeDaysOfDataBase.Count > 0) { theLastDay = tradeDaysOfDataBase[tradeDaysOfDataBase.Count - 1]; } //万德API接口的类。 WindAPI w = new WindAPI(); w.start(); //从万德数据库中抓取交易日信息。 WindData days = w.tdays("20100101", "20161231", ""); //将万德中读取的数据转化成object数组的形式。 object[] dayData = days.data as object[]; foreach (object item in dayData) { DateTime today = (DateTime)item; int now = DateTimeToInt(today); if (now > theLastDay) { tradeDaysOfDataBase.Add(now); } } w.stop(); }
/// <summary> /// 获取股票列表的信息。 /// </summary> /// <param name="market">市场</param> /// <returns>商品期货信息列表</returns> public List <stockFormat> GetStockList(string indexName) { List <stockFormat> myList = new List <stockFormat>(); //按日期遍历,添加股票信息。 WindAPI w = new WindAPI(); w.start(); string[] dateStr = { "2013-06-01", "2013-12-01", "2014-06-01", "2014-12-01", "2015-06-01", "2015-12-01", "2016-06-01", "2016-06-20" }; foreach (var item in dateStr) { WindData wd = w.wset("sectorconstituent", "date=" + item + ";windcode=" + indexName); object[] stockList = wd.data as object[]; int num = stockList.Length / 3; for (int i = 0; i < num; i++) { stockFormat myStock = new stockFormat(); myStock.market = "SH"; myStock.code = Convert.ToString(stockList[i * 3 + 1]).Substring(0, 6); myStock.name = (string)stockList[i * 3 + 2]; if (myList.Contains(myStock) == false) { myList.Add(myStock); } } } w.stop(); return(myList); }
//停止 private void stop() { if (null != m_w) { m_w.stop(); } }
//停止 public static void Logout() { if (null != windHandle) { windHandle.stop(); } }
private void Excute_Click(object sender, EventArgs e) { InitializeChart(); this.myChart.Series.Clear(); string strStartDate = this.startDateTimePicker.Text; string strEndDate = this.endDateTimePicker.Text; WindAPI w = new WindAPI(); w.start(); WindData wd = null; if (this.radioButton_KQ.Checked) { wd = w.edb("M5407921", strStartDate, strEndDate, ""); } else { wd = w.edb("M0000272", strStartDate, strEndDate, ""); } w.stop(); UpdateChart(wd); }
public void Execute() { //从万得提取数据 WindAPI w = new WindAPI(); w.start(); WindData wd = w.wsi("510050.SH", "open,high,low,close,volume" , "2016-07-01 09:00:00", "2016-07-22 15:00:00", "Fill=Previous"); w.stop(); //初始化各类变量 DateTime[] DataDate = wd.timeList; String.Format("yyyy/MM/dd hh:mm:ss", DataDate); //日期、开盘价、最高价、最低价、收盘价、成交量 int FieldLength = wd.GetFieldLength(); int DataLength = wd.GetDataLength(); int DateLength = DataLength / FieldLength; int sign = 0;//下标计数 double[] RawData = (double[])wd.data; double[,] MarketData = new double[DateLength, 5]; /* * double[] OP = new double[DateLength]; * double[] HP = new double[DateLength]; * double[] LP = new double[DateLength]; * double[] CP = new double[DateLength]; */ for (int i = 0; i < RawData.Length; i = i + FieldLength) { for (int j = 0; j < FieldLength; j++) { MarketData[sign, j] = RawData[i + j]; } sign++; } // System.Console.WriteLine("{0},{1},{2},{3}", sign, MarketData.GetLength(0), MarketData.GetLength(1), DateLength); /* * for (int now = 0; now <= sign; now++) * { * if (now != 0 && now % 60 == 0)//60min做一次停顿 * Console.ReadKey(); * System.Console.WriteLine("Time:{0} -- O:{1} H:{2} L:{3} C:{4} Volum:{5}\n" * , DataDate[now], MarketData[now,0], MarketData[now,1] * , MarketData[now,2], MarketData[now,3], MarketData[now,4]); * } * Console.ReadKey(); */ GenSignal myGS = new GenSignal(); myGS.getSignal(MarketData); }
/// <summary> /// 整个应用的终止 /// </summary> public static void ShutDown() { if (_windAPI != null) { log.Debug("正在关闭Wind API..."); if (_windAPI.isconnected()) { _windAPI.stop(); } } log.Info("------ Platform已关闭. ------"); }
//关闭API public static void stopAPI() { if (api != null && api.isconnected()) { try { api.stop(); } catch (Exception e) { Console.WriteLine(e); } } }
static void DoAPISameple() { WindAPI w = new WindAPI(); w.start(); //wset取沪深300指数成分 //WindData wd = w.wset("IndexConstituent", "date=20141215;windcode=000300.SH"); //OutputWindData(wd, "wset"); WindData wd = w.wsd("I2009.DCE", "open", "2020-05-24", "2020-05-24", ""); OutputWindData(wd, "wsd"); w.stop(); }
/// <summary> /// 按日期遍历,添加期权信息。写入静态哈希表myOptionList。 /// </summary> /// <param name="startDate">开始日期</param> /// <param name="endDate">结束日期</param> private void GetOptionInformationList(int startDate, int endDate) { //定义交易日期的类。 TradeDays myTradeDays = new TradeDays(startDate, endDate); //从数据库表中获取信息,如果已经是完整信息就不需要连接万德数据库。 if (GetOptionListFromTable(startDate, endDate) == true) { return; } //按日期遍历,添加期权信息。 WindAPI w = new WindAPI(); w.start(); foreach (int today in myTradeDays.myTradeDays) { if (TradeDays.IsOptionExerciseDate(today) || today == myTradeDays.myTradeDays[myTradeDays.myTradeDays.Count - 1]) { WindData optionToday = w.wset("OptionChain", "date=" + today.ToString() + ";us_code=" + Configuration.underlyingAsset + ";option_var=;month=全部;call_put=全部"); object[] optionList = optionToday.data as object[]; int num = optionList.Length / 13; for (int i = 0; i < num; i++) { optionFormat option = new optionFormat(); string codeString = (string)optionList[i * 13 + 4 - 1]; option.optionCode = Convert.ToInt32(codeString.Substring(0, 8)); option.market = codeString.Substring(9, 2); option.optionName = (string)optionList[i * 13 + 5 - 1]; option.executeType = (string)optionList[i * 13 + 6 - 1]; option.strike = (double)optionList[i * 13 + 7 - 1]; option.optionType = (string)optionList[i * 13 + 9 - 1]; option.startDate = TradeDays.DateTimeToInt((DateTime)optionList[i * 13 + 10 - 1]); option.endDate = TradeDays.DateTimeToInt((DateTime)optionList[i * 13 + 11 - 1]); if (myOptionList.ContainsKey(option.optionCode) == false) { myOptionList.Add(option.optionCode, option); } } } } w.stop(); }
/// <summary> /// 处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDeal_Click(object sender, EventArgs e) { //接口初始化 WindAPI w = new WindAPI(); //在接口操作前,用户首先要登录并启动C#插件,即使用w.start()命令。其中,返回值0表示登陆成功,负数表示登陆失败,可通过w.getErrorMsg()函数获取错误信息。 w.start(); //当需要判断是否连接c#接口时,可以使用w.isconnected()命令。返回值True表示处于连接状态,反之False表示连接断开。 if (w.isconnected()) { WindData wd = w.wss("000001.SZ,000002.SZ", "open,low,close,volume", "tradeDate=20180809;priceAdj=U;cycle=D"); //返回的数据转化为便于使用的数据结构, object[,] getData = (object[, ])wd.getDataByFunc("wss", false); MessageBox.Show("连接成功"); } //当需要停止使用C#接口时,可以使用w.stop()命令,由于程序退出时会自动执行w.stop()命令,用户一般并不需要执行。 w.stop(); }
public void DoIt() { WindAPI w = new WindAPI(); w.start(); //取昨天的最高、最低、收盘 double dLastHigh = 0; double dLastLow = 0; double dLastClose = 0; WindData wd = w.wss("600999.SH", "high,low,close", "tradeDate=20141202;priceAdj=U;cycle=D"); //WindData wd = w.wss("601000.SH", "high,low,close", "tradeDate=20141202;priceAdj=U;cycle=D"); //WindData wd = w.wss("M1309.DCE", "high,low,close", "tradeDate=20130411;priceAdj=U;cycle=D"); double[,] lastPriceData = (double[, ])wd.getDataByFunc("wss", true); if (null == lastPriceData) { return; } dLastHigh = lastPriceData[0, 0]; dLastLow = lastPriceData[0, 1]; dLastClose = lastPriceData[0, 2]; //计算出6个价位 double dSsetup = dLastHigh + 0.35 * (dLastClose - dLastLow); //观察卖出价 double dSenter = (1.07 / 2) * (dLastHigh + dLastLow) - 0.07 * dLastLow; //反转卖出价 double dBenter = (1.07 / 2) * (dLastHigh + dLastLow) - 0.07 * dLastHigh; //反转买入价 double dBsetup = dLastLow - 0.35 * (dLastHigh - dLastClose); //观察买入价 double dBbreak = dSsetup + 0.25 * (dSenter - dBsetup); //突破买入价 double dSbreak = dBsetup - 0.205 * (dSenter - dBsetup); //突破卖出价 //某天的交易数据 WindData wdWSI = w.wsi("600999.SH", "high,low,close", "2014-12-03 09:30:00", "2014-12-03 15:00:00", ""); //WindData wdWSI = w.wsi("601000.SH", "high,low,close", "2014-12-03 09:30:00", "2014-12-03 15:00:00", ""); //WindData wdWSI = w.wsi("M1309.DCE", "high,low,close", "2013-04-12 09:30:00", "2013-04-12 15:00:00", ""); double[,] curPriceData = (double[, ])wdWSI.getDataByFunc("wsi", true); if (null == curPriceData) { return; } int nTimeCount = curPriceData.GetLength(0); if (nTimeCount < 0) { return; } int nPriceDim = curPriceData.GetLength(1); if (nPriceDim < 3) { return; } //策略初值,1表示做多,-1表示做空,0表示无操作 List <int> curHoldList = new List <int>(nTimeCount); for (int i = 0; i < nTimeCount; i++) { curHoldList.Add(0); } double dCurHigh = curPriceData[0, 0]; double dCurLow = curPriceData[0, 1]; double dCurPrice = curPriceData[0, 2]; for (int i = 0; i < nTimeCount; i++) { dCurHigh = (curPriceData[i, 0] > dCurHigh) ? curPriceData[i, 0] : dCurHigh; dCurLow = (curPriceData[i, 1] > dCurLow) ? curPriceData[i, 1] : dCurLow; dCurPrice = curPriceData[i, 2]; //当前持仓 int nCurHold = curHoldList.Sum(); bool bCon1 = (dCurPrice > dBbreak) && (0 == nCurHold); //空仓做多条件 bool bCon2 = (dCurPrice < dSbreak) && (0 == nCurHold); //空仓做空条件 bool bCon3 = (nCurHold > 0) && (dCurHigh > dSsetup) && (dCurPrice < dSenter); //多单反转卖出条件:holding>0 and 今高>观察卖出价 and c<反转卖出价; bool bCon4 = (nCurHold < 0) && (dCurLow < dBsetup) && (dCurPrice > dBenter); //空单反转买入条件:=holding<0 and 今低<观察买入价 and c>反转买入价; //交易 if (bCon3) //多单反转 { curHoldList[i] = -1; //平多 //sell } else if (bCon2) //空单做空 { curHoldList[i] = -1; //sell } else if (bCon4) //空单反转 { curHoldList[i] = 1; //buy } else if (bCon1) //空仓做多 { curHoldList[i] = 1; //buy } else { curHoldList[i] = 0; } } int nCurHole = curHoldList.Sum(); //尾盘轧平 if (1 == nCurHole && 1 == curHoldList.Last()) { curHoldList[nTimeCount - 1] = 0; } else if (1 == nCurHole) { curHoldList[nTimeCount - 1] = -1; } else if (-1 == nCurHole && -1 == curHoldList.Last()) { curHoldList[nTimeCount - 1] = 0; } else if (-1 == nCurHole) { curHoldList[nTimeCount - 1] = 1; } //统计收益 double dProfit = 0; for (int i = 0; i < nTimeCount; i++) { dProfit += curPriceData[i, 2] * curHoldList[i]; } Console.WriteLine("策略收益:" + dProfit); w.stop(); }