Problem with Strategy Tester

 
#property copyright " N.Huy "
#property link      ""

//+------------------------------------------------------------------+

extern double Initial_Equity = 3000 ;

extern double Profit_Rate = 5 ;

//+------------------------------------------------------------------+

double Profit_Equity ;

//+------------------------------------------------------------------+

int init ()
        {
                Profit_Equity = Initial_Equity + ( Initial_Equity * Profit_Rate / 100 ) ;
        }

//+------------------------------------------------------------------+

int start ()
        {
                ...
                if ( AccountEquity () >= Profit_Equity )
                        {
                                Close_Orders ( "All" ) ;
                                init() ;
                        }
                ...
        }
//+------------------------------------------------------------------+

int Close_Orders ( string Orders )
        {
                int Total = OrdersTotal () ;
                int Count = 0 ;
                RefreshRates () ;

                for ( Count = 0 ; Count <= Total ; Count ++ )
                        {
                                OrderSelect ( 0 , SELECT_BY_POS , MODE_TRADES ) ;

                                if ( Orders == "Buy" )
                                        {
                                                if ( OrderType () == OP_BUY )
                                                        {
                                                                OrderClose ( OrderTicket () , OrderLots () , MarketInfo ( OrderSymbol () , MODE_BID ) , 10 , NULL ) ;
                                                        }
                                        }

                                if ( Orders == "Sell" )
                                        {
                                                if ( OrderType () == OP_SELL )
                                                        {
                                                                OrderClose ( OrderTicket () , OrderLots () , MarketInfo ( OrderSymbol () , MODE_ASK ) , 10 , NULL ) ;
                                                        }
                                        }

                                if ( Orders == "All" )
                                        {
                                                if ( OrderType () == OP_BUY )
                                                        {
                                                                OrderClose ( OrderTicket () , OrderLots () , MarketInfo ( OrderSymbol () , MODE_BID ) , 10 , NULL ) ;
                                                        }

                                                if ( OrderType () == OP_SELL )
                                                        {
                                                                OrderClose ( OrderTicket () , OrderLots () , MarketInfo ( OrderSymbol () , MODE_ASK ) , 10 , NULL ) ;
                                                        }

                                                if ( !OrderType () == OP_SELL && !OrderType () == OP_BUY )
                                                        {
                                                                OrderDelete ( OrderTicket () ) ;
                                                        }
                                        }
                        }
        }

Hi! Everyone

I have a question. Why doesn't my EA close all orders when I back test it ? It means my EA doesn't close all orders when it earn enough money in Strategy Tester. How to fix it?

 
What error message is in the logs if any? Also it's better to loop down for order_close process ex) for(i=OrdersTotal()-1;i>=0;i--){do whatever;}
 
Hi! I corrected my EA. It worked. :D
 
  1. for ( Count = 0 ; Count <= Total ; Count ++ )
    
    You must count down when closing orders (multiple orders/multiple charts) Also the EA should only handle its own orders on its own chart.
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
    

  2. if ( OrderType () == OP_BUY )
    {
        OrderClose ( OrderTicket () , OrderLots () , MarketInfo ( OrderSymbol () , MODE_BID ) , 10 , NULL ) ;
    }
    
    if ( OrderType () == OP_SELL )
    {
        OrderClose ( OrderTicket () , OrderLots () , MarketInfo ( OrderSymbol () , MODE_ASK ) , 10 , NULL ) ;
    }
    
    if ( !OrderType () == OP_SELL && !OrderType () == OP_BUY )
    {
        OrderDelete ( OrderTicket () ) ;
    }
    
    The !OrderType == x could be interpretered as (!OrderType) == x which is wrong. Simplified:
    if ( OrderType () <= OP_SELL )
    {
        OrderClose ( OrderTicket () , OrderLots () , OrderClosePrice() , 10 , NULL ) ;
    }
    else
    {
        OrderDelete ( OrderTicket () ) ;
    }
    

 

HI,

i'm kinda newbi in this staffs.


my strategy tester "start" button don't even work, what's the problem?


anybody can help guys?

 
HajAmir_75 #:

HI,

i'm kinda newbi in this staffs.


my strategy tester "start" button don't even work, what's the problem?


anybody can help guys?

Check your strategy tester journal for errors, usually some settings are wrong.

 
Or maybe not enough history