Limiting one entry per pair

 

Hi:

I have been attempting to limit only one transaction (either long or short) per pair with the following code but it constantly opens up two transactions of each pair. Could it be due to asynchronous messages between the terminal and server? I tries sleep(3000) but that did not work either so I doubt it. Must be something simpler that I am missing.

Anyway, following is the code I am using so if anyone can give me hand, I would appreciate it.

Thanks,

.....

int symbCnt=0;

tt=OrdersTotal();

for (int i=0;i<tt;i++) {

if (OrderSelect(i,SELECT_BY_POS)==true) {

if (OrderSymbol()==Symbol() )

symbCnt++;

}

}

if (symbCnt > 0)

return(0); //exit because symbol is already traded


...rest of the program

 
arm:

Hi:

I have been attempting to limit only one transaction (either long or short) per pair with the following code but it constantly opens up two pairs. Could it be due to asynchronous messages between the terminal and server? I tries sleep(3000) but that did not work either so I doubt it. Must be something simpler that I am missing.

Anyway, following is the code I am using so if anyone can give me hand, I would appreciate it.


Before posting please read some of the other threads . . . then you would have seen numerous requests like this one:

Please use this to post code . . . it makes it easier to read.

 
arm:

Hi:

...... it constantly opens up two transactions of each pair.


Tell also what about those trades

What is the orderopeningtime of those two trades with same symbol

Are they opened with other EA or yours...

 
RaptorUK:

Before posting please read some of the other threads . . . then you would have seen numerous requests like this one:

Please use this to post code . . . it makes it easier to read.



Hi RaptorUK:

I did look through the other threads and while some of the posts where similar, I did not find anyone with the exact same issue. That is, instead of opening one trade, two trades are opening at the same second, never one or three, always two.

Sorry about the code, I did not know about this feature. I will do so in the future

 
arm:


Hi RaptorUK:

I did look through the other threads and while some of the posts where similar, I did not find anyone with the exact same issue. That is, instead of opening one trade, two trades are opening at the same second, never one or three, always two.

Sorry about the code, I did not know about this feature. I will do so in the future

My post referred to using SRC, if you had search the Forum you would have seen other messages the same.

I don't see anything wrong with your code, I suspect the problem lies elsewhere, perhaps you should show more code, for example the code from the start of start() to where it places the orders.

 
deVries:


Tell also what about those trades

What is the orderopeningtime of those two trades with same symbol

Are they opened with other EA or yours...


Hi deVries:

For example, yesterday I had my EA placed on GBP/USD and USD/JPY. On 2012.08.17 at 14:12 the EA placed a long trade on GBP/USD at 1.57030 and in the same minute (I assume the same second but I did not register it), a second long trade on GBP/USD was opened at 1.57021. On 2012.08.17 at 14:26 the EA placed a short trade on USD/JPY at 79.503 and in the same minute (again I assume during the same second) a second short trade on USD/JPY was opened at 79.501. The time zone of the trades I indicated is CET.

All of these 4 trades were opened with the same EA.

Thanks for your help and or thoughts on this.

 
RaptorUK:

My post referred to using SRC, if you had search the Forum you would have seen other messages the same.

I don't see anything wrong with your code, I suspect the problem lies elsewhere, perhaps you should show more code, for example the code from the start of start() to where it places the orders.


Below is the code in start(). For what it is worth, I am intending to place this on longer timeframe bars, but as I am new to MT4, I have been writing and testing the code on 1M to understand the idiosyncrasies of the platform. I do not think that basic behaviour should change if the EA is on 1M, 1H or 1D, but just in case, I thought I would let you know.

int start()     {
        //Variables
        int totalTrades;
        int symbCnt=0;
        int orderReturn;

        //Do we have Enough Information
        if (Bars < emaSlowPeriod)       {
                Print("Not enough bars in the window to process. Stopping Process!!!");
                return(0);
        }
        
        symb=Symbol(); 

        //Initialize indicators
        //EMA
        double fastEma1=iMA(NULL,0,emaFastPeriod,0,emaMODE,emaFastPrice,1);
        double fastEma2=iMA(NULL,0,emaFastPeriod,0,emaMODE,emaFastPrice,2);
        double slowEma1=iMA(NULL,0,emaSlowPeriod,0,emaMODE,emaSlowPrice,1);
        double slowEma2=iMA(NULL,0,emaSlowPeriod,0,emaMODE,emaSlowPrice,2);

        //MAIN LOGIC
        //Limit Orders? 

        totalTrades=OrdersTotal();
        for (int i=0;i<totalTrades-1;i++) {
                if (OrderSelect(i,SELECT_BY_POS)==true) {
                        if (OrderSymbol()==symb)
                                symbCnt++;
                }
                else
                        Print("ERROR: ",GetLastError());
        }

        if (symbCnt>0) {
                Print(symbCnt, " ", symb, " Already has open trades. Exiting"); 
                return(0);
        }


        //LONG
        if (fastEma1 > slowEma1 && fastEma2 < slowEma2) {
                tradeCounter++;
                orderReturn=placeOrder(1); 
        }

        //SHORT
        if (fastEma1 < slowEma1 && fastEma2 > slowEma2) {
                tradeCounter++;
                orderReturn=placeOrder(2);
        }

        //Finish
        double free=AccountFreeMargin();
        Comment("3H Cross: ", Symbol(), " Account Free Margin is ", DoubleToStr(free,2), "\n", "Current Time is ", TimeToStr(TimeCurrent()), " Open Trades: ",totalTrades);

        return(0);
}
//+------------------------------------------------------------------+


int placeOrder(int orderType) {
        int orderTicket;
        int orderErr;
        int k;
        bool mod;
        switch (orderType) { 
                case 1 : //BUY
                        orderTicket=OrderSend(Symbol(),OP_BUY,lots,Ask,3,getLongStop(),getLongLimit(),"BUYING Order Number: ",tradeCounter,0,Yellow);
                        if (orderTicket<0)      {
                                orderErr=GetLastError();
                                Print("LONG. Main Order Failed with error #", orderErr);
                                return(orderErr); 
                        }
                        else {
                                Print("LONG. Successful Order Created");
                                Sleep(3000);
                                return(0); 
                        }
                        break;
                case 2 : //SELL
                        orderTicket=OrderSend(Symbol(),OP_SELL,lots,Bid,3,getShortStop(),getShortLimit(),"SELLING Order Number: ",tradeCounter,0,Yellow);
                        if (orderTicket<0)      {
                                orderErr=GetLastError();
                                Print("SHORT. Main Order Failed with error #", orderErr);
                                return(orderErr); 
                        }
                        else     {
                                Print("SHORT. Successful Order Created");
                                Sleep(3000);
                                return(0);
                        }
                        break;
        }
}

double getLongStop()     {
        double tmp;
        tmp=Ask - (stopLoss*Point*10);
        return(tmp);
}

double getLongLimit() {
        double tmp;
        tmp=Ask + (takeProfit*Point*10);
        return(tmp);
}

double getShortStop() {
        double tmp;
        tmp=Bid + (stopLoss*Point*10);
        return(tmp);
}

double getShortLimit() {
        double tmp;
        tmp=Bid - (takeProfit*Point*10);
        return(tmp);
}

 

For code readability instead of . . .

placeOrder(1);

placeOrder(2);

use

placeOrder(OP_BUY);

placeOrder(OP_SELL);

and use OP_BUY and OP_SELL in your case statements.

The totalTrades variable is out of date in this line of code . . .

Comment("3H Cross: ", Symbol(), " Account Free Margin is ", DoubleToStr(free,2), "\n", "Current Time is ", TimeToStr(TimeCurrent()), " Open Trades: "   totalTrades   );

this line should only be executed if there are no existing trades when start() runs, then a new trades is opened but the totalTrades variable is not updated to reflect this.

If your OrderSelect() fails, for whatever reason, you will fail to increment symbCnt and you assume that there are no trades, this is not fail safe, if the OrderSelect() fails shouldn't you return(0) instead ?

 



RaptorUK:

For code readability instead of . . .

use

and use OP_BUY and OP_SELL in your case statements.

The totalTrades variable is out of date in this line of code . . .

this line should only be executed if there are no existing trades when start() runs, then a new trades is opened but the totalTrades variable is not updated to reflect this.

If your OrderSelect() fails, for whatever reason, you will fail to increment symbCnt and you assume that there are no trades, this is not fail safe, if the OrderSelect() fails shouldn't you return(0) instead ?

Thanks. Yes you are right with totalTrades. I will fix that. You are also right that the OrderSelect() would have a better safety mechanism with a return(0) which I will correct also.

However, with the issue that I am having, if OrderSelect() fails with the pair previously opened, then a second order could indeed happen, but in that case, I would have captured the OrderSelect() fail error which I am pretty sure never happened, well, unless the Print("ERROR: ",GetLastError()); also failed.

It would also be very strange that both the OrderSelect() and the Print statement always fail when the conditions are met for a trade to take place, but never happens again. If the two trades are closed and then the conditions are met again, then both OrderSelect() and the Print statements fail once again. Don't you think that is a little odd?

 
arm:

Thanks. Yes you are right with totalTrades. I will fix that. You are also right that the OrderSelect() would have a better safety mechanism with a return(0) which I will correct also.

However, with the issue that I am having, if OrderSelect() fails with the pair previously opened, then a second order could indeed happen, but in that case, I would have captured the OrderSelect() fail error which I am pretty sure never happened, well, unless the Print("ERROR: ",GetLastError()); also failed.

It would also be very strange that both the OrderSelect() and the Print statement always fail when the conditions are met for a trade to take place, but never happens again. If the two trades are closed and then the conditions are met again, then both OrderSelect() and the Print statements fail once again. Don't you think that is a little odd?

It would be very odd and I don't think that is what is happening . . . I don't know what is causing your issue to be honest, but clearing up any other little problems might help you with some clarity and help you get to the root cause. I did see a possibility that a Long and a Short could be opened together, but not 2 longs or 2 shorts.

Are you running this in the Strategy Tester or Demo ? or maybe both ?

 
Juat a thought but will not tradecounter always be one and you seem to wish to use it as a magic-number in ordersend to state the order in which the trades were created?
Reason: