Download MetaTrader 5
To add comments, please log in or register
Do you need private account monitoring? The Signals will provide you one!
madmax999
26
madmax999 2015.09.22 01:45 

My EA places trades when I want it to, but the array I've created to record the trades details doesn't seem to work on a live chart.

Ive put the relevant bits of my EA code below:

//----
//+------------------------------------------------------------------+
//|                              Global Scope                        |
//+------------------------------------------------------------------+
//---- Global scope Declarations

bool Long_setup;
bool Short_setup;

double Entry_price_Long, Entry_price_Short;
double SL_Long, SL_Short;
double TP1_Long,TP1_Short;
double Move_stop_price_Long;
double Breakeven_price_Long;
double Move_stop_price_Short;
double Breakeven_price_Short;

double trade_array[15][99999]; 
                                                              
int e=0;       //entry
int sl=1;      //stoploss
int tp=2;      //take profit
int TP_type=3; //TP type(1-14)
int msp=4;     //price at which stop is moved
int brk=5;     //breakeven price
int closed=6;  //is the order open or closed, (1=open, 0=closed)
int sl4got=8;  
int sl4num=9; 
int sl4cnt=10;   
int modified=11;  
int ticket;    //ticket number of the trade

double debug1[15],debug2[15][99];
double debug3[15],debug4[15][99];

//End Global scope declarations


int PlaceTrade()
{
   ResetLastError();
   
   RefreshRates();
   bid=MarketInfo(symbol,MODE_BID);
   ask=MarketInfo(symbol,MODE_ASK);

            if(Short_setup==true && bid<=Entry_price_Short)
              { 
 
                  int sellresult_1=OrderSend(symbol,OP_SELL,Lot_size1,bid,2,SL_Short,TP1_Short,"TP1",MagicNum,0,clrNONE);
                  if(sellresult_1<0) { Print("TP1 Failed to Open New SELL ORDER ",symbol,"! Error code = ",GetLastError()); ResetLastError(); }
                   else { 
                         Print("-----Opened New SELL ORDER TP1 ",symbol); 
                         ticket=sellresult_1;
                         trade_array[e,ticket]=Entry_price_Short;                                                                              
                         trade_array[sl,ticket]=SL_Short;
                         trade_array[tp,ticket]=TP1_Short;
                         trade_array[TP_type,ticket]=1;
                         trade_array[msp,ticket]=Move_stop_price_Short;
                         trade_array[brk,ticket]=Breakeven_price_Short;
                         trade_array[closed,ticket]=1;                               
                         trade_array[sl4got,ticket]=0;                                
                         trade_array[sl4num,ticket]=0;
                         trade_array[sl4cnt,ticket]=0;
                         trade_array[modified,ticket]=0;
                         
                         debug1[msp]=ticket;
                         debug1[brk]=TP1_Short;
                         debug2[sl4got,sl4num]=ticket;
                         debug2[tp,modified]=TP1_Short;
                         
                         debug3[e]=ticket;
                         debug3[tp]=TP1_Short;
                         debug4[e,sl]=ticket;
                         debug4[tp,TP_type]=TP1_Short;
                         
                        
                        //DEBUG CODES 
                        Print("DEBUG Arrays ------ debug1 ticket: ",debug1[e]," debug1 TP: ",debug1[tp]," debug2 ticket: ",debug2[e,sl]," debug2 TP: ",debug2[tp,TP_type]," Error? ",GetLastError());
                        Print("DEBUG Arrays ------ debug3 ticket: ",debug3[msp]," debug3 TP: ",debug3[brk]," debug4 ticket: ",debug4[sl4got,sl4num]," debug4 TP: ",debug4[tp,modified]," Error? ",GetLastError());
                        
                        Print("Array Stats: E: ",DoubleToStr(trade_array[e,ticket],5)," SL: ",DoubleToStr(trade_array[sl,ticket],5)," tp: ",DoubleToStr(trade_array[tp,ticket],5)," TPlvl: ",DoubleToStr(trade_array[TP_type,ticket],5)," MSP: ",DoubleToStr(trade_array[msp,ticket],5));
                        Print("Array Stats: Break: ",DoubleToStr(trade_array[brk,ticket],5)," closed: ",trade_array[closed,ticket]);
                        Print("Actual numbers: Ticket=",ticket," E: ",DoubleToStr(Entry_price_Short,5)," SL: ",DoubleToStr(SL_Short,5)," TP1: ",DoubleToStr(TP1_Short,5)," msp: ",DoubleToStr(Move_stop_price_Short,5)," Brk: ",DoubleToStr(Breakeven_price_Short,5));
                        }
                Long_setup=false;            //trades have now been placed so variables are reset, so trades are not placed again this hour.
                Short_setup=false;   
                Trades_On=True;  
               }// end Short=true

 return(0);
}  

I added debug1[], debug2[][], debug3[], and debug4[][], to help me figure out where I was going wrong. They will not be part of the final EA.

Move_stop_price_Short, Entry_price_Short, SL_Short, TP1_Short,  and Breakeven_price_Short, are all double values and prices that have been calculated elsewhere in the EA. In the Print section of the code, the 'Actual numbers' part, has these values printed to the journal without going through an Array, as a control.


When I run the EA through the MT4 tester, I get the following in the journal:

