Help with EA ... not closing trades in tester

 

I am working on an EA, but no matter what I do, I can't make it close trades. It opens them just fine, but when I want them closed, the EA will not close them.

I'll post part of my EA code so maybe one of you experienced coders can figure out why I'm having this problem. The vars 'buyTrade' and 'sellTrade' are global to whole EA.

NOTE: I only want ONE trade open at any given time, either long or short. The trades are "always in" so when a long signal comes, the existing short should be closed and vice versa.

It is NOT doing this. What it is doing is opening trades and then opens more thrades ... and opens more ... and keeps them open until they all eventually hit their stops.

Thanks in advance.

 // buy signal ----------------------------------------------
if (STC_VIDYAprev < 10.00 && STC_VIDYAcur >= 10.00) {
         if (sellTrade==true) {
            for (int i = OrdersTotal() - 1; i >= 0; i--) {
               OrderSelect(i, SELECT_BY_TICKET);
                  if ( OrderType() == OP_BUY  )
                  { int OldTicket=OrderTicket(); double lots=OrderLots();}
               RefreshRates(); //OrderSelect() needed ?
               double symAsk = NormalizeDouble( MarketInfo( Sym, MODE_ASK ), SymDigits );
               bool closeSellOrder = OrderClose( OldTicket, lots, symAsk, 0, CLR_NONE );
            }   
               sellTrade = false;
         }
         if(buyTrade==false) {
            EnterLong(Sym, Lots, "");  
            buyTrade = true;
         }
      }
      //else
         //return(0); 
           
      // Sell signal ---------------------------------------       
   if (STC_VIDYAprev > 90.00 && STC_VIDYAcur <= 90.00 ) {
        if (buyTrade==true) {
           for (int j = OrdersTotal() - 1; j >= 0; j--) {
               OrderSelect(i, SELECT_BY_TICKET);
                  if ( OrderType() == OP_SELL )
                  { int oldTicket=OrderTicket(); double lotts=OrderLots();}
           RefreshRates(); //OrderSelect() needed ?
           double symBid = NormalizeDouble( MarketInfo( Sym, MODE_BID ), SymDigits );
           bool closeBuyOrder = OrderClose( oldTicket, lotts, symBid, 0, CLR_NONE ); 
           }
           buyTrade = false;
        }
        if(sellTrade==false) {
           EnterShrt( Sym, Lots, ""); 
           sellTrade = true;
        } 
    }
    //else
       //return(0); 
 

Do your signals to close the trades occur ? Do seelTrade and/or buyTrade ever = true ? why don't you print these variables so you can see what is happening ?

A few other comments:

if you are using MarketInfo instead of PredefinedVariables you don't need to use RefreshRates, RefreshRates refreshes the PredefinedVariables.

You don't need the NormalizeDouble, Ask is already Normalized.

Does your OrderSelect work ? check it's return value . . .

Why aren't you testing the return value from the OrderSend ? if it fails don't you want to know ?

You set sellTrade = false and buyTrade = false even if you don't close any trades . . . you don't check if there are any trades left open.

 
            for (int i = OrdersTotal() - 1; i >= 0; i--) {
               OrderSelect(i, SELECT_BY_TICKET);
                  if ( OrderType() == OP_BUY  )
                  { int OldTicket=OrderTicket(); double lots=OrderLots();}
               RefreshRates(); //OrderSelect() needed ?
               double symAsk = NormalizeDouble( MarketInfo( Sym, MODE_ASK ), SymDigits );
               bool closeSellOrder = OrderClose( OldTicket, lots, symAsk, 0, CLR_NONE );
  1. if the type is a buy you set the ticket/size to pass to orderClose. What are you passing if it's NOT a buy to close? What if there are multiple open orders?
  2. You open a buy at the Ask, and close at the Bid. Why are you trying to close at the Ask?
  3. RefreshRates is unnecessary unless you use Bid/Ask and have multiple server calls.
  4. Why are you not checking the OrderClose, return code and printing a message on failure so you find out WHY.
  5. If the OrderSelect fails so does everything there after. Always test return codes
  6. Why are you selecting by ticket when you're passing a position index.
  7. Not filtering by magic number means the EA in incompatible with every other, including itself on other charts.
  8. it is never necessary to use normalizeDouble, it's a kludge, don't use it, ever. ref1 ref2
    for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == magic.number                 // my magic number
    &&  OrderSymbol()       == chart.symbol                 // and my pair.
    &&  OrderType()         == OP_BUY  
    ){
        bool closeSellOrder = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(), 0, CLR_NONE );
        if ( !closeSellOrder) Alert("OrderClose failed: ", GetLastError());
 

Thanks you guys, I will look into every one of your comments/suggestions. Looks like I got some work to do!

BTW, I actually have written an entire function solely dedicated for opening/closing errors, I just haven't incorporated it into my EA yet.

For the most part, don't errors mainly occur in live trading and not so much in backtesting?

I figured I really don't need the error function now since I am only using the EA for backtesting at this point.

 
outofdebt:

For the most part, don't errors mainly occur in live trading and not so much in backtesting?

I figured I really don't need the error function now since I am only using the EA for backtesting at this point.

Try opening a Buy order at Ask and see what happens in the Strategy Tester, try selecting an Order ticket number that doesn't exist and see what happens . . . you NEED to take notice of return values and you need to report errors . . . you need this information so you caan make yu code work BEFORE you even consider going to DEMO let alone Live.
 

Ok, I got working after hours and hours of racking my brain on it! LOL.

Now i just need to get the signal timing improved and filter out unwanted trades.

Reason: