以文本方式查看主题 - 金字塔客服中心 - 专业程序化交易软件提供商 (http://222.73.7.161/bbs/index.asp) -- 程序化交易实盘俱乐部 (http://222.73.7.161/bbs/list.asp?boardid=9) ---- [原创]根据当日涨跌判断进行交易 (http://222.73.7.161/bbs/dispbbs.asp?boardid=9&id=4687) |
-- 作者:wangwatercup -- 发布时间:2011/1/2 17:06:06 -- [原创]根据当日涨跌判断进行交易 (1)采用SVM、CART和PNN进行股指期货涨跌预测,预测当日涨跌。其中输入变量为沪深300收益率、振幅、市场热度、成交额、资金净流入、趋势、道琼斯指数收益率等。 (2)根据涨跌预测,进行当日交易,规则如下: (2.1)开盘即买入或卖出,同时2个机制下单,争取在开盘后以最快的速度成交; (2.2) 2% 止损; (2.3) 如果未成交,则撤单之后继续在开盘价的微小区间之内追单 (2.4) 设置滑点,默认0.2 (2.5) 3点10分不惜一切,平仓 (3)代码如下 dim dmHY,dmJYS,signal,buy_amount,IDaccount,RangeTick,RangeStoploss,Stoploss,NumberStoploss,Slide dim orderid dim ConSign dim Filled dim Remaining dim Action dim OrderType dim LmtPrice dim Account dim Kaiping dim BuyHoding dim BuyCost dim BuyTodayHoding dim SellHoding dim SellCost dim SellTodayHoding dim PNL dim UseMargin dim FirstTrade,StatusFirstTrade sub b0() \'设置参数 \'一定要在开盘之前运行本程序, signal=1 \'1 买 2 卖空 buy_amount=9 \'开仓数量 IDaccount="40009975" \'帐号 dmHY="if01" \'合约 dmJYS="zj" \'交易所 RangeTick=.005 \'报价与开盘价之间价差变动范围百分比 RangeStoploss=.02 \'止损百分比 NumberStoploss=0 Slide=.2 end sub Sub APPLICATION_VBAStart() FirstTrade=1 \'一定要在未建仓之前运行本程序; \'如果想在开盘时最先成交,一定在9点14分之前启动本程序 call b0 call Application.SetTimer(1,5000) \'1S触发一次 call application.SetTimer (2,50) call marketdata.RegReportNotify(dmHY,dmJYS) msgbox "启动" End Sub Sub APPLICATION_Close() call Application.KillTimer(1) call marketdata.UnRegReportNotify(dmHY,dmJYS) End Sub Sub APPLICATION_Timer(ID) select case ID case 1 call b3 case 2 call b1 end select End Sub Sub ORDER_OrderStatusEx(oOrderID,oStatus,oFilled,oRemaining,oPrice,oCode,oMarket,oOrderType,oAspect,oKaiping) StatusFirstTrade = oStatus if oAspect = 0 and oKaiping = 0 then oTrade = 1 \'开多 elseif oAspect = 1 and oKaiping = 1 then oTrade = -.5 \'平多 elseif oAspect = 1 and oKaiping = 0 then oTrade = -1 \'开空 elseif oAspect = 0 and oKaiping = 1 then oTrade = .5 \'平空 end if \'filled 是全部成交 \'tradeing 已成交部分 End Sub Sub MARKETDATA_ReportNotify(ReportData) \'争取在启动的时候下单 timePart=time if timePart<=timevalue("09:14:30") or (timePart>timevalue("11:30:10") and timePart<=timevalue("12:59:50")) then exit sub end if if FirstTrade=1 and (isempty(StatusFirstTrade) or StatusFirstTrade="Cancelled" or StatusFirstTrade="Inactive") then Set rpt = marketdata.GetReportData(dmHY,dmJYS)\'得到这个品种的行情报价 PriceOpen=rpt.Open select case signal case 1 Ssx_Price_Buy=rpt.SellPrice1+Slide if abs((Ssx_Price_Buy-PriceOpen)/PriceOpen)<=RangeTick then call Order.Buy(0,buy_amount,Ssx_Price_Buy,0,dmHY,dmJYS,"",0) end if case 2 Ssx_Price_Sell=rpt.BuyPrice1-Slide if abs((Ssx_Price_Sell-PriceOpen)/PriceOpen)<=RangeTick then call Order.BuyShort(0,buy_amount,Ssx_Price_Sell,0,dmHY,dmJYS,"",0) end if end select if StatusFirstTrade ="Filled" or StatusFirstTrade ="Tradeing" or StatusFirstTrade ="Submitted" or StatusFirstTrade ="PreSubmitted" then FirstTrade = 0 \'不会存在下了指令却无法成为委托的情况,因为如果能获得注册的品种发出的价格,则一定是在交易时间 call document.WritePrivateProfileFloat("operation", "timePartReportNotify", timePart, "C:\\out.ini") end if end if end sub sub b1() \'争取在开盘的时候下第一单 timePart=time if timePart<=timevalue("09:14:40") or timePart>=timevalue("9:15:10") then exit sub end if if FirstTrade=1 and (isempty(StatusFirstTrade) or StatusFirstTrade="Cancelled" or StatusFirstTrade="Inactive") then Set rpt = marketdata.GetReportData(dmHY,dmJYS)\'得到这个品种的行情报价 PriceOpen=rpt.open select case signal case 1 Ssx_Price_Buy=rpt.SellPrice1+Slide if abs((Ssx_Price_Buy-PriceOpen)/PriceOpen)<=RangeTick then call Order.Buy(0,buy_amount,Ssx_Price_Buy,0,dmHY,dmJYS,"",0) end if case 2 Ssx_Price_Sell=rpt.BuyPrice1-Slide if abs((Ssx_Price_Sell-PriceOpen)/PriceOpen)<=RangeTick then call Order.BuyShort(0,buy_amount,Ssx_Price_Sell,0,dmHY,dmJYS,"",0) end if end select if StatusFirstTrade ="Filled" or StatusFirstTrade ="Tradeing" or StatusFirstTrade ="Submitted" or StatusFirstTrade ="PreSubmitted" then FirstTrade = 0 \'不会存在下了指令却无法成为委托的情况,因为如果能获得注册的品种发出的价格,则一定是在交易时间 call document.WritePrivateProfileFloat("operation", "timePart", timePart, "C:\\out.ini") end if end if end sub sub b3() if FirstTrade<>0 then \'若下单未成功,则退出 exit sub end if timePart=time if (timePart>timevalue("09:14:30") and timePart<=timevalue("11:30:00")) or (timePart>=timevalue("13:00:00") and timePart<=timevalue("15:10:00")) then \'是否成交 select case signal case 1 call b31 case 2 call b32 end select elseif timePart>timevalue("15:10:00") then \'平仓 select case signal case 1 call b41 case 2 call b42 end select end if end sub [此贴子已经被作者于2011-1-2 17:30:01编辑过]
|
-- 作者:wangwatercup -- 发布时间:2011/1/2 17:07:18 -- 继续 Sub b31() Result= order.OrderInfoByCode2(dmHY,dmJYS,orderid,ConSign,Filled,Remaining,Action,OrderType,LmtPrice,Account,Kaiping)\' 取得未成交订单情况 Result = Order.HoldingInfoByCode2(dmHY,dmJYS,BuyHoding,BuyCost,BuyTodayHoding,SellHoding,SellCost,SellTodayHoding,PNL,UseMargin,IDaccount)\' 取得持仓情况 \'If Result <> 1 Then \' Exit Sub \'End If Set rpt = marketdata.GetReportData(dmHY,dmJYS)\'得到这个品种的行情报价 PriceOpen=rpt.open \'开盘价 PriceNew=rpt.SellPrice1+Slide \' Stoploss=(((PriceNew-PriceOpen)/PriceOpen)<-RangeStoploss)\'是否需要止损 if PriceOpen=0 or isnull(PriceOpen) then \'保证不会在集合竞价阶段下单 exit sub end if if isempty(BuyHoding) and isempty(SellHoding) and not isempty(ConSign) and not Stoploss then \'空仓 下单未成交 不用止损 order.OrderQueue = 0 call order.CancelOrder(orderid) \'撤单 if Action=0 and Kaiping=0 and abs((PriceNew-PriceOpen)/PriceOpen)<=RangeTick then call Order.Buy(0,Remaining,PriceNew,0,dmHY,dmJYS,"",0) end if elseif isempty(BuyHoding) and isempty(SellHoding) and not isempty(ConSign) and Stoploss then \'空仓 下单未成交 并且要止损 order.OrderQueue = 0 call order.CancelOrder(orderid) \'撤单 \' NumberStoploss=NumberStoploss+1 elseif isempty(BuyHoding) and isempty(SellHoding) and isempty(ConSign) and not Stoploss then \'空仓 下单全成交 不用止损 if NumberStoploss=0 and abs((PriceNew-PriceOpen)/PriceOpen)<=RangeTick then \'若此前未止损且价格在合理范围之内,则买进 call Order.Buy(0,buy_amount,PriceNew,0,dmHY,dmJYS,"",0) end if elseif isempty(BuyHoding) and isempty(SellHoding) and isempty(ConSign) and Stoploss then \'空仓 下单全成交 并且要止损 elseif not isempty(BuyHoding) and not isempty(ConSign) and not Stoploss then \'持仓 下单未成交 不用止损 if Action=0 and Kaiping=0 then order.OrderQueue = 0 call order.CancelOrder(orderid) \'撤单 if abs((PriceNew-PriceOpen)/PriceOpen)<=RangeTick then call Order.Buy(0,Remaining,PriceNew,0,dmHY,dmJYS,"",0) end if elseif Action=1 and Kaiping=1 then order.OrderQueue = 0 call order.CancelOrder(orderid) \'撤单 PriceNew=rpt.BuyPrice1-Slide call Order.Sell(0,BuyHoding,PriceNew,0,dmHY,dmJYS,"",0) end if elseif not isempty(BuyHoding) and not isempty(ConSign) and Stoploss then \'持仓 下单未成交 不用止损 if Action=0 and Kaiping=0 then order.OrderQueue = 0 call order.CancelOrder(orderid) \'撤单 PriceNew=rpt.BuyPrice1-Slide call Order.Sell(0,BuyHoding,PriceNew,0,dmHY,dmJYS,"",0) if StatusFirstTrade ="Filled" then NumberStoploss=NumberStoploss+1 end if elseif Action=1 and Kaiping=1 then order.OrderQueue = 0 call order.CancelOrder(orderid) \'撤单 PriceNew=rpt.BuyPrice1-Slide call Order.Sell(0,BuyHoding,PriceNew,0,dmHY,dmJYS,"",0) if StatusFirstTrade ="Filled" then NumberStoploss=NumberStoploss+1 end if end if elseif not isempty(BuyHoding) and isempty(ConSign) and not Stoploss then \'持仓 无未成交 止损 if NumberStoploss=0 and abs((PriceNew-PriceOpen)/PriceOpen)<=RangeTick and buy_amount>BuyHoding then \'若此前未止损且价格在合理范围之内,则买进 call Order.Buy(0,buy_amount-BuyHoding,PriceNew,0,dmHY,dmJYS,"",0) end if elseif not isempty(BuyHoding) and isempty(ConSign) and Stoploss then PriceNew=rpt.BuyPrice1-Slide call Order.Sell(0,BuyHoding,PriceNew,0,dmHY,dmJYS,"",0) if StatusFirstTrade ="Filled" then NumberStoploss=NumberStoploss+1 end if end if end sub sub b32() Result= order.OrderInfoByCode2(dmHY,dmJYS,orderid,ConSign,Filled,Remaining,Action,OrderType,LmtPrice,Account,Kaiping)\' 取得未成交订单情况 Result = Order.HoldingInfoByCode2(dmHY,dmJYS,BuyHoding,BuyCost,BuyTodayHoding,SellHoding,SellCost,SellTodayHoding,PNL,UseMargin,IDaccount)\' 取得持仓情况 \'If Result <> 1 Then \' Exit Sub \'End If Set rpt = marketdata.GetReportData(dmHY,dmJYS)\'得到这个品种的行情报价 PriceOpen=rpt.open \'开盘价 PriceNew=rpt.BuyPrice1-Slide \' Stoploss=(((PriceNew-PriceOpen)/PriceOpen)>RangeStoploss)\'是否需要止损 if PriceOpen=0 or isnull(PriceOpen) then \'保证不会在集合竞价阶段下单 exit sub end if if isempty(BuyHoding) and isempty(SellHoding) and not isempty(ConSign) and not Stoploss then \'空仓 下单未成交 不用止损 order.OrderQueue = 0 call order.CancelOrder(orderid) \'撤单 if Action=1 and Kaiping=0 and abs((PriceNew-PriceOpen)/PriceOpen)<=RangeTick then call Order.BuyShort(0,Remaining,PriceNew,0,dmHY,dmJYS,"",0) end if elseif isempty(BuyHoding) and isempty(SellHoding) and not isempty(ConSign) and Stoploss then \'空仓 下单未成交 并且要止损 order.OrderQueue = 1 call order.CancelOrder(orderid) \'撤单 \' NumberStoploss=NumberStoploss+1 elseif isempty(BuyHoding) and isempty(SellHoding) and isempty(ConSign) and not Stoploss then \'空仓 下单全成交 不用止损 if NumberStoploss=0 and abs((PriceNew-PriceOpen)/PriceOpen)<=RangeTick then \'若此前未止损且价格在合理范围之内,则买进 call Order.BuyShort(0,buy_amount,PriceNew,0,dmHY,dmJYS,"",0) end if elseif isempty(BuyHoding) and isempty(SellHoding) and isempty(ConSign) and Stoploss then \'空仓 下单全成交 并且要止损 elseif not isempty(SellHoding) and not isempty(ConSign) and not Stoploss then \'持仓 下单未成交 不用止损 order.OrderQueue = 0 call order.CancelOrder(orderid) \'撤单 if Action=1 and Kaiping=0 and abs((PriceNew-PriceOpen)/PriceOpen)<=RangeTick then call Order.BuyShort(0,Remaining,PriceNew,0,dmHY,dmJYS,"",0) elseif Action=0 and Kaiping=1 then PriceNew = rpt.SellPrice1+Slide call Order.SellShort(0,SellHoding,PriceNew,0,dmHY,dmJYS,"",0) end if elseif not isempty(SellHoding) and not isempty(ConSign) and Stoploss then \'持仓 下单未成交 止损 order.OrderQueue = 0 call order.CancelOrder(orderid) \'撤单 call Order.SellShort(0,SellHoding,PriceNew,0,dmHY,dmJYS,"",0) if StatusFirstTrade ="Filled" then NumberStoploss=NumberStoploss+1 end if elseif not isempty(SellHoding) and isempty(ConSign) and not Stoploss then if buy_amount>SellHoding and abs((PriceNew-PriceOpen)/PriceOpen)<=RangeTick and NumberStoploss=0 then call Order.BuyShort(0,buy_amount,PriceNew,0,dmHY,dmJYS,"",0) end if elseif not isempty(SellHoding) and isempty(ConSign) and Stoploss then PriceNew=rpt.SellPrice1+Slide call Order.SellShort(0,SellHoding,PriceNew,0,dmHY,dmJYS,"",0) if StatusFirstTrade ="Filled" then NumberStoploss=NumberStoploss+1 end if end if end sub |
-- 作者:wangwatercup -- 发布时间:2011/1/2 17:07:43 -- 继续 sub b41() Result= order.OrderInfoByCode2(dmHY,dmJYS,orderid,ConSign,Filled,Remaining,Action,OrderType,LmtPrice,Account,Kaiping)\' 取得未成交订单情况 Result = Order.HoldingInfoByCode2(dmHY,dmJYS,BuyHoding,BuyCost,BuyTodayHoding,SellHoding,SellCost,SellTodayHoding,PNL,UseMargin,IDaccount)\' 取得持仓情况 Set rpt = marketdata.GetReportData(dmHY,dmJYS)\'得到这个品种的行情报价 if isempty(BuyHoding) and isempty(ConSign) then elseif isempty(BuyHoding) and not isempty(ConSign) then order.OrderQueue = 0 call order.CancelOrder(orderid) elseif not isempty(BuyHoding) and isempty(ConSign) then PriceNew=rpt.BuyPrice1-Slide call Order.Sell(0,BuyHoding,PriceNew,0,dmHY,dmJYS,"",0) elseif not isempty(BuyHoding) and not isempty(ConSign) then order.OrderQueue = 0 call order.CancelOrder(orderid) PriceNew=rpt.BuyPrice1-Slide call Order.Sell(0,BuyHoding,PriceNew,0,dmHY,dmJYS,"",0) end if end sub sub b42() Result= order.OrderInfoByCode2(dmHY,dmJYS,orderid,ConSign,Filled,Remaining,Action,OrderType,LmtPrice,Account,Kaiping)\' 取得未成交订单情况 Result = Order.HoldingInfoByCode2(dmHY,dmJYS,BuyHoding,BuyCost,BuyTodayHoding,SellHoding,SellCost,SellTodayHoding,PNL,UseMargin,IDaccount)\' 取得持仓情况 Set rpt = marketdata.GetReportData(dmHY,dmJYS)\'得到这个品种的行情报价 if isempty(SellHoding) and isempty(ConSign) then elseif isempty(SellHoding) and not isempty(ConSign) then order.OrderQueue = 0 call order.CancelOrder(orderid) elseif not isempty(SellHoding) and isempty(ConSign) then PriceNew=rpt.SellPrice1+Slide call Order.SellShort(0,SellHoding,PriceNew,0,dmHY,dmJYS,"",0) elseif not isempty(SellHoding) and not isempty(ConSign) then order.OrderQueue = 0 call order.CancelOrder(orderid) PriceNew=rpt.SellPrice1+Slide call Order.SellShort(0,SellHoding,PriceNew,0,dmHY,dmJYS,"",0) end if end sub |
-- 作者:wangwatercup -- 发布时间:2011/1/2 17:14:39 -- 上面的代码主要是如何交易的,关于神经网络的内容在matlab上面计算的,没有贴出。 所以内容都来自于这个论坛个位好心人的帮助,在此次一并感谢。 程序还是有些冗长,可以进一步改进的,但是关于如何判断和追单部分画了一些心思去份各种可能的情况讨论。 |
-- 作者:bdggl -- 发布时间:2011/1/2 23:39:58 -- 不错 支持一下 非常不错的尝试 希望后面的跟上 大家多多交流一下 |
-- 作者:天堂之上 -- 发布时间:2011/1/4 16:54:51 -- 谢谢,但怎么提示有问题呢 |
-- 作者:stocker -- 发布时间:2011/1/5 22:42:27 -- 非常好啊 非常好的实例。
具体怎么用啊?是利用宏还是窗体?请讲的具体一点。
谢谢! |
-- 作者:wangwatercup -- 发布时间:2011/1/6 12:43:33 -- 有些地方还需要修改,比如将我的帐号设为你自己的帐号。 在宏里面实施 |
-- 作者:bbking -- 发布时间:2011/1/11 21:23:56 -- 以下是引用wangwatercup在2011-1-2 17:14:39的发言:
上面的代码主要是如何交易的,关于神经网络的内容在matlab上面计算的,没有贴出。 所以内容都来自于这个论坛个位好心人的帮助,在此次一并感谢。 程序还是有些冗长,可以进一步改进的,但是关于如何判断和追单部分画了一些心思去份各种可能的情况讨论。
我晕~还以为你真是用这个来自动交易呢... 我说如何用VBA挂上matlab一起自动运算进行交易呢~ 看来是个静态的东西... |
-- 作者:z7c9 -- 发布时间:2011/1/27 11:03:05 -- Matlab+VBS应该做不了实时交易。 |