以文本方式查看主题

-  金字塔客服中心 - 专业程序化交易软件提供商  (http://www.weistock.com/bbs/index.asp)
--  高级功能研发区  (http://www.weistock.com/bbs/list.asp?boardid=5)
----  公式调用VBA函数,参数返回问题  (http://www.weistock.com/bbs/dispbbs.asp?boardid=5&id=56346)

--  作者:jason_udu
--  发布时间:2013/9/6 12:15:28
--  公式调用VBA函数,参数返回问题


Function zbDate(Formula,ZB,XH)
    \'系统会在逐K线模式解释公式时的每个周期都会调用此函数一遍,因此设计时应该注重程序的执行效率,不要重复的执行一些没必要的代码

dim resultMa
redim resultMa(2)
\'msgbox ubound(resultMa)
resultMa(0)=-2
resultMa(1)=-2
zbDate=resultMa
\'sgbox zbdate(1)

\'if Formula.workmode=0 then   \'如果是逐渐K模式,直接退出函数
 \'  exit function
\'end if
    
zbName="MACD"
seriseName="MACD1"
for i=0 to technic.GridCount-1  \'循环获得指标所在的grid
  Set Grid = Technic.GetGridByIndex(i)
  for j=0 to Grid.formulaCount-1
    set formula1=grid.getformulaByIndex(j)
      if formula1.name=zbName then
         exit for  
      end if
  next
  if j<> Grid.formulaCount then exit for
next
if i=technic.GridCount then  \'未找到对应的指标
   exit function
 end if

set history=grid.gethistorydata()
zb1=formula1.vardata(seriseName)
dataCount=UBound(zb1)
redim resultMa(dataCount)

for i=formula1.dataSize-1 to 1 step -1
    if zb1(i)>0 and zb1(i-1)<=0 then
       a1=history.date(i)
       a2=year(a1)*10000000000+month(a1)*100000000+day(a1)*1000000+hour(a1)*10000+minute(a1)*100+second(a1)

 

\'这里为了返回柱线的日期时间,特把其转换为数值(公式只能返回数值?),在VBA下用宏运行是没有问题的,但在公式中自定义函数调用传递回来时在小时、分钟、秒上出错,比如2013年9月2日11点30分,传递回\'来的结果是20130901786624。请问高手们帮忙解决,急急……


       msgbox a2
       resultMa(i)=a2
       EXIT FOR
    else
       resultMa(i)=-1
    end if
next

zbDate=resultMa
End Function


--  作者:rushtaotao
--  发布时间:2013/9/6 13:22:17
--  
用cdbl试试
--  作者:jason_udu
--  发布时间:2013/9/6 13:49:44
--  

金字塔公式的数值最大是几位的?似乎数值超过10位之后,数值计算就会出错,如果是,这就是一个很大的bug,希望改进


--  作者:fly
--  发布时间:2013/9/6 16:56:24
--  

32位的操作系统.

2的31次方,值为21 4748 3648

 

您的日期值已经远远超过这个值了,所以出现了异常显示

分成两个部分传递, 年月日  +   时分秒

 

 

 


--  作者:jason_udu
--  发布时间:2013/9/7 12:21:16
--  

谢谢,但改成这样还是有错

Function zbDate(Formula,ZB,XH)
    \'系统会在逐K线模式解释公式时的每个周期都会调用此函数一遍,因此设计时应该注重程序的执行效率,不要重复的执行一些没必要的代码

dim resultMa
redim resultMa(2)
\'msgbox ubound(resultMa)
resultMa(0)=-2
resultMa(1)=-2
zbDate=resultMa
\'sgbox zbdate(1)

\'if Formula.workmode=0 then   \'如果是逐渐K模式,直接退出函数
 \'  exit function
\'end if
    
zbName="MACD"
seriseName="MACD1"
for i=0 to technic.GridCount-1  \'循环获得指标所在的grid
  Set Grid = Technic.GetGridByIndex(i)
  for j=0 to Grid.formulaCount-1
    set formula1=grid.getformulaByIndex(j)
      if formula1.name=zbName then
         exit for  
      end if
  next
  if j<> Grid.formulaCount then exit for
next
if i=technic.GridCount then  \'未找到对应的指标
   exit function
 end if
cycT=grid.cyctype
\'msgbox cycT
set history=grid.gethistorydata()
zb1=formula1.vardata(seriseName)
dataCount=UBound(zb1)
redim resultMa(dataCount)

for i=formula1.dataSize-1 to 1 step -1
    if zb1(i)>0 and zb1(i-1)<=0 then
       a1=history.date(i)
       if cycT<=4 or (cycT>=10 and cycT<=14) or (cycT>=17 and cycT<=18) then \'由于返回值超过10位后在公式调用中会出错,所以这里在日频度以下只返回时分秒,日频度以上返回年月日
           a2=cdbl(hour(a1))*10000+cdbl(minute(a1))*100+cdbl(second(a1))
       else
           a2=cdbl(year(a1))*10000+cdbl(month(a1))*100+cdbl(day(a1))
       end if
       \'msgbox a1
       resultMa(i)=a2
      \' EXIT FOR 

 

在日线先本来应该返回20130903的,但在公式中显示出来的是20130904?好多个日子都是这样,请问是什么原因?你们可以拷贝代码去测试,谢谢!


    else
       resultMa(i)=-1
    end if
next

zbDate=resultMa
End Function