Want to create an alert/Expert Advisor or Indicator in MT5 if any pending order get executed - indicator

Can someone help me to create an indicator in which if client make Limit orders like Buy Limit, Sell Limit, Buy Stop or Sell Stop and if orders get executed then i should have an alert with some sound or without sound, if any pop up will come that is also fine for me.
I am looking for it from last so many days.
For the reference you can check MQL PDF.
"https://www.mql5.com/files/pdf/mql5.pdf".
Regards,
Abhishek

AFAIK only EAs can do that.
You can start with the following code:
void OnTradeTransaction (const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result ) {
switch(trans.type) {
case TRADE_TRANSACTION_DEAL_ADD:
if(HistoryDealSelect(trans.deal)) {
entry = (ENUM_DEAL_ENTRY)HistoryDealGetInteger(trans.deal, DEAL_ENTRY);
if(entry == DEAL_ENTRY_IN)
onDeal(trans); // An order was opened
}
break;
}
}
void onDeal(const MqlTradeTransaction &trans) {
// Your code goes here, e.x. Sound
PlaySound("news.wav")
}

Related

Media Foundation : Looping video unstable on 6th run

Took current media foundation sample from github (MF_ProtectedPlayback, but could have been one of the others).
Added the following so it loops when it gets to the end :
CPlayer::HandleEvent()
{
case MEEndOfPresentation:
CHECK_HR(hr = OnPresentationEnded(pEvent));
// ADV_SW: Loop.
{
static DWORD dbg_count = 0;
dbg_count++;
char title[100];
sprintf_s(title, "Loop: %d", dbg_count);
SetWindowTextA(m_hwndEvent, title);
}
Play();
break;
}
Also, in CPlayer::StartPlayback
... modified so second play starts from beginning
// Start from beginning
PROPVARIANT varStart = { 0 };
InitPropVariantFromInt64(0, &varStart);
hr = m_pSession->Start(&GUID_NULL, &varStart);
When I play example file http://advance-software.com/misc/ad.mp4 (download & run locally)
It works fine for first 5 loops, then starts breaking up on the 6th.
Anyone know what's up ?
Thanks in advance,
Steve.
Seems you must call
m_pSession->Stop();
before the Start() when looping to ensure stability.
Surprised this doesn't occur internally if a requirement to ensure API robustness but such are the dark arts of Media Foundation :)

How do I correctly create a copy of an array in C or set a reference to one?

