Closing order based on past event

 

Hi all, I have an EA that I created for couple of months. Since I still learning about MQL 4 the only close and open order that I can do is based on the present time. Right know, I'm creating an EA which consist of a lagging line (shifted 26 to the left) that will be used to close an open order. My coding is like this:

1. I creating a function to count the bar from the first bar. The code is like this:

int CountBar(int shift = 0)
{
   int counter = 0;
   for(int i=shift; i<=Bars; i++)
   {
     counter++; 
   }
   return(counter);
}

2. Creating a condition where lagging line touching the bar.

if(laggingline == CountBar(-25))
   {
    bool resCO = OrderClose(orderId,OrderLots(),Bid + spread,0);
    if(!resCO)
      {
       Print("Error in Closing Order. Error code=", GetLastError());
       return;
      }
    }

The problem is that the code will never close the order. Anyone has any tips how to make it works?

 
  1. Luandre Ezra:1. I creating a function to count the bar from the first bar. The code is like this:
    Your code
    int CountBar(int shift = 0)
    {
       int counter = 0;
       for(int i=shift; i<=Bars; i++)
       {
         counter++; 
       }
       return(counter);
    }
    Simplified
    int CountBar(int shift = 0){ return Bars - shift + 1; }
  2. Luandre Ezra: 2. Creating a condition where lagging line touching the bar.
    if(laggingline == CountBar(-25))
       {
        bool resCO = OrderClose(orderId,OrderLots(),Bid + spread,0);
        if(!resCO)
          {
           Print("Error in Closing Order. Error code=", GetLastError());
           return;
          }
        }
    We have no idea what laggingline is, a price or a bar index.
         How To Ask Questions The Smart Way. 2004
              Be precise and informative about your problem
  3. If it is an index it isn't likely to be twenty four (24) bars from the oldest bar in history.
  4. If it is a price, prices do not equal indexes.

    Doubles are rarely equal. Understand the links in:
              The == operand. - MQL4 programming forum #2 2013.06.07

    If the Bid goes below the line (for a buy) just close it.

  5. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY (OANDA) shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

 

Hi Luandre Ezra

Hope all is well, I think you maybe over complicating the calculation for the 26th candle.

One can use any of the following Open[26], High[26], Low[26], Close[26].

   bool resCO = OrderClose(orderId,OrderLots(),Bid + spread,0);

I intially had problems with mql4 code differing from manual pages and help pages. The Bid + Spread needs to be calculated outside of the close statement.

create a variable that will be the calculation of Bid+Spread

 
BS = NormalizeDouble(Bid+Spread*Point,Digits);

Then, you can use the variable in the statement

  bool resCO = OrderClose(orderId,OrderLots(),BS,0);


Hope this helps

 

Thank you @William Roeder for the simplified code, never thought it could be that simple.

We have no idea what laggingline is, a price or a bar index

the lagging line is price. I think this is where my problem lies. I thought like if the price hit the bar it will close the order because it "touches" the bar. If I'm not wrong, then I cannot compare the price with the bar because bars is not a set of price (different type of data), is it correct?

You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

I think I made a mistake here since I read somewhere online instead of buy at the ask it should be buy at the bid+spread (so the close or take profit has the same distance value with the stop loss or close order).

 
One can use any of the following Open[26], High[26], Low[26], Close[26].

I think this will works, gotta try it when I got the time. Gonna give update if it's works.

I intially had problems with mql4 code differing from manual pages and help pages. The Bid + Spread needs to be calculated outside of the close statement.

Thank you for the tips, never knew it should be that way. Do you know what happened when the calculation inside the close statement? does it becomes useless?

 
Michael Long: create a variable that will be the calculation of Bid+Spread

Bogus. See #1.5

 
William Roeder:

Bogus. See #1.5

I think I’m still kinda confuse with buy at the ask and sell at the bid. To get the same amount in each direction, logically when I buy at the ask then I should sell at the ask also.

In a scenario when I open order a currencies at the ask and close the order at the bid with 10 pips in each direction then there will be a difference amount in one of the direction and the difference is because of the spread (if I use “buy at the ask and sell at the bid”. 

So to fix it when I buy at the ask I should put SL and TP instead of bid, I should put bid+ spread which is the value of the ask. What makes me confuse is if Bid+spread is the same with the ask or is it not? If you may, could you explain in more detail about this?

Reason: