//+------------------------------------------------------------------+ //| scalping_system.mq5 | //| Copyright 2022, trandinhhung. | //| | //+------------------------------------------------------------------+ #property copyright "Copyright 2022, trandinhhung." #property link "https://www.tng.com" #property version "1.0" #include // Common setting input long MagicNumber = 12345; // Magic number input string Comment = ""; // Comments // Check sideway condition input int SideWayBar = 10; // How many bars to check sideway 2 EMA line. input double SideWayValue = 10; // the value calculate between FastEMA and SlowEMA // Indicator Setting, Please update if something wrong from the seting value input int Fast_EMA = 34; // input int Slow_EMA = 200; // input int MACD_Fast = 26; // input int MACD_Slow = 9; // input int MACD_Period = 10; // input ENUM_APPLIED_PRICE MACD_Mode = Closed_Price; // // TP/SL setting input int MinSL = 100; // Minimum point used to set SL for the order. input int MaxSL = 200; // Maximum points used to set SL for the order. input int TP_1 = 1; // Percent rate with SL (if Entry - SL = 10 points => TP_1 = 10 points) input int TP_2 = 2; // Percent rate with SL (if Entry - SL = 10 points => TP_2 = 20 points) input int TP_3 = 3; // Percent rate with SL (if Entry - SL = 10 points => TP_3 = 30 points) input int TP_4 = 5; // Percent rate with SL (if Entry - SL = 10 points => TP_4 = 50 points) // Money Management input enum EntryType = EntryType; // Entry type input double Lots = 0; input double RiskPercent = 1; // Risk percent input int Money = 100; // The maximum amount if the price touch SL. enum EntryType { FixedLot, // Using fixed [Lots] input value to set. Enquity, // Based on account enquity and [RiskPercent] input value, entry and stop loss price to calculate a lots number. Balance, // Based on account balance and [RiskPercent] input value, entry and stop loss price to calculate a lots number. Money // Based on [Money] input value, entry and stop loss price to calculate a lots number. }; CTrade trade; int OnInit() { Print("Successfully initialized"); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { EventKillTimer(); } void OnTick() { // Process new position if (isNewBar() == true and isSideWay() == false) { // get value fastEMA, slowEMA and MACD from previous bar // get highPrice, openPrice, closePrice, lowPrice from previous bar // Buy condition if if (fastEMA > slowEMA && MACD_signal > 0 && MACD_line > 0) { // Green bar and slowEMA between low and high price if (closePrice > openPrice && (lowPrice < slowEMA && highPrice > slowEMA)) { double slValue = 0; // select highest price value from 2 previous bars. var entryPrice = 0; // current market price; if (slValue < MinSL) { slValue = MinSL; } if (slValue > MaxSL) { slValue = MaxSL; } double lots = 0; //Based on EntryType input value, calculate lots for the position. // Calculate TP value for 4 positions // Create 4 buy positions with TP input value. trade.Buy(); } } // Sell condition if if (fastEMA < slowEMA && MACD_signal < 0 && MACD_line < 0) { // Red bar and slowEMA between low and high price if (closePrice < openPrice && (lowPrice < slowEMA && highPrice > slowEMA)) { double slValue = 0; // select lowest price value from 2 previous bars. var entryPrice = 0; // current market price; if (slValue < MinSL) { slValue = MinSL; } if (slValue > MaxSL) { slValue = MaxSL; } double lots = 0; //Based on EntryType, calculate lots for the position. // Calculate TP value for 4 positions // Create 4 sell positions with TP input value. trade.Sell(); } } } // Update SL for existing positions. for (int i = 0; i < totalOpenOpsition(); i ++) { double slDistance = ticket.Price - ticket.StopLoss; // Please make sure it work for buy and sell position. if (math.abs(MarketPrice - ticket.Price) > slDistance) { // Update SL for the current ticket // new SL = ticket.StopLoss +(buy)/-(sell) slDistance; trade.PositionModify(ticket, new SL, ticket.takeProfit); } } } bool isSideWay() { get value from fastEMA and slowEMA from 1 to [SideWayBar] for (i = 1; i <= SideWayBar; i ++) { //get fastEMA value //get slowEMA value // compare if Absolute value between fast and slow Ema is greater than [SideWayValue] input value, return false; if (math.abs(fastEmaValue - slowEMAvalue) > [SideWayValue]) return false; } return true; } bool isNewBar() { // Check is new bar open. return false; }