백 테스트 시 EA가 계속 마이너스 이익을 내는 이유는 무엇입니까? - 페이지 5

 
deVries :


메타 트레이더 스테이션에서 이동 평균 EA 코드를 살펴보고 어떻게 수행되는지 확인하십시오....


괜찮은
 
deVries :

이 순간에 이미 거래가 열려 있는지 확인해야 합니다.

거래를 열기 전에 거래가 열려 있는지 알아야 합니다

아직도 나는 당신이 거래를 세는 것을 볼 수 없습니다

.

메타 트레이더 스테이션에서 이동 평균 EA 코드를 살펴보고 어떻게 수행되는지 확인하십시오....


나는 많은 샘플을 읽고 그들의 코딩 스타일을 모방했습니다.

이것은 내 최신 코드입니다. 불행히도, 그것은 여전히 해서는 안 되는 마이너스 이익을 내고 있습니다.

//+------------------------------------------------------------------+
//|                                       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    = 10;



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

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
RefreshRates();
   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 pAskPrev = iClose(Symbol(),0,1);
   double pBidPrev = iClose(Symbol(),0,1);
   double pAskLast = iClose(Symbol(),0,2);
   double pBidLast = iClose(Symbol(),0,2);
   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) && (pAsk < pAskPrev) && (pAskPrev < pAskLast)) {    //Condition to execute buy entry
  
        Ticket1 = OrderSend(Symbol(), OP_BUY, Lots, pAsk, Slippage.Pips, pBid - ( StopLoss * Point ), pBid + ( 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 ), pAsk - ( 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 && (pBid > pBidPrev) && (pBidPrev > pBidLast))
         {
          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()));
          }
          }
      }
   }
   }
   
   
   for (int m = OrdersTotal() - 1; m >= 0; m--)
   {
   
      if (OrderSelect(m, SELECT_BY_POS))
      {
      if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == MagicNumber)) {


    if (OrderType() == OP_SELL && pAsk < 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);
}

어떤 부분이 잘못된 것인지 직접 말씀해 주시겠습니까?

 

나는 또한 이것을 시도했다

 //+------------------------------------------------------------------+
//|                                       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 maxTrades = 1 ;
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    = 10 ;



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


//----
   return ( 0 );
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return ( 0 );
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
RefreshRates();
   int Ticket1;
   int Ticket2;
   bool Ticket3;
   bool Ticket4;
   
   double SL,TP;

   double MagicNo;
   double Slippage;
  
   
   double pAsk = MarketInfo( Symbol (), MODE_ASK);
   double pBid = MarketInfo( Symbol (), MODE_BID);
   double pAskPrev = iClose( Symbol (), 0 , 1 );
   double pBidPrev = iClose( Symbol (), 0 , 1 );
   double pAskLast = iClose( Symbol (), 0 , 2 );
   double pBidLast = iClose( Symbol (), 0 , 2 );
   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
int ticket;
   int total=CheckOpenTrade(MagicNumber, Symbol ());
   
   if (total<maxTrades)
   {
       if ((LastRSI > PrevRSI) && (PrevRSI > CurrentRSI) && (CurrentRSI < LowerBound) && (pAsk > MA200) && (pAsk < pAskPrev) && (pAskPrev < pAskLast)) {     //Condition to execute buy entry
  
        Ticket1 = OrderSend ( Symbol (), OP_BUY, Lots, pAsk, Slippage.Pips, pBid - ( StopLoss * Point ), pBid + ( 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 ), pAsk - ( 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 ); 
        }
   } 
     } 
      
 
double lots=OrderLots();