so, I'm very new to C, coming from a Java/C# background and I can't quite wrap my head around it so far.
What I'm trying to do is program a microcontroller (Adafruit Feather running an atmega32u4 in this case) to pose as a USB-coupled controller for Nintendo Switch and run automated commands.
The project I'm trying to expand upon is using a struct array of commands like this:
typedef struct {
Buttons_t button;
uint16_t duration; // 1 equals 0.025s on ATmega32u4 => 1s = 40
} command;
static const command loop[] = {
// do stuff
{ NOTHING, 150 },
{ TRIGGERS, 15 }, { NOTHING, 150 },
{ TRIGGERS, 15 }, { NOTHING, 150 },
{ A, 5 }, { NOTHING, 250 }
};
Now initially this was all there was to it, the program would loop through the commands, send a button to the console and "hold" it for the defined period of time. When the program ran to the end of the array, it would simply reset the index and start anew.
Now I'm trying to send different commands to the console, based on a few easy if..else queries. Specifically, the program will start with a day, month and year variable (the date the Switch console is currently set to) and roll days forward individually to get to a set date in the future. To this end, I want to check at every 'step' if the date +1 day is valid as described in this tutorial and based on the result either roll one day, one day and one month or one day, one month and one year forward. Then I want it to end after a set amount of days.
I wrote several arrays of commands to represent the different steps needed for setting up the controller, moving to where it's supposed to loop, rolling a day, a month or a year like this:
static const command setupController[] = {
// Setup controller
...
};
static const command moveToLoop[] = {
// Go into date settings
...
};
static const command rollDay[] = {
//roll to next day
...
};
static const command rollMonth[] = {
//roll to next month
...
};
static const command rollYear[] = {
//roll to next year
...
};
And another array I want to copy those to like this:
#define COMMANDMAXSIZE 100
static command activeCommand[COMMANDMAXSIZE];
I know this is (extremely) wasteful of memory, but I'm definitely not good enough at C to come up with fancier, more conservative solutions yet.
Then I go into my program, which looks like this:
int main(void) {
SetupHardware(); //Irrelevant, because it is exactly like I downloaded it and it works even with the bumbling changes I've made
GlobalInterruptEnable(); //Ditto
RunOnce(setupController);
RunOnce(moveToLoop);
while (daysSkipped != stopDay)
{
if (datevalid((dayOfMonth + 1), month, year)) {
dayOfMonth++;
RunOnce(rollDay);
}
else if (datevalid(1, (month + 1), year)) {
dayOfMonth = 1;
month++;
RunOnce(rollMonth);
}
else if (datevalid(1, 1, (year + 1))) {
dayOfMonth = 1;
month = 1;
year++;
RunOnce(rollYear);
}
daysSkipped++;
}
}
and finally (I swear I'll be done soon), the start of RunOnce looks like this
void RunOnce(command stepsToRun[]) {
memcpy(activeCommand, stepsToRun, sizeof(activeCommand)); //set the setup commands to be active
activeBounds = sizeof(stepsToRun) / sizeof(stepsToRun[0]);
...
Later in the program, the task that translates commands into button presses for the console actually runs one fixed array, so I figured I'd just "mark" the commands to run as active, and only ever run the active array. Only, it doesn't work as expected:
The program runs, sets up the controller, moves to the date settings and indeed starts to roll a date, but then, regardless if the next day is valid or not, it rolls forward a month, then a year and then it gets stuck moving the simulated analog stick upwards and pressing A indefinitely.
I figure the problem lies in my memcpy to overwrite the active array with the steps I want to run next, but I can't think of a way to solve it. I tried writing a function that was supposed to overwrite the active array element by element using a for loop, but this way the controller wouldn't even set itself up correctly and effectively nothing happened. Usually with any kind of output capabilities I'd try to fit in prints at points of interest, but I have virtually no way of getting feedback on my microcontroller.
Any help would be greatly appreciated.
Ignoring that doing a hard copy of the data is incredibly slow and wasteful, it is also incorrect indeed.
memcpy(activeCommand, stepsToRun, sizeof(activeCommand));
Here you need to copy the size of the data you pass on, not the size of the target buffer! Right now you end up copying more data than you have, because all of these declarations static const command rollDay[] etc get a variable size depending on the number of items in the initializer list.
The quick & dirty fix to your immediate problem would be to pass along the size:
void RunOnce(size_t size, command stepsToRun[size])
{
memcpy(activeCommand, stepsToRun, size);
and then call this function with RunOnce(sizeof rollDay, rollDay); etc.
The activeBounds = sizeof(stepsToRun) / sizeof(stepsToRun[0]); part is also incorrect but not the immediate reason for the bug. See How to find the 'sizeof' (a pointer pointing to an array)? and What is array to pointer decay? etc.
When you pass array to function it decays to a pointer.
RunOnce(rollYear);
Thus
void RunOnce(command stepsToRun[]) {
memcpy(activeCommand, stepsToRun, sizeof(activeCommand)); //set the setup commands to be active
activeBounds = sizeof(stepsToRun) / sizeof(stepsToRun[0]);
}
sizeof(stepsToRun) doesn't yield the correct result as you expected, since it is now sizeof(pointer) in function.
You will have to pass the size of the array as an extra argument to RunOnce function.

Monitor flashing when running a Windows SendInput API

Well, I certainly should go to python since I did several functions of this type, keyboard event and mouse event, but decide to try to learn the windows api.
My goal is to know when button 1 of the mouse is pressed.
I created this file in a very beginner way, it returns in mouseData only 0.
The curious thing is that whenever I run it, it flashes my monitor at short intervals in blinks, but between 1 second with it off. Very strange that, execution is not viable.
Could someone help me understand and try to execute to see if it is only here.
Code:
int main()
{
DWORD mouseData = 0;
MOUSEINPUT tagMouse;
tagMouse.dx = 0;
tagMouse.dy = 0;
tagMouse.mouseData = mouseData;
tagMouse.dwFlags = MOUSEEVENTF_XDOWN;
tagMouse.dwExtraInfo = 0;
INPUT tagInput;
tagInput.type = INPUT_MOUSE;
tagInput.mi = tagMouse;
while (true) {
if (GetAsyncKeyState(VK_DELETE)) break;
SendInput(1, &tagInput, sizeof(INPUT));
printf("KEYWORD: %d\n", mouseData);
Sleep(500);
}
system("pause");
return 0;
}
I can reproduce your reported 'symptoms' - and the effect is really brutal!
Now, while I cannot offer a full explanation, I can offer a fix! You have an uninitialized field in your tagMouse structure (the time member, which is a time-stamp used by the system). Setting this to zero (which tells the system to generate its own time-stamp) fixes the problem. So, just add this line to your other initializer statements:
//...
tagMouse.dwExtraInfo = 0;
tagMouse.time = 0; // Adding this line fixes it!
//...
Note: I, too, would appreciate a fuller explanation; however, an uninitialized field, to me, smells like undefined behaviour! I have tried a variety of other values (i.e. not zero) for the time field but haven't yet found one that works.
The discussion here on devblogs may help. This quote seems relevant:
And who knows what sort of havoc that will create if a program checks
the timestamps and notices that they are either from the future or
have traveled back in time.

C - Statements not executing in order

I have the following code which I am trying to run, however the wait_for_connection() appears to be being run before the preceding 2 lines, and I can't seem to understand why? I really need the two preceding lines to be run before the wait_for_connection() function is called. Even if I enter a sleep(1) before calling the wait_for_connection() function, it is still run before the preceding lines.
My code is as follows:
void wait_for_connection() {
for (int i = 0; i < 10; i++) {
g_print("Checking server...\n");
if (connected == 1) {
g_print("Connected to: %s", selectedServerStr);
break;
}
sleep(1);
}
}
int connect_server(GtkButton *button, gpointer user_data) {
.......
if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selectedServer), &model, &iter)) {
path = gtk_tree_model_get_path(model, &iter);
serverIndex = *gtk_tree_path_get_indices(path);
g_print("Selected Server IP: %s\n", serverIPArray[serverIndex][0]);
gtk_widget_set_sensitive(serverList, FALSE); // These 2 lines needs to be run first
append_to_log("Attemping connection, please wait...", 1);
wait_for_connection(); // This is where the error lies, this runs before the above 2 lines
return 1;
} else {
gtk_widget_set_name(serverBox, "fieldsError");
g_print("No server selected: Aborting...\n");
return -1;
}
}
The line gtk_widget_set_sensitive() is used to disable user interaction of a selection widget, however this doesn't seem to happen until after the wait_for_connection() function has finished printing "Checking server..." 10 times. The value connected is 0, so currently the function just print "Checking server..." 10 times.
Does anyone have any idea as to what may be happening? Any help would be greatly appreciated, thanks.
GUI applications on almost all platforms are event-driven. They need their event-handling functions to execute at regular interval, or the user-interface will seem unresponsive and maybe even seem like it locks up or nothing is happening.
In your wait_for_connection you have a loop which can iterate up to ten times, and each iteration you will do sleep(1) which sleeps a whole second. While this loop is iterating and sleeping, the event-handling will not happen, and as mentioned that will make it seem like your user-interface is unresponsive and doesn't do anything.
You need to modify your code to be event-driven as well, so the "connection" status will be sent as an event that you can handle, or somehow make the polling asynchronous so it happens in parallel to the event-handling.

