有没有可能看到一个例子,说明如何在一个指标内插入一个文本(独立窗口)?
谢谢
我试过
if(ObjectFind(0,label_name)<0) { Print("Generating a label"); ObjectCreate(0,label_name,OBJ_LABEL,0,0,0); ObjectSetInteger(0,label_name,OBJPROP_XDISTANCE,10); ObjectSetInteger(0,label_name,OBJPROP_YDISTANCE,30); ObjectSetInteger(0,label_name,OBJPROP_COLOR,YellowGreen); ObjectSetString(0,label_name,OBJPROP_TEXT,"This is an example text inside chart window"); ObjectSetString(0,label_name,OBJPROP_FONT,"Arial"); ObjectSetInteger(0,label_name,OBJPROP_FONTSIZE,14); ObjectSetInteger(0,label_name,OBJPROP_SELECTABLE,false); ChartRedraw(0); } int window=-1; if((ENUM_PROGRAM_TYPE)MQL5InfoInteger(MQL5_PROGRAM_TYPE)==PROGRAM_INDICATOR) { window=ChartWindowFind(); Print(window); } if(ObjectFind(window,label2_name)<0) { Print("Generating a label2"); ObjectCreate(window,label2_name,OBJ_LABEL,0,0,0); Print(GetLastError()); ObjectSetInteger(window,label2_name,OBJPROP_XDISTANCE,10); ObjectSetInteger(window,label2_name,OBJPROP_YDISTANCE,30); ObjectSetInteger(window,label2_name,OBJPROP_COLOR,YellowGreen); ObjectSetString(window,label2_name,OBJPROP_TEXT,"This is an example text inside indicator, cool, huh?"); ObjectSetString(window,label2_name,OBJPROP_FONT,"Arial"); ObjectSetInteger(window,label2_name,OBJPROP_FONTSIZE,14); ObjectSetInteger(window,label2_name,OBJPROP_SELECTABLE,false); ChartRedraw(window); }
但
ObjectCreate(window,label2_name,OBJ_LABEL,0,0,0);
返回错误4101
有什么想法吗?(MT5 build 237)
http://clip2net.com/clip/m19204/1263295278-clip-38kb.png
只在图表中,而不是在指标中。
如果有人要我帮忙,我是否可以上传指标的代码。
我很乐意接受建议和修改.....
谢谢Investeo
我尝试了不同的配置,结果发现在MT5的指标窗口内不允许有任何对象。
见
https://www.mql5.com/en/docs/index
自定义指标部分说。
自定义指标是在已经集成到客户端的指标之外独立编写的技术指标。与内置指标一样,它们不能自动交易,只用于实现分析功能。
自定义指标不能使用函数来处理图表 或图表对象 。自定义指标存储在terminal_directory/MQL5/Indicators 中。
因此,将文本放入指标窗口的唯一方法是通过设置其 INDICATOR_SHORTNAME 属性。
比如说
IndicatorSetString(INDICATOR_SHORTNAME, "Hello "+label2_name+"...")。
如果我说错了,请纠正我。
最好的问候
我请你帮个忙:你能不能写一个例子?
谢谢
这在目前是可能的。请试一下。
Rosh,我试着用build 238来实现它,但还是没有成功。这个代码片断
#property indicator_separate_window #property indicator_buffers 1 #property indicator_plots 1 #property indicator_minimum 0 #property indicator_maximum 100 string label_name="mainwindow_label"; string label2_name="indicator_label"; int window=-1; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping //--- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ 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[]) { //--- //--- return value of prev_calculated for next call if(ObjectFind(0,label_name)<0) { Print("Generating a label"); ObjectCreate(0,label_name,OBJ_LABEL,0,0,0); ObjectSetInteger(0,label_name,OBJPROP_XDISTANCE,10); ObjectSetInteger(0,label_name,OBJPROP_YDISTANCE,30); ObjectSetInteger(0,label_name,OBJPROP_COLOR,YellowGreen); ObjectSetString(0,label_name,OBJPROP_TEXT,"This is an example text inside chart window"); ObjectSetString(0,label_name,OBJPROP_FONT,"Arial"); ObjectSetInteger(0,label_name,OBJPROP_FONTSIZE,14); ObjectSetInteger(0,label_name,OBJPROP_SELECTABLE,false); ChartRedraw(0); } if((ENUM_PROGRAM_TYPE)MQL5InfoInteger(MQL5_PROGRAM_TYPE)==PROGRAM_INDICATOR) { window=ChartWindowFind(); Print(window); } if(ObjectFind(window,label2_name)<0) { Print("Generating a label2"); ObjectCreate(window,label2_name,OBJ_LABEL,0,0,0); Print(GetLastError()); ObjectSetInteger(window,label2_name,OBJPROP_XDISTANCE,10); ObjectSetInteger(window,label2_name,OBJPROP_YDISTANCE,10); ObjectSetInteger(window,label2_name,OBJPROP_COLOR,YellowGreen); ObjectSetString(window,label2_name,OBJPROP_TEXT,"This is an example text inside indicator, cool, huh?"); ObjectSetString(window,label2_name,OBJPROP_FONT,"Arial"); ObjectSetInteger(window,label2_name,OBJPROP_FONTSIZE,14); ObjectSetInteger(window,label2_name,OBJPROP_SELECTABLE,false); ChartRedraw(window); } IndicatorSetString(INDICATOR_SHORTNAME,"Hello "+label2_name+"..."); return(rates_total); } void OnDeinit(const int reason) { if(ObjectFind(0,label_name)>=0) ObjectDelete(0,label_name); if(ObjectFind(window,label2_name)>=0) ObjectDelete(window,label2_name); }
试图把两个对象(标签),一个放在主图表窗口,第二个放在指标窗口。
if((ENUM_PROGRAM_TYPE)MQL5InfoInteger(MQL5_PROGRAM_TYPE)==PROGRAM_INDICATOR) { window=ChartWindowFind(); Print(window); }
窗口变量被设置为1,用于指标窗口,当我运行指标时,我看到的是
而
Print("Generating a label2"); ObjectCreate(window,label2_name,OBJ_LABEL,0,0,0); Print(GetLastError());
GetLastError返回值'4101'。
试试我的指标样本
//+------------------------------------------------------------------+ //| testObjectsInIndicator.mq5 | //| Copyright 2009, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2009, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 2 #property indicator_plots 1 //---- plot Bollinger #property indicator_label1 "BB High; BB Low" #property indicator_type1 DRAW_FILLING #property indicator_color1 Magenta,LimeGreen #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- input parameters input int BBperiod=20; // период BBands input int length=30; // длина канала input double width=2.0; // ширина BBands input color channelColor=Turquoise; // цвет канала //--- indicator buffers double BollingerBuffer1[]; double BollingerBuffer2[]; int handleBBands; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,BollingerBuffer1,INDICATOR_DATA); SetIndexBuffer(1,BollingerBuffer2,INDICATOR_DATA); handleBBands=iBands(NULL,0,BBperiod,0,width,PRICE_CLOSE); //--- Print("Function ",__FUNCTION__," Build ",__MQ5BUILD__); return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ 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[]) { //--- static int prevbars=-1; //int bars=Bars(NULL,0); int copied=CopyBuffer(handleBBands,1,0,rates_total,BollingerBuffer1); if(copied<=0) { Print("Copying of indicator values failed"); return(0); } copied=CopyBuffer(handleBBands,2,0,rates_total,BollingerBuffer2); if(copied<=0) { Print("Copying of indicator values failed"); return(0); } if(prevbars!=rates_total) { prevbars=rates_total; ArraySetAsSeries(time,true); SetupChannel(time[length],time[1]); } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void SetupChannel(datetime time1,datetime time2) { static string channelName="Channel"; static string startLine="start"; static string finishLine="finish"; if(ObjectFind(0,channelName)<0) { Print("Channel not found, let's create it"); if(!ObjectCreate(0,channelName,OBJ_STDDEVCHANNEL,0,0,0)) Print("Channel creation failed error =",GetLastError()); else { ObjectSetInteger(0,channelName,OBJPROP_COLOR,channelColor); Print("Set channel beginning at",time1); ObjectSetInteger(0,channelName,OBJPROP_TIME,0,time1); Print("Set channel finish at",time2); bool modified=ObjectSetInteger(0,channelName,OBJPROP_TIME,1,time2); if(!modified) { Print("Can not to set up time coordinates. Error",GetLastError()); } Print("Move channel on background"); ObjectSetInteger(0,channelName,OBJPROP_BACK,true); } } if(ObjectFind(0,startLine)<0) { Print("Линия start не найдена, создадим сами"); if(!ObjectCreate(0,startLine,OBJ_VLINE,0,time1,0)) Print("Не удалось создать линию startLine, error =",GetLastError()); } if(ObjectFind(0,finishLine)<0) { Print("Линия finish не найдена, создадим сами"); if(!ObjectCreate(0,finishLine,OBJ_VLINE,0,time2,0)) Print("Не удалось создать линию finishLine, error =",GetLastError()); } } //+------------------------------------------------------------------+
试试我的指标样本
你的指标工作正常,但它在主图上绘制对象,而不是在独立窗口上(我们希望在独立的指标窗口上绘制对象)。当我用
#property indicator_separate_window
而不是
#property indicator_chart_window
(如你的例子)
并设置
ObjectCreate(1,startLine,OBJ_VLINE,0,time1,0)
我得到以下错误输出
2010.01.14 16:11:40 testObjectOnIndicator (EURUSD,M5) Не удалось создать линию finishLine, error = 4101 2010.01.14 16:11:40 testObjectOnIndicator (EURUSD,M5) Линия finish не найдена, создадим сами 2010.01.14 16:11:40 testObjectOnIndicator (EURUSD,M5) Не удалось создать линию startLine, error = 4101 2010.01.14 16:11:40 testObjectOnIndicator (EURUSD,M5) Линия start не найдена, создадим сами 2010.01.14 16:11:40 testObjectOnIndicator (EURUSD,M5) Channel creation failed error = 4101 2010.01.14 16:11:40 testObjectOnIndicator (EURUSD,M5) Channel not found, let's create it 2010.01.14 16:11:40 testObjectOnIndicator (EURUSD,M5) Function OnInit Build 223
Object Create方法中的第一个参数("1")是ChartWindowFind()中返回的值。
if((ENUM_PROGRAM_TYPE)MQL5InfoInteger(MQL5_PROGRAM_TYPE)==PROGRAM_INDICATOR) { window=ChartWindowFind(); }
):
按照这里的建议:https://www.mql5.com/en/docs/chart_operations/chartwindowfind
- www.mql5.com
//| testObjectsInIndicator.mq5 |
//| Copyright 2009, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2009, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 1
//---- plot Bollinger
#property indicator_label1 "BB High; BB Low"
#property indicator_type1 DRAW_FILLING
#property indicator_color1 Magenta,LimeGreen
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- input parameters
input int BBperiod=20; // период BBands
input int length=30; // длина канала
input double width=2.0; // ширина BBands
input color channelColor=Turquoise; // цвет канала
//--- indicator buffers
double BollingerBuffer1[];
double BollingerBuffer2[];
int handleBBands;
//---
int window=1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- get window number
window=ChartWindowFind();
//--- indicator buffers mapping
SetIndexBuffer(0,BollingerBuffer1,INDICATOR_DATA);
SetIndexBuffer(1,BollingerBuffer2,INDICATOR_DATA);
handleBBands=iBands(NULL,0,BBperiod,0,width,PRICE_CLOSE);
//---
Print("Function ",__FUNCTION__," Build ",__MQ5BUILD__);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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[])
{
//---
static int prevbars=-1;
//int bars=Bars(NULL,0);
int copied=CopyBuffer(handleBBands,1,0,rates_total,BollingerBuffer1);
if(copied<=0)
{
Print("Copying of indicator values failed");
return(0);
}
copied=CopyBuffer(handleBBands,2,0,rates_total,BollingerBuffer2);
if(copied<=0)
{
Print("Copying of indicator values failed");
return(0);
}
if(prevbars!=rates_total)
{
prevbars=rates_total;
ArraySetAsSeries(time,true);
SetupChannel(time[length],time[1]);
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void SetupChannel(datetime time1,datetime time2)
{
static string channelName="Channel";
static string startLine="start";
static string finishLine="finish";
if(ObjectFind(0,channelName)<0)
{
Print("Channel not found, let's create it");
if(!ObjectCreate(0,channelName,OBJ_STDDEVCHANNEL,window,0,0))
Print("Channel creation failed error =",GetLastError());
else
{
ObjectSetInteger(0,channelName,OBJPROP_COLOR,channelColor);
Print("Set channel beginning at",time1);
ObjectSetInteger(0,channelName,OBJPROP_TIME,0,time1);
Print("Set channel finish at",time2);
bool modified=ObjectSetInteger(0,channelName,OBJPROP_TIME,1,time2);
if(!modified)
{
Print("Can not to set up time coordinates. Error",GetLastError());
}
Print("Move channel on background");
ObjectSetInteger(0,channelName,OBJPROP_BACK,true);
}
}
if(ObjectFind(0,startLine)<0)
{
Print("Линия start не найдена, создадим сами");
if(!ObjectCreate(0,startLine,OBJ_VLINE,window,time1,0))
Print("Не удалось создать линию startLine, error =",GetLastError());
}
if(ObjectFind(0,finishLine)<0)
{
Print("Линия finish не найдена, создадим сами");
if(!ObjectCreate(0,finishLine,OBJ_VLINE,window,time2,0))
Print("Не удалось создать линию finishLine, error =",GetLastError());
}
}
//+------------------------------------------------------------------+
有没有可能看到一个例子,说明如何在一个指标内插入一个文本(独立窗口)?
谢谢