¿Por qué mi EA sigue dando ganancias negativas cuando hace pruebas de espalda? - página 4

 

este es mi último código. no puedo pensar en nada malo, excepto que no me está dando los resultados que quiero

//+------------------------------------------------------------------+
//|                                       RSI_strategy_cyxstudio.mq4 |
//|                                  Copyright 2013, Tjipke de Vries |
//|                                     https://forum.mql4.com/53695/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
#include <stderror.mqh>
#include <stdlib.mqh>


extern int RSIPeriod        =  2;      //number of periods for RSI
extern double UpperBound    =  95;     //set upper bound value for RSI
extern double LowerBound    =  5;      //set lower bound value for RSI

extern double Lots  = 0.1;
extern double StopLoss      = 60;       //Set the stop loss level
extern double TakeProfit    = 120;       //Set the take profit level
extern double TrailingStop = 40;
//extra settings for OrderSend
extern int        MagicNumber = 54333;
extern string     CommentEA = "RSI strategy";
extern int        Slippage.Pips    = 3;



//---
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----   
  Alert(OrdersTotal());

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int Ticket1;
   int Ticket2;
   bool Ticket3;
   bool Ticket4;
   
   double SL,TP;
   int Total;
   double MagicNo;
   double Slippage;
    int cnt;
   
   double pAsk = MarketInfo(Symbol(), MODE_ASK);
   double pBid = MarketInfo(Symbol(), MODE_BID);
   double MA200 = iMA(NULL, 1440, 200, 0,MODE_SMA,PRICE_CLOSE, 0);  //200 day Moving Average   
   double MA5 = iMA(NULL, 1440, 5, 0,MODE_SMA,PRICE_CLOSE, 0);      //  5 day Moving Average
   double CurrentRSI = iRSI (NULL, 0, RSIPeriod,PRICE_CLOSE ,0);
   double PrevRSI =  iRSI (NULL, 0, RSIPeriod,PRICE_CLOSE ,1);
   double LastRSI = iRSI (NULL, 0, RSIPeriod,PRICE_CLOSE ,2);
   
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   
   if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }



//Check for open orders if there are none then check for conditions to open one

      if (OrdersTotal() ==0 && LastRSI > PrevRSI && PrevRSI > CurrentRSI && CurrentRSI < LowerBound && pAsk > MA200) {    //Condition to execute buy entry
  
        Ticket1 = OrderSend(Symbol(), OP_BUY, Lots, pAsk, Slippage.Pips, pBid - ( StopLoss * Point ), pAsk + ( TakeProfit * Point ), "Buy.", MagicNumber,0,Yellow);       //execute buy order
   
    if(Ticket1>0)
           {
            if(OrderSelect(Ticket1,SELECT_BY_TICKET,MODE_TRADES)) 
               Print("BUY order opened : ",OrderOpenPrice());
            
           }
         if (Ticket1 < 0) {
         Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
   }  
  

  if (OrdersTotal() ==0 && LastRSI < PrevRSI && PrevRSI < CurrentRSI && CurrentRSI > UpperBound && pBid < MA200) {     //Condition to execute sell entry
  
       Ticket2 = OrderSend(Symbol(), OP_SELL, Lots, pBid, Slippage.Pips, pAsk + ( StopLoss * Point ), pBid - ( TakeProfit * Point ), "Sell.",MagicNumber, 0, Yellow)  ;     //execute sell order
       if(Ticket2>0)
           {
            if(OrderSelect(Ticket2,SELECT_BY_TICKET,MODE_TRADES)) 
               Print("SELL order opened : ",OrderOpenPrice());
           
           }
         if (Ticket2<0) {
          Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
   } 
      
      }
 


   
    int ticket=OrderTicket();
    double lots=OrderLots();
   
   
   for (int i = OrdersTotal() - 1; i >= 0; i--)
   {
   
      if (OrderSelect(i, SELECT_BY_POS))
      {
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {


         if (OrderType() == OP_BUY && pBid > MA5)
         {
          Ticket3 = OrderClose(ticket, lots, pBid, Slippage.Pips);
          
          if (Ticket3 == true ) {
          Print("BUY position closed", OrderClosePrice());
          }
          if (Ticket3 == false) {
          Print("Error closing BUY position", ErrorDescription(GetLastError()));
          }
          }
          
          if (OrderType() == OP_SELL && pBid < MA5)
          {
          Ticket4 = OrderClose(ticket, lots, pAsk, Slippage.Pips);
           if (Ticket4 == true ) {
          Print("SELL position closed", OrderClosePrice());
          }
          if (Ticket4 == false) {
          Print("Error closing SELL position", ErrorDescription(GetLastError()));
          }
    }
      }
      
   }
   }
   
   
   
        
        
           return(0);
}
 

