Download MetaTrader 5

Not working BuySignal, please help a newbie

To add comments, please log in or register
marve7ou_marc
21
marve7ou_marc  

Dear Gents,

I just started coding a little bit with mql4 and after studying some literature I wrote my first own EA. Compiling was without errors, but I cant get my EA to go into positions. My Idea was to open a Buy and a Sell Position at once, when the current Bar is crossing a shortterm Moving Average.

The code is the following:

//Globale externe Variablen
extern int MA_short = 12;
extern int Std_short = 12;
extern double TPFactor = 0.75;
extern double SLFactor = 0.25;
extern double TradeLots = 0.1;
extern int MagicNumber = 12345;

//Globale Variablen
datetime StartPeriod;
bool NewPeriod;
bool BuySignal;
bool SellSignal;
double vspread = (double)MarketInfo(Symbol(),MODE_SPREAD);
int LongOrder;
int ShortOrder;

int OnInit()
  {
//---
   StartPeriod = Time[0];
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      //Neuen Periodenbeginn prüfen
      if(StartPeriod != Time[0])
         {
         NewPeriod = true;
         StartPeriod = Time[0];
         }
      else NewPeriod = false;
      
      //Marktdatenermitteln
      double MAshort = iMA(NULL,0,MA_short,0,MODE_EMA,PRICE_CLOSE,1);
      double StdDev = iStdDev(NULL,0,Std_short,0,MODE_EMA,PRICE_CLOSE,1);
      
      //Handelssignale ermitteln
      if(NewPeriod == true)
         {
         //BuySignal
         if(MAshort==(Ask-(vspread/2)))
            {
            BuySignal = true;
            }
         else BuySignal = false;
         
         //SellSignal
         if(MAshort==(Ask-(vspread/2)))
            {
            SellSignal = true;
            }
         else SellSignal = false;
         }
      else
         {
            BuySignal = false;
            SellSignal = false;
         }
         
         
      //Buy Signal umsetzen
      if(BuySignal == true)
         {
            while(LongOrder<=0)
               {
                  LongOrder = OrderSend(Symbol(),OP_BUY,TradeLots,Ask,10,0,0,"MaxingLong",MagicNumber,0,Green);
               }
         }
      //Sell Signal umsetzen
      if (SellSignal == true)
         {
            while(ShortOrder<=0)
               {
                  ShortOrder = OrderSend(Symbol(),OP_SELL,TradeLots,Bid,10,0,0,"MaxingShort",MagicNumber,0,Red);
               }
         }
         
      //StopLoss BuyOrder
      if(OrderSelect(LongOrder,SELECT_BY_TICKET)==true)
         {
            if(OrderCloseTime()==0 && OrderStopLoss()==0)
            {
               double StopLoss= NormalizeDouble(OrderOpenPrice()-(StdDev*SLFactor),Digits);
               bool OrderChange = OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,OrderTakeProfit(),0,Yellow);
            }
         }
         
      //StopLoss SellOrder
      if(OrderSelect(ShortOrder,SELECT_BY_TICKET)==true)
         {
            if(OrderCloseTime()==0 && OrderStopLoss()==0)
            {
               double StopLoss= NormalizeDouble(OrderOpenPrice()+(StdDev*SLFactor),Digits);
               bool OrderChange = OrderModify(OrderTicket(),OrderOpenPrice(),StopLoss,OrderTakeProfit(),0,Yellow);
            }
         }
         
       //TakeProfit BuyOrder
      if(OrderSelect(LongOrder,SELECT_BY_TICKET)==true)
         {
            if(OrderCloseTime()==0 && OrderTakeProfit()==0)
            {
               double TakeProfit= NormalizeDouble(OrderOpenPrice()+(StdDev*TPFactor),Digits);
               bool OrderChange = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,0,Orange);
            }
         }
         
       //TakeProfit SellOrder
      if(OrderSelect(ShortOrder,SELECT_BY_TICKET)==true)
         {
            if(OrderCloseTime()==0 && OrderTakeProfit()==0)
            {
               double TakeProfit= NormalizeDouble(OrderOpenPrice()-(StdDev*TPFactor),Digits);
               bool OrderChange = OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),TakeProfit,0,Orange);
            }
         }
         
      //Ticketnummer nach Closing auf 0 zurücksetzen
      if(OrderSelect(LongOrder,SELECT_BY_TICKET)==true)
         {
            if(OrderTicket()>0 && OrderCloseTime()>0) LongOrder=0;
         }
         
      if(OrderSelect(ShortOrder,SELECT_BY_TICKET)==true)
         {
            if(OrderTicket()>0 && OrderCloseTime()>0) ShortOrder=0;
         }
  }
//+------------------------------------------------------------------+
//| Tester function                                                  |
//+------------------------------------------------------------------+
double OnTester()
  {
//---
   double ret=0.0;
//---

//---
   return(ret);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   
  }
//+------------------------------------------------------------------+

Thank you in advance for any help.

whroeder1
17111
whroeder1  
marve7ou_marc: a Buy and a Sell Position at once,
  1. Can't be done, must be done sequentially. Can't be done on US brokers, no hedging allowed.
  2.        if(MAshort==(Ask-(vspread/2)))
    Doubles are rarely equal. The == operand. - MQL4 forum And that isn't checking for a cross.

  3.       if(BuySignal == true)
          if(OrderSelect(ShortOrder,SELECT_BY_TICKET)==true)
    
    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).
  4.  if(MAshort==(Ask-(vspread/2)))
                {
                BuySignal = true;
                }
             else BuySignal = false;
    Simplify https://www.mql5.com/en/forum/193541#comment_5118866
marve7ou_marc
21
marve7ou_marc  
whroeder1:
  1. Can't be done, must be done sequentially. Can't be done on US brokers, no hedging allowed.
  2. Doubles are rarely equal. The == operand. - MQL4 forum And that isn't checking for a cross.

  3. 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).
  4. Simplify https://www.mql5.com/en/forum/193541#comment_5118866


Thank you very much whroeder1,


I think I got most of your points, and now it is trading. But the sell and buy orders are most of the time executed not at the same time, can you maybe explain how to do the order sequentially to achieve simultaneous orders or what you mean by this in general. My Broker allows hedging in general. Thank you very very much.

To add comments, please log in or register