total=CheckOpenTrade(MagicNumber, Symbol ());
   for ( int cnt=total- 1 ;cnt>= 0 ;cnt--)
    {
       OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
       if (   OrderType()<=OP_SELL                       // check for opened position 
         && OrderSymbol()== Symbol ()                   // check for symbol
         && OrderMagicNumber() == MagicNumber)         // my magic number
      {



         if (OrderType() == OP_BUY && pBid > MA5 && (pBid > pBidPrev) && (pBidPrev > pBidLast))
         {
          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 && pAsk < 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 );
}


int CheckOpenTrade( int iMN, string sOrderSymbol)
{
   int icnt, itotal, retval;
 
   retval= 0 ;
   itotal= OrdersTotal ();
 
       for (icnt=itotal- 1 ;icnt>= 0 ;icnt--) // for loop
      {
         OrderSelect (icnt, SELECT_BY_POS, MODE_TRADES);
         // check for opened position, symbol & MagicNumber
         if (OrderSymbol()== sOrderSymbol)
         {
             if (OrderMagicNumber()==iMN) 
               retval++;             
         } // sOrderSymbol
      } // for loop
 
   return (retval);
}
 
cyxstudio :


나는 많은 샘플을 읽고 그들의 코딩 스타일을 모방했습니다.

이것은 내 최신 코드입니다. 불행히도, 그것은 여전히 해서는 안 되는 마이너스 이익을 내고 있습니다.

어떤 부분이 잘못된 것인지 직접 말씀해 주시겠습니까?

나는 무엇이 잘못되었다고 말했는지 충분히 명확하지 않았습니까 ??

당신은 내가 당신에게 준 코드를 변경했습니다

그 부분에 대한 귀하의 변경 사항은 모두 잘못되었으며 잘못된 위치에 이전에 썼습니다 ...

 //+------------------------------------------------------------------+
//|                                       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
extern double UpperBound    =   90 ;     //set upper bound value for RSI
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 ;
//++++ 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        
//----      

//----
   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 );
   
   
   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 )
        {
         BUYS= 0 ;
         SELLS= 0 ;
        } 


   if (BUYS> 0 ||SELLS> 0 )    //condition start   LOOP FOR CHECKING TRADES THIS EA                     
    {
     BUYS= 0 ;
     SELLS= 0 ;             //RESET VALUES TO ZERO BEFORE START COUNTING
     for ( int i = OrdersTotal ()- 1 ; i >= 0 ; i--) 
     {
       if ( OrderSelect (i,SELECT_BY_POS,MODE_TRADES)== false )         break ;
       if (OrderMagicNumber()!=MagicNumber || OrderSymbol()!= Symbol ()) continue ;
       //.
       //.
       //---- check order type
       if (OrderType()==OP_BUY)  // <==  IMPORTANT FUNCTION TO KNOW WHAT KIND OF TRADE IS SELECTED
        {
         BUYS++;           //COUNT BUY TRADES
         //.
         if (pAsk > MA5) {OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage.Pips*pips2points,White);}
        }
 
       if (OrderType()==OP_SELL)
        {
         SELLS++;           //COUNT SELL TRADES
         //.
         if (pBid < MA5) {OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage.Pips*pips2points,White);}
        }
     }
    }

// MAKE PART FOR OPENING BUY TRADE             
..........          

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

나중에 줄 루프 내부에 더 많은 것이 있습니다.

루프 후 구매 거래를 여는 부분을 만드십시오 ...

 // MAKE PART FOR OPENING BUY TRADE           

4/5 자리 브로커에서 일해야 하고 ECN 계정에서 일해야 합니다.

.

.

.

.

나는 보는 것을 좋아한다

당신은 당신이 가지고 있던이 원래 코드를 변경

     if (CurrentRSI < LowerBound && MarketInfo( Symbol (), MODE_ASK) > MA200 ) 
       {     //Condition to execute buy entry
        Ticket = OrderSend ( Symbol (), OP_BUY, BuyVolume, Ask, 3 , Bid - ( StopLoss * Point ), Ask + ( TakeProfit * Point ), "Buy." , 111 , 0 ,Yellow)   ;       //execute buy order
   
         if (Ticket> 0 )
           {
             if ( OrderSelect (Ticket,SELECT_BY_TICKET,MODE_TRADES)) 
               Print ( "BUY order opened : " ,OrderOpenPrice());
           }
         if (Ticket < 0 ) 
           {
             Print ( "Error opening BUY order : " , GetLastError ()); 
             return ( 0 ); 
           }
         return ( 0 );
        }

4/5 자리 브로커에서 작동해야 하고 ECN 계정에서 작동해야 합니다.