2015.09.22 13:04:06    2015.08.31 07:00  AUDUSD,H1: open #7 buy 0.01 AUDUSD at 0.71436 sl: 0.71237 tp: 0.71578 ok
2015.09.22 13:04:06    2015.08.31 07:00  AUDUSD,H1: -----Opened New BUY ORDER TP1 AUDUSD
2015.09.22 13:04:06    2015.08.31 07:00  AUDUSD,H1: DEBUG Arrays ------ debug1 ticket: 5 debug1 TP: 0.7132 debug2 ticket: 5 debug2 TP: 0.7132 Error? 0
2015.09.22 13:04:06    2015.08.31 07:00  AUDUSD,H1: DEBUG Arrays ------ debug3 ticket: 5 debug3 TP: 0.7132 debug4 ticket: 5 debug4 TP: 0.7132 Error? 0
2015.09.22 13:04:06    2015.08.31 07:00  AUDUSD,H1: Array Stats: E: 0.71428 SL: 0.71237 tp: 0.71578 TPlvl: 1.00000 MSP: 0.71728
2015.09.22 13:04:06    2015.08.31 07:00  AUDUSD,H1: Array Stats: Break: 0.71429 closed: 1.00000
2015.09.22 13:04:06    2015.08.31 07:00  AUDUSD,H1: Actual numbers: Ticket=7 E: 0.71428 SL: 0.71237 TP1: 0.71578 msp: 0.71728 Brk: 0.71429

However when I run the EA on a live chart, I get:

2015.09.22 13:07:50.241    AUDUSD,H1: open #30013742 sell 0.01 AUDUSD at 0.71322 sl: 0.71378 tp: 0.71175 ok
2015.09.22 13:07:50.241    AUDUSD,H1: -----Opened New SELL ORDER TP1 AUDUSD
2015.09.22 13:07:50.241    AUDUSD,H1: DEBUG Arrays ------ debug1 ticket: 0 debug1 TP: 0 debug2 ticket: 0 debug2 TP: 0 Error? 0
2015.09.22 13:07:50.241    AUDUSD,H1: DEBUG Arrays ------ debug3 ticket: 0 debug3 TP: 0 debug4 ticket: 0 debug4 TP: 0 Error? 0
2015.09.22 13:07:50.241    AUDUSD,H1: Array Stats: E: 0.00000 SL: 0.00000 tp: 0.00000 TPlvl: 0.00000 MSP: 0.00000
2015.09.22 13:07:50.241    AUDUSD,H1: Array Stats: Break: 0.00000 closed: 0
2015.09.22 13:07:50.241    AUDUSD,H1: Actual numbers: Ticket=30013742 E: 0.71325 SL: 0.71378 TP1: 0.71175 msp: 0.71025 Brk: 0.71324

Why is all the array data is 0's? Whats different about the live chart that causes this problem? Is there something fundamentally wrong with the code?

Please help, Ive tried everything I can think of.

whroeder1
13596
whroeder1 2015.09.22 13:11  
madmax999: doesn't seem to work on a live chart.

Why is all the array data is 0's? Whats different about the live chart that causes this problem? Is there something fundamentally wrong with the code?

  1. Had you used property strict, you would know why. In the tester, ticket numbers are 1, 2, .... Live ticket numbers are 82935610, 82999464, 83253929. But you only allow a maximum of 99999.

  2. Don't use ints, when you mean an enumeration.
    double trade_array[15][99999]; 
                                                                  
    int e=0;       //entry
    int sl=1;      //stoploss
    int tp=2;      //take profit
    int TP_type=3; //TP type(1-14)
    int msp=4;     //price at which stop is moved
    int brk=5;     //breakeven price
    int closed=6;  //is the order open or closed, (1=open, 0=closed)
    int sl4got=8;  
    int sl4num=9; 
    int sl4cnt=10;   
    int modified=11;  
    int ticket;    //ticket number of the trade
    
    trade_array[e,ticket]=Entry_price_Short;
    Don't use a 2D array when you mean a struct.
    enum index ={e,       //entry
                 sl,      //stoploss
                 tp,      //take profit
                 TP_type, //TP type(1-14)
                 msp,     //price at which stop is moved
                 brk,     //breakeven price
                 closed,  //is the order open or closed, (1=open, 0=closed)
                 sl4got, sl4num=9, sl4cnt, modified, 
                 ticket,    //ticket number of the trade
                 INDEX_COUNT};
    double trade_array[INDEX_COUNT][99999]; 
    
    trade_array[e,ticket]=Entry_price_Short;
    Note the different datatypes in the struct.
    struct trade_info {
       double e;      //entry
       double sl;     //stoploss
       double tp;     //take profit
       int    TP_type;//TP type(1-14)
       double msp     //price at which stop is moved
       double brk     //breakeven price
       bool   closed; //is the order open or closed
       int    sl4got;
       int    sl4num;
       int    sl4cnt;
       bool   modified;
       int    ticket;    //ticket number of the trade
    };
    trade_info trade_array[99999]; 
    
    trade_array[ticket].e=Entry_price_Short;
    

madmax999
26
madmax999 2015.09.23 23:40  
Thanks WHRoeder. You've given me a lot to read up on. Once I wrap my head around your fixes, Ill post the updated code.
/
To add comments, please log in or register