Forex Factory
  • Login

  • Username: Password:
  • 2:55am

  • Search
  • Home

  • Forums

  • Trades

  • Calendar

  • News

  • Market

  • Brokers

Options

Search

Subscribe to Thread

Bookmark Thread

First Page First Unread Last Page Last Post

Printable Version

Similar Threads

Alpari demo account, is it in real real time? 20 replies

Forex Bunker | Real People, Real Education, Real Results 6 replies

difference in real prices on Demo and real micro account 1 reply

  • Platform Tech
  • /
  • Reply to Thread
  • 1

A "real" coding challenge

  • Post# 1
  • Quote
  • First Post: Dec 19, 2012 7:30pm
  • gspajon
    Joined Jan 2007 | 389 Posts | Status: Failure creates discipline
Hello again...

I am really stuck...I'm trying to code an EA that will close a certain percent of a total number of lots. Example: I have positions as follows:
  1. Position 1 = 0.3 lots
  2. Position 2 = 1.2 lots
  3. Position 3 = 0.5 lots
  4. Total = 2.0 lots
when price reaches a certain point I want to close 75% (1.5) of my total lots. Clearly this could be accomplished by closing positions 1 & 2, but I don't seem to be able to get MT4 to calculate what it has closed and compare that to what is still open. It just closes everything. I am using a for loop to calculate the open lots but for some reason that loop doesn't catch up fast enough to trigger to bool: exit = false.

I've tried putting this loop in before and after the exit loop but it doesn't seem to make any difference.

Again I want it to continue closing until it reaches a user defined percent of open lots...then stop and close nothing else. Any Ideas?
  • Post# 2
  • Quote
  • Dec 19, 2012 9:53pm
  • CodeMeister
    Joined Sep 2009 | 1,392 Posts | Status: Making Code While Making Pips
Which way do you control the loop? By incrementing or decrementing? MQL4 is quirky in that respect. I'm not going to give a detailed explanation, but make sure that you have considered decrementing the loop. I always do it that way to avoid problems like I had in the past.
  • Post# 3
  • Quote
  • Dec 19, 2012 10:12pm
  • NowAndLater
    Joined Sep 2007 | 677 Posts | Status: Breakout Baby
Try this and let me know if it helps
Attached File
File Type: mq4 closePercentOfAllTrades.mq4   2 KB | 17 downloads
  • Post# 4
  • Quote
  • Dec 20, 2012 7:28am
  • m.m.m.
    Commercial Member | 256 Posts | Joined Aug 2010
Quoting NowAndLater
Try this and let me know if it helps
I'd say there is a logical flaw in your code. This is what CodeMeister above talked about.
You are starting from order position zero (i = 0) and counting upwards while you possibly close orders.
This way you might skip orders (when an order is closed it is removed from the queue and all orders that have followed shift downwards by one position).
So, instead, you have to start from i = OrdersTotal() - 1 and count down to zero.
Otherwise your code is to the point.
  • Post# 5
  • Quote
  • Dec 20, 2012 8:13am | Edited at 8:50am
  • gspajon
    Joined Jan 2007 | 389 Posts | Status: Failure creates discipline
Quoting m.m.m.
I'd say there is a logical flaw in your code. This is what CodeMeister above talked about.
You are starting from order position zero (i = 0) and counting upwards while you possibly close orders.
This way you might skip orders (when an order is closed it is removed from the queue and all orders that have followed shift downwards by one position).
So, instead, you have to start from i = OrdersTotal() - 1 and count down to zero.
Otherwise your code is to the point.
Here is what I had to do...its crude but seems to work temporarily. I put a for loop ahead of this code to calculate the percent of positions to close based on the open positions. I open and close this loop with a bool: find = true/false. then I run the following code:

if (Lots>0 && Bid>=Price)
{
Exit = true;
}
else
Exit = false;
if (Exit)
{
for (cnt = OrdersTotal(); cnt >=0; cnt--) //--look for already open orders
{
if (OrderSelect(cnt,SELECT_BY_POS,MODE _TRADES))
{
if (OrderSymbol() == Symbol())
{
if (OrderType() == OP_BUY)
{
if (OrderLots()<Lots)
{
closedlots = OrderLots();
Lots = Lots-closedlots;
}
else
if (OrderLots()>Lots)
{
closedlots = Lots;
Lots = 0;
}
OrderClose(OrderTicket(),closedlots ,Bid,3,Violet);
Recheck = true;
}
}
}
}
}
}

