Why the EA back test only selling?

 
I am a newbie, programming a Chandelier Exit with EMA entry and exit strategy.
When I backtest it, it’s only active sell order, but buy order useing the same logic code, so I am confusing why is no any active  buy order and also liquidating when the liquidation conditions are not met.
Is my program full of errors?
#include <Trade\Trade.mqh>
CTrade trade;
input double  Size_1         = 0.01;        // Size(1)
input double  Size_2         = 0.01;        // Size(2)
input double  Size_3         = 0.01;        // Size(3)
input int     DEMA_Period    = 250;         // EMA
input int     RSI_Period     = 14;          // ATR
input int     ATR_Period     = 14;          // ATR
input int     Range          = 7;          // Range
input int     Shift          = 1;         // shift
input int     ATRPeriod      = 14;         // Atr period
input double  ATRMultipl     = 3.0;          // Atr multiplier
const string  CE_Name        = "Chandelier Exit MT5";
int EMA;
int RSI;
int ATR;
int CE;
double Bid;
double Ask;
double AveragValue;
double belowlin;
double uplin;
double m_rsi;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   return (INIT_SUCCEEDED);
  }
//
void OnDeinit(const int reason)
  {
    
  }
//+------------------------------------------------------------------+
//| BUY/SELL                                                         |
//+------------------------------------------------------------------+
void OnTick(){
               
    EMA = iDEMA  (_Symbol, _Period, DEMA_Period, 0, PRICE_CLOSE);
   
    RSI = iRSI   (_Symbol, _Period, RSI_Period, PRICE_CLOSE);
   
    ATR = iATR   (_Symbol, _Period, ATR_Period);
   
    CE  = iCustom(_Symbol, _Period, CE_Name, Range, Shift, ATRPeriod, ATRMultipl);
    
    Ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK), _Digits);
    Bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), _Digits);
    
   double        Buffer_belowlin[];
     
      CopyBuffer(CE, 1, 0, 1, Buffer_belowlin);
        belowlin = Buffer_belowlin[0];
    
   double        Buffer_uplin[];
     
      CopyBuffer(CE, 2, 0, 1, Buffer_uplin);
      
        uplin = Buffer_uplin[0];
//EMA
   double MyEaArray [];
// ArraySetAsSeries(MyEaArray, true);
   CopyBuffer(EMA, 0, 0, 1, MyEaArray);
   AveragValue = MyEaArray[0];
   
//ATR
   double ATRNumber [];
//   ArraySetAsSeries(ATRNumber, true);
   
   CopyBuffer(ATR, 0, 0, 1, ATRNumber);
   
   double TPSL = ATRNumber[0];
   
   
   double RSINumber [];
   CopyBuffer(RSI, 0, 0, 1, RSINumber);
    m_rsi = RSINumber[0];
   
   
//Chart
   MqlRates PriceInfo [];
//  ArraySetAsSeries(PriceInfo, true);
   int PriceData = CopyRates(_Symbol, _Period, 0, 1, PriceInfo);
   string signal = "";
    if(PriceInfo[0].close > belowlin && PriceInfo[0].open > AveragValue)
    {
        signal = "buy";
    }
     else if(PriceInfo[0].close < uplin && PriceInfo[0].open < AveragValue)
    {
        signal = "sell";
    } 
       
         if(signal =="buy" && PositionsTotal()<=0){
         
          trade.Buy(Size_1, NULL, Ask, 0, PriceInfo[0].close + TPSL*2, NULL)&&
            
          trade.Buy(Size_2, NULL, Ask, 0, PriceInfo[0].close + TPSL*3, NULL)&&
          
          trade.Sell(Size_3, NULL, Ask, 0, 0, NULL);
          
          }
      
         if(signal =="sell" && PositionsTotal()<=0){
             
          trade.Sell(Size_1, NULL, Bid, 0, PriceInfo[0].close -  TPSL*2, NULL)&&
            
          trade.Sell(Size_2, NULL, Bid, 0, PriceInfo[0].close - TPSL*3, NULL)&&
          
          trade.Sell(Size_3, NULL, Bid, 0, 0, NULL);
          
         }     
     
    Colespositions();
     Comment("The signal below is: ", belowlin,
             "The signal up is: ", uplin,
             "The current signal is: ", signal); 
     
}
//+------------------------------------------------------------------+
//| Colespositions                                                   |
//+------------------------------------------------------------------+    
void Colespositions()
{
   MqlRates PriceInfo [];
//   ArraySetAsSeries(PriceInfo, true);
   int PriceData = CopyRates(_Symbol, _Period, 0, 1, PriceInfo);
   
    string signal = "";  
        
    for(int i = PositionsTotal()-1; i >= 0; i--)
    {
        ulong ticket = PositionGetTicket(i);
        { 
    if(PriceInfo[0].close < belowlin)
    {
        signal = "close_buy";
    }
     else if(PriceInfo[0].close > uplin)
    {
        signal = "close_sell";
    } 
    
            if( signal == "close_buy")
            {
                trade.PositionClose(ticket);
            }
             
            if(signal == "close_sell")
            {
                
                trade.PositionClose(ticket);
            }
            
              
            }
        }
    }

