分享

//[]i 44 分钟时框图表显示小时K线

 影子蓝蓝天空 2011-11-15

i 44 分钟时框图表显示小时K线  

2011-06-26 15:41:42|  分类: [i] 指标待理解 |  标签: |字号 订阅

 

//[]i 44 分钟时框图表显示小时K线 - 牛在天上飞 - hance66的博客

 

//[i]44 分钟时框图表显示小时K线

#property indicator_chart_window

extern int   时框= 60;                 // 目标时间框选 默认60分钟
extern color 阳烛色= Red;
extern color 阴烛色= DeepSkyBlue;
extern color 星字色= White;        
extern int   线边宽= 3;
extern bool  使用实体填色= false;          // K线实体是否填充颜色
//--
int      时框等分整倍数, OpenBar, timer, i, timerTF, name, MidBar;
double   HighPrevBar, LowPrevBar, ClosePrevBar;
double   OpenNewBar, HighNewBar, LowNewBar, CloseNewBar;
double   HighCurBar, LowCurBar, CloseCurBar;
double   priceNewSH, priceNewSL, pricePrevSH, pricePrevSL, priceCurSH, priceCurSL;
string   nameNewCandle, namePrevCandle;
string   nameNewShadowH, nameNewShadowL, namePrevShadowH, namePrevShadowL;
string   NameBar, NameHigh, NameLow;
datetime TimeOpenNewBar, TimeCloseNewBar, TimeClosePrevBar;
datetime timeNewShadow, timePrevShadow;
bool     NewBar;

//--------------初始化---------------+

int init()
  { 
   IndicatorShortName(时框+"上的K线"+" 在"+Period()+"图上");
   时框等分整倍数= 时框/Period();
   timer= Period()*60;                                       // 计时器设定为周期乘以60
   timerTF= 时框*60;
   name= 0;
   TimeOpenNewBar= Time[Bars-1];                             // 新棒开盘时间为 上一时间
   OpenNewBar= Open[Bars-1];
   NewBar= false;
  
   NameBar  = "Bar M"+ 时框+"-";                             // 字符串内容
   NameHigh = "High M"+时框+"-";
   NameLow  = "Low M"+ 时框+"-";
   return(0);
  } 

//-------------反初始化--------------+

int deinit() 
  {
   for(int DelOBJ= 1; DelOBJ<=name; DelOBJ++){
      ObjectDelete(NameBar+DelOBJ);
      ObjectDelete(NameHigh+DelOBJ);
      ObjectDelete(NameLow+DelOBJ);  
     }
   Comment("");
   return(0);   
  }             
                                 
//--------------主函数---------------+

