//+------------------------------------------------------------------+
//| Demo_iIchimoku.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 iIchimoku 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 just like in the standard Ichimoku Kinko Hyo."
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots 4
//--- Tenkan_sen 标图
#property indicator_label1 "Tenkan_sen"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- Kijun_sen 标图
#property indicator_label2 "Kijun_sen"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrBlue
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- Senkou_Span 标图
#property indicator_label3 "Senkou Span A;Senkou Span B" // two fields will be shown in Data Window
#property indicator_type3 DRAW_FILLING
#property indicator_color3 clrSandyBrown, clrThistle
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//--- Chinkou_Span 标图
#property indicator_label4 "Chinkou_Span"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrLime
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
//+------------------------------------------------------------------+
//| 枚举处理创建方法 |
//+------------------------------------------------------------------+
enum Creation
{
Call_iIchimoku, // 使用iIchimoku
Call_IndicatorCreate // 使用IndicatorCreate
};
//--- 输入参数
input Creation type=Call_iIchimoku; // 函数类型
input int tenkan_sen=9; // Tenkan-sen周期
input int kijun_sen=26; // Kijun-sen周期
input int senkou_span_b=52; // Senkou Span B周期
input string symbol=" "; // 交易品种
input ENUM_TIMEFRAMES period=PERIOD_CURRENT; // 时间帧
//--- 指标缓冲区
double Tenkan_sen_Buffer[];
double Kijun_sen_Buffer[];
double Senkou_Span_A_Buffer[];
double Senkou_Span_B_Buffer[];
double Chinkou_Span_Buffer[];
//--- 存储iIchimoku指标处理程序的变量
int handle;
//--- 存储变量
string name=symbol;
//--- 图表上的指标名称
string short_name;
//--- 我们将在一目均衡图指标中保持值的数量
int bars_calculated=0;
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 分配指标缓冲区数组
SetIndexBuffer(0,Tenkan_sen_Buffer,INDICATOR_DATA);
SetIndexBuffer(1,Kijun_sen_Buffer,INDICATOR_DATA);
SetIndexBuffer(2,Senkou_Span_A_Buffer,INDICATOR_DATA);
SetIndexBuffer(3,Senkou_Span_B_Buffer,INDICATOR_DATA);
SetIndexBuffer(4,Chinkou_Span_Buffer,INDICATOR_DATA);
//--- 设置未来方向kijun_sen 柱形的Senkou 跨通道的移动
PlotIndexSetInteger(2,PLOT_SHIFT,kijun_sen);
//--- 设置无需Chinkou跨线的移动,因为数据跨度
//--- 已经存储在iIchimoku移动
//--- 定义绘制指标的交易品种
name=symbol;
//--- 删除向左和向右的空格
StringTrimRight(name);
StringTrimLeft(name);
//--- 如果它返回 'name' 字符串的零长度
if(StringLen(name)==0)
{
//--- 获得指标附属的图表交易品种
name=_Symbol;
}
//--- 创建指标处理程序
if(type==Call_iIchimoku)
handle=iIchimoku(name,period,tenkan_sen,kijun_sen,senkou_span_b);
else
{
//--- 以指标参数填充结构
MqlParam pars[3];
//--- 鳄鱼线的周期和移动
pars[0].type=TYPE_INT;
pars[0].integer_value=tenkan_sen;
pars[1].type=TYPE_INT;
pars[1].integer_value=kijun_sen;
pars[2].type=TYPE_INT;
pars[2].integer_value=senkou_span_b;
//--- 创建处理程序
handle=IndicatorCreate(name,period,IND_ICHIMOKU,3,pars);
}
//--- 如果没有创建处理程序
if(handle==INVALID_HANDLE)
{
//--- 叙述失败和输出错误代码
PrintFormat("Failed to create handle of the iIchimoku indicator for the symbol %s/%s, error code %d",
name,
EnumToString(period),
GetLastError());
//--- 指标提前停止
return(INIT_FAILED);
}
//--- 显示一目均衡图指标计算的交易品种/时间帧
short_name=StringFormat("iIchimoku(%s/%s, %d, %d ,%d)",name,EnumToString(period),
tenkan_sen,kijun_sen,senkou_span_b);
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[])
{
//--- 从iIchimoku指标复制的值数
int values_to_copy;
//--- 确定指标计算的数量值
int calculated=BarsCalculated(handle);
if(calculated<=0)
{
PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());
return(0);
}
//--- 如果它是指标计算的最初起点或如果iIchimoku指标数量值更改
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化)
if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
{
//--- 如果Tenkan_sen_Buffer数组大于交易品种/周期iIchimoku指标的数量值,那么我们不会复制任何内容
//--- 否则,我们复制小于指标缓冲区的大小
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(Tenkan_sen_Buffer,Kijun_sen_Buffer,Senkou_Span_A_Buffer,Senkou_Span_B_Buffer,Chinkou_Span_Buffer,
kijun_sen,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);
}
//+------------------------------------------------------------------+
//| 填充 iIchimoku 指标的指标缓冲区 |
//+------------------------------------------------------------------+
bool FillArraysFromBuffers(double &tenkan_sen_buffer[], // Tenkan-sen线的指标缓冲区
double &kijun_sen_buffer[], // Kijun_sen线的指标缓冲区
double &senkou_span_A_buffer[], // Senkou Span A 线的指标缓冲区
double &senkou_span_B_buffer[], // Senkou Span B线的指标缓冲区
double &chinkou_span_buffer[], // Chinkou Span线的指标缓冲区
int senkou_span_shift, // 未来方向Senkou 的跨线移动
int ind_handle, // iIchimoku 指标的处理程序
int amount // 复制值的数量
)
{
//--- 重置错误代码
ResetLastError();
//--- 以0标引指标缓冲区的值填充部分Tenkan_sen_Buffer数组
if(CopyBuffer(ind_handle,0,0,amount,tenkan_sen_buffer)<0)
{
//--- 如果复制失败,显示错误代码
PrintFormat("1.Failed to copy data from the iIchimoku indicator, error code %d",GetLastError());
//--- 退出零结果 - 它表示被认为是不计算的指标
return(false);
}
//--- 以1标引指标缓冲区的值填充部分Kijun_sen_Buffer 数组
if(CopyBuffer(ind_handle,1,0,amount,kijun_sen_buffer)<0)
{
//--- 如果复制失败,显示错误代码
PrintFormat("2.Failed to copy data from the iIchimoku indicator, error code %d",GetLastError());
//--- 退出零结果 - 它表示被认为是不计算的指标
return(false);
}
//--- 以2标引指标缓冲区的值填充部分Chinkou_Span_Buffer 数组
//--- 如果senkou_span_shift>0,线根据senkou_span_shift柱形的未来方向移动。
if(CopyBuffer(ind_handle,2,-senkou_span_shift,amount,senkou_span_A_buffer)<0)
{
//--- 如果复制失败,显示错误代码
PrintFormat("3.Failed to copy data from the iIchimoku indicator, error code %d",GetLastError());
//--- 退出零结果 - 它表示被认为是不计算的指标
return(false);
}
//--- 以3标引指标缓冲区的值填充部分Senkou_Span_A_Buffer 数组
//--- 如果senkou_span_shift>0,线根据senkou_span_shift柱形的未来方向移动。
if(CopyBuffer(ind_handle,3,-senkou_span_shift,amount,senkou_span_B_buffer)<0)
{
//--- 如果复制失败,显示错误代码
PrintFormat("4.Failed to copy data from the iIchimoku indicator, error code %d",GetLastError());
//--- 退出零结果 - 它表示被认为是不计算的指标
return(false);
}
//--- 以0标引指标缓冲区的值填充部分Senkou_Span_B_Buffer 数组
//--- 当复制Chinkou Span时,我们无需考虑移动,因为Chinkou数据跨度
//--- 已经存储在iIchimoku移动
if(CopyBuffer(ind_handle,4,0,amount,chinkou_span_buffer)<0)
{
//--- 如果复制失败,显示错误代码
PrintFormat("5.Failed to copy data from the iIchimoku indicator, error code %d",GetLastError());
//--- 退出零结果 - 它表示被认为是不计算的指标
return(false);
}
//--- 一切顺利
return(true);
}
//+------------------------------------------------------------------+
//| 指标去初始化函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(handle!=INVALID_HANDLE)
IndicatorRelease(handle);
//--- 删除指标后清空图表
Comment("");
}
|