The "recheck" bool then confirms that the open positions AFTER this code are smaller than the open positions before this code....it seems to work...what do you think? too clumsy?
  • Post# 6
  • Quote
  • Dec 20, 2012 8:31am
  • nubcake
    Joined Oct 2009 | 2,372 Posts | Status: schadenfreude tastes like joy
Quoting gspajon
Here is what I had to do...its crude but seems to work temporarily. I put a for ahead of this code to calculate the percent of positions to close based on the open positions. I open and close this loop with a bool: find = true/false. then I run the following code:

if (Lots>0 && Bid>=Price)
{
Exit = true;
}
else
Exit = false;
if (Exit)
{
for (cnt = OrdersTotal(); cnt >=0; cnt--) //--look for already open orders
{
if (OrderSelect(cnt,SELECT_BY_POS,MODE _TRADES))
{
if (OrderSymbol() == Symbol())
{
if (OrderType()...
you won't understand this, but this thread is irritating me. here is why :

1. this is not, in any shape or form, any type of challenge. it's just not. it's boring, and a little tedious. to do it robustly requires a little bit of coding, but nothing that is at all difficult. calling this a challenge, let alone emphasizing it as a "real" coding challenge is just insulting. YOU don't know what a "real" challenge is.

2. you don't even bother to use the code tags in your latest post. surely by now you have seen other people use quotes and also post code and insert url links etc etc. that is, by now you should know how to do these simple things.

now, with that said, despite your lack of code tags i can still see a glaring flaw which possibly won't manifest into much of an issue most of the time but is a tiny error you must fix.

Inserted Code
for (cnt = OrdersTotal();
add MINUS 1. that last order in the list is positioned at index of "OrdersTotal() - 1". the index starts at zero, making the the first order positioned at index zero and the last order positioned at OrdersTotal - 1

apart from that i don't know why you seem to think this is 'crude', as though it's some kind of kludge or hack. perhaps whatever you are doing with the other loops you mention or whatever are ferral and done badly.
  • Post# 7
  • Quote
  • Dec 20, 2012 8:53am
  • m.m.m.
    Commercial Member | 256 Posts | Joined Aug 2010
Quoting gspajon
it seems to work...what do you think? too clumsy?
Yeah, except the "OrdersTotal()" versus "OrdersTotal()-1", as pointed out by nubcake, and as you say, if it works it works. A litte bit clumsy yes, possibly not perfectly robust, but if you are satisfied, so be it.
And: work on that formatting tho.
  • Post# 8
  • Quote
  • Dec 20, 2012 10:08am
  • gspajon
    Joined Jan 2007 | 389 Posts | Status: Failure creates discipline
I want to thank you both Nubcake and MMM for your help. I have applied your suggestion and find it was very helpful. As you have already noted, I am self taught, and only know how to code mq4 (and not very well). I meant no insult to anyone with more knowledge. Perhaps I should've said "A 'real' challenge...for me".

I am grateful for your help and will be more estute in the future. Merry Christmas to you and your families.
  • Post# 9
  • Quote
  • Dec 21, 2012 6:34am
  • NowAndLater
    Joined Sep 2007 | 677 Posts | Status: Breakout Baby
Quoting m.m.m.
I'd say there is a logical flaw in your code. This is what CodeMeister above talked about.
You are starting from order position zero (i = 0) and counting upwards while you possibly close orders.
This way you might skip orders (when an order is closed it is removed from the queue and all orders that have followed shift downwards by one position).
So, instead, you have to start from i = OrdersTotal() - 1 and count down to zero.
Otherwise your code is to the point.
Ok, I see the issue, thanks for pointing it out!
  • Post# 10
  • Quote
  • Last Post: Dec 23, 2012 6:56am
  • RaptorUK
    Joined Jan 2008 | 531 Posts | Status: Member
If you are closing or deleting trade/orders from inside a loop you MUST count down, not up . . . this is why . . .

Loops and Closing or Deleting Orders
20 pips a day isn't too much to ask . . .
Thread Tools Search this Thread
Show Printable Version Show Printable Version
Email This Thread Email This Thread
Search this Thread:

Advanced Search

  • Platform Tech
  • /
  • A "real" coding challenge
  • Reply to Thread
0 traders viewing now

©2013 Forex Factory, Inc. / Terms of Use / Privacy Policy

Forex Factory® is a registered trademark.

Connect

  • Facebook
  • Twitter
  • RSS

Company

  • About FF
  • FF Blog
  • Careers at FF
  • Advertising
  • Contact FF

Products

  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer

Website

  • Homepage
  • Search
  • User Guide
  • Member List
  • Online Now
  • Report a Bug