Download MetaTrader 5

how to attach an EA to a chart programmatic

To add comments, please log in or register
Publish your article. We will translate it into other languages!
Anvar
31
Anvar 2011.02.02 05:13 

Hi i need help. Can you please show to me how to attach EA to chart programmatic.

Thanks

qjol
3243
qjol 2011.02.02 11:01  

the only way that i found make a template & use PostMessageA(handle, WM_COMMAND, 34800, 0); // 34800 = the first template 34801 = the second 34802 = the third & so on

qjol
3243
qjol 2011.02.02 15:17  
Anvar
31
Anvar 2011.02.02 16:22  

Hi qjol,

Thanks for your help. I am trying to do this. It is working fine but EA not attaching to the chart

ChartWindow("USD/JPY");

//+------------------------------------------------------------------+

//| Open a new chart |

//+------------------------------------------------------------------+

int ChartWindow(string SymbolName)

{

int hFile, SymbolsTotal, hTerminal, hWnd;


hFile = FileOpenHistory("symbols.sel", FILE_BIN|FILE_READ);

if(hFile < 0) return(-1);


SymbolsTotal = (FileSize(hFile) - 4) / 128;

FileSeek(hFile, 4, SEEK_SET);


hTerminal = GetAncestor(WindowHandle(Symbol(), Period()), 2);

hWnd = GetDlgItem(hTerminal, 0xE81C);

hWnd = GetDlgItem(hWnd, 0x50);

hWnd = GetDlgItem(hWnd, 0x8A71);


PostMessageA(hWnd, WM_KEYDOWN, VK_HOME, 0);


for(int i = 0; i < SymbolsTotal; i++)

{

if(FileReadString(hFile, 12) == SymbolName)

{

PostMessageA(hTerminal, WM_COMMAND, 33160, 0);

int MessageNumber = RegisterWindowMessageA("MetaTrader4_Internal_Message");

PostMessageA(hWnd, MessageNumber, 14, "myEA");

return(0);

}

PostMessageA(hWnd, WM_KEYDOWN, VK_DOWN, 0);

FileSeek(hFile, 116, SEEK_CUR);

}


FileClose(hFile);


return(-1);

}


//+------------------------------------------------------------------+

qjol
3243
qjol 2011.02.02 16:43  
PostMessageA(hTerminal, WM_COMMAND, 33160, 0);
Sleep(500); // u should give some time to open the chart
int MessageNumber = RegisterWindowMessageA("MetaTrader4_Internal_Message");
PostMessageA(hTerminal, MessageNumber, 14, "myEA");

Anvar
31
Anvar 2011.02.02 18:23  
Thanks, It is not working for me. Did you test it by your self?
qjol
3243
qjol 2011.02.02 19:34  

yes

the new chart opens by u ?

cesar zambrano
43
cesar zambrano 2011.02.02 20:08  

hi

anvarn and gjol

good day

i have this EA, but does not attach with my gbp/usd chart, my I help please

thanks

