Hi everyone,
I am wondering if any code gurus might be able to help point me in the right direction on this one. I am the opposite of a programming professional, but I've spent countless hours on a trading panel that is coming along very nicely, and I'm hitting a couple of bugs that I just can't figure out how to get through. This post addresses the first bug. Sorry in advance for length.
I found and used the code block from this thread to enable me to add indicators from my own custom UI on demand: https://www.mql5.com/en/forum/73391/page3 (post #27 by Kray). I had to make minor tweaks in terms of using Sleep and the keybd commands, and generally, it has been working. (Note as I admit below, I am a hack, so if you think my use of these commands is duplicate in nature and dumb, I am sure you're right -- but when I found a combo that worked I didn't want to mess with it, since it's been brittle for me.)
Mine below:
The issue early on is it was failing. I'd get a "Cannot open file 'path' " error. This was a blocker early on but then I figured out that if I made my indicator filename exactly 15 characters, I was unblocked. This carried on generally successfully for days, maybe even a week or two. But intermittently, I would have an issue where my indicator and path string is was interpreted incorrectly by what I can only determine to be the PostMessageW function. When it failed, it was reporting back extra or otherwise nonsense characters in the filename it's trying to open. So instead of saying "Cannot open file 'MyIndies\MyIndicator.ex4'", it would say "Cannot open file 'MyIndies\MyIndicator€L.ex4.'" So for a good week or two, usually the indicator would load fine, but intermittently my indicator path and name would be interpreted incorrectly. In those cases (perhaps after I'd changed chartframes, or clicked around to other tabs, or been away for awhile), I could usually just reload my template or EA and be back to good. Interestingly, in all cases, my Print statements in the above code block always return consistent results. They are consistent in terms of when the indicator is called successfully as well as when multiple variants of the incorrect filename are interpreted. I can only guess that the error occurs in the PostMessageW command, although I was unable to force it to reproduce the error on demand, and I certainly have no idea as to why.
In the last day or two, suddenly I was always getting the error messages. I discovered through some trial and error that if I reduced my indicator name length from 15 characters to 14, I'm back to good. 15 was very clearly the magic number before, and now the magic number is 14. I don't know why any number would be a magic number, let alone why it has clearly and definitively changed as of a day or two ago. I am totally stuck making sense of any of this and blind to how this Windows API stuff works, and I'm a noob when it comes to working with uchars as well. I've spent hours googling and trying to sort it out but with no success.
If anyone can explain this, help me resolve, or even point me to a resource where I can help myself, I would be most appreciative.
If you've read this far, thank you so much.
FtN
I am wondering if any code gurus might be able to help point me in the right direction on this one. I am the opposite of a programming professional, but I've spent countless hours on a trading panel that is coming along very nicely, and I'm hitting a couple of bugs that I just can't figure out how to get through. This post addresses the first bug. Sorry in advance for length.
I found and used the code block from this thread to enable me to add indicators from my own custom UI on demand: https://www.mql5.com/en/forum/73391/page3 (post #27 by Kray). I had to make minor tweaks in terms of using Sleep and the keybd commands, and generally, it has been working. (Note as I admit below, I am a hack, so if you think my use of these commands is duplicate in nature and dumb, I am sure you're right -- but when I found a combo that worked I didn't want to mess with it, since it's been brittle for me.)
Mine below:
Inserted Code
#import "user32.dll" int GetAncestor(int, int); int RegisterWindowMessageW(string MessageName); // For Start custom indicator int PostMessageW(int hwnd,int msg,int wparam,uchar &Name[]); // For Start custom indicator int FindWindowW(string lpszClass,string lpszWindow); // For Start custom indicator // int keybd_event(int bVk, int bScan, int dwFlags, int dwExtraInfo); // For Start custom indicator #import #define VK_RETURN 13 //ENTER key#import // For Start custom indicator void StartCustomIndicator(int hWnd,string IndicatorName,bool AutomaticallyAcceptDefaults=true) // true = skip the indicator dialog box { Sleep(100); uchar name2[]; StringToCharArray(IndicatorName,name2,0,StringLen(IndicatorName)); Print("IndicatorName="+IndicatorName); Print("ArraySize(name2)="+ArraySize(name2)); for (int i = 0; i < ArraySize(name2); i++){ Print("name2["+i+"]="+name2[i]); } int MessageNumber=RegisterWindowMessageW("MetaTrader4_Internal_Message"); Sleep(100); int r=PostMessageW(hWnd,MessageNumber,15,name2); Sleep(200); if(AutomaticallyAcceptDefaults) { keybd_event(13, 0, 0, 0); int ind_settings = FindWindowW(NULL, "Custom Indicator - "+IndicatorName); PostMessageW(ind_settings,0x100,VK_RETURN,name2); keybd_event(13, 0, 0, 0); } }
The issue early on is it was failing. I'd get a "Cannot open file 'path' " error. This was a blocker early on but then I figured out that if I made my indicator filename exactly 15 characters, I was unblocked. This carried on generally successfully for days, maybe even a week or two. But intermittently, I would have an issue where my indicator and path string is was interpreted incorrectly by what I can only determine to be the PostMessageW function. When it failed, it was reporting back extra or otherwise nonsense characters in the filename it's trying to open. So instead of saying "Cannot open file 'MyIndies\MyIndicator.ex4'", it would say "Cannot open file 'MyIndies\MyIndicator€L.ex4.'" So for a good week or two, usually the indicator would load fine, but intermittently my indicator path and name would be interpreted incorrectly. In those cases (perhaps after I'd changed chartframes, or clicked around to other tabs, or been away for awhile), I could usually just reload my template or EA and be back to good. Interestingly, in all cases, my Print statements in the above code block always return consistent results. They are consistent in terms of when the indicator is called successfully as well as when multiple variants of the incorrect filename are interpreted. I can only guess that the error occurs in the PostMessageW command, although I was unable to force it to reproduce the error on demand, and I certainly have no idea as to why.
In the last day or two, suddenly I was always getting the error messages. I discovered through some trial and error that if I reduced my indicator name length from 15 characters to 14, I'm back to good. 15 was very clearly the magic number before, and now the magic number is 14. I don't know why any number would be a magic number, let alone why it has clearly and definitively changed as of a day or two ago. I am totally stuck making sense of any of this and blind to how this Windows API stuff works, and I'm a noob when it comes to working with uchars as well. I've spent hours googling and trying to sort it out but with no success.
If anyone can explain this, help me resolve, or even point me to a resource where I can help myself, I would be most appreciative.
If you've read this far, thank you so much.
FtN