找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
首页论坛EAHub 外汇论坛
查看: 16|回复: 0

点差与点差平均值

[复制链接]

4万

主题

54

回帖

4万

积分

管理员

积分
45954
发表于 2026-8-21 23:13:00 | 显示全部楼层 |阅读模式
#property strict
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Spread
#property indicator_label1  "Spread"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot SpArray
#property indicator_label2  "SpreadMap"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrYellow
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- indicator buffers
extern int    ExtSize=0;
input bool     InpPrint=false;
double         SpreadBuffer[];
double         SpreadMapBuffer[];
const string SpreadMapName="SpreadMap";
int Showbars=ExtSize;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   Showbars=(ExtSize<=0)?(int)ChartGetInteger(0,CHART_WIDTH_IN_BARS,ChartWindowFind()):ExtSize;//ChartGetInteger(0,CHART_WINDOWS_TOTAL)CHART_VISIBLE_BARS
   if(ChartSetInteger(0,CHART_SHOW_ASK_LINE,0,true))
      ChartSetInteger(0,CHART_COLOR_ASK,clrSnow);
   if(ChartSetInteger(0,CHART_SHOW_BID_LINE,0,true))
      ChartSetInteger(0,CHART_COLOR_BID,clrSilver);
   if(ObjectFind(SpreadMapName)< 0) //ObjectFind(Name),if (ObjectFind(name + " Label") != 0)
     {
      ObjectCreate(SpreadMapName, OBJ_ARROW_RIGHT_PRICE,ChartWindowFind(),TimeCurrent(), 0);
      ObjectSet(SpreadMapName, OBJPROP_SELECTABLE,false);
      ObjectSet(SpreadMapName, OBJPROP_COLOR, clrYellow);
      ObjectSet(SpreadMapName, OBJPROP_STYLE, STYLE_SOLID);
      ObjectSet(SpreadMapName, OBJPROP_WIDTH, 1);//粗细
     }
//--- indicator buffers mapping
   SetIndexEmptyValue(0,0.0);
   SetIndexBuffer(0,SpreadBuffer);
   SetIndexBuffer(1,SpreadMapBuffer);
//---
   ArraySetAsSeries(SpreadBuffer,true);
   ArraySetAsSeries(SpreadMapBuffer,true);
   ZeroMemory(SpreadBuffer);
   ZeroMemory(SpreadMapBuffer);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   static MqlTick last_tick;
   int size = prev_calculated;
   if(iRefreshRates(last_tick))//last_tick.bid!=Bid
     {
      if(size<Showbars && size<rates_total)
         size+=1;
      if(size>1)
         ArrayCopy(SpreadBuffer,SpreadBuffer,1,0,size-1);

      double mspread=MarketInfo(_Symbol,MODE_SPREAD);
      if(mspread<=0)
         mspread=(last_tick.ask-last_tick.bid)/_Point;
      SpreadBuffer[0]=mspread;
      //---
      double sum=0.0;
      for(int i=0; i<size; i++)
         sum+=SpreadBuffer; // Summation for the double
      if(size<=4)
         SpreadMapBuffer[0]=size>0?(sum/size):SpreadBuffer[0]; // Just divide the sum by the number
      else                                                     //--- now, get the highest value itself in the array
        {
         double terminal=SpreadBuffer[ArrayMaximum(SpreadBuffer)]+SpreadBuffer[ArrayMinimum(SpreadBuffer)];
         SpreadMapBuffer[0]=(sum-terminal)/(size-2);
        }
      ArrayInitialize(SpreadMapBuffer,SpreadMapBuffer[0]);
      ObjectMove(SpreadMapName, 0, last_tick.time, SpreadMapBuffer[0]);//修改移动
     }
//--- return value of prev_calculated for next call
   return(size);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(id == CHARTEVENT_CHART_CHANGE)
     {
      long v_bars;
      if(ExtSize<=0)
         if(ChartGetInteger(0,CHART_VISIBLE_BARS,0,v_bars) || ChartGetInteger(0,CHART_WIDTH_IN_BARS,0,v_bars))
           {
            if(Showbars!=v_bars)
              {
               Showbars=(int)v_bars;
               if(InpPrint)
                  Print("ArrarySize Freshed:"+IntegerToString(Showbars));
              }
           }
     }
  }
//+------------------------------------------------------------------+
bool iRefreshRates(MqlTick &last_tick,string symbol=NULL)export
  {
   if(symbol==NULL)
      symbol=_Symbol;
   if(!IsConnected())
      return(false);

   if(SymbolInfoTick(symbol,last_tick))
      return(true);
//--- refresh rates
   if(symbol==_Symbol&&RefreshRates())
      return(true);
   Print("RefreshRates failed");
   return(false);
  }复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|EAHub外汇论坛

GMT+8, 2026-10-11 16:46 , Processed in 0.098190 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表