Files:
 
Attach only source code mq4/5 files.
 
埃萊尼·安娜·布拉諾 #:
僅附加原始碼 mq4/5 檔案。

I did not have the Chandelier Exit source code files.

Files:
test_4.0.mq5  11 kb
 
L W #:

I did not have the Chandelier Exit source code files.

i doubt that the sell trades have met your conditions due to the code. search this site for "indicator handle" and you should always Normalise your prices if you have used math or custom calculations.

 
Michael Charles Schefe #:

i doubt that the sell trades have met your conditions due to the code. search this site for "indicator handle" and you should always Normalise your prices if you have used math or custom calculations.

Here are some snippets from a different indicator's code. You will have to edit the variables, handle names, buffers, and logic to suit your code but it should give you the general layout.

On the global scope:

int handle_ma_now;
int handle_iatr;
int handle_istddev;

double ma_now[];
double iatr[];
double istddev[];

In Deinit():

void OnDeinit(const int reason)
  {
   if(handle_ma_now != INVALID_HANDLE) IndicatorRelease(handle_ma_now);
   if(handle_istddev != INVALID_HANDLE) IndicatorRelease(handle_istddev);
   if(handle_iatr != INVALID_HANDLE) IndicatorRelease(handle_iatr);      
  }

In OnInit():

        SetIndexBuffer(7, ma_now, INDICATOR_CALCULATIONS);
        SetIndexBuffer(8, iatr, INDICATOR_CALCULATIONS);
        SetIndexBuffer(9, istddev, INDICATOR_CALCULATIONS);

        handle_ma_now = iCustom(NULL, 0, "Custom_Moving_Average_sep_win", fast_period, 0, fast_period_method, Price_Constant);
        handle_istddev = iCustom(NULL, 0, "StdDev_sep_win", slow_period, 0, slow_period_method, Price_Constant);        
        handle_iatr = iCustom(NULL, 0, "ATR_sep_win", slow_period, Price_Constant);

In OnCalculate():

        if(CopyBuffer(handle_ma_now, 0, 0, copyBars, ma_now) <= 0
           || CopyBuffer(handle_istddev, 0, 0, copyBars, istddev) <= 0     
           || CopyBuffer(handle_iatr, 0, 0, copyBars, iatr) <= 0)

     slow_array[bar] = istddev[bar] - iatr[bar];
     
     if(ma_now[bar] >= ma_now[bar+1] && move_array[bar] >= min_move)
      {
       up_hist[bar] = slow_array[bar];
      }

Note that there is no NormalizeDouble() code here because price data is already normalized in the called indicators (not attached).

 
Michael Charles Schefe #i doubt that the sell trades have met your conditions due to the code. search this site for "indicator handle" and you should always Normalise your prices if you have used math or custom calculations.
Ryan L Johnson #Here are some snippets from a different indicator's code. You will have to edit the variables, handle names, buffers, and logic to suit your code but it should give you the general layout.
     if(PriceInfo[0].open > belowlin && PriceInfo[0].open > AveragValue)

        if( PositionsTotal()<=0){
         
          trade.Buy(Size_1, NULL, Ask, 0, PriceInfo[0].close + TPSL*2, NULL)&&
........
    }
     else if(PriceInfo[0].open < uplin && PriceInfo[0].open < AveragValue)

    {
        if(PositionsTotal()<=0){
             
          trade.Sell(Size_1, NULL, Bid, 0, PriceInfo[0].close -  TPSL*2, NULL)&&
......
    } 
   }
Thank you and why back test only sell no buy order? 
 The price below the EMA and Chandelier exit signal sell --> sell

The price higher than EMA and Chandelier exit signal buy --> buy

but both coding the same logic

 
L W #:
Thank you and why back test only sell no buy order? 
 The price below the EMA and Chandelier exit signal sell --> sell

The price higher than EMA and Chandelier exit signal buy --> buy

but both coding the same logic

because in your code you do not have handles. take especial note of Ryans response. I also recommend that you look in codebase for any ea. In an ea that uses indicators you will see how to use handles that will give you your signals.

Your code as it is, does not meet the signal conditions that you think.

EMA = iDEMA  (_Symbol, _Period, DEMA_Period, 0, PRICE_CLOSE);
does not give a price or ema price.