{\rtf1\ansi\ansicpg1252\cocoartf2709 \cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica;} {\colortbl;\red255\green255\blue255;} {\*\expandedcolortbl;;} \paperw11900\paperh16840\margl1440\margr1440\vieww11520\viewh8400\viewkind0 \pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0 \f0\fs24 \cf0 //+------------------------------------------------------------------+\ //| 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);\ \}\ \}\ \}\ \}\ \}\ \}\ \} \ \}}