Exit C function cleanly

I have a couple of functions. Basically a menu where a user can choose 1-n different options, and each of those options have a function associated with them. In this example it's been dumbed down.
What I am trying to determine is the best way to exit a function prematurely. For example, when the user presses enter whilst in the function of a menu option, I want the program to send them back to the menu without running anything else in that function.
In the case below, I simply call showMenu() and place a return statement after it. The only thing is, if the user quits multiple functions there will be a trail of return statements that needs to be unraveled at the end.
Could somebody please show me if there is a more efficient way to achieve this or whether I am on the money.
void showMenu()
{
//Display menu
//Prompt user for menu option
//Run function of appropriate menu option
runSelectedFunction();
}
void runSelectedFunction()
{
//Get user input for the function and validate
//Check if the user input was only a '\n' if so show the menu and exit
showMenu();
return;
//Do the stuff that this function is meant to do.
}
Looks good to me. Or - since there are many around that are against having multiple exit points form a single function - you could do:
void func()
{
//get input
if ( checkMenu() )
{
//do the stuff I am meant to do
}
else
{
showMenu();
}
}
so you are avoiding adding a second return to your function. Also you could have the showMenu() call always at the end of the function, depending on your needs
hth
Mario
The best way? In short, don't.
Why?
Although there's nothing technically wrong with it and you'll find it all over the place, it can sometimes lead to headaches when trying to track down bugs or memory leaks in complex code.
Use early returns only when absolutely necessary and even then try to find an alternative first :)
An alternative is to use the following pattern to ensure your function always returns from one place, giving you the opportunity to always free resources (or report errors, etc):
int func(void)
{
int ret = 0;
do
{
if (!allocate_resource())
{
ret = -1;
break;
}
if (!allocate_more_resources())
{
ret = -2;
break;
}
do_stuff();
}
while (0);
free_allocated_resources();
return (ret);
}

Resources