首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

MT4主图长趋势转换+开关按钮指标源码

量化团队将不定期的公布一些经典技术指标改版或者源码公式

主图长趋势转换+开关按钮指标源码

这是一个趋势反转指标,不重绘

全部源码获取方式:

加入我们的知识星球获取

#property indicator_chart_window#property indicator_buffers 4#property indicator_color1  clrNONE;#property indicator_color2  clrNONE;#property indicator_color3  C'105,50,60';#property indicator_color4  C'45,60,110';#property indicator_width1  1#property indicator_width2  1#property indicator_width3  8#property indicator_width4  8

extern ENUM_TIMEFRAMES TimeFrame       = PERIOD_D1;   // Time frameextern int             MaPeriod        = 2;extern ENUM_MA_METHOD  MaMetod         = 2;extern int             Step            = 25;extern bool            BetterFormula   = false;extern bool            jurik           = false;extern int             Length          = 1;extern double          Phase           = 0;extern bool            alertsOn        = false;extern bool            alertsOnCurrent = false;extern bool            alertsMessage   = false;extern bool            alertsSound     = false;extern bool            alertsEmail     = false;input int              btn_Offset_x    = 204;              // button x offsetinput int              btn_Offset_y    = 20;               // Button y offsetinput int              btn_Width       = 65;               // Button widthinput int              btn_Height      = 20;               // Button heightinput int              btn_FontSize    = 10;               // Button font size     input string           DisplayID       = "HA";            // Button IDextern bool            Interpolate     = true;             // Interpolate in mtf mode

double Buffer1[],Buffer2[],Buffer3[],Buffer4[],trend[],count[];string indicatorFileName;#define _mtfCall(_buff,_ind) iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,MaPeriod,MaMetod,Step,BetterFormula,jurik,Length,Phase,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsEmail,btn_Offset_x,btn_Offset_y,btn_Width,btn_Height,btn_FontSize,DisplayID,_buff,_ind)

int init(){  if (ObjectFind(DisplayID)!=0)  {     ObjectCreate(ChartID(),DisplayID,OBJ_BUTTON,0,0,0);        ObjectSetString( ChartID(),DisplayID,OBJPROP_TEXT,"HA on");        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_FONTSIZE,btn_FontSize);        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_CORNER,0);        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_COLOR,clrWhite);        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_BGCOLOR,clrDimGray);        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_YDISTANCE,btn_Offset_y);        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_XDISTANCE,btn_Offset_x);        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_XSIZE,btn_Width);        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_YSIZE,btn_Height);        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_SELECTABLE,false);        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_SELECTED,false);        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_HIDDEN,true);        ObjectSetInteger(ChartID(),DisplayID,OBJPROP_STATE,false);  }  IndicatorBuffers(6);  SetIndexBuffer(0, Buffer1);  SetIndexBuffer(1, Buffer2);  SetIndexBuffer(2, Buffer3);  SetIndexBuffer(3, Buffer4);  SetIndexBuffer(4, trend);  SetIndexBuffer(5, count);

 if (GetButtonState(DisplayID)!="off")  {        SetIndexStyle(0,DRAW_HISTOGRAM);        SetIndexStyle(1,DRAW_HISTOGRAM);        SetIndexStyle(2,DRAW_HISTOGRAM);        SetIndexStyle(3,DRAW_HISTOGRAM);   }   else for (int i=0; i<4; i++) SetIndexStyle(i,DRAW_NONE);

 indicatorFileName = WindowExpertName();  TimeFrame         = fmax(TimeFrame,_Period);

return(0);}void OnDeinit(const int reason){     switch(reason)     {        case REASON_PARAMETERS  :        case REASON_CHARTCHANGE :        case REASON_RECOMPILE   :        case REASON_CLOSE       : break;        default :        {           ObjectDelete(DisplayID);        }                     }}

//////

void OnChartEvent(const int id, const long& lparam, const double& dparam, const string& sparam){  static string prevState ="";  if (id==CHARTEVENT_OBJECT_CLICK && sparam==DisplayID)  {     string newState = GetButtonState(DisplayID);        if (newState!=prevState)        if (newState=="off")        {          SetIndexStyle(0, DRAW_NONE);          SetIndexStyle(1, DRAW_NONE);          SetIndexStyle(2, DRAW_NONE);          SetIndexStyle(3, DRAW_NONE);          prevState=newState;        }        else        {          SetIndexStyle(0, DRAW_HISTOGRAM);          SetIndexStyle(1, DRAW_HISTOGRAM);          SetIndexStyle(2, DRAW_HISTOGRAM);          SetIndexStyle(3, DRAW_HISTOGRAM);          prevState=newState;        }        ObjectSetString(ChartID(),DisplayID,OBJPROP_TEXT,"HA "+newState);  }}

