EA opens only Buy Orders

 

Hello,


I coded a simple EA based on one indicator which uses inidcator values from the current chart and the H4 chart. During the Backtest it came to my intention, that the EA only opens Buy Orders. When I change the code to only Sell Signals, it doesnt open any Trades. Does someone have an idea why this EA only opens Buy Signals?

#include <Trade\Trade.mqh>

//create an instance of CTrade
CTrade trade;

int Signal;
int Confirmation;

int OnInit()
  {
   //Defined EA, current candle
   Signal = iCustom(_Symbol,PERIOD_CURRENT,"Indicator",PERIOD_CURRENT,1,PRICE_CLOSE,true);
   Confirmation  = iCustom(_Symbol,PERIOD_H4,"Indicator",PERIOD_H4,1,PRICE_CLOSE,true);
  
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
   //create a string for the signal
   string signal = "";
   
   //calculate the Ask price
   double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
   //calculate the Bid price
   double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   //create an array for the KV1 and KV4
   double KV1Array[];
   double KV4Array[];
   
   //sort the array from the current candle downwards
   ArraySetAsSeries(KV1Array,true);
   ArraySetAsSeries(KV4Array,true);
   
   //We fill the array with the price data
   CopyBuffer(Signal,0,0,3,KV1Array);
   CopyBuffer(Confirmation,1,0,3,KV4Array);
   
   //We calculate the value for the current candle
   double KV1Value0 = KV1Array[0];
   double KV4Value0 = KV4Array[0];
   
   //We calculate the value for the current+1 candle
   double KV1Value1 = KV1Array[1];
   double KV4Value1 = KV1Array[1];
   
   //BUY Signal
   if (KV4Value0 > 0)                        //Uptrend confirmation
   if (KV1Value0 > 0)
   if (KV1Value1 < 0)                        //Indicator closes above 0
      {
      signal = "Buy";
      }
      
   //SELL Signal
   if (KV4Value0 < 0)                        //Downtrend confirmation
   if (KV1Value0 < 0)
   if (KV1Value1 > 0)                        //Indicator closes above 0
      {
      signal = "Sell";
      }
      
   //Open trade
   if(signal == "Buy" && PositionsTotal()<1)
   trade.Buy(1,NULL,Ask,0,(Ask+150 * _Point),NULL);
   
   
   if(signal == "Sell" && PositionsTotal()<1)
   trade.Sell(1,NULL,Bid,0,(Bid-150 * _Point),NULL);
   
     }


Thanks in advance for your help!

Nils

 

So such conditions simply do not exist (an error in the logic of signal construction)

//--- BUY Signal
   if(KV4Array[0] > 0.0)            // uptrend confirmation
      if(KV1Array[0] > 0.0)
         if(KV1Array[1] < 0.0)      // indicator closes above 0
           {
            signal = "Buy";
           }
//--- SELL Signal
   if(KV4Array[0] < 0.0)            // downtrend confirmation
      if(KV1Array[0] < 0.0)
         if(KV1Array[1] > 0.0)      // indicator closes above 0
           {
            signal = "Sell";
           }
 
Vladimir Karputov:

So such conditions simply do not exist (an error in the logic of signal construction)

Hi Vladimir,

thanks for your quick reply.

So instead of 0 I need to use 0.0 ? If not could u pls explain it ?

Thanks,
Nils
 
Nils1990 :
Hi Vladimir,

thanks for your quick reply.

So instead of 0 I need to use 0.0 ? If not could u pls explain it ?

Thanks,
Nils

You need to fix: indicator logic, signal search logic.

 
Vladimir Karputov:

You need to fix: indicator logic, signal search logic.

Could u give me an example or a more specific advise? I read a lot articles, but Iam still new to this.

Thanks,
Nils
 
Nils1990:
Could u give me an example or a more specific advise? I read a lot articles, but Iam still new to this.

Thanks,
Nils

did you code this yourself?

did you try instead of 0 to use 0.0 ?

and it didnt work?

 
Ahmet Metin Yilmaz:

did you code this yourself?

did you try instead of 0 to use 0.0 ?

and it didnt work?

Yes I coded it by myself. I used 0.0 and still only Buy trades.
 
Nils1990 :
Yes I coded it by myself. I used 0.0 and still only Buy trades.

If there is an open order, it will not open a second order. If the first opened order is buy, it does not open the sell order.

 
Ahmet Metin Yilmaz:

If there is an open order, it will not open a second order. If the first opened order is buy, it does not open the sell order.

So it means, because the 1st Signal is a Buy Signal it only trades Buy Signals? I just deleted the Buy Signal condition, the EA should only tradesSell Signals, but no trades?!

The logic should be fine, because it triggers the Buy Signal condition and they look fine (Value based).


Do you have an idea where I need to fix my code? Vladimir said already that the " indicator logic, signal search logic" needs to be fixed, but I have no idea where.

 
Nils1990 :

So it means, because the 1st Signal is a Buy Signal it only trades Buy Signals? I just deleted the Buy Signal condition, the EA should only tradesSell Signals, but no trades?!

The logic should be fine, because it triggers the Buy Signal condition and they look fine (Value based).


Do you have an Idea where I need to fix my code? Vladimir said alread that the " indicator logic, signal search logic" needs to be fixed, but I have no idea where.

We only see the logic of opening orders here

maybe the problem is in the indicator you use

   //We calculate the value for the current+1 candle
   double KV1Value1 = KV1Array[1];   ////
   double KV4Value1 = KV1Array[1];   //// two double equal to same parameters KV4Value1=KV4Array[1]????
   
   //BUY Signal
   if (KV4Value0 > 0)                        //Uptrend confirmation
   if (KV1Value0 > 0)
   if (KV1Value1 < 0)                        //Indicator closes above 0
      {
      signal = "Buy";
      }
      
   //SELL Signal
   if (KV4Value0 < 0)                        //Downtrend confirmation
   if (KV1Value0 < 0)
   if (KV1Value1 > 0)                        //Indicator closes above 0
      {
      signal = "Sell";
      }


And the logic should be like this

as a sample

if(KV4Value0<0.0 && KV1Value0<0.0 || KV1Value1>0.0) { signal = "Sell"; }
 
Ahmet Metin Yilmaz:

We only see the logic of opening orders here

maybe the problem is in the indicator you use



And the logic should be like this

as a sample

I attahced the indicator. I hope its not to much to ask for, but could u maybe have a quick look at it?

During the visual backtest I found out the EA opens more trades then it should. Sometimes it only opens 1 trade and sometimes more, see below. Any idea how is that possible, if the EA checks the open positions before he opens a trade?

Files:
Screen.JPG  21 kb
Reason: