Нет описания

valFvgMt5.mq5 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //+------------------------------------------------------------------+
  2. //| valFvgMt5.mq5 |
  3. //| Copyright 2025, MQL Development |
  4. //| https://www.mqldevelopment.com/ |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2025, MQL Development"
  7. #property link "https://www.mqldevelopment.com/"
  8. #property version "1.00"
  9. #include <Trade\Trade.mqh>
  10. CTrade trade;
  11. #resource "\\Indicators\\SequentialVolumeProfileWithFVG.ex5"
  12. //+------------------------------------------------------------------+
  13. //| Expert initialization function |
  14. //+------------------------------------------------------------------+
  15. enum lotcalculator
  16. {
  17. fix, //Fixed Lot Size
  18. rsk, //Risk Percentage
  19. };
  20. enum tp_options
  21. {
  22. zone_based,
  23. risk_reward_based,
  24. };
  25. sinput string string_0 = "<><><><><><> General SETTINGS <><><><><><>"; //__
  26. input int magic_no = 333; // Magic no
  27. input tp_options select_tp = zone_based; // Select Tp
  28. input double stoploss = 10; // Fixed Stop Loss in Pips
  29. input double takeprofit = 10; // Fixed Take Profit in Pips
  30. input string string_0_3 = "<><><><><><> Lot Management<><><><><><>"; //__
  31. input double lot_size = 0.1; // Lot Size
  32. input lotcalculator lot_calculator = fix; // Lot Size Calculator
  33. input double risk = 0.1; // Risk in Percentage %
  34. input string time_setting = "<><><><><> Time Filter Settings <><><><><>"; //_
  35. input bool EnableTimeFilter = false; // Enable Time Filter
  36. input string startTime = "03:00"; // Start Time Session
  37. input string endTime = "09:00"; // End Time Session
  38. sinput string string_1 = "<><><><><><> Sequential Volume Indicator SETTINGS <><><><><><>"; //__
  39. input int BinsCount = 100; // Number of price bins
  40. input double ValueAreaPercent = 70; // Value Area percentage (70% default)
  41. input color VALColor = clrDarkBlue; // Value Area Low color
  42. input color VAHColor = clrDarkBlue; // Value Area High color
  43. input color AbsLowColor = clrBlack; // Absolute Low color
  44. input color AbsHighColor = clrBlack; // Absolute High color
  45. input color TimeLineColor = clrRed; // Time marker line color
  46. input int LineWidth = 2; // Line width for all value lines
  47. input int TimeLineWidth = 2; // Line width for time marker lines
  48. input int MaxDaysBack = 30; // Maximum number of trading days to look back
  49. input ENUM_LINE_STYLE VALStyle = STYLE_SOLID; // Value Area Low line style
  50. input ENUM_LINE_STYLE VAHStyle = STYLE_SOLID; // Value Area High line style
  51. input ENUM_LINE_STYLE AbsLowStyle = STYLE_SOLID; // Absolute Low line style
  52. input ENUM_LINE_STYLE AbsHighStyle = STYLE_SOLID; // Absolute High line style
  53. input bool ShowLabels = true; // Show price labels
  54. input bool ShowComment = true; // Show comment with most recent levels
  55. input bool ShowFVG = true; // Enable Fair Value Gap detection
  56. input color BullishFVGColor = clrLime; // Bullish FVG color
  57. input color BearishFVGColor = clrDeepPink; // Bearish FVG color
  58. input double MinFVGSize = 0.0; // Minimum FVG size in points (0 = any size)
  59. input int MaxBarsBack = 300; // How many bars to look back for FVG
  60. // Global Variables
  61. int sequential_handler;
  62. datetime startTradingTime = 0, endTradingTime = 0;
  63. string sep = ":"; // A separator as a character
  64. ushort u_sep; // The code of the separator character
  65. string result1[];
  66. //+------------------------------------------------------------------+
  67. //| |
  68. //+------------------------------------------------------------------+
  69. int OnInit()
  70. {
  71. //---
  72. trade.SetExpertMagicNumber(magic_no);
  73. trade.SetDeviationInPoints(10);
  74. trade.SetTypeFilling(ORDER_FILLING_IOC);
  75. trade.LogLevel(LOG_LEVEL_ALL);
  76. trade.SetAsyncMode(false);
  77. sequential_handler = iCustom(Symbol(), PERIOD_CURRENT, "::Indicators\\SequentialVolumeProfileWithFVG.ex5",
  78. BinsCount,
  79. ValueAreaPercent,
  80. VALColor,
  81. VAHColor,
  82. AbsLowColor,
  83. AbsHighColor,
  84. TimeLineColor,
  85. LineWidth,
  86. TimeLineWidth,
  87. MaxDaysBack,
  88. VALStyle,
  89. VAHStyle,
  90. AbsLowStyle,
  91. AbsHighStyle,
  92. ShowLabels,
  93. ShowComment,
  94. ShowFVG,
  95. BullishFVGColor,
  96. BearishFVGColor,
  97. MinFVGSize,
  98. MaxBarsBack);
  99. //---
  100. return(INIT_SUCCEEDED);
  101. }
  102. //+------------------------------------------------------------------+
  103. //| Expert deinitialization function |
  104. //+------------------------------------------------------------------+
  105. void OnDeinit(const int reason)
  106. {
  107. //---
  108. }
  109. //+------------------------------------------------------------------+
  110. //| Expert tick function |
  111. //+------------------------------------------------------------------+
  112. void OnTick()
  113. {
  114. //---
  115. double values[];
  116. CopyBuffer(sequential_handler,0,0,3,values);
  117. double val = lines("VAL");
  118. double vah = lines("VAH");
  119. double absHigh = lines("AbsHigh");
  120. double absLow = lines("AbsLow");
  121. // Print("Val: ", val, " Vah: ", vah, " AbsHigh: ", absHigh, " AbsLow: ", absLow);
  122. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  123. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  124. timeConversion();
  125. // Comment(" Start Time: ", startTradingTime, " Close time: ", endTradingTime);
  126. if((EnableTimeFilter && TimeCurrent() >= startTradingTime && TimeCurrent() < endTradingTime) || !EnableTimeFilter)
  127. {
  128. if(todayTradesCount(DEAL_TYPE_BUY) == 0)
  129. {
  130. if(Ask < val && Ask > absLow)
  131. {
  132. //if(fvgOverLap("VProfFVG_up_", val))
  133. {
  134. placeBuyTrade();
  135. }
  136. }
  137. }
  138. if(todayTradesCount(DEAL_TYPE_SELL) == 0)
  139. {
  140. if(Bid > vah && Bid < absHigh)
  141. {
  142. //if(fvgOverLap("VProfFVG_dn_", vah))
  143. {
  144. placeSellTrade();
  145. }
  146. }
  147. }
  148. }
  149. }
  150. //+------------------------------------------------------------------+
  151. //| |
  152. //+------------------------------------------------------------------+
  153. void timeConversion()
  154. {
  155. MqlDateTime date, date1;
  156. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date);
  157. u_sep=StringGetCharacter(sep,0);
  158. StringSplit(startTime,u_sep,result1);
  159. date.hour = (int)StringToInteger(result1[0]);
  160. date.min = (int)StringToInteger(result1[1]);
  161. startTradingTime = StructToTime(date);
  162. TimeToStruct(iTime(Symbol(),PERIOD_CURRENT,0),date1);
  163. StringSplit(endTime,u_sep,result1);
  164. date.hour = (int)StringToInteger(result1[0]);
  165. date.min = (int)StringToInteger(result1[1]);
  166. endTradingTime = StructToTime(date);
  167. }
  168. //+------------------------------------------------------------------+
  169. //| |
  170. //+------------------------------------------------------------------+
  171. double lines(string name)
  172. {
  173. datetime todayStart = iTime(Symbol(), PERIOD_D1, 0);
  174. datetime nextDayStart = todayStart + 86400;
  175. datetime latestObjectTime = 0;
  176. for(int i = 0; i < ObjectsTotal(0, 0, OBJ_TREND); i++)
  177. {
  178. string object_name = ObjectName(0, i, 0, OBJ_TREND);
  179. datetime object_time = (datetime)ObjectGetInteger(0, object_name, OBJPROP_TIME, 1);
  180. if(object_time > todayStart && object_time < nextDayStart)
  181. {
  182. // Print(" Object Name: ", object_name, " Day Start Time: ", todayStart, " Next Day Time: ", nextDayStart);
  183. if((StringFind(object_name, name) != -1))
  184. {
  185. double objectPrice = ObjectGetDouble(0, object_name, OBJPROP_PRICE, 0);
  186. return objectPrice;
  187. }
  188. }
  189. }
  190. return 0;
  191. }
  192. //+------------------------------------------------------------------+
  193. //| |
  194. //+------------------------------------------------------------------+
  195. bool fvgOverLap(string name, double price_overlap)
  196. {
  197. datetime todayStart = iTime(Symbol(), PERIOD_D1, 0);
  198. datetime nextDayStart = todayStart + 86400;
  199. for(int i = 0; i < ObjectsTotal(0, 0, OBJ_RECTANGLE); i++)
  200. {
  201. string object_name = ObjectName(0, i, 0, OBJ_RECTANGLE);
  202. datetime object_time1 = (datetime)ObjectGetInteger(0, object_name, OBJPROP_TIME, 1);
  203. datetime object_time0 = (datetime)ObjectGetInteger(0, object_name, OBJPROP_TIME, 0);
  204. double object_price0 = ObjectGetDouble(0, object_name, OBJPROP_PRICE, 0);
  205. double object_price1 = ObjectGetDouble(0, object_name, OBJPROP_PRICE, 1);
  206. if(object_time1 > todayStart && object_time1 < nextDayStart)
  207. {
  208. if((StringFind(object_name, name) != -1))
  209. {
  210. double fvg_top = MathMax(object_price0, object_price1);
  211. double fvg_bottom = MathMin(object_price0, object_price1);
  212. if(fvg_top > price_overlap && fvg_bottom < price_overlap)
  213. {
  214. Print(" Called By: (", name, ") Object Name: ", object_name,
  215. " | Time 0: ", TimeToString(object_time0), " | Price 0: ", object_price0,
  216. " | Time 1: ", TimeToString(object_time1), " | Price 0: ", object_price0, " | Price 1: ", object_price1, " | \n FVG Top: ", fvg_top, " | FVG Bottom: ", fvg_bottom);
  217. return true;
  218. }
  219. }
  220. }
  221. }
  222. return false;
  223. }
  224. //+------------------------------------------------------------------+
  225. //| |
  226. //+------------------------------------------------------------------+
  227. int todayTradesCount(ENUM_DEAL_TYPE dealType)
  228. {
  229. int count = 0;
  230. ulong ticket_deal_Out=0, ticket_deal_In = 0;
  231. if(HistorySelect(iTime(Symbol(),PERIOD_D1,0), TimeCurrent()))
  232. {
  233. int total = HistoryDealsTotal();
  234. for(int i = total-1; i >= 0 ; i--)
  235. {
  236. ticket_deal_In = HistoryDealGetTicket(i);
  237. if((HistoryDealGetInteger(ticket_deal_In,DEAL_MAGIC) == magic_no) && HistoryDealGetInteger(ticket_deal_In,DEAL_ENTRY) == DEAL_ENTRY_IN
  238. && HistoryDealGetString(ticket_deal_In,DEAL_SYMBOL) == Symbol()) // here is the problem solved after break
  239. {
  240. if(HistoryDealGetInteger(ticket_deal_In, DEAL_TYPE) == dealType)
  241. {
  242. count++;
  243. }
  244. }
  245. }
  246. }
  247. return count;
  248. }
  249. //+------------------------------------------------------------------+
  250. //| |
  251. //+------------------------------------------------------------------+
  252. void placeBuyTrade()
  253. {
  254. double buySL = 0, buyTp=0;
  255. //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  256. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  257. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  258. if(stoploss != 0)
  259. {
  260. buySL = Ask - (stoploss * 10 * Point());
  261. }
  262. if(select_tp == zone_based)
  263. {
  264. buyTp = lines("VAH");
  265. }
  266. //if(select_tp == risk_reward_based)
  267. // {
  268. //distance = ((price - buySl) / Point());
  269. //distance = (distance * tpMultiplier);
  270. // }
  271. //if(takeprofit != 0)
  272. // {
  273. // buyTp = Ask + (takeprofit * 10 * Point());
  274. // }
  275. if(trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,getlot(stoploss * 10),Ask,buySL,buyTp,"Buy Trade Placed"))
  276. {
  277. Print("Buy Trade Placed: ",trade.ResultOrder());
  278. }
  279. else
  280. {
  281. Print("Error in placing Buy: "+Symbol()+" ",GetLastError());
  282. }
  283. }
  284. //+------------------------------------------------------------------+
  285. //| |
  286. //+------------------------------------------------------------------+
  287. void placeSellTrade()
  288. {
  289. double sellSL = 0, sellTp = 0;
  290. //openPrice = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  291. double Ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
  292. double Bid = SymbolInfoDouble(Symbol(),SYMBOL_BID);
  293. if(stoploss != 0)
  294. {
  295. sellSL = Bid + (stoploss * 10 * Point());
  296. }
  297. if(takeprofit != 0)
  298. {
  299. sellTp = Bid - (takeprofit * 10 * Point());
  300. }
  301. if(trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,getlot(stoploss * 10),Bid,sellSL,sellTp,"Sell Trade Placed"))
  302. {
  303. Print("Sell Trade PLaced: ",trade.ResultOrder());
  304. }
  305. else
  306. {
  307. Print("Error in placing Sell: "+Symbol()+" ",GetLastError());
  308. }
  309. }
  310. //+------------------------------------------------------------------+
  311. //| |
  312. //+------------------------------------------------------------------+
  313. double getlot(double stop_loss)
  314. {
  315. Print("Tick Value: ",SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE));
  316. Print("Tick Size: ",SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE));
  317. double modeTickV=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)
  318. ,modeTickS=SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE);
  319. // Print("Pip value: ", NormalizeDouble(((SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)/(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE)/Point))*10),2));
  320. double pipvalue = NormalizeDouble(((SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE)/(SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_SIZE)/Point()))*10),2);
  321. // pipvalue=NormalizeDouble((modeTickV/modeTickS/Point()),)
  322. // pipvalue=
  323. pipvalue = pipvalue / 10;
  324. double lotSize = lot_size;
  325. if(lot_calculator==rsk) //calculating risk
  326. {
  327. double riskamount=(risk/100)*AccountInfoDouble(ACCOUNT_BALANCE);
  328. double pipvalue_required=riskamount/stop_loss;
  329. lotSize = pipvalue_required/pipvalue;
  330. //sl=riskamount/pipValuelot
  331. int roundDigit=0;
  332. double step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
  333. while(step<1)
  334. {
  335. roundDigit++;
  336. step=step*10;
  337. }
  338. Print("Round Digits:",roundDigit);
  339. lotSize = NormalizeDouble(lotSize,roundDigit);
  340. //
  341. }
  342. Print("Lot Size: ",lotSize);
  343. if(lotSize > SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX))
  344. {
  345. lotSize=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
  346. }
  347. else
  348. if(lotSize<SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN))
  349. {
  350. lotSize=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
  351. }
  352. //---
  353. return lotSize;
  354. }
  355. //+------------------------------------------------------------------+
  356. //| |
  357. //+------------------------------------------------------------------+
  358. //+------------------------------------------------------------------+