#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#define OBJ_NAME "TestObjectGetDouble" // 对象名称
#define WND 0 // 图表子窗口
#define EXT " (%$)" // 格式化用于显示价格水平的字符串
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 当前图表 ID, 图表交易品种和交易品种小数位数
long chart_id= ChartID();
string symbol = ChartSymbol(chart_id);
int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
//--- 以可见图表的最高和最低价格构建斐波那契水平图形对象
if(!CreateFibo(chart_id))
return;
//---对象水平数
int total=(int)ObjectGetInteger(chart_id, OBJ_NAME, OBJPROP_LEVELS);
double value =0;
double price0=0;
double price1=0;
//--- 锚点价格
price0=ObjectGetDouble(chart_id, OBJ_NAME, OBJPROP_PRICE, 0);
price1=ObjectGetDouble(chart_id, OBJ_NAME, OBJPROP_PRICE, 1);
//--- 在对象水平数的循环中
for(int i=0; i<total; i++)
{
//--- 取得当前水平的值
ResetLastError();
if(!ObjectGetDouble(chart_id, OBJ_NAME, OBJPROP_LEVELVALUE, i, value))
{
Print("ObjectGetDouble() failed. Error ", GetLastError());
return;
}
//--- 获取对象绑定的最大和最小价格以及它们在价格值中的距离
double max=fmax(price0, price1);
double min=fmin(price0, price1);
double range=max-min;
//--- 计算对象当前水平的价格值
double level_price=min+range*value;
//--- 设置水平的颜色,使其在图表的深色和浅色背景上都可见
ObjectSetInteger(chart_id, OBJ_NAME, OBJPROP_LEVELCOLOR, i, clrRed);
//--- 为水平设置格式字符串,以便其价格值与水平值一起显示
string level_text=ObjectGetString(chart_id, OBJ_NAME, OBJPROP_LEVELTEXT, i);
if(StringFind(level_text, EXT)<0)
{
level_text+=EXT;
ObjectSetString(chart_id, OBJ_NAME, OBJPROP_LEVELTEXT, i, level_text);
}
//--- 在日志中输出水平编号及其数据 - 水平值及其价格
PrintFormat("Fibo level [%d] value: %.3f, price: %.*f", i, value, digits, level_price);
}
/*
结果:
Fibo level [0] value: 0.000, price: 0.61989
Fibo level [1] value: 0.236, price: 0.62533
Fibo level [2] value: 0.382, price: 0.62869
Fibo level [3] value: 0.500, price: 0.63140
Fibo level [4] value: 0.618, price: 0.63412
Fibo level [5] value: 1.000, price: 0.64292
Fibo level [6] value: 1.618, price: 0.65715
Fibo level [7] value: 2.618, price: 0.68018
Fibo level [8] value: 4.236, price: 0.71745
*/
}
//+------------------------------------------------------------------+
//| 在指定图表上创建斐波那契水平图形对象 |
//+------------------------------------------------------------------+
bool CreateFibo(const long chart_id)
{
//--- 在图表上从最高到最低的可见价格值绘制斐波那契水平,并获取它们
double price_high=0, price_low=0;
datetime time_high =0, time_low =0;
if(!GetChartExtremums(chart_id, price_high, price_low, time_high, time_low))
return(false);
//--- 在找到的价格/时间坐标上构造斐波那契水平对象
if(!ObjectCreate(chart_id, OBJ_NAME, OBJ_FIBO, WND, time_high, price_high, time_low, price_low))
{
PrintFormat("%s: ObjectCreate() failed. Error %d",__FUNCTION__, GetLastError());
return(false);
}
//--- 一切正常 - 更新图表并返回 “true”
ChartRedraw();
return(true);
}
//+------------------------------------------------------------------+
//| 返回图表上的最高和最低价格及其时间 |
//+------------------------------------------------------------------+
bool GetChartExtremums(const long chart_id, double &price_high, double &price_low, datetime &time_high, datetime &time_low)
{
//--- 重置变量
price_high=price_low=0;
time_high =time_low =0;
//--- 图表交易品种
string symbol = ChartSymbol(chart_id);
//--- 根据第一个可见柱形的编号和图表上的柱形数量计算复制时间序列范围的开始
int first = (int)ChartGetInteger(chart_id, CHART_FIRST_VISIBLE_BAR);
int count = (int)ChartGetInteger(chart_id, CHART_VISIBLE_BARS);
int start = first+1-count;
//--- 要复制时间序列的数组
double array_high[];
double array_low[];
datetime array_time[];
int index;
//--- 将三个时间序列复制到数组中,数量为“count”,从“start”开始
ResetLastError();
if(CopySeries(symbol, PERIOD_CURRENT, start, count, COPY_RATES_TIME|COPY_RATES_HIGH|COPY_RATES_LOW, array_time, array_high, array_low)!=count)
{
PrintFormat("%s: CopySeries() failed. Error %d",__FUNCTION__, GetLastError());
return(false);
}
//--- 在 array_high 数组中查找最高价格的索引
index=ArrayMaximum(array_high);
if(index<0)
{
PrintFormat("%s: ArrayMaximum() failed. Error %d",__FUNCTION__, GetLastError());
return(false);
}
//--- 记住可见图表上的最高价格以及该价格所在的柱形的时间值
price_high=array_high[index];
time_high=array_time[index];
//--- 在array_low数组中查找最低价格的索引
index=ArrayMinimum(array_low);
if(index<0)
{
PrintFormat("%s: ArrayMinimum() failed. Error %d",__FUNCTION__, GetLastError());
return(false);
}
//--- 记住可见图表上的最低价格以及该价格所在的柱形的时间值
price_low=array_low[index];
time_low=array_time[index];
//--- 一切正常
return(true);
}
|