• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 6:08am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 6:08am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Help closing trades (learning MQL) 1 reply

MQL Coding - Let’s talk about how to code mql 0 replies

Learning at the speed of snail [Newbie Learning Journal] 9 replies

MQL learning resources 3 replies

Can I Ask About MQL Coding pls.. 1 reply

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
Tags: Guide me in learning MQL coding
Cancel

Guide me in learning MQL coding

  • Post #1
  • Quote
  • First Post: Mar 3, 2011 2:46am Mar 3, 2011 2:46am
  •  usasa
  • Joined Jun 2010 | Status: Member | 878 Posts
Hi

I have started learning MQL and coding my first EA. Whenever I stuck with logic/syntax I search FF for any similar code for reference.

If I failed to find a solution I will post my doubts in this thread and hope Seniors/Experts guide me whenever they find some spare time.

I also welcome any beginners to post their coding doubts here instead of opening a new thread so that we can keep this thread active.

...........
logical doubt.

my EA places buystop /sellstop orders and modifies stoploss to BE at certain pips movement.

Now I wanted to include some commands whenever a BE order is hit.

How do I findout if an Order is stopped out at BE.


Thanks
Weigh well output the loss and gain And proper action ascertain
  • Post #2
  • Quote
  • Mar 3, 2011 3:52pm Mar 3, 2011 3:52pm
  •  CodeMeister
  • Joined Sep 2009 | Status: Making Code While Making Pips | 1,672 Posts
I am assuming that your EA is as simple as possible. If not, there are too many possibilities for me to answer.

Once the order is opened, you should keep track of its ticket number, which is returned by the OrderSend() function. I assume that you update the SL with OrderModify() at the appropriate point with the order open price. You should also keep track of this price. Once the Bid/Ask has hit this price, you should use OrderSelect() to verify that the trade was closed. It will return false if the order doesn't exist (ie. closed) and true if still open. I suppose you could call OrderSelect() with the History option as well, but that shouldn't be necessary.

Hope this helps.
 
 
  • Post #3
  • Quote
  • Mar 4, 2011 12:42am Mar 4, 2011 12:42am
  •  usasa
  • Joined Jun 2010 | Status: Member | 878 Posts
Thank you.

I have found a solution and it works for now. I am not sure is it fool proof, any comments?

i=OrdersHistoryTotal();
OrderSelect(i, SELECT_BY_TICKET,MODE_HISTORY);
if(OrderCloseTime()!=0)
{
if(OrderType() == OP_BUY && OrderClosePrice() == OrderStopLoss() && secondlong==false)
{
Print("BUY BreakEven "+OrderClosePrice());
longsignal1=true;
secondlong=true;
retrace=0.001;
}
if(OrderType() == OP_SELL && OrderClosePrice() == OrderStopLoss() && secondshort==false)
{
Print("SELL BreakEven "+OrderClosePrice());
shortsignal1=true;
secondshort=true;
retrace=0.001;
}
}
Weigh well output the loss and gain And proper action ascertain
 
 
  • Post #4
  • Quote
  • Mar 4, 2011 1:27am Mar 4, 2011 1:27am
  •  CodeMeister
  • Joined Sep 2009 | Status: Making Code While Making Pips | 1,672 Posts
Your code is trying to select the latest trade, but you specified the ticket option, not the position option.

Inserted Code
OrderSelect(i, SELECT_BY_TICKET,MODE_HISTORY);

I'm not sure why this clause is required. If you selected the right trade, you don't need this logic - it is redundant.

Inserted Code
&& OrderClosePrice() == OrderStopLoss()

You should use code tags (# icon) to make your code more readable.
 
 
  • Post #5
  • Quote
  • Mar 4, 2011 1:51am Mar 4, 2011 1:51am
  •  usasa
  • Joined Jun 2010 | Status: Member | 878 Posts
I am basically a copy/paste programmer. I now understand its very dangerous to code without understanding the real concept. I will work on this.

Thanks.
Weigh well output the loss and gain And proper action ascertain
 
 
  • Post #6
  • Quote
  • Mar 4, 2011 2:28am Mar 4, 2011 2:28am
  •  Free4Life
  • | Joined May 2010 | Status: Member | 9 Posts
http://www.amazon.com/Expert-Advisor.../dp/0982645902

This book gives a reasonable overview and can get you started.
 
 
  • Post #7
  • Quote
  • Mar 4, 2011 4:45am Mar 4, 2011 4:45am
  •  kennyhubbard
  • Joined Sep 2008 | Status: Member | 357 Posts
usasa,

You may as well learn this now.........you are going to have grey hairs with this statement :-

Inserted Code
OrderClosePrice() == OrderStopLoss()

You cannot compare doubles in MQL directly. This is due to the way MQL deals with floating point arithmetic. Let us not even consider the fact that this statement does not factor in any slippage. Your closeprice(or fill) will almost never be spot on with your stoploss.

I can also tell you that you that you are attempting to create an 'event monitoring' function.......and this particular task is quite challenging in MQL....... I, personally, would find something easier to start with. Even amongst the 'pros' there is some debate on the best way to detect if an order has been closed by Stoploss.
 
 
  • Post #8
  • Quote
  • Last Post: Mar 4, 2011 1:02pm Mar 4, 2011 1:02pm
  •  usasa
  • Joined Jun 2010 | Status: Member | 878 Posts
Thanks and I totally agree with you.

I am trying to code as advised by CodeMeister to keep track of orders.
which is also a big task for me... let's see.
Weigh well output the loss and gain And proper action ascertain
 
 
  • Platform Tech
  • /
  • Guide me in learning MQL coding
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023