FAQ > 金融建模 > 应用案例 > 图形实现

Q:天软宝塔线的高中低值的实现    

  • A:本文中主要还原天软宝塔线图形实现过程中相关指标计算的过程。
    首先在天软中,我们可以画一段时间的宝塔线如下图所示:

      begt:=20210301T;
      endt:=20210506T;
      SetSysParam(pn_stock(),'SZ000002');
      SetSysParam(pn_date(),endt);
      setsysparam(pn_rate(),1);
      setsysparam(PN_RateDay(),endt);
      N:=TradeDays(begt,endt);
      r:=nday(N,gftime(),datetimetostr(sp_time()),
          gfclose(),close(),
          gfopen(),open(),
          gfhigh(),high(),
          gflow(),low());
     //画宝塔线
      return graph(gttower(),stockname(DefaultStockID()),r);

    返回图形:


    在实际应用中,如何得到这个图中的高值、中值与低值呢?
    可通过下面的算法获取,封装函数gettower(r:array):Array
    其中参数r即为画图前的数组,时间序列的高开低收
    在原数组r中新增计算出来的'thigh','tmid','tlow','color'四列分别代表每个时间点的高点、中点、低点与宝塔线框颜色
    函数具体实现:

    Function gettower(r);
    begin
     for i:=0 to length(r)-1 do
     begin
       if i=0 then
       begin
        r[i,'tlow']:=r[i,gfclose()];
        r[i,'thigh']:=r[i,gfclose()];
        r[i,'tmid']:=r[i,gfclose()];
        r[i,gfcolor()]:='无色';
       end
       else begin
         if r[i-1,gfcolor()]='无色' then
         begin
          if r[i,gfclose()]=r[i-1,gfclose()] then
          begin
            r[i,'tlow']:=r[i-1,gfclose()];
            r[i,'thigh']:=r[i,gfclose()];
            r[i,'tmid']:=r[i-1,gfclose()];
            r[i,gfcolor()]:='无色';
          end
          else if r[i,gfclose()]>r[i-1,gfclose()] then
          begin
            r[i,'tlow']:=r[i-1,gfclose()];
            r[i,'thigh']:=r[i,gfclose()];
            r[i,'tmid']:=r[i-1,gfclose()];
            r[i,gfcolor()]:='红色';
          end
          else begin
            r[i,'tlow']:=r[i,gfclose()];
            r[i,'thigh']:=r[i-1,gfclose()];
            r[i,'tmid']:=r[i-1,gfclose()];
            r[i,gfcolor()]:='绿色';
          end
         end
         else if r[i-1,gfcolor()]='红色' or r[i-1,gfcolor()]='绿翻红' then //昨为红
         begin
          star:=r[i-1,'thigh'];//昨上端为今开端 ->上升红框
          if r[i,gfclose()]>=star then
          begin
            if r[i,gfclose()]=star then r[i,gfcolor()]:='无色';
            else r[i,gfcolor()]:='红色';
            r[i,'tlow']:=star;
            r[i,'thigh']:=r[i,gfclose()];
            r[i,'tmid']:=r[i,'tlow']; //r[i-1,'tlow'];
          end
          else if r[i,gfclose()]<star and r[i,gfclose()]>=r[i-1,'tlow'] then
          begin        //与昨上端平齐->下垂红框
            r[i,gfcolor()]:='红色';
            r[i,'tlow']:=r[i,gfclose()];
            r[i,'thigh']:=star;
            r[i,'tmid']:=r[i,'tlow'];//r[i-1,'tlow'];
          end
          else begin //红翻绿->上端平齐的下垂红翻绿
            r[i,gfcolor()]:='红翻绿';
            r[i,'thigh']:=r[i-1,'thigh'];
            r[i,'tmid']:=r[i-1,'tlow'];
            r[i,'tlow']:=r[i,gfclose()];
          end
         end
         else begin //昨为绿
           star:=r[i-1,'tlow'];
           if r[i,gfclose()]<=star then  //昨下端为开端->下垂绿框
           begin
             if r[i,gfclose()]=star then r[i,gfcolor()]:='无色';
             else r[i,gfcolor()]:='绿色';
             r[i,'tlow']:=r[i,gfclose()];
             r[i,'thigh']:=star;
             r[i,'tmid']:=r[i,'thigh'];
           end
           else if r[i,gfclose()]>star and r[i,gfclose()]<=r[i-1,'thigh'] then
           begin       //下端齐平的上升绿框
             r[i,gfcolor()]:='绿色';
             r[i,'tlow']:=star;
             r[i,'thigh']:=r[i,gfclose()];
             r[i,'tmid']:=r[i,'thigh'];
           end
           else begin   //下端齐平的上升绿翻红
             r[i,gfcolor()]:='绿翻红';
             r[i,'thigh']:=r[i,gfclose()];
             r[i,'tmid']:=r[i-1,'thigh'];
             r[i,'tlow']:=r[i-1,'tlow'];
           end
         end
       end
     end
     return r;

    返回: