以文本方式查看主题

-  金字塔客服中心 - 专业程序化交易软件提供商  (http://weistock.com/bbs/index.asp)
--  高级功能研发区  (http://weistock.com/bbs/list.asp?boardid=5)
----  [原创]自动开关金字塔软件、自动开启交易等技巧  (http://weistock.com/bbs/dispbbs.asp?boardid=5&id=57898)

--  作者:klc
--  发布时间:2013/10/23 13:31:45
--  [原创]自动开关金字塔软件、自动开启交易等技巧

整个流程为:

  8:40  自动开启金字塔软件,软件登录马上加载交易图表(Frame1)

  30秒后自动启动图表交易(整个过程需要一两分钟)

  17:30 自动执行收盘作业(请先设置好收盘作业的市场,和需要保存的K线周期)

  19:00 自动关闭金字塔软件

  20:40 自动开启金字塔软件,软件登录马上加载夜盘交易图表(Frame2)

  30秒后自动启动图表交易(整个过程需要一两分钟)

  06:00 自动关闭金字塔软件

 

软件分为两部分,VC++部分用途为每天两次自动开启金字塔软件并自动点击“登录”按钮。程序在附件中,只有两个文件(testTraderApi.exethostmduserapi.dll),该程序自动连接某期货公司的ctp行情服务器,取得交易日日期,从而自动判断当天是否交易,不交易不开启金字塔软件。请将testTraderApi.exe设置为随Windows自动启动即可(放个快捷方式到“开始菜单》程序》启动”)。软件下载:

 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:自动开启金字塔.zip

为防止网友怀疑我在exe中加入盗号、盗策略等木马程序,特附上该exe文件的源代码供下载,大家也可以下载后自行编译,并且还可以修改开启金字塔的时间,源代码下载:
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:金字塔开机软件.zip


--  作者:klc
--  发布时间:2013/10/23 13:32:58
--  

第二部分为金字塔VBA代码,可复制并放入金字塔的任何一个VBA模块中:

 

dim StartTime
dim StartTrade
dim HasDoClose

Sub APPLICATION_Start()
 \'\'\'\'\'\'\'自动启动相应的框架(分日盘、夜盘调出相应图表,请自行叠加相应策略公式)
 if cdate(time)>cdate("06:00:00") and cdate(time)<cdate("18:00:00") then
 \'\'\'\'\'\'\'日盘交易,打开框架名“Frame1”
  application.ActivateFrame "Frame1"
 else
 \'\'\'\'\'\'\'夜盘交易,打开框架名“Frame2”
  application.ActivateFrame "Frame2"
 end if
 call application.Settimer(100,30000) \'30秒时钟循环
 StartTime = cdate(time)
 StartTrade = 0
 HasDoClose = 0
End Sub

Sub APPLICATION_Timer(ID)
 if ID=100 then
  if StartTrade=0 then
   \'\'\'\'\'\'\'\'\'\'\'\'\'\'启动图表交易
   Set Wrap = CreateObject("DynamicWrapper")
   Wrap.Register "user32.dll","FindWindowA","i=ss","f=s", "r=l"
   Wrap.Register "user32.dll","FindWindowExA","i=llss","f=s", "r=l"
   Wrap.Register "user32.dll","SendMessageA","i=lull","f=s", "r=l"
   WM_LBUTTONDOWN=513
   WM_LBUTTONUP=514
   h = Wrap.FindWindowA("#32770","图表程式化交易 - 日盘框架 - 交易中") \'框架名称请自行定义,下同
   if h=0 then h = Wrap.FindWindowA("#32770","图表程式化交易 - 夜盘框架 - 交易中")
   if h<>0 then
    Wrap.PostMessageA h,WM_CLOSE,0,0
    StartTrade=1
   else
    h = Wrap.FindWindowA("#32770","金字塔")
    if h<>0 then
     h2 = Wrap.FindWindowExA(h,0,"Button","是(&Y)")
     if h2<>0 then
      Wrap.SendMessageA h2,WM_LBUTTONDOWN,0,0
      Wrap.SendMessageA h2,WM_LBUTTONUP,0,0
     end if
    else
     h = Wrap.FindWindowA("#32770","图表程式化交易 - 日盘框架 - 已停止")
     if h=0 then h = Wrap.FindWindowA("#32770","图表程式化交易 - 夜盘框架 - 已停止")
     if h<>0 then
      h2 = Wrap.FindWindowExA(h,0,"Button","启动交易")
      if h2<>0 then
       Wrap.SendMessageA h2,WM_LBUTTONDOWN,0,0
       Wrap.SendMessageA h2,WM_LBUTTONUP,0,0
      end if
     else
      application.PostMessage(33873)
     end if
    end if
   end if
  elseif HasDoClose=0 and cdate(time)>cdate("17:30:00") and cdate(time)<cdate("18:00:00") then
   \'\'\'\'\'\'\'\'17:30~18:00启动收盘
   HasDoClose=1
   call application.Settimer(5,2000)
   application.PostMessage(454)\'收盘
  elseif (StartTime<cdate("16:00:00") and cdate(time)>cdate("19:00:00") and cdate(time)<cdate("19:30:00")) or ((StartTime<cdate("2:30:00") or StartTime>cdate("6:30:00")) and cdate(time)>cdate("06:00:00") and cdate(time)<cdate("06:30:00")) then
   \'\'\'\'\'\'\'\'\'\'\'\'\'关闭金字塔程序(早上2:30前或前一日6:30后开启的凌晨6点关闭,下午16点前开启的19点关闭)
   Set Wrap = CreateObject("DynamicWrapper")
   Wrap.Register "user32.dll","FindWindowA","i=ss","f=s", "r=l"
   Wrap.Register "user32.dll","FindWindowExA","i=llss","f=s", "r=l"
   Wrap.Register "user32.dll","SendMessageA","i=lull","f=s", "r=l"
   WM_LBUTTONDOWN=513
   WM_LBUTTONUP=514
   h = Wrap.FindWindowA("#32770","金字塔")
   if h<>0 then
    h2 = Wrap.FindWindowExA(h,0,"Button","是(&Y)")
    if h2<>0 then
     Wrap.SendMessageA h2,WM_LBUTTONDOWN,0,0
     Wrap.SendMessageA h2,WM_LBUTTONUP,0,0
    end if
   else
    application.PostMessage(57665)
   end if
  end if
 elseif ID=5 then
  \'\'\'\'\'\'\'\'\'\'\'开始收盘
  call application.killtimer(5)
  Set Wrap = CreateObject("DynamicWrapper")
  Wrap.Register "user32.dll","FindWindowA","i=ss","f=s", "r=l"
  Wrap.Register "user32.dll","FindWindowExA","i=llss","f=s", "r=l"
  Wrap.Register "user32.dll","PostMessageA","i=lull","f=s", "r=l"
  WM_CLOSE=16
      call application.Settimer(7,600000)\'10分钟后关闭收盘窗口
  h = Wrap.FindWindowA("#32770","数据管理器")
  tab=Wrap.FindWindowExA(h,0,"SysTabControl32","Tab1")
  BM_CLICK=245
  h1=Wrap.FindWindowExA(tab,0,"#32770","")
  do while h1<>0
   bt=Wrap.FindWindowExA(h1,0,"Button","执行收盘(&X)")
   if bt<>0 then
    Wrap.PostMessageA bt,BM_CLICK,0,0
    h1=0
   else
    h1=Wrap.FindWindowExA(tab,h1,"#32770","")
   end if
  loop
  call application.Settimer(6,2000)
 elseif ID=6 then
      call application.killtimer(6)
      BM_CLICK=245
  Set Wrap = CreateObject("DynamicWrapper")
  Wrap.Register "user32.dll","FindWindowA","i=ss","f=s", "r=l"
  Wrap.Register "user32.dll","FindWindowExA","i=llss","f=s", "r=l"
  Wrap.Register "user32.dll","PostMessageA","i=lull","f=s", "r=l"
  h = Wrap.FindWindowA("#32770","金字塔")
  bt=Wrap.FindWindowExA(h,0,"Button","是(&Y)")
  Wrap.PostMessageA bt,BM_CLICK,0,0
 elseif ID=7 then
  \'\'\'\'\'\'\'结束收盘
      Set Wrap = CreateObject("DynamicWrapper")
  Wrap.Register "user32.dll","FindWindowA","i=ss","f=s", "r=l"
  Wrap.Register "user32.dll","FindWindowExA","i=llss","f=s", "r=l"
  Wrap.Register "user32.dll","PostMessageA","i=lull","f=s", "r=l"
  WM_CLOSE=16
  BM_CLICK=245
  h = Wrap.FindWindowA("#32770","金字塔")
  bt = 0
  bt = Wrap.FindWindowExA(h,0,"Button","确定")
  st = 0
  st = Wrap.FindWindowExA(h,0,"Static","收盘作业结束。")
  if bt<>0 and st<>0 then
   Wrap.PostMessageA h,WM_CLOSE,0,0
   h = Wrap.FindWindowA("#32770","数据管理器")
   Wrap.PostMessageA h,WM_CLOSE,0,0
   call application.killtimer(7)
   \'SendMail "收盘作业完成","收盘作业完成" \'\'\'\'发信息通知收盘完成,是否需要个人决定
  end if
 end if
end sub


--  作者:chnxgd
--  发布时间:2013/10/23 13:34:44
--  
感谢klc无私提供的帮助
--  作者:klc
--  发布时间:2013/10/23 13:43:14
--  

VBA部分,以往有不少网友不知道怎么用,上面仅用到两个VBA事件:Sub APPLICATION_StartSub APPLICATION_Timer(ID),都是系统内置的,用金字塔菜单》工具》宏》V...B...工程就可以打开看到几个模块,任选一个模块,把上面代码添加即可。

 

需要注意的是两点:

1、必须先停止VBA才能编辑模块中的代码

2、APPLICATION_Start和APPLICATION_Timer,在整个金字塔中都必须各只有一个,如果原来已经有了,不要直接覆盖,而是修改原来的代码,加到Sub内部。

 

另外,我原来写过提高图表交易程序效率的帖子,也写过自动检查金字塔运行状态的帖子,都可以和本帖子的内容结合起来,结合的时候,注意同名函数的问题就行。同时,我原来还写过盘后自动进行数据补充,可以结合起来,我自己的做法是,盘后先自动下载一次当天的分笔数据,下载完成后,进行一次当天数据K线检查,检查如果没有发现问题,才启动收盘,如果有问题,不收盘,短信通知。这个需要大家通过我几个帖子结合起来,自行修改了。

 

 

 

 

最后补充一个问题:exe可改名,dll不可改名

[此贴子已经被作者于2013/10/23 13:45:12编辑过]

--  作者:绿草地77
--  发布时间:2013/10/23 13:57:47
--  
感谢klc无私提供的帮助
--  作者:klc
--  发布时间:2013/10/23 14:45:56
--  

需要修改部分摘录如下:

\'\'\'\'\'\'\'日盘交易,打开框架名“Frame1”
  application.ActivateFrame "Frame1"
 else
 \'\'\'\'\'\'\'夜盘交易,打开框架名“Frame2”
  application.ActivateFrame "Frame2"


h = Wrap.FindWindowA("#32770","图表程式化交易 - 日盘框架 - 交易中") \'框架名称请自行定义,下同
   if h=0 then h = Wrap.FindWindowA("#32770","图表程式化交易 - 夜盘框架 - 交易中")

h = Wrap.FindWindowA("#32770","图表程式化交易 - 日盘框架 - 已停止")
     if h=0 then h = Wrap.FindWindowA("#32770","图表程式化交易 - 夜盘框架 - 已停止")

上面需要修改框架命名,一共6处


--  作者:guotx2010
--  发布时间:2013/10/23 15:58:50
--  
太好了,收下了!
--  作者:不思量
--  发布时间:2013/10/23 18:00:02
--  
这个是一定要顶的。
--  作者:chnxgd
--  发布时间:2013/10/23 19:42:10
--  
klc好,出现2个错误:
1、testTraderApi.exe多次运行后(约5次),双击打开,就是一个黑色的窗口,里面什么内容都没有,不知什么原因。
2、vba运行,加载图表程式化--开启程式化;过了一会儿,跳出vba调试窗口,同时跳出一个weisoft  Visual Basic的对话框,里面提示:
编译错误‘438’  对象不支持此属性或方法:\'Wrap.PostMessageA\'
请问怎样解决?

--  作者:klc
--  发布时间:2013/10/24 16:00:17
--  

第二部分为金字塔VBA代码,可复制并放入金字塔的任何一个VBA模块中:

 

dim StartTime
dim StartTrade
dim HasDoClose

Sub APPLICATION_Start()
 \'\'\'\'\'\'\'自动启动相应的框架(分日盘、夜盘调出相应图表,请自行叠加相应策略公式)
 if cdate(time)>cdate("06:00:00") and cdate(time)<cdate("18:00:00") then
 \'\'\'\'\'\'\'日盘交易,打开框架名“Frame1”
  application.ActivateFrame "Frame1"
 else
 \'\'\'\'\'\'\'夜盘交易,打开框架名“Frame2”
  application.ActivateFrame "Frame2"

 end if
 call application.Settimer(100,30000) \'30秒时钟循环
 StartTime = cdate(time)
 StartTrade = 0
 HasDoClose = 0
End Sub

Sub APPLICATION_Timer(ID)
 if ID=100 then
  if StartTrade=0 then
   \'\'\'\'\'\'\'\'\'\'\'\'\'\'启动图表交易
   Set Wrap = CreateObject("DynamicWrapper")
   Wrap.Register "user32.dll","FindWindowA","i=ss","f=s", "r=l"
   Wrap.Register "user32.dll","FindWindowExA","i=llss","f=s", "r=l"
   Wrap.Register "user32.dll","SendMessageA","i=lull","f=s", "r=l"

   Wrap.Register "user32.dll","PostMessageA","i=lull","f=s", "r=l"
   WM_LBUTTONDOWN=513
   WM_LBUTTONUP=514
   h = Wrap.FindWindowA("#32770","图表程式化交易 - 日盘框架 - 交易中") \'框架名称请自行定义,下同
   if h=0 then h = Wrap.FindWindowA("#32770","图表程式化交易 - 夜盘框架 - 交易中")
   if h<>0 then
    Wrap.PostMessageA h,WM_CLOSE,0,0
    StartTrade=1
   else
    h = Wrap.FindWindowA("#32770","金字塔")
    if h<>0 then
     h2 = Wrap.FindWindowExA(h,0,"Button","是(&Y)")
     if h2<>0 then
      Wrap.SendMessageA h2,WM_LBUTTONDOWN,0,0
      Wrap.SendMessageA h2,WM_LBUTTONUP,0,0
     end if
    else
     h = Wrap.FindWindowA("#32770","图表程式化交易 - 日盘框架 - 已停止")
     if h=0 then h = Wrap.FindWindowA("#32770","图表程式化交易 - 夜盘框架 - 已停止")
     if h<>0 then
      h2 = Wrap.FindWindowExA(h,0,"Button","启动交易")
      if h2<>0 then
       Wrap.SendMessageA h2,WM_LBUTTONDOWN,0,0
       Wrap.SendMessageA h2,WM_LBUTTONUP,0,0
      end if
     else
      application.PostMessage(33873)
     end if
    end if
   end if
  elseif HasDoClose=0 and cdate(time)>cdate("17:30:00") and cdate(time)<cdate("18:00:00") then
   \'\'\'\'\'\'\'\'17:30~18:00启动收盘
   HasDoClose=1
   call application.Settimer(5,2000)
   application.PostMessage(454)\'收盘
  elseif (StartTime<cdate("16:00:00") and cdate(time)>cdate("19:00:00") and cdate(time)<cdate("19:30:00")) or ((StartTime<cdate("2:30:00") or StartTime>cdate("6:30:00")) and cdate(time)>cdate("06:00:00") and cdate(time)<cdate("06:30:00")) then
   \'\'\'\'\'\'\'\'\'\'\'\'\'关闭金字塔程序(早上2:30前或前一日6:30后开启的凌晨6点关闭,下午16点前开启的19点关闭)
   Set Wrap = CreateObject("DynamicWrapper")
   Wrap.Register "user32.dll","FindWindowA","i=ss","f=s", "r=l"
   Wrap.Register "user32.dll","FindWindowExA","i=llss","f=s", "r=l"
   Wrap.Register "user32.dll","SendMessageA","i=lull","f=s", "r=l"
   WM_LBUTTONDOWN=513
   WM_LBUTTONUP=514
   h = Wrap.FindWindowA("#32770","金字塔")
   if h<>0 then
    h2 = Wrap.FindWindowExA(h,0,"Button","是(&Y)")
    if h2<>0 then
     Wrap.SendMessageA h2,WM_LBUTTONDOWN,0,0
     Wrap.SendMessageA h2,WM_LBUTTONUP,0,0
    end if
   else
    application.PostMessage(57665)
   end if
  end if
 elseif ID=5 then
  \'\'\'\'\'\'\'\'\'\'\'开始收盘
  call application.killtimer(5)
  Set Wrap = CreateObject("DynamicWrapper")
  Wrap.Register "user32.dll","FindWindowA","i=ss","f=s", "r=l"
  Wrap.Register "user32.dll","FindWindowExA","i=llss","f=s", "r=l"
  Wrap.Register "user32.dll","PostMessageA","i=lull","f=s", "r=l"
  WM_CLOSE=16
      call application.Settimer(7,600000)\'10分钟后关闭收盘窗口
  h = Wrap.FindWindowA("#32770","数据管理器")
  tab=Wrap.FindWindowExA(h,0,"SysTabControl32","Tab1")
  BM_CLICK=245
  h1=Wrap.FindWindowExA(tab,0,"#32770","")
  do while h1<>0
   bt=Wrap.FindWindowExA(h1,0,"Button","执行收盘(&X)")
   if bt<>0 then
    Wrap.PostMessageA bt,BM_CLICK,0,0
    h1=0
   else
    h1=Wrap.FindWindowExA(tab,h1,"#32770","")
   end if
  loop
  call application.Settimer(6,2000)
 elseif ID=6 then
      call application.killtimer(6)
      BM_CLICK=245
  Set Wrap = CreateObject("DynamicWrapper")
  Wrap.Register "user32.dll","FindWindowA","i=ss","f=s", "r=l"
  Wrap.Register "user32.dll","FindWindowExA","i=llss","f=s", "r=l"
  Wrap.Register "user32.dll","PostMessageA","i=lull","f=s", "r=l"
  h = Wrap.FindWindowA("#32770","金字塔")
  bt=Wrap.FindWindowExA(h,0,"Button","是(&Y)")
  Wrap.PostMessageA bt,BM_CLICK,0,0
 elseif ID=7 then
  \'\'\'\'\'\'\'结束收盘
      Set Wrap = CreateObject("DynamicWrapper")
  Wrap.Register "user32.dll","FindWindowA","i=ss","f=s", "r=l"
  Wrap.Register "user32.dll","FindWindowExA","i=llss","f=s", "r=l"
  Wrap.Register "user32.dll","PostMessageA","i=lull","f=s", "r=l"
  WM_CLOSE=16
  BM_CLICK=245
  h = Wrap.FindWindowA("#32770","金字塔")
  bt = 0
  bt = Wrap.FindWindowExA(h,0,"Button","确定")
  st = 0
  st = Wrap.FindWindowExA(h,0,"Static","收盘作业结束。")
  if bt<>0 and st<>0 then
   Wrap.PostMessageA h,WM_CLOSE,0,0
   h = Wrap.FindWindowA("#32770","数据管理器")
   Wrap.PostMessageA h,WM_CLOSE,0,0
   call application.killtimer(7)
   \'SendMail "收盘作业完成","收盘作业完成" \'\'\'\'发信息通知收盘完成,是否需要个人决定
  end if
 end if
end sub

 

注:第一个红色的代码段,是打开夜盘的图表(这里假设日盘和夜盘的图表是不一样的),如果不交易夜盘可以去掉

     其他红色字体的东西是你们可能需要修改的,Frame1、日盘图表。。。。这些,需要根据你实际的框架名称来设定

     绿色字体部分,是原来代码中漏掉的一句,必须加上,否则会vba出错

     橙色字体的是自动收盘的,如果不需要就删除。