#property description "The indicator shows the central limit theorem, which states:"
#property description "The sum of a sufficiently large number of weakly dependent random variables, "
#property description "having approximately equal magnitude (none of the summands dominates,"
#property description "or makes a determining contribution to the sum), has a distribution close to normal."
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
//--- 图形结构属性
#property indicator_label1 "Label"
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_color1 clrRoyalBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 5
//--- 输入变量
input int sample_number=10;
//--- 用于绘制分布图的指标缓冲区
double LabelBuffer[];
//--- 订单号计数器
double ticks_counter;
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
void OnInit()
{
//--- 绑定数组和指标缓冲区
SetIndexBuffer(0,LabelBuffer,INDICATOR_DATA);
//--- 从现在到过去转移指标缓冲区
ArraySetAsSeries(LabelBuffer,true);
//--- 初始化随机数生成程序
MathSrand(GetTickCount());
//--- 初始化订单号计数器
ticks_counter=0;
}
//+------------------------------------------------------------------+
//| 自定义指标迭代函数 |
//+------------------------------------------------------------------+
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[])
{
//--- 为零值计数器重置指标缓冲区
if(ticks_counter==0) ArrayInitialize(LabelBuffer,0);
//--- 增加计数器
ticks_counter++;
//--- 我们应该周期性的重置计数器,恢复分配
if(ticks_counter>100)
{
Print("We've reset the indicator values, let's start filling the cells once again");
ticks_counter=0;
}
//--- 得到从0到7三个数字总和的随机值样本
for(int i=0;i<sample_number;i++)
{
//--- 计算单元标引,随机数下跌三个数字的总和
int rand_index=0;
//--- 得到从0到7的三个随机数字
for(int k=0;k<3;k++)
{
//--- 除以7的余数将返回从0到6的值
rand_index+=MathRand()%7;
}
//--- 增加rand_index单元数的值为1
LabelBuffer[rand_index]++;
}
//--- 退出OnCalculate() 处理程序
return(rates_total);
}
|