| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- //+------------------------------------------------------------------+
- //| line_level_ea.mq5 |
- //| Copyright 2025, MQL Development |
- //| https://www.mqldevelopment.com/ |
- //+------------------------------------------------------------------+
- #property copyright "Copyright 2025, MQL Development"
- #property link "https://www.mqldevelopment.com/"
- #property version "1.1"
- #include <Trade\Trade.mqh>
- CTrade trade;
-
- enum tradeType
- {
- buyTrades, // Buy
- sellTrades, // Sell
- };
-
- enum NewsCloseOrder
- {
- CloseAllRunningOrder=0,
- LetTheOrderRun=1,
- };
- enum selectLine
- {
- LineOnNewsBar =0,
- LineOnNewsStop=1
- };
-
- #import "line_level_news_filter.ex5"
-
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void initiateNews();
- int returnNewsStatus(bool High_Impact_News=true
- ,int High_Start_Time=60//Stop Trade before high News (min)
- ,int High_Stop_Time=15 //Stop Trade after high News (min)
- ,bool show_high_line=true//Show verticle Line when high news comes
-
- ,bool Medium_Impact_News=true
- ,int Medium_Start_Time=60//Stop Trade before medium News (min)
- ,int Medium_Stop_Time=15 //Stop Trade after medium News (min)
- ,bool show_medium_line=true//Show verticle Line when medium news comes
-
- ,bool Low_Impact_News=true
- ,int Low_Start_Time=60//Stop Trade before low News (min)
- ,int Low_Stop_Time=15 //Stop Trade after low News (min)
- ,bool show_low_line=true//Show verticle Line when low news comes
-
- ,string symbol=""
- ,string expert=""
- ,int GMT_Broker_Time=2
- ,selectLine sl=0
- ,string extraSymbol=""
- );
- #import
-
-
- input string string_0 = "<><><><><><> General SETTINGS <><><><><><>"; //__
- input int magic_no = 333; // Magic no
- input tradeType selectSide = buyTrades; // Select Side
- input double lot_size = 0.1; // Lot Size
- input double lot_multiplier = 2; // Lot Multiplier on Loss Trade
- input double levels_distence = 10; // Levels Distance in Dollars
- input double stoploss = 10; // Fixed Stop Loss in Pips
- input double takeprofit = 10; // Fixed Take Profit in Pips
- input bool countinueCycleAfterProfit = true; // Continue Trading After Profit
- input bool openTradeSameLevel = false; // Open trade on the same level
-
- input string time_setting = "<><><><><> Time Filter Settings <><><><><>"; //_
- input bool EnableTimeFilter = false; // Enable Time Filter
- input string startTime = "03:00"; // Start Time Session
- input string endTime = "09:00"; // End Time Session
-
- input string news = "<><><><><><> News Settings <><><><><><>"; // News
- input NewsCloseOrder newsClose = CloseAllRunningOrder; // On News Action on Running Orders
- input bool High_Impact_News = true; //High Impact News
- input int High_Start_Time = 60; //Stop Trade before high News (min)
- input int High_Stop_Time = 15; //Stop Trade after high News (min)
- input bool show_high_line = true; //Show verticle Line when high news comes
- input bool Medium_Impact_News = true; //Medium Impact News
- input int Medium_Start_Time = 60; //Stop Trade before medium News (min)
- input int Medium_Stop_Time = 15; //Stop Trade after medium News (min)
- input bool show_medium_line = true; //Show verticle Line when medium news comes
- input bool Low_Impact_News = true; //Low Impact News
- input int Low_Start_Time = 60; //Stop Trade before low News (min)
- input int Low_Stop_Time = 15; //Stop Trade after low News (min)
- input bool show_low_line = true; //Show verticle Line when low news comes
- input selectLine Select_News_Line = 0; //News Line
- input bool mobileAlert = true; //Mobile Alert
-
-
- int GMT_Broker_Time = +2; //GMT_Broker_Time Time of your Broker
-
- // Global Variables
- double above_level = 0, below_level = 0;
- static double tickCurrentBid = 0, tickCurrentAsk = 0;
- double tickPreviousBid = 0, tickPreviousAsk = 0;
- datetime startTradingTime = 0, endTradingTime = 0;
- string sep = ":"; // A separator as a character
- ushort u_sep; // The code of the separator character
- string result1[];
- datetime ea_start_time = 0;
- bool tradeNow = true;
- bool printingDisplay = true;
- string Lname="newsLabel3";
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //---
-
- // news
- int timeDifference = (int)TimeCurrent() - (int)TimeGMT();
- Print("Time Difference is: ", timeDifference);
- if(timeDifference > 0)
- {
- GMT_Broker_Time = +timeDifference/3600;
- }
- else
- if(timeDifference < 0)
- {
- GMT_Broker_Time = timeDifference/3600;
- }
- else
- if(timeDifference == 0)
- {
- GMT_Broker_Time = timeDifference;
- }
-
- Print("Gmt Time: ", TimeGMT(), " Broker Gmt Time: ", GMT_Broker_Time);
-
- if(!MQLInfoInteger(MQL_TESTER))
- {
- initiateNews();
- }
-
-
- // Fill Values above and below levels
- trade.SetExpertMagicNumber(magic_no);
- trade.SetDeviationInPoints(10);
- trade.SetTypeFilling(ORDER_FILLING_IOC);
- trade.LogLevel(LOG_LEVEL_ALL);
- trade.SetAsyncMode(false);
- ea_start_time = TimeCurrent();
- double currentPrice = iClose(Symbol(), PERIOD_CURRENT, 0);
- getNearestLevels(currentPrice, levels_distence, below_level, above_level);
-
- //---
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason)
- {
- //---
- ObjectsDeleteAll(0, -1, OBJ_HLINE);
- }
- //+------------------------------------------------------------------+
- //| Main Activity function |
- //+------------------------------------------------------------------+
- void mainActivity()
- {
- //---
- double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
- double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
- tickPreviousBid = tickCurrentBid;
- tickCurrentBid = Bid;
- tickPreviousAsk = tickCurrentAsk;
- tickCurrentAsk = Ask;
- timeConversion();
- // Comment(" Below Value is: ", below_level, " Above Value: ", above_level, " Previous Tick: ", tickPreviousAsk, " Tick Current Ask: ", tickCurrentAsk);
- double lastLot = 0;
- if(!countinueCycleAfterProfit)
- if(selectLatestTicket(lastLot) > 0)
- {
- tradeNow = false;
- }
- // Comment(" Trades Count: ", orderCount(), " Below Value is: ", below_level, " Above Value: ", above_level);
- if(openTradeSameLevel)
- {
- if(orderCount() == 0)
- {
- double latestClosePrice = getClosePriceByTicket();
- if(latestClosePrice > 0)
- {
- getNearestLevels(latestClosePrice, levels_distence, below_level, above_level);
- if(printingDisplay)
- {
- Print(" ---------------------------------- Resetting Level no trade open. Setting new Levels using last ticket close price: ", latestClosePrice, " ------------------------------------------------ ");
- Print(" Current Price: ", latestClosePrice, " Below Value is: ", below_level, " Above Value: ", above_level);
- printingDisplay = false;
- }
- }
- }
- else
- {
- printingDisplay = true;
- }
- }
- if(tradeNow)
- if((EnableTimeFilter && TimeCurrent() >= startTradingTime && TimeCurrent() < endTradingTime) || !EnableTimeFilter)
- {
- if(above_level == 0 && below_level == 0)
- {
- double currentPrice = iClose(Symbol(), PERIOD_CURRENT, 0);
- getNearestLevels(currentPrice, levels_distence, below_level, above_level);
- Print(" Price Levels Updated on New Session Start. ");
- }
- if(selectSide == buyTrades) // Buy trade case
- {
- if(((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level)) ||
- ((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
- {
- placeBuyTrade();
- Print(" ----------------------------- Buy Trade Executed and Levels Updated -----------------------------------------------------");
-
- double currentPrice = 0;
- if((tickPreviousAsk < above_level) && (tickCurrentAsk >= above_level))
- {
- currentPrice = above_level;
- }
- else
- if(((tickPreviousAsk > below_level) && (tickCurrentAsk <= below_level)))
- {
- currentPrice = below_level;
- }
- getNearestLevels(currentPrice, levels_distence, below_level, above_level);
-
- Print(" Current Price: ", currentPrice, " Below Value is: ", below_level, " Above Value: ", above_level);
- }
- }
- if(selectSide == sellTrades)
- {
- if(((tickPreviousBid < above_level) && (tickCurrentBid >= above_level)) ||
- ((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
- {
- placeSellTrade();
- Print(" ----------------------------- Sell Trade Executed and Levels Updated -----------------------------------------------------");
-
- double currentPrice = 0;
- if((tickPreviousBid < above_level) && (tickCurrentBid >= above_level))
- {
- currentPrice = above_level;
- }
- else
- if(((tickPreviousBid > below_level) && (tickCurrentBid <= below_level)))
- {
- currentPrice = below_level;
- }
- getNearestLevels(currentPrice, levels_distence, below_level, above_level);
-
- Print(" Current Price: ", currentPrice, " Below Value: ", below_level, " Above Value: ", above_level);
- }
- }
- }
- else
- {
- below_level = 0;
- above_level = 0;
- }
- }
-
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- void OnTick()
- {
- if(!MQLInfoInteger(MQL_TESTER))
- {
- static int status=-1,preStatus=-1;
- status=returnNewsStatus(High_Impact_News
- ,High_Start_Time
- ,High_Stop_Time
- ,show_high_line
- ,Medium_Impact_News
- ,Medium_Start_Time
- ,Medium_Stop_Time
- ,show_medium_line
- ,Low_Impact_News
- ,Low_Start_Time
- ,Low_Stop_Time
-
- ,show_low_line
- ,Symbol()
- ,MQLInfoString(MQL_PROGRAM_NAME)
- ,GMT_Broker_Time
- ,Select_News_Line
- ,""
- );
-
- if(status==0)
- {
- mainActivity();
- }
- if(status==1 || status==2 || status==3)
- {
- if(newsClose==0)
- {
- if(orderCount_1(POSITION_TYPE_BUY,magic_no)>0)
- closeTrades(POSITION_TYPE_BUY,magic_no);
- if(orderCount_1(POSITION_TYPE_SELL,magic_no)>0)
- closeTrades(POSITION_TYPE_SELL,magic_no);
- }
- else
- if(newsClose==1)
- {
- mainActivity();
- }
-
- }
- if(status!=preStatus)
- {
- if(status==0 && preStatus!=-1)
- {
- {
- ObjectSetString(0,Lname,OBJPROP_TEXT,"");
- }
- Alert("Trading is start");
- if(mobileAlert)
- SendNotification("Trading is start");
-
- preStatus=status;
- }
- else
- if(status==1)
- {
- ObjectSetInteger(0,Lname,OBJPROP_COLOR,clrRed);
- ObjectSetString(0,Lname,OBJPROP_TEXT,"High Impact News");
- Alert("Trading Stop due to high impact news");
-
- if(mobileAlert)
- SendNotification("Trading Stop due to high impact news");
-
- preStatus=status;
- }
- else
- if(status==2)
- {
- ObjectSetInteger(0,Lname,OBJPROP_COLOR,clrBlue);
- ObjectSetString(0,Lname,OBJPROP_TEXT,"Medium Impact News");
- Alert("Trading Stop due to medium impact news");
-
- if(mobileAlert)
- SendNotification("Trading Stop due to medium impact news");
-
- preStatus=status;
- }
- else
- if(status==3)
- {
- ObjectSetInteger(0,Lname,OBJPROP_COLOR,clrGreen);
- ObjectSetString(0,Lname,OBJPROP_TEXT,"Low Impact News");
- Alert("Trading Stop due to low impact news");
-
- if(mobileAlert)
- SendNotification("Trading Stop due to low impact news");
-
- preStatus=status;
- }
- }
- }
- else
- {
- mainActivity();
- }
- }
-
- //+------------------------------------------------------------------+
- //| Calculate Base Level Function |
- //+------------------------------------------------------------------+
- double calculateBaseLevel(double price, double step)
- {
- return step * MathFloor(price / step);
- }
- //+------------------------------------------------------------------+
- //| Get Nearest Levels Function |
- //+------------------------------------------------------------------+
- void getNearestLevels(double currentPrice, double step,
- double &LowerLevel, double &UpperLevel)
- {
- double epsilon = 0.000001;
- // Compute the anchor as the nearest multiple of step.
- double anchor = MathRound(currentPrice / step) * step;
- double diff = fabs(currentPrice - anchor);
-
- // If currentPrice exactly equals the anchor, use symmetric levels.
- if(diff < epsilon)
- {
- LowerLevel = anchor - step;
- UpperLevel = anchor + step;
- return;
- }
-
- // Special handling for step == 10: use non–symmetric levels.
- if(step == 10)
- {
- if(currentPrice > anchor)
- {
- LowerLevel = anchor;
- UpperLevel = anchor + step;
- }
- else // currentPrice < anchor
- {
- LowerLevel = anchor - step;
- UpperLevel = anchor;
- }
- return;
- }
-
- // For all other steps, use the ratio threshold logic.
- double ratio = diff / step;
- double threshold = 0.45; // Adjusted threshold
-
- if(currentPrice > anchor)
- {
- if(ratio > threshold)
- {
- LowerLevel = anchor - step;
- UpperLevel = anchor + step;
- }
- else
- {
- LowerLevel = anchor;
- UpperLevel = anchor + step;
- }
- }
- else // currentPrice < anchor
- {
- if(ratio > threshold)
- {
- LowerLevel = anchor - step;
- UpperLevel = anchor + step;
- }
- else
- {
- LowerLevel = anchor - step;
- UpperLevel = anchor;
- }
- }
- }
- //+------------------------------------------------------------------+
- //| Place Buy Trade Function |
- //+------------------------------------------------------------------+
- void placeBuyTrade()
- {
-
- double buySL = 0, buyTp=0;
- //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
- double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
- double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
-
- if(stoploss != 0)
- {
- buySL = Ask - (stoploss * 10 * Point());
- }
- if(takeprofit != 0)
- {
- buyTp = Ask + (takeprofit * 10 * Point());
- }
- double lot = 0;
- double lastLot = 0;
- if(selectLatestTicket(lastLot) < 0)
- {
- lot = lastLot * lot_multiplier;
- }
- else
- {
- lot = lot_size;
- }
- if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,NormalizeDouble(lot, 2),Ask,buySL,buyTp,"Buy Trade Placed"))
- {
- Print("Buy Trade Placed: ", trade.ResultOrder());
- }
- else
- {
- Print("Error in placing Buy: "+Symbol()+" ",GetLastError());
- }
- }
- //+------------------------------------------------------------------+
- //| Place Sell Trade Function |
- //+------------------------------------------------------------------+
- void placeSellTrade()
- {
-
- double sellSL = 0, sellTp = 0;
- //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
- double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
- double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
-
- if(stoploss != 0)
- {
- sellSL = Bid + (stoploss * 10 * Point());
- }
- if(takeprofit != 0)
- {
- sellTp = Bid - (takeprofit * 10 * Point());
- }
- double lot = 0;
- double lastLot = 0;
- if(selectLatestTicket(lastLot) < 0)
- {
- lot = lastLot * lot_multiplier;
- }
- else
- {
- lot = lot_size;
- }
- if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,NormalizeDouble(lot, 2),Bid,sellSL,sellTp,"Sell Trade Placed"))
- {
- Print("Sell Trade PLaced: ", trade.ResultOrder());
- }
- else
- {
- Print("Error in placing Sell: "+Symbol()+" ",GetLastError());
- }
-
- }
- //+------------------------------------------------------------------+
- //| Time Conversion Function |
- //+------------------------------------------------------------------+
- void timeConversion()
- {
- MqlDateTime date, date1;
-
- TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date);
- u_sep=StringGetCharacter(sep,0);
- StringSplit(startTime,u_sep,result1);
- date.hour = (int)StringToInteger(result1[0]);
- date.min = (int)StringToInteger(result1[1]);
- startTradingTime = StructToTime(date);
-
- TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date1);
- StringSplit(endTime,u_sep,result1);
- date.hour = (int)StringToInteger(result1[0]);
- date.min = (int)StringToInteger(result1[1]);
- endTradingTime = StructToTime(date);
- }
- //+------------------------------------------------------------------+
- //| Select Latest Ticket Function |
- //+------------------------------------------------------------------+
- double selectLatestTicket(double &lastOrderLot)
- {
-
- int count = 0;
- ulong ticket_deal_Out=0, ticket_deal_In = 0;
- datetime latestCloseTime = 0;
- double orderProfit = 0;
- ulong latestTicket = 0;
- if(HistorySelect(ea_start_time, TimeCurrent()))
- {
- int total = HistoryDealsTotal();
- for(int i = total-1; i >= 0 ; i--)
- {
- ticket_deal_Out = HistoryDealGetTicket(i);
- if((HistoryDealGetInteger(ticket_deal_Out,DEAL_MAGIC) == magic_no) && HistoryDealGetInteger(ticket_deal_Out,DEAL_ENTRY) == DEAL_ENTRY_OUT
- && HistoryDealGetString(ticket_deal_Out,DEAL_SYMBOL) == Symbol()) // here is the problem solved after break
- {
- datetime orderCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
- if(orderCloseTime > latestCloseTime)
- {
- latestCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
- orderProfit = HistoryDealGetDouble(ticket_deal_Out, DEAL_PROFIT);
- latestTicket = ticket_deal_Out;
- lastOrderLot = HistoryDealGetDouble(ticket_deal_Out, DEAL_VOLUME);
- // Print(" Last order Lot: ", lastOrderLot);
- }
- }
- }
- }
- // Print(" Latest Selected Ticket: ", latestTicket, " Order Close Time: ", latestCloseTime, " Ticket Profit: ", orderProfit);
-
- return orderProfit;
- }
- //+------------------------------------------------------------------+
- //| Get Close Price By Ticket Function |
- //+------------------------------------------------------------------+
- double getClosePriceByTicket()
- {
-
- ulong ticket_deal_Out=0, ticket_deal_In = 0;
- datetime latestCloseTime = 0;
- double orderClosePrice = 0;
- ulong latestTicket = 0;
- if(HistorySelect(ea_start_time, TimeCurrent()))
- {
- int total = HistoryDealsTotal();
- for(int i = total-1; i >= 0 ; i--)
- {
- ticket_deal_Out = HistoryDealGetTicket(i);
- if((HistoryDealGetInteger(ticket_deal_Out,DEAL_MAGIC) == magic_no) && HistoryDealGetInteger(ticket_deal_Out,DEAL_ENTRY) == DEAL_ENTRY_OUT
- && HistoryDealGetString(ticket_deal_Out,DEAL_SYMBOL) == Symbol()) // here is the problem solved after break
- {
- datetime orderCloseTime = (datetime) HistoryDealGetInteger(ticket_deal_Out, DEAL_TIME);
- if(orderCloseTime > latestCloseTime)
- {
- orderClosePrice = HistoryDealGetDouble(ticket_deal_Out, DEAL_PRICE);
- latestCloseTime = orderCloseTime;
- }
- }
- }
- }
-
- return orderClosePrice;
- }
- //+------------------------------------------------------------------+
- //| Order Count Function |
- //+------------------------------------------------------------------+
- int orderCount()
- {
- int count = 0;
- for(int i= PositionsTotal()-1; i>=0; i--)
- {
- ulong ticket = PositionGetTicket(i);
-
- if(PositionSelectByTicket(ticket))
- {
- if(PositionGetInteger(POSITION_MAGIC) == magic_no && PositionGetString(POSITION_SYMBOL) == Symbol())
- {
- //if(PositionGetInteger(POSITION_TYPE) == type)
- {
- count++;
- }
- }
- }
- }
- return count;
- }
-
- //+------------------------------------------------------------------+
- //| Order Count 1 with Parameters Function |
- //+------------------------------------------------------------------+
- int orderCount_1(int type,int magic)
- {
- int count1=0;
- for(int i= PositionsTotal()-1; i>=0; i--)
- {
- ulong ticket = PositionGetTicket(i);
- if(PositionSelectByTicket(ticket))
- {
- if(PositionGetInteger(POSITION_MAGIC) == magic && PositionGetString(POSITION_SYMBOL) == Symbol())
- {
- if(PositionGetInteger(POSITION_TYPE) == type)
- {
- count1++;
- }
- }
- }
- }
- return count1;
- }
- //+------------------------------------------------------------------+
- //| Close Trades Function |
- //+------------------------------------------------------------------+
- void closeTrades(int type,int magicno)
- {
- Print("Total order: ",OrdersTotal());
- for(int i= PositionsTotal()-1; i>=0; i--)
- {
- ulong ticket = PositionGetTicket(i);
- if(PositionSelectByTicket(ticket))
- {
- if(PositionGetInteger(POSITION_TYPE) == type)
- {
- if(PositionGetInteger(POSITION_MAGIC) == magicno && PositionGetString(POSITION_SYMBOL) == Symbol())
- {
- if(!trade.PositionClose(PositionGetInteger(POSITION_TICKET)))
- {Print("Problem in closing order order ",PositionGetInteger(POSITION_TICKET)); }
- else
- {
- Print("Order Closed by closeTrades() new filter",PositionGetInteger(POSITION_TICKET));
- }
- }
- }
- }
- }
- }
- //+------------------------------------------------------------------+
|