Methods of Object Binding
对象绑定方法
图形对象Text、Label、Bitmap和Bitmap Label(OBJ_TEXT、OBJ_LABEL、OBJ_BITMAP和OBJ_BITMAPLABEL)可以通过OBJPROP_ANCHOR属性定义9种不同的坐标绑定方式。
| 对象 | ID | X/Y | 宽度/高度 | 日期/价格 | OBJPROP_CORNER | OBJPROP_ANCHOR | OBJPROP_ANGLE |
|---|---|---|---|---|---|---|---|
| Text | OBJ_TEXT | — | — | 是 | — | 是 | 是 |
| Label | OBJ_LABEL | 是 | 是(仅读取) | — | 是 | 是 | 是 |
| Button | OBJ_BUTTON | 是 | 是 | — | 是 | — | — |
| Bitmap | OBJ_BITMAP | — | 是(仅读取) | 是 | — | 是 | — |
| Bitmap Label | OBJ_BITMAP_LABEL | 是 | 是(仅读取) | — | 是 | 是 | — |
| Edit | OBJ_EDIT | 是 | 是 | — | 是 | — | — |
| Rectangle Label | OBJ_RECTANGLE_LABEL | 是 | 是 | — | 是 | — | — |
表中使用的术语如下:
- X/Y – 相对于图表角落的锚点坐标,以像素为单位;
- 宽度/高度 – 对象具有宽度和高度。对于“仅读取”,宽度和高度值仅在对象在图表上渲染时计算;
- 日期/价格 – 锚点坐标使用日期和价格值指定;
- OBJPROP_CORNER – 定义相对于哪个图表角落指定锚点坐标。可以是ENUM_BASE_CORNER枚举中的4个值之一;
- OBJPROP_ANCHOR – 在对象本身中定义锚点,可以是ENUM_ANCHOR_POINT枚举中的9个值之一。坐标以像素为单位,从这一点到选定的图表角落指定;
- OBJPROP_ANGLE – 定义对象的旋转角度,逆时针方向。
可以使用函数ObjectSetInteger(chart_handle, object_name, OBJPROP_ANCHOR, anchor_point_mode)指定所需的变体,其中anchor_point_mode是ENUM_ANCHOR_POINT中的值之一。
ENUM_ANCHOR_POINT
| ID | 描述 |
|---|---|
| ANCHOR_LEFT_UPPER | 位于左上角的锚点 |
| ANCHOR_LEFT | 位于中心的左侧的锚点 |
| ANCHOR_LEFT_LOWER | 位于左下角的锚点 |
| ANCHOR_LOWER | 位于中心下方的锚点 |
| ANCHOR_RIGHT_LOWER | 位于右下角的锚点 |
| ANCHOR_RIGHT | 位于中心的右侧的锚点 |
| ANCHOR_RIGHT_UPPER | 位于右上角的锚点 |
| ANCHOR_UPPER | 位于中心上方的锚点 |
| ANCHOR_CENTER | 严格位于对象中心的锚点 |
OBJ_BUTTON、OBJ_RECTANGLE_LABEL和OBJ_EDIT对象在左上角有一个固定的锚点(ANCHOR_LEFT_UPPER)。
示例:
void OnStart()
{
string text_name="my_OBJ_TEXT_object";
if(ObjectFind(0,text_name)<0)
{
Print("Object ",text_name," not found. Error code = ",GetLastError());
//--- Get the maximal price of the chart
double chart_max_price=ChartGetDouble(0,CHART_PRICE_MAX,0);
//--- Create object Label
ObjectCreate(0,text_name,OBJ_TEXT,0,TimeCurrent(),chart_max_price);
//--- Set color of the text
ObjectSetInteger(0,text_name,OBJPROP_COLOR,clrWhite);
//--- Set background color
ObjectSetInteger(0,text_name,OBJPROP_BGCOLOR,clrGreen);
//--- Set text for the Label object
ObjectSetString(0,text_name,OBJPROP_TEXT,TimeToString(TimeCurrent()));
//--- Set text font
ObjectSetString(0,text_name,OBJPROP_FONT,"Trebuchet MS");
//--- Set font size
ObjectSetInteger(0,text_name,OBJPROP_FONTSIZE,10);
//--- Bind to the upper right corner
ObjectSetInteger(0,text_name,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
//--- Rotate 90 degrees counter-clockwise
ObjectSetDouble(0,text_name,OBJPROP_ANGLE,90);
//--- Forbid the selection of the object by mouse
ObjectSetInteger(0,text_name,OBJPROP_SELECTABLE,false);
//--- redraw object
ChartRedraw(0);
}
}图形对象Arrow(OBJ_ARROW)只有2种链接坐标的方式。标识符列在ENUM_ARROW_ANCHOR中。
ENUM_ARROW_ANCHOR
| ID | 描述 |
|---|---|
| ANCHOR_TOP | 位于顶部的锚点 |
| ANCHOR_BOTTOM | 位于底部的锚点 |
示例:
#property strict
void OnStart()
{
//--- Auxiliary arrays
double Ups[],Downs[];
datetime Times[];
//--- Set the arrays as timeseries
ArraySetAsSeries(Ups,true);
ArraySetAsSeries(Downs,true);
ArraySetAsSeries(Times,true);
//--- Set Last error value to Zero
ResetLastError();
//--- Copy timeseries containing the opening bars of the last 1000 ones
int copied=CopyTime(NULL,0,0,1000,Times);
if(copied<=0)
{
Print("Unable to copy the Open Time of the last 1000 bars");
return;
}
//--- prepare the Ups[] and Downs[] arrays
ArrayResize(Ups,copied);
ArrayResize(Downs,copied);
//--- copy the values of iFractals indicator
for(int i=0;i<copied;i++)
{
Ups[i]=iFractals(NULL,0,MODE_UPPER,i);
Downs[i]=iFractals(NULL,0,MODE_LOWER,i);
}
//---
int upcounter=0,downcounter=0; // count there the number of arrows
bool created;// the result of attempts to create an object
for(int i=2;i<copied;i++)// Run through the values of the indicator iFractals
{
if(Ups[i]!=0)// Found the upper fractal
{
if(upcounter<10)// Create no more than 10 "Up" arrows
{
//--- Try to create an "Up" object
created=ObjectCreate(0,string(Times[i]),OBJ_ARROW_THUMB_UP,0,Times[i],Ups[i]);
if(created)// If set up - let's make tuning for it
{
//--- Point anchor is below in order not to cover bar
ObjectSetInteger(0,string(Times[i]),OBJPROP_ANCHOR,ANCHOR_BOTTOM);
//--- Final touch - painted
ObjectSetInteger(0,string(Times[i]),OBJPROP_COLOR,clrBlue);
upcounter++;
}
}
}
if(Downs[i]!=0)// Found a lower fractal
{
if(downcounter<10)// Create no more than 10 arrows "Down"
{
//--- Try to create an object "Down"
created=ObjectCreate(0,string(Times[i]),OBJ_ARROW_THUMB_DOWN,0,Times[i],Downs[i]);
if(created)// If set up - let's make tuning for it
{
//--- Point anchor is above in order not to cover bar
ObjectSetInteger(0,string(Times[i]),OBJPROP_ANCHOR,ANCHOR_TOP);
//--- Final touch - painted
ObjectSetInteger(0,string(Times[i]),OBJPROP_COLOR,clrRed);
downcounter++;
}
}
}
}
}脚本执行后,图表将像此图所示。

最后更新于