int start()
  {
   if(时框>1440){
      Comment("\n", " 不支持时框超过日线!!!");
      return(0);
     }
    
   if(Period()>240){
      Comment("\n", " 不支持时框超过四小时线!!!");
      return(0);
     }
    
   if(时框<=Period() || MathMod(时框, Period())!=0){
      Comment("\n", " 时框不能等分到当前(", Period(), ")图表上");
      return(0);
     }
    
   i= Bars-IndicatorCounted();
   while(i>0){
      i--;
      while(i>=0)
         if(Time[i]==TimeOpenNewBar || BarNew(i, 时框)==false)
            i--;
         else{
            NewBar= true;
            name++;
            break;
           }
      if(i<0)
         i= 0;
     
      if(NewBar==true){
         //--Previos Bar
         OpenBar= iBarShift(0, 0, TimeOpenNewBar, false);
         TimeClosePrevBar= Time[i+1];
         ClosePrevBar= Close[i+1];
         HighPrevBar= High[Highest(0, 0, MODE_HIGH, OpenBar-i, i+1)];
         LowPrevBar= Low[Lowest(0, 0, MODE_LOW, OpenBar-i, i+1)];
         namePrevCandle= NameBar+(name-1);
         MidBar= OpenBar-MathRound((OpenBar-i)/2);
         timePrevShadow= Time[MidBar];
         pricePrevSH= PriceShadow(OpenNewBar, ClosePrevBar, 0);
         pricePrevSL= PriceShadow(OpenNewBar, ClosePrevBar, 1);
         namePrevShadowH= NameHigh+(name-1);
         namePrevShadowL= NameLow+(name-1);
        
         //--Modifi Previos Bar & Shadow 上下影线
         if(ObjectFind(namePrevCandle)==0){
            ObjectMove(namePrevCandle, 1, TimeClosePrevBar, ClosePrevBar);
            PropBar(OpenNewBar, ClosePrevBar, namePrevCandle);
         if(OpenBar==i+1)
            ObjectSet(namePrevCandle, OBJPROP_WIDTH, 线边宽*5);
        }
    
      if(ObjectFind(namePrevShadowH)==0){
         if(pricePrevSH==HighPrevBar)
            ObjectDelete(namePrevShadowH);
         else{
            ObjectMove(namePrevShadowH, 0, timePrevShadow, pricePrevSH);
        
            ObjectMove(namePrevShadowH, 1, timePrevShadow, HighPrevBar);
            确定K线色子(OpenNewBar, ClosePrevBar, namePrevShadowH);
            ObjectSetText(namePrevShadowH, "High= "+DoubleToStr(HighPrevBar, Digits), 7, "Tahoma");
           }
        }
       
      if(ObjectFind(namePrevShadowL)==0){
         if(pricePrevSL==LowPrevBar)
            ObjectDelete(namePrevShadowL);
         else{
            ObjectMove(namePrevShadowL, 0, timePrevShadow, pricePrevSL);
            ObjectMove(namePrevShadowL, 1, timePrevShadow, LowPrevBar);
            确定K线色子(OpenNewBar, ClosePrevBar, namePrevShadowL);
            ObjectSetText(namePrevShadowL, "Low= "+DoubleToStr(LowPrevBar, Digits), 7, "Tahoma");
           }
        }
       
      //--新棒
      OpenNewBar= Open[i];
      TimeOpenNewBar= Time[i];
      HighNewBar= High[i];
      LowNewBar= Low[i];
      CloseNewBar= Close[i];
      TimeCloseNewBar= Time[i]+timerTF-timer;
      nameNewCandle= NameBar+name;
      timeNewShadow= Time[i]+MathRound(时框等分整倍数/2)*timer;
      priceNewSH= PriceShadow(OpenNewBar, CloseNewBar, 0);
      priceNewSL= PriceShadow(OpenNewBar, CloseNewBar, 1);
      nameNewShadowH= NameHigh+name;
      nameNewShadowL= NameLow+name;
      NewBar= false;
     }
     
     //--当前棒
   else{
      OpenBar= iBarShift(0, 0, TimeOpenNewBar, false);
      CloseCurBar= Close[i];
      HighCurBar= High[Highest(0, 0, MODE_HIGH, OpenBar+1, i)];
      LowCurBar= Low[Lowest(0, 0, MODE_LOW, OpenBar+1, i)];
      priceCurSH= PriceShadow(OpenNewBar, CloseCurBar, 0);
      priceCurSL= PriceShadow(OpenNewBar, CloseCurBar, 1);
     }

   //--创建新物件 & Modifi Current
   if(ObjectFind(nameNewCandle)!= 0){
      ObjectCreate(nameNewCandle, OBJ_RECTANGLE, 0, TimeOpenNewBar, OpenNewBar, TimeCloseNewBar, CloseNewBar);
      ObjectSet(nameNewCandle, OBJPROP_STYLE, STYLE_SOLID);
      PropBar(OpenNewBar, CloseNewBar, nameNewCandle);
     }
   else{
      ObjectMove(nameNewCandle, 1, TimeCloseNewBar, CloseCurBar);      
      PropBar(OpenNewBar, CloseCurBar, nameNewCandle);
     }
    
   if(ObjectFind(nameNewShadowH)!= 0){
      ObjectCreate(nameNewShadowH, OBJ_TREND, 0, timeNewShadow, priceNewSH, timeNewShadow, HighNewBar);
      ObjectSet(nameNewShadowH, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet(nameNewShadowH, OBJPROP_WIDTH, 线边宽);
      ObjectSet(nameNewShadowH, OBJPROP_RAY, false);
      确定K线色子(OpenNewBar, CloseNewBar, nameNewShadowH);
     }
   else{
      ObjectMove(nameNewShadowH, 0, timeNewShadow, priceCurSH);
      ObjectMove(nameNewShadowH, 1, timeNewShadow, HighCurBar);
      确定K线色子(OpenNewBar, CloseCurBar, nameNewShadowH);
     }
    
   if(ObjectFind(nameNewShadowL)!= 0){
      ObjectCreate(nameNewShadowL, OBJ_TREND, 0, timeNewShadow, priceNewSL, timeNewShadow, LowNewBar);
      ObjectSet(nameNewShadowL, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet(nameNewShadowL, OBJPROP_WIDTH, 线边宽);
      ObjectSet(nameNewShadowL, OBJPROP_RAY, false);
      确定K线色子(OpenNewBar, CloseNewBar, nameNewShadowL);
     }
   else{
      ObjectMove(nameNewShadowL, 0, timeNewShadow, priceCurSL);
      ObjectMove(nameNewShadowL, 1, timeNewShadow, LowCurBar);
      确定K线色子(OpenNewBar, CloseCurBar, nameNewShadowL);
     }
  }

//-----------------------------窗显虚K线的OHLC-----------------------------+

   Comment(Symbol(), ",目标K线(M", 时框, ")   开= ", OpenNewBar, ";  高= ", HighCurBar, ";  低= ", LowCurBar, ";  收= ", CloseCurBar, "\n");
   return(0);
  }

//----------------------主要函数 "新棒或旧棒"-----------------------------+

bool BarNew(int j, int tmf)
  {
   int t0= 1440*(TimeDayOfWeek(Time[j])-1)+60*TimeHour(Time[j])+TimeMinute(Time[j]),
   t1= 1440*(TimeDayOfWeek(Time[j+1])-1)+60*TimeHour(Time[j+1])+TimeMinute(Time[j+1]);
  
   if(MathMod(t0, tmf)-MathMod(t1, tmf)==t0-t1)
      return(false);
   else
      return(true);
  }

//----------------------Function "Price Shadow"-----------------------------+

double PriceShadow(double OpnB, double ClsB, int swt)
  {
   double prH, prL;
   if(OpnB<ClsB){
      prH= ClsB;
      prL= OpnB;
     }
   if(OpnB>ClsB){
      prH= OpnB;
      prL= ClsB;
     }
   if(OpnB==ClsB){
      prH= ClsB;
      prL= ClsB;
     }
   switch(swt){
      case 0: return(prH);
              break;
      case 1: return(prL);
              break;
     }
  }

//----------------------Function "Properti Bars"-----------------------------+

void PropBar(double OpPr, double ClPr, string NmOBJ)
  {
   string Cl= " Close= "+DoubleToStr(ClPr, Digits);
   string Op= " Open= "+DoubleToStr(OpPr, Digits);
  
   if(OpPr==ClPr){
      ObjectSet(NmOBJ, OBJPROP_BACK, false);
      ObjectSet(NmOBJ, OBJPROP_COLOR, 星字色);
      ObjectSetText(NmOBJ, "Doji "+Op+Cl, 7, "Tahoma");
     }
   if(OpPr<ClPr){
      ObjectSet(NmOBJ, OBJPROP_COLOR, 阳烛色);
      ObjectSet(NmOBJ, OBJPROP_BACK, 使用实体填色);
      ObjectSetText(NmOBJ, "UpBar "+Op+Cl, 7, "Tahoma");
     }
   if(OpPr>ClPr){
      ObjectSet(NmOBJ, OBJPROP_COLOR, 阴烛色);
      ObjectSet(NmOBJ, OBJPROP_BACK, 使用实体填色);
      ObjectSetText(NmOBJ, "DnBar "+Op+Cl, 7, "Tahoma");
     }
    
   ObjectSet(NmOBJ, OBJPROP_WIDTH, 线边宽);
  }

//-------------------确定K线色 子函数()----------------------+

void 确定K线色子(double OP, double CP, string NOBJ)
  {
   if(OP==CP)
      ObjectSet(NOBJ, OBJPROP_COLOR, 星字色);
   if(OP<CP)
      ObjectSet(NOBJ, OBJPROP_COLOR, 阳烛色);
   if(OP>CP)
      ObjectSet(NOBJ, OBJPROP_COLOR, 阴烛色);
  }

//----------------------------------------------------------------------------+

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多