help with code

 
//+------------------------------------------------------------------+
//|                                                 ema-pips-mt4.mq4 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
// EMA - RSI - Pips- Trail Strategy
extern double RiskValue = 0.01; // Amount of contracts to trade Pineconnector
extern int prof = 1000; // Profit Points
extern int sl = 300; // Stop Loss Points

input double TrailingStart = 50;  // Trailing start distance in pips
input double TrailingStop = 10;   // Trailing stop distance in pips
input double TrailingStep = 5;    // Trailing step in pips

double TrailStop = 0; // Variable to store trailing stop value



extern int fast = 5; // Fast EMA
extern int slow = 8; // Slow EMA
extern int dir = 200; // Dir EMA

double rsi;

// Initialization function
int OnInit()
{
    return(INIT_SUCCEEDED);
}

// Execution function
void OnTick()
{

        static datetime timestamp;
        datetime time = iTime(Symbol(), PERIOD_CURRENT, 0);
        
        if (timestamp != time)
        {
            timestamp = time;
            
            
            static int ema1 = iMA(Symbol(), 0, fast, 0, MODE_EMA, PRICE_CLOSE, 0);
            static int ema2 = iMA(Symbol(), 0, slow, 0, MODE_EMA, PRICE_CLOSE, 0);
            static int ema3 = iMA(Symbol(), 0, dir, 0, MODE_EMA, PRICE_CLOSE, 0);
            
               
            rsi = iRSI(Symbol(), 0, 10, PRICE_CLOSE, 0);
            
            if (Close[0] > ema1 && Close[0] > ema2 && Close[0] > ema3 && rsi < 50)
            {
                double ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
                double longsl = ask - sl * SymbolInfoDouble(Symbol(), SYMBOL_POINT);
                OrderSend(Symbol(), OP_BUY, RiskValue, ask, 3, longsl, ask + prof * Point, "Long", 0, 0, Blue);    
            }
            if (Close[0] < ema1 && Close[0] < ema2 && Close[0] < ema3 && rsi > 50)
            {
                double bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
                double shortsl = bid + sl * SymbolInfoDouble(Symbol(), SYMBOL_POINT);
                OrderSend(Symbol(), OP_SELL, RiskValue, bid, 3, shortsl, bid - prof * Point, "Short", 0, 0, Red);
            }     
          }
            
            // Trailing stop logic for open positions
    for (int k = OrdersTotal() - 1; k >= 0; k--)
    {
        if (OrderSelect(k, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderType() <= OP_SELL && OrderSymbol() == Symbol())
            {
                double openPrice = OrderOpenPrice();
                double currentPrice = MarketInfo(OrderSymbol(), MODE_BID);

                // Calculate trailing stop levels
                if (OrderType() == OP_BUY)
                {
                    if (currentPrice - openPrice > TrailingStart * Point)
                    {
                        TrailStop = currentPrice - TrailingStop * Point;
                        TrailStop = NormalizeDouble(TrailStop, Digits);
                        if (TrailStop > OrderStopLoss())
                        {
                            if (TrailStop - OrderStopLoss() >= TrailingStep * Point)
                            {
                                OrderModify(OrderTicket(), OrderOpenPrice(), TrailStop, OrderTakeProfit(), 0, Blue);
                            }
                        }
                    }
                }
                else if (OrderType() == OP_SELL)
                {
                    if (openPrice - currentPrice > TrailingStart * Point)
                    {
                        TrailStop = currentPrice + TrailingStop * Point;
                        TrailStop = NormalizeDouble(TrailStop, Digits);
                        if (TrailStop < OrderStopLoss())
                        {
                            if (OrderStopLoss() - TrailStop >= TrailingStep * Point)
                            {
                                OrderModify(OrderTicket(), OrderOpenPrice(), TrailStop, OrderTakeProfit(), 0, Red);
                            }
                        }
                    }
                }
            }
        }
    } 
}

Hi,


I'm new to mql4 and have started with a simple code.

It seems to work fine except that it doesn't open short positions. At the moment only opens long positions.

Can you help me to figure out why is this happening?

Thanks a lot.

Files:
3EMA-mt4.txt  5 kb
 
            static int ema1 = iMA(Symbol(), 0, fast, 0, MODE_EMA, PRICE_CLOSE, 0);
            static int ema2 = iMA(Symbol(), 0, slow, 0, MODE_EMA, PRICE_CLOSE, 0);
            static int ema3 = iMA(Symbol(), 0, dir, 0, MODE_EMA, PRICE_CLOSE, 0);

Those are not assignments; they are initialization of a common (globally declared), or static variable(s) with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
despinozaj:
              static int ema1 = iMA(Symbol(), 0, fast, 0, MODE_EMA, PRICE_CLOSE, 0);             static int ema2 = iMA(Symbol(), 0, slow, 0, MODE_EMA, PRICE_CLOSE, 0);             static int ema3 = iMA(Symbol(), 0, dir, 0, MODE_EMA, PRICE_CLOSE, 0);
 
          double ema1 = iMA(Symbol(), 0, fast, 0, MODE_EMA, PRICE_CLOSE, 0);
          double ema2 = iMA(Symbol(), 0, slow, 0, MODE_EMA, PRICE_CLOSE, 0);
          double ema3 = iMA(Symbol(), 0, dir, 0, MODE_EMA, PRICE_CLOSE, 0);
 
Thank you very much!!