MQL questions

garyfritz

MQL questions

Post by garyfritz »

Time to demonstrate my lack of clue with Empty4 and MQL... (The Coders forum says "Proficient, clueless - doesn't matter. Just talk." I'm fulfilling the "clueless" mandate. :))

Empty4 doesn't support entering OCO orders. You can enter a Pending Order with attached TP/SL, and the TP/SL are effectively OCO. But I want to be able to enter two POs above & below the current price, each with their TP/SL, and have the two POs be OCO.

So I looked around a bit and found some OCO scripts, and modified one slightly for my particular needs. (So I don't take blame or credit for most of the code in this thing.) But I'm running into a few problems:

* It passes arrow color values to OrderSend, which draws arrows &etc on the chart. Fine. But it doesn't clean up after itself. When the buy stop got hit, the signal arrows & line & various other bits get left on the chart. How can I fix that, without having to delete the objects manually? The original code didn't pass arrow color to the OrderClose / OrderDelete functions, but it didn't improve when I added them.

* Sometimes I want to cancel my orders. I don't see a good way to do that. I added a deinit() to the script, thinking I could just remove the script and let the deinit cancel the orders. But that had the unfortunate result of closing the order immediately after hitting the buy/sell stop. Whoops. Plus (after removing the deinit) when I remove the script, it just starts flashing the "OCO script running" message faster. If I remove it again it kills the script, but there's no deinit() to cancel the orders. What's a good way to do this without having to do everything manually?

Is there a better way to handle this for manually-placed orders? Or does somebody have a better script? I haven't been able to spend much time learning MQL yet so I'm not quite clueful enough to write a proper EA for this.

Thanks,
Gary
You do not have the required permissions to view the files attached to this post.
User avatar
gaheitman
Trader
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Re: MQL questions

Post by gaheitman »

garyfritz wrote:Time to demonstrate my lack of clue with Empty4 and MQL... (The Coders forum says "Proficient, clueless - doesn't matter. Just talk." I'm fulfilling the "clueless" mandate. :))

Empty4 doesn't support entering OCO orders. You can enter a Pending Order with attached TP/SL, and the TP/SL are effectively OCO. But I want to be able to enter two POs above & below the current price, each with their TP/SL, and have the two POs be OCO.

So I looked around a bit and found some OCO scripts, and modified one slightly for my particular needs. (So I don't take blame or credit for most of the code in this thing.) But I'm running into a few problems:

* It passes arrow color values to OrderSend, which draws arrows &etc on the chart. Fine. But it doesn't clean up after itself. When the buy stop got hit, the signal arrows & line & various other bits get left on the chart. How can I fix that, without having to delete the objects manually? The original code didn't pass arrow color to the OrderClose / OrderDelete functions, but it didn't improve when I added them.

* Sometimes I want to cancel my orders. I don't see a good way to do that. I added a deinit() to the script, thinking I could just remove the script and let the deinit cancel the orders. But that had the unfortunate result of closing the order immediately after hitting the buy/sell stop. Whoops. Plus (after removing the deinit) when I remove the script, it just starts flashing the "OCO script running" message faster. If I remove it again it kills the script, but there's no deinit() to cancel the orders. What's a good way to do this without having to do everything manually?

Is there a better way to handle this for manually-placed orders? Or does somebody have a better script? I haven't been able to spend much time learning MQL yet so I'm not quite clueful enough to write a proper EA for this.

Thanks,
Gary
A quick answer....

Add the following function:

Code: Select all

void DeleteArrows(string ticket) {

   int z = ObjectsTotal()-1;
   for (int y=z;y>=0;y--) {
      if (StringFind(ObjectName(y),ticket) >= 0)
         ObjectDelete(ObjectName(y));
   }
}
And then call it after the "HideLabel() call in the main loop like so:

Code: Select all

      DeleteArrows(bt);
      DeleteArrows(st);
George
User avatar
gaheitman
Trader
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Re: MQL questions

Post by gaheitman »

That just cleaned up the arrows after a successful order was entered. Unfortunately, you can only have one script running on a chart at a time, so any script to close orders would have to interrupt the current script. You could have the script create a text object on the chart that says "DELETE ME TO CANCEL ORDERS" ( :D ) or maybe something else, and have the script look to see if the object exists. If it doesn't, then the script would close nicely.

Or, you could scan for keystrokes using GetAsyncKeyState(int nVirtKey) from user32.dll and if you press whatever combination you are looking for you exit the script closing the orders.

I'd recommend throwing it out and writing an ea. :D

How important is this to you?

George
User avatar
gaheitman
Trader
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Re: MQL questions

Post by gaheitman »

OK, I was curious, so...

The attached OCO script will work as before, but if the user presses ALT-X, it will close the pending orders and exit.

Code is "commented", just search for "gah".

IMPORTANT: For testing purposes, I added code to enter the trades for me, right above and below market. You'll want to remove those lines after you are done testing.

I still like an EA for this. Perhaps with a script to enter the orders (assuming you can't program that logic as well) with paired MagicNumbers and the EA would just manage them as they appear.

George
You do not have the required permissions to view the files attached to this post.
garyfritz

Re: MQL questions

Post by garyfritz »

George, you rock! Thanks for your help.

That seems to meet my needs fine for now. I'll let you know if I run into any issues.

Yes, this is important to me (I've been trading it in various flavors on my Tradestation account for almost a year -- manually, since I don't trust TS's auto-execution), and yes I'd love an EA for it. I've been swamped with work, holidays, etc for the last month+, and I've been spending all my productive time pontificating about position sizing :lol:, so I haven't dug into writing an EA yet. Diving into Steve's multi-thousand-line shell code is a bit daunting, but I don't know if any little EA I wrote, without all that error checking &etc, would be reliable.

The EA I want is really incredibly simple:
* Run the system logic once per day, generally at 5:30pm ET = 00:30 GMT, Sun-Thu.
* Calculate D1 ADR = EMA(H-L,N), i.e. the N-day EMA of daily H - daily L. I assume the D1 day is 5pm-5pm. (In Tradestation I run this with at least a year of data on the chart, because N is usually 250. I seed the EMA with the N/2-day average of H-L, so the xaverage is fully stabilized. When backtesting I don't start trading until after the N/2-day seeding event.)
* If yesterday's 5pm-5pm H-L range was less than X * ADR, place OCO breakout stops at yesterday's H & L. Long TP is entry + Y * ADR, long SL is entry - Z * (yesterday's H-L), opposite for shorts. If today's price (in the 5:00-5:30 period) is already outside yesterday's 5pm-5pm H-L range, don't place a trade for today.
* Risk K% of the account on each trade. (Guess what K stands for. :mrgreen:)
* Next day at 4:30pm ET, close any open positions, cancel yesterday's orders, rinse and repeat.

I like to run the chart on M30 but it doesn't affect the logic. It would be very nice if it could be restarted in the middle of the day without interrupting the day's orders -- not sure how that usually gets handled in Empty4. It would be terrific if instead of two OCO orders, it simulated OCO by placing only one active order in the market at a time, to avoid double margin charges.

Just in case you're bored... :)
User avatar
gaheitman
Trader
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Re: MQL questions

Post by gaheitman »

garyfritz wrote:George, you rock! Thanks for your help.

That seems to meet my needs fine for now. I'll let you know if I run into any issues.

Yes, this is important to me (I've been trading it in various flavors on my Tradestation account for almost a year -- manually, since I don't trust TS's auto-execution), and yes I'd love an EA for it. I've been swamped with work, holidays, etc for the last month+, and I've been spending all my productive time pontificating about position sizing :lol:, so I haven't dug into writing an EA yet. Diving into Steve's multi-thousand-line shell code is a bit daunting, but I don't know if any little EA I wrote, without all that error checking &etc, would be reliable.

The EA I want is really incredibly simple:
* Run the system logic once per day, generally at 5:30pm ET = 00:30 GMT, Sun-Thu.
* Calculate D1 ADR = EMA(H-L,N), i.e. the N-day EMA of daily H - daily L. I assume the D1 day is 5pm-5pm. (In Tradestation I run this with at least a year of data on the chart, because N is usually 250. I seed the EMA with the N/2-day average of H-L, so the xaverage is fully stabilized. When backtesting I don't start trading until after the N/2-day seeding event.)
* If yesterday's 5pm-5pm H-L range was less than X * ADR, place OCO breakout stops at yesterday's H & L. Long TP is entry + Y * ADR, long SL is entry - Z * (yesterday's H-L), opposite for shorts. If today's price (in the 5:00-5:30 period) is already outside yesterday's 5pm-5pm H-L range, don't place a trade for today.
* Risk K% of the account on each trade. (Guess what K stands for. :mrgreen:)
* Next day at 4:30pm ET, close any open positions, cancel yesterday's orders, rinse and repeat.

I like to run the chart on M30 but it doesn't affect the logic. It would be very nice if it could be restarted in the middle of the day without interrupting the day's orders -- not sure how that usually gets handled in Empty4. It would be terrific if instead of two OCO orders, it simulated OCO by placing only one active order in the market at a time, to avoid double margin charges.

Just in case you're bored... :)
The complicated part is the ADR calculation, since many of us have Sat/Sun bars. How would you want those dealt with (other than changing brokers :>)?

Also, 5:30 ET is 22:30 GMT, isn't it? It is here on the East Coast, at least. :D

Finally, pending orders shouldn't impact your margin, at least they don't with my broker. Of course the EA could just watch the price and send a market order when it hit the buy or sell targets. But then you'd need it running 24/5.

Enough rambling. Time to go yell at EURUSD to try to get it moving.

George
garyfritz

Re: MQL questions

Post by garyfritz »

Duh. Yes, of course 5pm ET is 22:00 GMT. Not sure what I was thinking there. But 5pm ET seems to be the "daily close" for most providers I've seen, so that's what I've used. Would be nice if it was adjustable.

I just want 5 D1 bars per week: Sun 5pm - Mon 5pm through Thu 5pm - Fri 5pm. The criminal I plan to run this on initially is at GMT+2, and their data goes from 00:00 Monday through 23:59 Friday. So I assume their daily bars run from 5pm ET to 5pm ET.

Pending orders DO get charged margin on Tradestation, unfortunately. Not sure about my new Empty4 crim.

Watching the price and hitting the market is simple, and I'll probably be getting a VPS so 24/5 isn't a big problem. And that way you don't show your hand to the crim, in case you worry about that. But I'd want an emergency exit stop in the market at all times, maybe just 5-10 pips beyond my "real" stop, in case I lose my net connection or whatever. Even a VPS can lose connectivity or hang, so I wouldn't want to carry any naked positions.

Looks like you got the EU moving! How'd you like to yell at my teenagers?? :lol:
garyfritz

Re: MQL questions

Post by garyfritz »

Hm. I tested it yesterday and it seemed to work OK. Yesterday afternoon I placed 3 trades and let it run overnight. This morning one of the trades hit its TP, and the other two hit their entry stop and still have open trades. One of those two (USDJPY) properly canceled the other side of the OCO, but AUDCHF (still open) and AUDUSD (which hit its TP) didn't:
OCO.gif
OCO2.gif
I have no idea why it would work on one pair and not on two others??
You do not have the required permissions to view the files attached to this post.
User avatar
gaheitman
Trader
Posts: 655
Joined: Tue Nov 15, 2011 10:55 pm
Location: Richmond, VA, US

Re: MQL questions

Post by gaheitman »

garyfritz wrote:Hm. I tested it yesterday and it seemed to work OK. Yesterday afternoon I placed 3 trades and let it run overnight. This morning one of the trades hit its TP, and the other two hit their entry stop and still have open trades. One of those two (USDJPY) properly canceled the other side of the OCO, but AUDCHF (still open) and AUDUSD (which hit its TP) didn't:
OCO.gif
OCO2.gif
I have no idea why it would work on one pair and not on two others??
That is odd. So, the SELL_STOP that got triggered closed it's BUY_STOP, but the two BUY_STOPs didn't close their associated SELL_STOPs. Are the scripts still running on any of the pairs?
garyfritz

Re: MQL questions

Post by garyfritz »

Ah, right: the short (USDJPY) canceled its buy properly, but the longs didn't cancel their sells. I should have seen that.

The BUY_STOP in the chart I posted had clearly converted to a BUY. I don't know why it didn't trigger the code to cancel the SELL_STOP. The code looks to be symmetric with the sell case that worked.

No, all the positions exited shortly after I posted.
Post Reply

Return to “Coders Hangout”