My EA only opens buy positions

 

Hi all,


I'm creating an EA which call's  one of my custom indicators and i want to open positions when the indicator gives me arrow signals.

The problem is my EA is opening buy positions on both arrow's.

I'm going to leave my code here with the hope that someone more experience could help me! 


Many Thanks

Joao Santo  


//+------------------------------------------------------------------+
//|                                                        EATM5.mq4 |
//|                                                       Joao Santo |
//|                                        dataonly22@protonmail.com |
//+------------------------------------------------------------------+
#property copyright "Joao Santo"
#property link      "dataonly22@protonmail.com"
#property version   "1.00"
#property strict

#include <Customfunctions.mqh>

// chart window settings
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1  clrLimeGreen        //middle line color
#property indicator_color2  clrRed      //upper band color
#property indicator_color3  clrBlue     //lower band color
#property indicator_color4  clrSkyBlue  //up arrow
#property indicator_color5  clrCrimson  //down arrow
#property indicator_style1  STYLE_DASH  //centerline style
#property indicator_width1  1 //middle band line width
#property indicator_width2  2 //upper band line width
#property indicator_width3  2 //lower band line width

//----Trading Parameters
sinput  string  _0_             = "*-Trading Parameters-*";              
input  double InpLots           = 0.1;            // Lots      
input  double InpStopLoss       = 7000;            // Stop Loss (in pips)
input  double InpTakeProfit     = 60;             // Take Profit (in pips)
input  ushort InpTrailingStop   = 5;              // Trailing Stop (in pips)
input  ushort InpTrailingStep   = 1;              // Trailing Step (in pips)
input  uchar  InpMaxPositions   = 2;              // Max positions

//----Indicator Inputs
sinput string  _00_           = "*---Indicator Inputs---*";          
extern string TimeFrame       = "*--Current Time Frame--*";
extern int    HalfLength      = 33;
extern int    Price           = PRICE_WEIGHTED;
extern double BandsDeviations = 2.5;
extern bool   Interpolate     = true;  
extern bool   alertsOn        = true;
extern bool   alertsOnCurrent = false;
extern bool   alertsOnHighLow = true;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = true;
extern bool   alertsEmail     = false;

//double dnArrow  = EMPTY_VALUE;
//double upArrow  = EMPTY_VALUE;
double Slippage = 5;              // Difference of pips allowed bettewen Ask/Bid and server
string OrderCommentSTRING;
                      
// DECLARE string
//double stopLossPrice;
//double takeProfitPrice;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
      CalculateNormalizedDigits();
      return (0);
  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if (IsNewCandle()) CheckForSignal();
  }
  
//+------------------------------------------------------------------+
//+
//+------------------------------------------------------------------+
void CheckForSignal()
{
static datetime candletime = 0;
if (candletime != Time[0])
{
  
      //dit it made an UP ARROW on candle 1?
      double upArrow = iCustom(NULL,0,"StockInsider\\2NDTM51",TimeFrame,HalfLength,Price,BandsDeviations,3,1); 
      Print("UP ARROW value: " + upArrow + ". Time to buy!");  
   if (upArrow != EMPTY_VALUE)
   {
   
      EnterTrade(true);
      
   }
      upArrow = EMPTY_VALUE;
      //dit it made a DOWN ARROW on candle 1?
      double dnArrow = iCustom(NULL,0,"StockInsider\\2NDTM51",TimeFrame,HalfLength,Price,BandsDeviations,4,1); 
      Print("DOWN ARROW value: " + dnArrow + ". Time to sell!");  
   if (dnArrow != EMPTY_VALUE)
   {

      EnterTrade(false);
     
   }
candletime = Time[0];
}  
}