Si miras en la página 2 este tema puedes encontrar lo que te di

//+------------------------------------------------------------------+
//|                                       RSI_strategy_cyxstudio.mq4 |
//|                                  Copyright 2013, Tjipke de Vries |
//|                                     https://forum.mql4.com/53695/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"


extern int RSIPeriod        =  3;      //number of periods for RSI//WHY DIDN't  YOU LEAVE THIS VALUE 3 
extern double UpperBound    =  90;     //set upper bound value for RSI//WHY DID YOU MAKE THIS TO 95
extern double LowerBound    =  5;      //set lower bound value for RSI
extern int MASlowPeriod     = 200;
extern int MAFastPeriod     = 5;
extern double Lots  = 0.1;
extern double StopLoss      = 60;       //Set the stop loss level
extern double TakeProfit    = 120;       //Set the take profit level
extern double TrailingStop = 40;
//extra settings for OrderSend
extern int        MagicNumber = 54333;
extern string     CommentEA = "RSI strategy";
extern int        Slippage.Pips    = 3;


int    BUYS=1,SELLS=1;                            //WHY DID YOU REMOVE THIS FROM YOUR CODE
//++++ These are adjusted for 5 digit brokers.
int     pips2points;      // slippage  3 pips    3=points    30=points
double  pips2dbl;         // Stoploss 15 pips    0.015      0.0150
int     Digits.pips;      // DoubleToStr(dbl/pips2dbl, Digits.pips)
//---
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----   
   if(Digits % 2 == 1)  // DE30=1/JPY=3/EURUSD=5 forum.mql4.com/43064#515262
     {pips2dbl = Point*10; pips2points = 10;   Digits.pips = 1;}
     else {pips2dbl = Point;    pips2points =  1;   Digits.pips = 0;}
     // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl //WHY DID YOU REMOVE THIS       
//----      
//??    Alert(OrdersTotal());   //WHAT IS THIS DOING HERE  THIS IS NOT IN MY CODE
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int Ticket;
   double SL,TP;
   int Total;
   
   double pAsk = MarketInfo(Symbol(), MODE_ASK);
   double pBid = MarketInfo(Symbol(), MODE_BID);
   double MA200 = iMA(NULL, 1440, MASlowPeriod, 0,MODE_SMA,PRICE_CLOSE, 0);  //200 day Moving Average   
   double MA5 = iMA(NULL, 1440, MAFastPeriod, 0,MODE_SMA,PRICE_CLOSE, 0);      //  5 day Moving Average
   double CurrentRSI = iRSI (NULL, 1440, RSIPeriod,PRICE_CLOSE ,0);
   //WHY DID YOU MAKE  PrevRSI and LastRSI  REMOVE IT IT IS NOT IN MY CODE
   
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   
   if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }


   if(OrdersTotal()<1)            //  WHY DID YOU REMOVE THIS
        {
         BUYS=0;
         SELLS=0;
        } 
 
28
cyxstudio2013.01.31 18:36
deVries:

Este es el comienzo.....

Pon tu comentario ..... sobre lo que es diferente a lo tuyo hasta ahora...

Luego echa un vistazo a https://www.mql5.com/en/forum/139654 e intenta hacer un bucle de cuenta atrás comprobando las operaciones


Como puedes ver, te pido que hagas un bucle que cuente las operaciones de compra y de venta por separado.

Ese es mi siguiente paso dentro del código

Solo te pido esa parte del código

hacer que cuente las operaciones de compra y venta por separado

 
deVries:

Si miras en la página 2 este tema puedes encontrar lo que te di



Lo quité porque no sabía cómo usarlo. Me diste el código parcialmente y no pude ver cómo funciona.
 

--

 
deVries:

28
cyxstudio2013.01.31 18:36
deVries:

Este es el comienzo.....

Poner su comentario ..... sobre lo que es diferente de la suya hasta el momento ...

Luego echa un vistazo a https://www.mql5.com/en/forum/139654 e intenta hacer un bucle de cuenta atrás comprobando las operaciones


Como puedes ver he pedido hacer un bucle de cuenta atrás comprobando las operaciones

Ese es mi siguiente paso dentro del código

Solo te pido esa parte del código

hacer que cuente las operaciones de compra y venta por separado

¿así?

int ticket=OrderTicket();
double lotes=OrderLots();
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {


//bloques de códigos a ejecutar

}
}

}

 
cyxstudio:

me lo quité porque no sabía cómo usarlo. me diste el código parcialmente y no pude ver cómo funciona.
 if(OrdersTotal()<1)  
        {
         BUYS=0;
         SELLS=0;
        } 

En el momento en que el EA se reinicia

BUYS se pone a 1

SELLS se establece en 1

OrdersTotal() da el total de todas las operaciones abiertas en su cuenta

Puede ser cero, entonces no tenemos operaciones abiertas y no necesitamos comprobar si hay operaciones de este EA

SiOrdersTotal() > 0 BUYS se mantiene en 1 y SELLS se mantiene en 1

tenemos que comprobar en ese caso si es de nuestro EA y tenemos que contar los diferentes tipos (buy,sell,buylimit....)

así que

 
cyxstudio:

¿así?

int ticket=OrderTicket();
double lotes=OrderLots();
for (int i = OrdersTotal() - 1; i >= 0; i--)
{
if (OrderSelect(i, SELECT_BY_POS))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {


//bloques de códigos a ejecutar

}
}

}

Usar el botón SRC

Este bucle sólo lo iniciamos ( para qué condición )

¿Cómo sabes que la operación seleccionada en el bucle es de compra o de venta?

¿Y cómo las cuentas?

 
deVries:

Usar el botón SRC

Este bucle sólo lo iniciamos ( para qué condición )

¿Cómo sabes que la operación seleccionada en el bucle es de compra o de venta?

¿Y cómo las cuentas?


opps.

 if (OrderType() == OP_BUY && pBid > MA5)
         {
          Ticket3 = OrderClose(ticket, lots, pBid, Slippage.Pips);
          
          if (Ticket3 == true ) {
          Print("BUY position closed", OrderClosePrice());
          }
          if (Ticket3 == false) {
          Print("Error closing BUY position", ErrorDescription(GetLastError()));
          }
          }
          
          if (OrderType() == OP_SELL && pBid < MA5)
          {
          Ticket4 = OrderClose(ticket, lots, pAsk, Slippage.Pips);
           if (Ticket4 == true ) {
          Print("SELL position closed", OrderClosePrice());
          }
          if (Ticket4 == false) {
          Print("Error closing SELL position", ErrorDescription(GetLastError()));
          }
    }
      }

para la función de comercio cerrado.

utilizando

 if (OrderType() == OP_SELL && pBid < MA5)

para diferenciar la compra y la venta.

¿hay algo malo en mis condiciones para abrir la orden?

if (OrdersTotal() == 0 )

¿debo eliminarlo y sustituirlo por el bucle que utilicé para la función de cierre de la orden?

 
cyxstudio:


opps.

para la función de comercio cerrado.

utilizando

para diferenciar la compra y la venta.

¿hay algo malo en mis condiciones para abrir la orden?

¿debo eliminarlo y sustituirlo por el bucle que utilicé para la función de cierre de la orden?

En este momento tienes que comprobar si ya hay una operación abierta

antes de abrir una operación tiene que saber si hay una operación abierta

todavía no veo que hayas hecho el recuento de operaciones

.

Echa un vistazo al código de la media móvil EA en su estación de metatrader y ver cómo se hace allí ....

Razón de la queja: