//+------------------------------------------------------------------+
//| Demo_iGator.mq5 |
//| Copyright 2011, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2000-2024, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#property description "The indicator demonstrates how to obtain data"
#property description "of indicator buffers for the iGator technical indicator."
#property description "A symbol and timeframe used for calculation of the indicator,"
#property description "are set by the symbol and period parameters."
#property description "The method of creation of the handle is set through the 'type' parameter (function type)."
#property description "All other parameters are as in a standard Gator Oscillator."
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots 2
//--- 绘制GatorUp
#property indicator_label1 "GatorUp"
#property indicator_type1 DRAW_COLOR_HISTOGRAM
#property indicator_color1 clrGreen, clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- 绘制GatorDown
#property indicator_label2 "GatorDown"
#property indicator_type2 DRAW_COLOR_HISTOGRAM
#property indicator_color2 clrGreen, clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//+------------------------------------------------------------------+
//| 枚举处理创建方法 |
//+------------------------------------------------------------------+
enum Creation
{
Call_iGator, // 使用iGator
Call_IndicatorCreate // 使用IndicatorCreate
};
//--- 输入参数
input Creation type=Call_iGator; // 函数类型
input string symbol=" "; // 交易品种
input ENUM_TIMEFRAMES period=PERIOD_CURRENT; // 时间帧
input int jaw_period=13; // 咽喉线周期
input int jaw_shift=8; // 咽喉线移动
input int teeth_period=8; // 牙齿线周期
input int teeth_shift=5; // 牙齿线移动
input int lips_period=5; // 唇部线周期
input int lips_shift=3; // 唇部线移动
input ENUM_MA_METHOD MA_method=MODE_SMMA; // 鳄鱼平均线的方法
input ENUM_APPLIED_PRICE applied_price=PRICE_MEDIAN;// 用于计算鳄鱼的价格类型
//--- 指标缓冲区
double GatorUpBuffer[];
double GatorUpColors[];
double GatorDownBuffer[];
double GatorDownColors[];
//--- 存储iGator 指标处理程序的变量
int handle;
//--- 存储变量
string name=symbol;
//--- 图表上的指标名称
string short_name;
//--- 移动上下直方图的值
int shift;
//--- 我们将在加多摆动指标中保持值的数量
int bars_calculated=0;
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 分配指标缓冲区数组
SetIndexBuffer(0,GatorUpBuffer,INDICATOR_DATA);
SetIndexBuffer(1,GatorUpColors,INDICATOR_COLOR_INDEX);
SetIndexBuffer(2,GatorDownBuffer,INDICATOR_DATA);
SetIndexBuffer(3,GatorDownColors,INDICATOR_COLOR_INDEX);
/*
参数中指定的所有移动指的是绘制加多摆动指标基础上的鳄鱼指标!
这就是我们不能移动加多摆动指标本身的原因,但是他们移动鳄鱼线,
其值用于计算加多摆动指标!
*/
//--- 我们计算上下直方图的移动,这等于咽喉线和牙齿线之间的区别
shift=MathMin(jaw_shift,teeth_shift);
PlotIndexSetInteger(0,PLOT_SHIFT,shift);
//--- 尽管指标包含两个直方图,也使用相同的移动 - 就是实施iGator 指标。
PlotIndexSetInteger(1,PLOT_SHIFT,shift);
//--- 定义绘制指标的交易品种
name=symbol;
//--- 删除向左和向右的空格
StringTrimRight(name);
StringTrimLeft(name);
//--- 如果它返回 'name' 字符串的零长度
if(StringLen(name)==0)
{
//--- 获得指标附属的图表交易品种
name=_Symbol;
}
//--- 创建指标处理程序
if(type==Call_iGator)
handle=iGator(name,period,jaw_period,jaw_shift,teeth_period,teeth_shift,
lips_period,lips_shift,MA_method,applied_price);
else
{
//--- 以指标参数填充结构
MqlParam pars[8];
//--- 鳄鱼线的周期和移动
pars[0].type=TYPE_INT;
pars[0].integer_value=jaw_period;
pars[1].type=TYPE_INT;
pars[1].integer_value=jaw_shift;
pars[2].type=TYPE_INT;
pars[2].integer_value=teeth_period;
pars[3].type=TYPE_INT;
pars[3].integer_value=teeth_shift;
pars[4].type=TYPE_INT;
pars[4].integer_value=lips_period;
pars[5].type=TYPE_INT;
pars[5].integer_value=lips_shift;
//--- 平滑类型
pars[6].type=TYPE_INT;
pars[6].integer_value=MA_method;
//--- 价格类型
pars[7].type=TYPE_INT;
pars[7].integer_value=applied_price;
//--- 创建处理程序
handle=IndicatorCreate(name,period,IND_GATOR,8,pars);
}
//--- 如果没有创建处理程序
if(handle==INVALID_HANDLE)
{
//--- 叙述失败和输出错误代码
PrintFormat("Failed to create handle of the iGator indicator for the symbol %s/%s, error code %d",
name,
EnumToString(period),
GetLastError());
//--- 指标提前停止
return(INIT_FAILED);
}
//--- 显示加多摆动指标计算的交易品种/时间帧
short_name=StringFormat("iGator(%s/%s, %d, %d ,%d, %d, %d, %d)",name,EnumToString(period),
jaw_period,jaw_shift,teeth_period,teeth_shift,lips_period,lips_shift);
IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- 指标正常初始化
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 自定义指标迭代函数 |
//+------------------------------------------------------------------+
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[])
{
//--- 从iGator指标复制的值数
int values_to_copy;
//--- 确定指标计算的数量值
int calculated=BarsCalculated(handle);
if(calculated<=0)
{
PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());
return(0);
}
//--- 如果它是指标计算的最初起点或如果iGator指标数量值更改
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化)
if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
{
//--- 如果GatorUpBuffer数组大于交易品种/周期iGator指标的数量值,那么我们不会复制任何内容
//--- 否则,我们复制小于指标缓冲区的大小
if(calculated>rates_total) values_to_copy=rates_total;
else values_to_copy=calculated;
}
else
{
//--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用
//--- 为了计算,添加不超过一柱
values_to_copy=(rates_total-prev_calculated)+1;
}
//--- 以加多摆动指标的值填充数组
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作
if(!FillArraysFromBuffers(GatorUpBuffer,GatorUpColors,GatorDownBuffer,GatorDownColors,
shift,handle,values_to_copy)) return(0);
//--- 形成信息
string comm=StringFormat("%s ==> Updated value in the indicator %s: %d",
TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS),
short_name,
values_to_copy);
//--- 在图表上展示服务信息
Comment(comm);
//--- 记住加多摆动指标的数量值
bars_calculated=calculated;
//--- 返回prev_calculated值以便下次调用
return(rates_total);
}
//+------------------------------------------------------------------+
//| 填充 iGator 指标的指标缓冲区 |
//+------------------------------------------------------------------+
bool FillArraysFromBuffers(double &ups_buffer[], // 向上直方图的指标缓冲区
double &up_color_buffer[], // 向上直方图价格指数的指标缓冲区
double &downs_buffer[], // 向下直方图的指标缓冲区
double &downs_color_buffer[], // 向下直方图价格指数的指标缓冲区
int u_shift, // 上下直方图移动
int ind_handle, // iGator 指标的处理程序
int amount // 复制值的数量
)
{
//--- 重置错误代码
ResetLastError();
//--- 以0标引指标缓冲区的值填充部分GatorUpBuffer 数组
if(CopyBuffer(ind_handle,0,-u_shift,amount,ups_buffer)<0)
{
//--- 如果复制失败,显示错误代码
PrintFormat("Failed to copy data from the iGator indicator, error code %d",GetLastError());
//--- 退出零结果 - 它表示被认为是不计算的指标
return(false);
}
//--- 以1标引指标缓冲区的值填充部分GatorUpColors 数组
if(CopyBuffer(ind_handle,1,-u_shift,amount,up_color_buffer)<0)
{
//--- 如果复制失败,显示错误代码
PrintFormat("Failed to copy data from the iGator indicator, error code %d",GetLastError());
//--- 退出零结果 - 它表示被认为是不计算的指标
return(false);
}
//--- 以2标引指标缓冲区的值填充部分GatorDownBuffer 数组
if(CopyBuffer(ind_handle,2,-u_shift,amount,downs_buffer)<0)
{
//--- 如果复制失败,显示错误代码
PrintFormat("Failed to copy data from the iGator indicator, error code %d",GetLastError());
//--- 退出零结果 - 它表示被认为是不计算的指标
return(false);
}
//--- 以3标引指标缓冲区的值填充部分GatorDownColors 数组
if(CopyBuffer(ind_handle,3,-u_shift,amount,downs_color_buffer)<0)
{
//--- 如果复制失败,显示错误代码
PrintFormat("Failed to copy data from the iGator indicator, error code %d",GetLastError());
//--- 退出零结果 - 它表示被认为是不计算的指标
return(false);
}
//--- 一切顺利
return(true);
}
//+------------------------------------------------------------------+
//| 指标去初始化函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(handle!=INVALID_HANDLE)
IndicatorRelease(handle);
//--- 删除指标后清空图表
Comment("");
}
|