Questions from Beginners MQL4 MT4 MetaTrader 4 - page 264

czap1  

Hello,

 

I am new to Metatrader. I would like to test run a simple code for EURUSD.

 

At 08:00m on a Wednesday, if the previous Monday was an up day (i.e close 24:00 >open 00:00), then buy 1 contract. If the previous Monday was a down day, then sell 1 contract.

At 20:00, close the position.

 

Repeat for Thursday.

If the previous Tuesday was an up day (i.e close 24:00 >open 00:00), then buy 1 contract. If the previous Tuesday was a down day, then sell 1 contract.

At 20:00, close the position.

 

Can you assist here?

 

Thank you.

CC

William Roeder  
czap1 #: Can you assist here?

Help you with what? You haven't stated a problem, you stated a want.
     How To Ask Questions The Smart Way. (2004)
          Prune pointless queries.

You have only four choices:

  1. Search for it (CodeBase or Market). Do you expect us to do your research for you?

  2. Try asking at:

  3. MT4: Learn to code it.
    MT5: Begin learning to code it.

    If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

  4. Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2019)

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help (2017)

czap1  
William Roeder #:

Help you with what? You haven't stated a problem, you stated a want.
     How To Ask Questions The Smart Way. (2004)
          Prune pointless queries.

You have only four choices:

  1. Search for it (CodeBase or Market). Do you expect us to do your research for you?

  2. Try asking at:

  3. MT4: Learn to code it.
    MT5: Begin learning to code it.

    If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

  4. Or pay (Freelance) someone to code it. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum (2019)

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help (2017)

Hello William, thank you for your helpful comments. As said, I am new to metatrader. I have previously used excel VB forums where programmers helped me with building an application. Clearly this is not the case here. I will try the 'coding help' and 'Freelancer' links that you provided.
Balint Rubovszky  

I have written an MQL4 Expert Advisor code: It should open a BUY position, if the EMA12 crosses above the EMA28, and then a SELL position, when the EMA12 crosses below the EMA28. EMA values are rounded to 5 decimals. It examines this condition at the close of every 1 minute candle. Only one BUY and one SELL should be traded, so I used a flag variable to achieve that. When EMA12 gets equal to EMA28, it is also considered a crossing. The EA should only operate from a begin time. This is only a test code, so instead of trade orders, only alert messages are sent. When I compile the code and run it on MT4, it does not work! It should give an alert message at the beginning of every minute "one minute has passed", but it does not do that. It should also give an alert message on BUY and SELL, but it does not do that! Why is that? Please, help!

Refer to "GDAX EMA Cross[26,12] by stefano89" indicator on TradingView, since this EA was inspired by and should work like that indicator.

Here is the code, which I have written:

//+------------------------------------------------------------------+
//|                                           EMA_12_26_crossing.mq4 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
extern datetime BeginTime = D'2023.05.25 17:01:00'; // trading should begin at this time; only one cross up and one cross down of EMA12 and EMA28 should be traded afterwards
extern double BaseATRMultiplier = 3; // these are not used in current probe version
extern double StopATRMultiplier = 1.15; // these are not used in current probe version
extern double ProfitATRMultiplier = 2.05; // these are not used in current probe version

int OnInit()
  {  
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+

// This expert advisor should only be started when the EMA12 is below the EMA28; an opposite expert advisor should be written and used for the other case

void OnTick()
  {

    static bool SignalForBuy = false; // this is a flag to track if there has already been and upward EMA crossing; we use this to make sure we only trade one such crossing
    static bool SignalForSell = false; // this is a flag to track if there has already been and downward EMA crossing; we use this to make sure we only trade one such crossing
    double ExponentialMovingAverage12[2];
    double ExponentialMovingAverage28[2];
    static datetime OneMinuteCandleTime = BeginTime;
 
       if (TimeCurrent() >= BeginTime)
   
          {

            if(OneMinuteCandleTime != iTime(Symbol(),PERIOD_M1,0)) // new candle on the 1 minute timeframe
            
               {
                                                       
                     Alert("One minute has passed.");
                     OneMinuteCandleTime = iTime(Symbol(),PERIOD_M1,0);
         
                     ExponentialMovingAverage12[1] = NormalizeDouble(iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,1),5);
                     ExponentialMovingAverage12[2] = NormalizeDouble(iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE,2),5);
                     ExponentialMovingAverage28[1] = NormalizeDouble(iMA(NULL,0,28,0,MODE_EMA,PRICE_CLOSE,1),5);
                     ExponentialMovingAverage28[2] = NormalizeDouble(iMA(NULL,0,28,0,MODE_EMA,PRICE_CLOSE,2),5);
   
                     
                     if (SignalForBuy == false) // if there has't been an upward EMA crossing, we will trade; we only want to trade the first downward crossing
         
                         {

                              if (ExponentialMovingAverage12[2] - ExponentialMovingAverage28[2] < 0 && ExponentialMovingAverage12[1] - ExponentialMovingAverage28[1] >= 0 )
                  
                               {
              
                                     SignalForBuy = true;  // we set this to "true", so that only one upward EMA crossing is traded
                                     Alert("We will BUY at this time.");
                     
                               }
                  
                         }  
          
       
                     if (SignalForSell == false) // if there has't been a downward EMA crossing, we will trade; we only want to trade the first upward crossing
         
                         {

                              if ((ExponentialMovingAverage12[2] - ExponentialMovingAverage28[2] > 0 && ExponentialMovingAverage12[1] - ExponentialMovingAverage28[1] <= 0 ) || (ExponentialMovingAverage12[2] - ExponentialMovingAverage28[2] == 0 && ExponentialMovingAverage12[1] - ExponentialMovingAverage28[1] < 0 ))
                  
                               {
              
                                     SignalForSell = true; // we set this to "true", so that only one downward EMA crossing is traded
                                     Alert("We will SELL at this time");
                       
                               }
                  
                         }
             
    
    
               }

   
          }
      
      
  }
Reason: