Easy Trader

Master Trader
Sep 17, 2011
240
5
59
If there is anyone that can help me add some code to a EA I would greatly appreciate it.

I have a EA that I managed to tweak to make it trade reversals off of round numbers. Basically just fading the round numbers. Problem is that it places
a trade every time the round number is touched.

I would like to be able to have a extern setting that would allow me to set the amount of bars that would need to have passed since the last trade in order to take the present trade.
if last trade > 10 bars = ok to trade.

I have tried and tried to no avail. and can't seem to quantify how to do this.
Here are some pics of what it does and what I would like it to do.
Not sure if my post here is going to be in vane but its worth a shot.
Thanks in advance:)
 

Attachments

  • EURUSDH1.2.jpg
    EURUSDH1.2.jpg
    79.6 KB · Views: 30
  • EURUSDH1.3.jpg
    EURUSDH1.3.jpg
    81.8 KB · Views: 34
  • Round_Number_EA (2) - Fade.mq4
    8.3 KB · Views: 31

Enivid

Administrator
Staff member
Nov 30, 2008
18,403
1,332
144
Odesa
www.earnforex.com
Without looking into your code, here are some questions and suggestions:

1. Does your EA use a Magic number for its trades?

2. Do you want the number of bars to count between Opening time and Opening time, or between Closing time and Opening time?

3. The best way to do what you want is to run a check before each position opening. The check would find the EA's latest trade in the order history and compare its open/close time to the time in your threshold.
 

Easy Trader

Master Trader
Sep 17, 2011
240
5
59
Thanks for the reply,

Its does use a magic number, and I would like open to "potential" open.
Not sure how to check for last position, I am basically teaching myself MQL as we speak. I am crafty enough to change existing code fairly well but currently all I have managed to code is a comment that posts the spread and broker time...:eek:

I wasn't sure if this was a easy function to just add based on my lack of experience.
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,403
1,332
144
Odesa
www.earnforex.com
First, you need to add this order sorting function to your code:
MQL4:
//+------------------------------------------------------------------+
//| Order History Sorting Function by WHRoeder:                      |
//| [url]http://www.mql4.com/users/WHRoeder[/url]                               |
//+------------------------------------------------------------------+
int GetHistoryOrderByCloseTime(int& tickets[], int dsc = 1)
{
   /* [url]http://forum.mql4.com/46182[/url] zzuegg says history ordering "is not reliable
    * (as said in the doc)" [not in doc] dabbler says "the order of entries is
    * mysterious (by actual test)" */
 
   int nOrders = 0;
   datetime OCTs[];
 
   for (int iPos = OrdersHistoryTotal() - 1; iPos >= 0; iPos--)
   {
      if ((OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY))  // Only orders w/
      &&  (OrderMagicNumber() == Magic)             // my magic number
      &&  (OrderSymbol()      == Symbol())             // and my pair.
      &&  (OrderType()        <= OP_SELL) // Avoid cr/bal forum.mql4.com/32363#325360
      )
      {
         int nextTkt = OrderTicket();
         datetime nextOCT = OrderCloseTime();
         nOrders++;
         ArrayResize(tickets, nOrders);
         ArrayResize(OCTs, nOrders);
         // Insertn sort.
         for (int iOrders = nOrders - 1; iOrders > 0; iOrders--)
         {
            datetime prevOCT = OCTs[iOrders-1];
            if ((prevOCT - nextOCT) * dsc >= 0) break;
 
            int prevTkt = tickets[iOrders - 1];
            tickets[iOrders] = prevTkt;
            OCTs[iOrders] = prevOCT;
         }
         tickets[iOrders] = nextTkt;
         OCTs[iOrders] = nextOCT; // Insert.
      }
   }  
   return(nOrders);
}

Then, you can call this code to select the latest order.
MQL4:
      int tickets[];
      int nTickets = GetHistoryOrderByCloseTime(tickets);
 
      OrderSelect(tickets[0], SELECT_BY_TICKET);

Do you know how to get the open time of the selected order in MQL?
 
Last edited:

Easy Trader

Master Trader
Sep 17, 2011
240
5
59
Thank you so much for your time Enivid,
No I do not know how to get the open time....
This is way out of my experience. Would you like to take on a project?:rolleyes:
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,403
1,332
144
Odesa
www.earnforex.com
No I do not know how to get the open time....
This is way out of my experience.

OrderOpenTime() gets you the order's open time. TimeCurrent() gets you the current time. It's quite simple and can be found using the MQL's help files (F1).

Would you like to take on a project?:rolleyes:

No, I'd prefer that you do it. That way you will be able to fix or tweak it further without asking someone else every time you need to change in your code something.
 

Easy Trader

Master Trader
Sep 17, 2011
240
5
59
I have been reading watching video's, and pulling my hair out. I have put about 14 hours into this already and the only code I have written is post a comment on the chart with the time, spread etc. Everything else is just me copying and attempting to mash different parts and pieces together.

I am going to commission this out, I know you are a good clean coder Enivid, so I'll give you first dibs if you want the job. If not I thanks for being here to help me, I appreciate it.
 

Easy Trader

Master Trader
Sep 17, 2011
240
5
59
I picked up this project again and have made some headway, I am getting stuck with creating the function for my time threshold. I have attached the EA with some notes in it, please don't laugh at my horrible programming skill.
Any help is appreciated.
 

Attachments

  • Round_Number_EA (2).1_stop_hunter.mq4
    10.4 KB · Views: 25