//+------------------------------------------------------------------+
//| BreakOutPANCA-EAGLE.mq4 |
//| hapalkos |
//| 2007.11.20 |
//| ++ modified so that rectangles do not overlay |
//| ++ this makes color selection more versatile |
//| ++ code consolidated |
//+------------------------------------------------------------------+
#property copyright "hapalkos"
#property link ""
#property indicator_chart_window
//----
extern int NumberOfDays=50;
extern string periodBegin ="00:00";
extern string periodEnd ="05:30";
extern string BoxEnd ="23:00";
extern int BoxBreakOut_Offset=10;
extern color BoxHLColor =MidnightBlue;
extern color BoxBreakOutColor =LimeGreen;
extern color BoxPeriodColor =OrangeRed;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void init()
{
DeleteObjects();
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
void deinit()
{
DeleteObjects();
return(0);
}
//+------------------------------------------------------------------+
//| Remove all Rectangles |
//+------------------------------------------------------------------+
void DeleteObjects()
{
ObjectsDeleteAll(0,OBJ_RECTANGLE);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
void start()
{
datetime dtTradeDate=TimeCurrent();
for(int i=0; i<NumberOfDays; i++) {
//----
DrawObjects(dtTradeDate, "BoxHL " + TimeToStr(dtTradeDate,TIME_DATE), periodBegin, periodEnd, BoxEnd, BoxHLColor, 0, 1);
DrawObjects(dtTradeDate, "BoxBreakOut_High " + TimeToStr(dtTradeDate,TIME_DATE), periodBegin, periodEnd, BoxEnd, BoxBreakOutColor, BoxBreakOut_Offset,2);
DrawObjects(dtTradeDate, "BoxBreakOut_Low " + TimeToStr(dtTradeDate,TIME_DATE), periodBegin, periodEnd, BoxEnd, BoxBreakOutColor, BoxBreakOut_Offset,3);
DrawObjects(dtTradeDate, "BoxPeriod " + TimeToStr(dtTradeDate,TIME_DATE), periodBegin, periodEnd, periodEnd, BoxPeriodColor, BoxBreakOut_Offset,4);
dtTradeDate=decrementTradeDate(dtTradeDate);
while(TimeDayOfWeek(dtTradeDate) > 5) dtTradeDate=decrementTradeDate(dtTradeDate);
}
}
//+------------------------------------------------------------------+
//| Create Rectangles |
//+------------------------------------------------------------------+
void DrawObjects(datetime dtTradeDate, string sObjName, string sTimeBegin, string sTimeEnd, string sTimeObjEnd, color cObjColor, int iOffSet, int iForm)
{
datetime dtTimeBegin, dtTimeEnd, dtTimeObjEnd;
double dPriceHigh, dPriceLow;
int iBarBegin, iBarEnd;
//----
dtTimeBegin=StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + sTimeBegin);
dtTimeEnd=StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + sTimeEnd);
dtTimeObjEnd=StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + sTimeObjEnd);
//----
iBarBegin=iBarShift(NULL, 0, dtTimeBegin);
iBarEnd=iBarShift(NULL, 0, dtTimeEnd);
dPriceHigh=High[Highest(NULL, 0, MODE_HIGH, iBarBegin-iBarEnd, iBarEnd)];
dPriceLow=Low [Lowest (NULL, 0, MODE_LOW, iBarBegin-iBarEnd, iBarEnd)];
//----
ObjectCreate(sObjName, OBJ_RECTANGLE, 0, 0, 0, 0, 0);
ObjectSet(sObjName, OBJPROP_TIME1, dtTimeBegin);
ObjectSet(sObjName, OBJPROP_TIME2, dtTimeObjEnd);
//---- High-Low Rectangle
if(iForm==1)
{
ObjectSet(sObjName, OBJPROP_PRICE1, dPriceHigh);
ObjectSet(sObjName, OBJPROP_PRICE2, dPriceLow);
ObjectSet(sObjName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(sObjName, OBJPROP_COLOR, cObjColor);
ObjectSet(sObjName, OBJPROP_BACK, True);
}
//---- Upper Rectangle
if(iForm==2)
{
ObjectSet(sObjName, OBJPROP_PRICE1, dPriceHigh);
ObjectSet(sObjName, OBJPROP_PRICE2, dPriceHigh + iOffSet*Point);
ObjectSet(sObjName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(sObjName, OBJPROP_COLOR, cObjColor);
ObjectSet(sObjName, OBJPROP_BACK, True);
}
//---- Lower Rectangle
if(iForm==3)
{
ObjectSet(sObjName, OBJPROP_PRICE1, dPriceLow - iOffSet*Point);
ObjectSet(sObjName, OBJPROP_PRICE2, dPriceLow);
ObjectSet(sObjName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(sObjName, OBJPROP_COLOR, cObjColor);
ObjectSet(sObjName, OBJPROP_BACK, True);
}
//---- Period Rectangle
if(iForm==4)
{
ObjectSet(sObjName, OBJPROP_PRICE1, dPriceHigh + iOffSet*Point);
ObjectSet(sObjName, OBJPROP_PRICE2, dPriceLow - iOffSet*Point);
ObjectSet(sObjName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(sObjName, OBJPROP_COLOR, cObjColor);
ObjectSet(sObjName, OBJPROP_WIDTH, 2);
ObjectSet(sObjName, OBJPROP_BACK, False);
}
string sObjDesc=StringConcatenate("High: ",dPriceHigh," Low: ", dPriceLow, " OffSet: ",iOffSet);
ObjectSetText(sObjName, sObjDesc,10,"Times New Roman",Black);
}
//+------------------------------------------------------------------+
//| Decrement Date to draw objects in the past |
//+------------------------------------------------------------------+
datetime decrementTradeDate (datetime dtTimeDate)
{
int iTimeYear=TimeYear(dtTimeDate);
int iTimeMonth=TimeMonth(dtTimeDate);
int iTimeDay=TimeDay(dtTimeDate);
int iTimeHour=TimeHour(dtTimeDate);
int iTimeMinute=TimeMinute(dtTimeDate);
//----
iTimeDay--;
if (iTimeDay==0)
{
iTimeMonth--;
if (iTimeMonth==0)
{
iTimeYear--;
iTimeMonth=12;
}
// Thirty days hath September...
if (iTimeMonth==4 || iTimeMonth==6 || iTimeMonth==9 || iTimeMonth==11) iTimeDay=30;
// ...all the rest have thirty-one...
if (iTimeMonth==1 || iTimeMonth==3 || iTimeMonth==5 || iTimeMonth==7 || iTimeMonth==8 || iTimeMonth==10 || iTimeMonth==12) iTimeDay=31;
// ...except...
if (iTimeMonth==2) if (MathMod(iTimeYear, 4)==0) iTimeDay=29; else iTimeDay=28;
}
return(StrToTime(iTimeYear + "." + iTimeMonth + "." + iTimeDay + " " + iTimeHour + ":" + iTimeMinute));
}
//+------------------------------------------------------------------+

Anvar
31
Anvar 2011.02.02 20:48  
qjol:

yes

the new chart opens by u ?

Yes, chart opening but without EA.

When I use Sleep(500); terminal crashing. May I send to you my all code, you can check it and you will see all code.

Anvar
31
Anvar 2011.02.02 21:27  
cesarz18:

hi

anvarn and gjol

good day

i have this EA, but does not attach with my gbp/usd chart, my I help please

thanks

//+------------------------------------------------------------------+
//| BreakOutPANCA-EAGLE.mq4 |
//| hapalkos |
//| 2007.11.20 |
//| ++ modified so that rectangles do not overlay |
//| ++ this makes color selection more versatile |
//| ++ code consolidated |
//+------------------------------------------------------------------+
#property copyright "hapalkos"
#property link ""
#property indicator_chart_window
//----
extern int NumberOfDays=50;
extern string periodBegin ="00:00";
extern string periodEnd ="05:30";
extern string BoxEnd ="23:00";
extern int BoxBreakOut_Offset=10;
extern color BoxHLColor =MidnightBlue;
extern color BoxBreakOutColor =LimeGreen;
extern color BoxPeriodColor =OrangeRed;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
void init()
{
DeleteObjects();
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
void deinit()
{
DeleteObjects();
return(0);
}
//+------------------------------------------------------------------+
//| Remove all Rectangles |
//+------------------------------------------------------------------+
void DeleteObjects()
{
ObjectsDeleteAll(0,OBJ_RECTANGLE);
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
void start()
{
datetime dtTradeDate=TimeCurrent();
for(int i=0; i<NumberOfDays; i++) {
//----
DrawObjects(dtTradeDate, "BoxHL " + TimeToStr(dtTradeDate,TIME_DATE), periodBegin, periodEnd, BoxEnd, BoxHLColor, 0, 1);
DrawObjects(dtTradeDate, "BoxBreakOut_High " + TimeToStr(dtTradeDate,TIME_DATE), periodBegin, periodEnd, BoxEnd, BoxBreakOutColor, BoxBreakOut_Offset,2);
DrawObjects(dtTradeDate, "BoxBreakOut_Low " + TimeToStr(dtTradeDate,TIME_DATE), periodBegin, periodEnd, BoxEnd, BoxBreakOutColor, BoxBreakOut_Offset,3);
DrawObjects(dtTradeDate, "BoxPeriod " + TimeToStr(dtTradeDate,TIME_DATE), periodBegin, periodEnd, periodEnd, BoxPeriodColor, BoxBreakOut_Offset,4);
dtTradeDate=decrementTradeDate(dtTradeDate);
while(TimeDayOfWeek(dtTradeDate) > 5) dtTradeDate=decrementTradeDate(dtTradeDate);
}
}
//+------------------------------------------------------------------+
//| Create Rectangles |
//+------------------------------------------------------------------+
void DrawObjects(datetime dtTradeDate, string sObjName, string sTimeBegin, string sTimeEnd, string sTimeObjEnd, color cObjColor, int iOffSet, int iForm)
{
datetime dtTimeBegin, dtTimeEnd, dtTimeObjEnd;
double dPriceHigh, dPriceLow;
int iBarBegin, iBarEnd;
//----
dtTimeBegin=StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + sTimeBegin);
dtTimeEnd=StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + sTimeEnd);
dtTimeObjEnd=StrToTime(TimeToStr(dtTradeDate, TIME_DATE) + " " + sTimeObjEnd);
//----
iBarBegin=iBarShift(NULL, 0, dtTimeBegin);
iBarEnd=iBarShift(NULL, 0, dtTimeEnd);
dPriceHigh=High[Highest(NULL, 0, MODE_HIGH, iBarBegin-iBarEnd, iBarEnd)];
dPriceLow=Low [Lowest (NULL, 0, MODE_LOW, iBarBegin-iBarEnd, iBarEnd)];
//----
ObjectCreate(sObjName, OBJ_RECTANGLE, 0, 0, 0, 0, 0);
ObjectSet(sObjName, OBJPROP_TIME1, dtTimeBegin);
ObjectSet(sObjName, OBJPROP_TIME2, dtTimeObjEnd);
//---- High-Low Rectangle
if(iForm==1)
{
ObjectSet(sObjName, OBJPROP_PRICE1, dPriceHigh);
ObjectSet(sObjName, OBJPROP_PRICE2, dPriceLow);
ObjectSet(sObjName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(sObjName, OBJPROP_COLOR, cObjColor);
ObjectSet(sObjName, OBJPROP_BACK, True);
}
//---- Upper Rectangle
if(iForm==2)
{
ObjectSet(sObjName, OBJPROP_PRICE1, dPriceHigh);
ObjectSet(sObjName, OBJPROP_PRICE2, dPriceHigh + iOffSet*Point);
ObjectSet(sObjName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(sObjName, OBJPROP_COLOR, cObjColor);
ObjectSet(sObjName, OBJPROP_BACK, True);
}
//---- Lower Rectangle
if(iForm==3)
{
ObjectSet(sObjName, OBJPROP_PRICE1, dPriceLow - iOffSet*Point);
ObjectSet(sObjName, OBJPROP_PRICE2, dPriceLow);
ObjectSet(sObjName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(sObjName, OBJPROP_COLOR, cObjColor);
ObjectSet(sObjName, OBJPROP_BACK, True);
}
//---- Period Rectangle
if(iForm==4)
{
ObjectSet(sObjName, OBJPROP_PRICE1, dPriceHigh + iOffSet*Point);
ObjectSet(sObjName, OBJPROP_PRICE2, dPriceLow - iOffSet*Point);
ObjectSet(sObjName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSet(sObjName, OBJPROP_COLOR, cObjColor);
ObjectSet(sObjName, OBJPROP_WIDTH, 2);
ObjectSet(sObjName, OBJPROP_BACK, False);
}
string sObjDesc=StringConcatenate("High: ",dPriceHigh," Low: ", dPriceLow, " OffSet: ",iOffSet);
ObjectSetText(sObjName, sObjDesc,10,"Times New Roman",Black);
}
//+------------------------------------------------------------------+
//| Decrement Date to draw objects in the past |
//+------------------------------------------------------------------+
datetime decrementTradeDate (datetime dtTimeDate)
{
int iTimeYear=TimeYear(dtTimeDate);
int iTimeMonth=TimeMonth(dtTimeDate);
int iTimeDay=TimeDay(dtTimeDate);
int iTimeHour=TimeHour(dtTimeDate);
int iTimeMinute=TimeMinute(dtTimeDate);
//----
iTimeDay--;
if (iTimeDay==0)
{
iTimeMonth--;
if (iTimeMonth==0)
{
iTimeYear--;
iTimeMonth=12;
}
// Thirty days hath September...
if (iTimeMonth==4 || iTimeMonth==6 || iTimeMonth==9 || iTimeMonth==11) iTimeDay=30;
// ...all the rest have thirty-one...
if (iTimeMonth==1 || iTimeMonth==3 || iTimeMonth==5 || iTimeMonth==7 || iTimeMonth==8 || iTimeMonth==10 || iTimeMonth==12) iTimeDay=31;
// ...except...
if (iTimeMonth==2) if (MathMod(iTimeYear, 4)==0) iTimeDay=29; else iTimeDay=28;
}
return(StrToTime(iTimeYear + "." + iTimeMonth + "." + iTimeDay + " " + iTimeHour + ":" + iTimeMinute));
}
//+------------------------------------------------------------------+

How about Expert Adviser settings. To check settings go tools-options-ExpertAdviser
cesar zambrano
43
cesar zambrano 2011.02.02 21:40  


Hi anvarb

I setting this configuration but does not work

enable expert advisor, enable

disable expert when the account has been changed, disable
disable expert when the profile has been changes, disable

allow live tarding, enable
ask manual confirmation, disable

alloe dll import, enable
confirm dll funtion calls, enable
allow external expert imports, enable

12
To add comments, please log in or register