int start(){  double maOpen, maClose, maLow, maHigh;  double haOpen, haClose, haLow, haHigh;

 int i,pointModifier,counted_bars=IndicatorCounted();     if(counted_bars<0) return(-1);     if(counted_bars>0) counted_bars--;        int limit = fmin(Bars-counted_bars,Bars-1); count[0] = limit;        if (_Digits==3 || _Digits==5)              pointModifier = 10;        else  pointModifier = 1;              if (TimeFrame != _Period)        {           limit = (int)fmax(limit,fmin(Bars-1,_mtfCall(5,0)*TimeFrame/_Period));           for (i=limit;i>=0 && !_StopFlag; i--)           {                 int y = iBarShift(NULL,TimeFrame,Time[i]);                    Buffer1[i] = _mtfCall(0,y);                 Buffer2[i] = _mtfCall(1,y);                 Buffer3[i] = _mtfCall(2,y);                    Buffer4[i] = _mtfCall(3,y);

                   //                    //                    //                    //                    //

                    if (!Interpolate || (i>0 && y==iBarShift(NULL,TimeFrame,Time[i-1]))) continue;                     #define _interpolate(buff) buff[i+k] = buff[i]+(buff[i+n]-buff[i])*k/n                     int n,k; datetime time = iTime(NULL,TimeFrame,y);                        for(n = 1; (i+n)<Bars && Time[i+n] >= time; n++) continue;                        for(k = 1; k<n && (i+n)<Bars && (i+k)<Bars; k++)                        {                           _interpolate(Buffer1);                           _interpolate(Buffer2);                           _interpolate(Buffer3);                           _interpolate(Buffer4);                        }           }  return(0);  }    

 //  //  //  //  //

 for(int pos=limit; pos >= 0; pos--)  {     if (jurik)        {           maOpen  = iSmooth(Open[pos], Length,Phase,pos,0);           maClose = iSmooth(Close[pos],Length,Phase,pos,10);           maLow   = iSmooth(Low[pos],  Length,Phase,pos,20);           maHigh  = iSmooth(High[pos], Length,Phase,pos,30);        }     else        {           maOpen  = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_OPEN, pos);           maClose = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_CLOSE,pos);           maLow   = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_LOW,  pos);           maHigh  = iMA(NULL,0,MaPeriod,0,MaMetod,PRICE_HIGH, pos);        }

    if (BetterFormula) {              if (maHigh!=maLow)                    haClose = (maOpen+maClose)/2+(((maClose-maOpen)/(maHigh-maLow))*MathAbs((maClose-maOpen)/2));              else  haClose = (maOpen+maClose)/2; }        else        haClose = (maOpen+maHigh+maLow+maClose)/4;                    haOpen  = (Buffer3[pos+1]+Buffer4[pos+1])/2;                    haHigh  = fmax(maHigh,fmax(haOpen,haClose));                    haLow   = fmin(maOpen,fmin(haOpen,haClose));

       if (haOpen<haClose) { Buffer1[pos]=haLow;  Buffer2[pos]=haHigh; }        else                { Buffer1[pos]=haHigh; Buffer2[pos]=haLow;  }                              Buffer3[pos]=haOpen;                              Buffer4[pos]=haClose;     //     //     //     //     //

    if (Step>0)     {        if( MathAbs(Buffer1[pos]-Buffer1[pos+1]) < Step*pointModifier*_Point ) Buffer1[pos]=Buffer1[pos+1];        if( MathAbs(Buffer2[pos]-Buffer2[pos+1]) < Step*pointModifier*_Point ) Buffer2[pos]=Buffer2[pos+1];        if( MathAbs(Buffer3[pos]-Buffer3[pos+1]) < Step*pointModifier*_Point ) Buffer3[pos]=Buffer3[pos+1];        if( MathAbs(Buffer4[pos]-Buffer4[pos+1]) < Step*pointModifier*_Point ) Buffer4[pos]=Buffer4[pos+1];     }               trend[i] = trend[i+1];     if (Buffer1[i]>Buffer2[i]) trend[i] = -1;     if (Buffer1[i]<Buffer2[i]) trend[i] =  1;  }

 //  //  //  //  //

 if (alertsOn)  {     if (alertsOnCurrent)          int whichBar = 0;     else     whichBar = 1;

       //        //        //        //        //

       if (trend[whichBar] != trend[whichBar+1])        if (trend[whichBar] == 1)              doAlert("up");        else  doAlert("down");      } return(0);}

//////

void doAlert(string doWhat){  static string   previousAlert="nothing";  static datetime previousTime;  string message;

    if (previousAlert != doWhat || previousTime != Time[0]) {         previousAlert  = doWhat;         previousTime   = Time[0];

        //         //         //         //         //

        message = timeFrameToString(_Period)+" "+_Symbol+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" heiken ashi jurik smoothed trend changed to "+doWhat;            if (alertsMessage) Alert(message);            if (alertsEmail)   SendMail(_Symbol+" heiken ashi jurik smoothed ",message);            if (alertsSound)   PlaySound("alert2.wav");     }}

//////

double wrk[][40];#define bsmax  5#define bsmin  6#define volty  7#define vsum   8#define avolty 9

//////////

double iSmooth(double price, double length, double phase, int i, int s=0){  if (length <=1) return(price);  if (ArrayRange(wrk,0) != Bars) ArrayResize(wrk,Bars);

 int r = Bars-i-1;     if (r==0) { for(int k=0; k<7; k++) wrk[r][k+s]=price; for(; k<10; k++) wrk[r][k+s]=0; return(price); }

 //  //  //  //  //

    double len1   = MathMax(MathLog(MathSqrt(0.5*(length-1)))/MathLog(2.0)+2.0,0);     double pow1   = MathMax(len1-2.0,0.5);     double del1   = price - wrk[r-1][bsmax+s];     double del2   = price - wrk[r-1][bsmin+s];     double div    = 1.0/(10.0+10.0*(MathMin(MathMax(length-10,0),100))/100);     int    forBar = MathMin(r,10);

       wrk[r][volty+s] = 0;              if(MathAbs(del1) > MathAbs(del2)) wrk[r][volty+s] = MathAbs(del1);              if(MathAbs(del1) < MathAbs(del2)) wrk[r][volty+s] = MathAbs(del2);        wrk[r][vsum+s] =wrk[r-1][vsum+s] + (wrk[r][volty+s]-wrk[r-forBar][volty+s])*div;

       //        //        //        //        //

       wrk[r][avolty+s] = wrk[r-1][avolty+s]+(2.0/(MathMax(4.0*length,30)+1.0))*(wrk[r][vsum+s]-wrk[r-1][avolty+s]);           if (wrk[r][avolty+s] > 0)              double dVolty = wrk[r][volty+s]/wrk[r][avolty+s]; else dVolty = 0;               if (dVolty > MathPow(len1,1.0/pow1)) dVolty = MathPow(len1,1.0/pow1);                 if (dVolty < 1)                      dVolty = 1.0;

    //     //     //     //     //

 double pow2 = MathPow(dVolty, pow1);     double len2 = MathSqrt(0.5*(length-1))*len1;     double Kv   = MathPow(len2/(len2+1), MathSqrt(pow2));

       if (del1 > 0) wrk[r][bsmax+s] = price; else wrk[r][bsmax+s] = price - Kv*del1;        if (del2 < 0) wrk[r][bsmin+s] = price; else wrk[r][bsmin+s] = price - Kv*del2;

 //  //  //  //  //

    double R     = MathMax(MathMin(phase,100),-100)/100.0 + 1.5;     double beta  = 0.45*(length-1)/(0.45*(length-1)+2);     double alpha = MathPow(beta,pow2);

       wrk[r][0+s] = price + alpha*(wrk[r-1][0+s]-price);        wrk[r][1+s] = (price - wrk[r][0+s])*(1-beta) + beta*wrk[r-1][1+s];        wrk[r][2+s] = (wrk[r][0+s] + R*wrk[r][1+s]);        wrk[r][3+s] = (wrk[r][2+s] - wrk[r-1][4+s])*MathPow((1-alpha),2) + MathPow(alpha,2)*wrk[r-1][3+s];        wrk[r][4+s] = (wrk[r-1][4+s] + wrk[r][3+s]);

 //  //  //  //  //

 return(wrk[r][4+s]);}

////////

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

string timeFrameToString(int tf){  for (int i=ArraySize(iTfTable)-1; i>=0; i--)        if (tf==iTfTable[i]) return(sTfTable[i]);                             return("");}

//////

string GetButtonState(string whichbutton){     bool selected = ObjectGetInteger(ChartID(),whichbutton,OBJPROP_STATE);     if (selected) { return ("on");  }     else          { return ("off"); }}

  • 发表于:
  • 原文链接https://page.om.qq.com/page/O0gstkYzGREI2lSeaHCD18dQ0
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券