Send Email - but ignore pending orders
-
I am writing an EA that does nothing but send me an email whenever an order is opened or closed.
I am sooo close. The final step is to make it ignore pending trades.
https://fxdreema.com/shared/OJR7NCPTd
Just testing/working on the buy trades for now, I'll do the sell trades next.
It works fine when I open/close a normal trade.
It also works when I open a pending order (ie: I do NOT get an email). But when I delete the pending trade, it sends me an email (but it should not).
I'm comfortable tweaking the MQL code if I know where/what to tweak. I'm also making adjustments in the MQL so it shows me a nice date/time format and closed trade profit in pips (instead of dollars). But I wasn't sure where or how to tell it "not pending orders".
-Scott
-
you are working with "trade" - trade is result of order (order can be created, modified, deleted, etc.)
you can try to work with block "Order created" (in this block are separeted settings for pendings) ... -
Using "Order created" didn't work either - I still couldn't figure out how to filter out the pending trades.
But that's OK because I figured out a workaround by modifying the source code.
Below I'm going to share what I did so anyone else can use this too, but first I want to ask, what is the easiest way to make this EA run every 1 second instead of on every tick?
I'm currently using the "onTrade" tab. If I move all the blocks to the "onTimer" tab and set my project settings timer to 1 second, nothing worked at all. So it looks like I need to use the onTrade tab, but the blocks only seem to execute whenever there's an active tick on the chart that the EA is attached to. Can I force it to "run" every second in addition to every tick?
Ok, here's what I did:
https://fxdreema.com/shared/cb2Kkt6Fb
And then I modified the MQ4 file. Find the spot where the trade data is calculated and I did this:
Replace OpenTime code with:
string ic_eventTrade_e_attrOpenTime() { return TimeToString(e_attrOpenTime(), TIME_DATE) + " " + TimeToString(e_attrOpenTime(), TIME_MINUTES); }This makes it a string and includes the date and time in a nice format.
Do the same thing with CloseTime:
string ic_eventTrade_e_attrCloseTime() { return TimeToString(e_attrCloseTime(), TIME_DATE) + " " + TimeToString(e_attrCloseTime(), TIME_MINUTES); }I wanted the PROFIT for closed trades reported as pips instead of dollars, so I replaced the Profit section with this:
double ic_eventTrade_e_attrProfit() { int pipMult = 10000; if(StringFind(OrderSymbol(),"JPY",0) != -1) pipMult = 100; double orderpipsprofit; if(OrderType()==0) { orderpipsprofit = (OrderClosePrice() - OrderOpenPrice()) * pipMult; } if(OrderType()==1) { orderpipsprofit = (OrderOpenPrice() - OrderClosePrice()) * pipMult; } return(orderpipsprofit); }And then I wanted to NOT send emails for pending trades, so I went to all four of the SendEmail commands and added the OrderType() if statement:
if(OrderType()==0 || OrderType()==1) { SendMail(EmailSubject, EmailMessage+"\n"+EmailDataRows); }So the emails are nicely formatted and look like this:
__We have closed our BUY trade as follows:
Symbol: GBPCAD
Opened At: 1.6352
Closed At: 1.6347
Open Time: 2016.10.24 22:07
Close Time: 2016.10.24 22:15
Profit: -5 pips
[/quote:1rw2qf9v]I still want to make it run every 1 second but if I can't figure that out, I'll call this "good enough" for now.

-Scott
-
__But when I delete the pending trade[/quote:1cs8v4qm]
What do you mean by pending trade? Trade closed should pass if running trade is closed, not if pending order is deleted.
In the second shared project you are using some pink blocks, but they require "For each..." block in order to work correctly. If you don't use such block, these pink blocks could do unexpected things
https://fxdreema.com/demo/mt4-loop-how-it-worksI will not make those times to be strings. Time is only string when printed, for the eyes of the person, but in the code it should be integer value

-
__What do you mean by pending trade? Trade closed should pass if running trade is closed, not if pending order is deleted.[/quote:3gkxmgo2]
That's what I thought initially, but using "Trade created" and "Trade closed" was still causing emails to be sent when pending orders were created or deleted. I've spent the last day trying to figure out how to make it NOT execute on pending orders but couldn't figure it out.
I'll redo this and get rid of the pink blocks and just use separate Trade created and Trade closed blocks for buys and sells. Oops!
__I will not make those times to be strings. Time is only string when printed, for the eyes of the person, but in the code it should be integer value :)[/quote:3gkxmgo2]
That was the point - I wanted them in a nice "pretty" format in the email message, which means it needs to be a string, right? So it can be printed like 2016.10.24 05:34 in the email.
-Scott
-
nice work

I am not tried this, but you can, this way working in OnTimer:
https://fxdreema.com/shared/xdSGKe0ad -
Maybe you are right
I added extra checks in the purple blocks, it should work fine now -
__Maybe you are right
I added extra checks in the purple blocks, it should work fine now[/quote:8dwo361u]YES YES YES!!! Pending orders are ignored now, yahooooo.
Thank you! The only item left on my wishlist is to make it run every 1 second instead of waiting for a tick. I would create a new share link but looks like I'm out of shares on my basic account. I scrapped the pink blocks!__nice work

I am not tried this, but you can, this way working in OnTimer:
https://fxdreema.com/shared/xdSGKe0ad[/quote:8dwo361u]I might give this a try when I have a clear head tomorrow - what you did here definitely makes sense. Thank you very much for the idea!
-Scott
-
Try the Send e-mail block again

-
__Try the Send e-mail block again :)[/quote:mkdv4up5]
I think it's still waiting for a tick before "Trade created" or "Trade closed" gets triggered, so it doesn't get to the SendEmail block.
-
__
I might give this a try when I have a clear head tomorrow - what you did here definitely makes sense. Thank you very much for the idea!
-Scott[/quote:3m46rfok]
the first one is with bad connected blocks, this one is closer to reality:
https://fxdreema.com/shared/2t3NZtEIb