//+------------------------------------------------------------------+
//+To open a new trade                                               |
//+------------------------------------------------------------------+
double EnterTrade(bool value)
{
   int ordernr;
   if (value = true) 
   {
      Alert("");
      Alert("Price is Bellow the TM5, Sending BUY Order");
      double StopLossPrice = (Ask - InpStopLoss * CalculateNormalizedDigits());
      double TakeProfitPrice = CalculateTakeProfit(true,Ask,InpTakeProfit);
      Alert("Entry Price = " + Ask);
      Alert("Stop Loss Price = " + StopLossPrice);
      Alert("Take Profit Price = " + TakeProfitPrice);
      double OpenPrice=NormalizeDouble(Ask,Digits);
      
      
      OrderCommentSTRING = "EA TM5 BUY ORDER";
      ordernr = OrderSend(_Symbol,OP_BUY,InpLots,OpenPrice,Slippage,StopLossPrice,TakeProfitPrice);
       //We verify if the order has gone through or not and print the result
      if(ordernr>0){
         Print("Order ",ordernr," open");
      }
      else{
         Print("Order failed with error - ",GetLastError());
      }
     return true;
   }   
   else 
   {
      Alert("");
      Alert("Price is Above the TM5, Sending SELL Order");
      double StopLossPrice = (Bid - InpStopLoss * CalculateNormalizedDigits());
      double TakeProfitPrice = CalculateTakeProfit(false,Bid,InpTakeProfit);
      Alert("Entry Price = " + Bid);
      Alert("Stop Loss Price = " + StopLossPrice);
      Alert("Take Profit Price = " + TakeProfitPrice);
      double OpenPrice=NormalizeDouble(Bid,Digits);

         
      OrderCommentSTRING = "EA TM5 SELL ORDER";
      ordernr = OrderSend(_Symbol,OP_SELL,InpLots,OpenPrice,Slippage,StopLossPrice,TakeProfitPrice);
       //We verify if the order has gone through or not and print the result
      if(ordernr>0){
         Print("Order ",ordernr," open");
      }
      else{
         Print("Order failed with error - ",GetLastError());
      }  
      return false; 
   } 
}
   

//Creating a variable to store the Time a new candle start
//Populating the variable with the current server time
datetime NewCandleTime=TimeCurrent();

//Creating a boolean function that returns
//True if this is a new candle
//False if this is still a candle in formation
//Remember that the function returns true every time the you initialize the script
//for example the first time you load the indicator or EA and when you change timeframe
bool IsNewCandle(){

   //If the time of the candle when the function last run
   //is the same as the time of the time this candle started
   //return false, because it is not a new candle
   if(NewCandleTime==iTime(Symbol(),0,0)) return false;
   
   //otherwise it is a new candle and return true
   else{
      //if it is a new candle then we store the new value
      NewCandleTime=iTime(Symbol(),0,0);
      return true;
   }
}
 
I am looking on mobile.. Are you get any compling errors or warns! 
 
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.
 
Ahmet Metin Yilmaz:
I am looking on mobile.. Are you get any compling errors or warns! 

Hi Ahlmet, no at the moment with this code i am not getting any error's. but before yes, i was getting error 130 when it would open only buy positions. now it get no erros but it open buy positions on the SELL ARROW. 

 
Keith Watford:
In future please post in the correct section
I will move your topic to the MQL4 and Metatrader 4 section.

Hi Keith, I tried to but every time i log in it automatically direct's me to mql5.

 
João Santo:

Hi Keith, I tried to but every time i log in it automatically direct's me to mql5.

The MQL4 and Metatrader 4 section is in the MQL5 website.

https://www.mql5.com/en/forum/mql4

 
João Santo:

Hi all,


I'm creating an EA which call's  one of my custom indicators and i want to open positions when the indicator gives me arrow signals.

The problem is my EA is opening buy positions on both arrow's.

I'm going to leave my code here with the hope that someone more experience could help me! 


Many Thanks

Joao Santo  


Should be:

If(value == true)

Not

If (value = true)
 
This is why I say: You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
 
Andrey Barinov:
Should be:

If(value == true)

Not

If (value = true)

Thanks Andrey, the code works fine now! :)

 
William Roeder:
This is why I say: You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

Code working, many thanks! :)

Reason: