#property copyright "MAtteo ricco" #property link "bot " #property version "19.120" #property description " " #property strict enum ENUM_ST { Awerage = 0, // Average PartClose = 1 // Part Close }; input int iTakeProfit = 300; // Take Profit (in pips) input double iStartLots = 0.01; // Start lot input double iMaximalLots = 2.56; // Maximal Lots input ENUM_ST iCloseOrder = Awerage; // Type close orders input int iPointOrderStep = 390; // Point order step (in pips) input int iMinimalProfit = 70; // Minimal profit for close grid (in pips) input int iMagicNumber = 227; // Magic Number (in number) input int iSlippage = 30; // Slippage (in pips) input int iStopLoss = 50; // Stop Loss (in pips) input int iMaxTradeHours = 10; // Maximum hours to keep a trade open input double iTradeMultiplier = 2.0; // Multiplier for each new trade input int iNoTradeStartHour = 22; // No Trade Start Hour input int iNoTradeEndHour = 6; // No Trade End Hour int OnInit() { Comment(""); return(INIT_SUCCEEDED); } void OnTick() { int currentHour = Hour(); // Check if current hour is within the no-trade range if (currentHour >= iNoTradeStartHour && currentHour < iNoTradeEndHour) { return; // Do not trade during this time range } double BuyPriceMax = 0, BuyPriceMin = 0, BuyPriceMaxLot = 0, BuyPriceMinLot = 0; double SelPriceMin = 0, SelPriceMax = 0, SelPriceMinLot = 0, SelPriceMaxLot = 0; int BuyPriceMaxTic = 0, BuyPriceMinTic = 0, SelPriceMaxTic = 0, SelPriceMinTic = 0; double op = 0, lt = 0, tp = 0; int tk = 0, b = 0, s = 0; for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber() == iMagicNumber && OrderSymbol() == Symbol()) { op = NormalizeDouble(OrderOpenPrice(), Digits()); lt = NormalizeDouble(OrderLots(), 2); tk = OrderTicket(); datetime openTime = OrderOpenTime(); datetime currentTime = TimeCurrent(); int elapsedHours = (int)((currentTime - openTime) / 3600); if (elapsedHours >= iMaxTradeHours) { // Close the trade if it has been open for more than iMaxTradeHours OrderClose(tk, lt, Bid, iSlippage, clrRed); continue; // Skip to the next iteration of the loop } if (OrderType() == OP_BUY) { b++; if (op > BuyPriceMax || BuyPriceMax == 0) { BuyPriceMax = op; BuyPriceMaxLot = lt; BuyPriceMaxTic = tk; } if (op < BuyPriceMin || BuyPriceMin == 0) { BuyPriceMin = op; BuyPriceMinLot = lt; BuyPriceMinTic = tk; } } // === if (OrderType() == OP_SELL) { s++; if (op > SelPriceMax || SelPriceMax == 0) { SelPriceMax = op; SelPriceMaxLot = lt; SelPriceMaxTic = tk; } if (op < SelPriceMin || SelPriceMin == 0) { SelPriceMin = op; SelPriceMinLot = lt; SelPriceMinTic = tk; } } } } } double AwerageBuyPrice = 0, AwerageSelPrice = 0; if (iCloseOrder == Awerage) { if (b >= 2) AwerageBuyPrice = NormalizeDouble((BuyPriceMax * BuyPriceMaxLot + BuyPriceMin * BuyPriceMinLot) / (BuyPriceMaxLot + BuyPriceMinLot) + iMinimalProfit * Point(), Digits()); if (s >= 2) AwerageSelPrice = NormalizeDouble((SelPriceMax * SelPriceMaxLot + SelPriceMin * SelPriceMinLot) / (SelPriceMaxLot + SelPriceMinLot) - iMinimalProfit * Point(), Digits()); } if (iCloseOrder == PartClose) { if (b >= 2) AwerageBuyPrice = NormalizeDouble((BuyPriceMax * iStartLots + BuyPriceMin * BuyPriceMinLot) / (iStartLots + BuyPriceMinLot) + iMinimalProfit * Point(), Digits()); if (s >= 2) AwerageSelPrice = NormalizeDouble((SelPriceMax * SelPriceMaxLot + SelPriceMin * iStartLots) / (SelPriceMaxLot + iStartLots) - iMinimalProfit * Point(), Digits()); } double BuyLot = (BuyPriceMinLot == 0) ? iStartLots : BuyPriceMinLot * iTradeMultiplier; double SelLot = (SelPriceMaxLot == 0) ? iStartLots : SelPriceMaxLot * iTradeMultiplier; if (iMaximalLots > 0) { BuyLot = (BuyLot > iMaximalLots) ? iMaximalLots : BuyLot; SelLot = (SelLot > iMaximalLots) ? iMaximalLots : SelLot; } if (!CheckVolumeValue(BuyLot) || !CheckVolumeValue(SelLot)) return; if (Close[1] > Open[1]) { if ((b == 0) || (b > 0 && (BuyPriceMin - Ask) > (iPointOrderStep * Point()))) { if (OrderSend(Symbol(), OP_BUY, NormalizeDouble(BuyLot, 2), NormalizeDouble(Ask, Digits()), iSlippage, 0, NormalizeDouble(Ask - iStopLoss * Point(), Digits()), "VR Setka Lite", iMagicNumber, 0, clrGreen) < 0) Print("OrderSend error #", GetLastError()); } } if (Close[1] < Open[1]) { if ((s == 0) || (s > 0 && (Bid - SelPriceMax) > (iPointOrderStep * Point()))) { if (OrderSend(Symbol(), OP_SELL, NormalizeDouble(SelLot, 2), NormalizeDouble(Bid, Digits()), iSlippage, 0, NormalizeDouble(Bid + iStopLoss * Point(), Digits()), "VR Setka Lite", iMagicNumber, 0, clrGreen) < 0) Print("OrderSend error #", GetLastError()); } } for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { if (OrderMagicNumber() == iMagicNumber && OrderSymbol() == Symbol()) { op = NormalizeDouble(OrderOpenPrice(), Digits()); tp = NormalizeDouble(OrderTakeProfit(), Digits()); lt = NormalizeDouble(OrderLots(), 2); tk = OrderTicket(); datetime openTime = OrderOpenTime(); datetime currentTime = TimeCurrent(); int elapsedHours = (int)((currentTime - openTime) / 3600); if (OrderType() == OP_BUY && b == 1 && tp == 0) { if (!OrderModify(tk, op, OrderStopLoss(), NormalizeDouble(Ask + iTakeProfit * Point(), Digits()), 0, clrRed)) Print("OrderModify error #", GetLastError()); } if (OrderType() == OP_SELL && s == 1 && tp == 0) { if (!OrderModify(tk, op, OrderStopLoss(), NormalizeDouble(Bid - iTakeProfit * Point(), Digits()), 0, clrRed)) Print("OrderModify error #", GetLastError()); } if (iCloseOrder == Awerage) { if (OrderType() == OP_BUY && b >= 2) { if (tk == BuyPriceMaxTic || tk == BuyPriceMinTic) if (Bid < AwerageBuyPrice && tp != AwerageBuyPrice) if (!OrderModify(tk, op, OrderStopLoss(), AwerageBuyPrice, 0, clrRed)) Print("OrderModify error #", GetLastError()); if (tk != BuyPriceMaxTic && tk != BuyPriceMinTic && tp != 0) if (!OrderModify(tk, op, 0, 0, 0, clrRed)) Print("OrderModify error #", GetLastError()); } if (OrderType() == OP_SELL && s >= 2) { if (tk == SelPriceMaxTic || tk == SelPriceMinTic) if (Ask > AwerageSelPrice && tp != AwerageSelPrice) if (!OrderModify(tk, op, OrderStopLoss(), AwerageSelPrice, 0, clrRed)) Print("OrderModify error #", GetLastError()); if (tk != SelPriceMaxTic && tk != SelPriceMinTic && tp != 0) if (!OrderModify(tk, op, 0, 0, 0, clrRed)) Print("OrderModify error #", GetLastError()); } } if (iCloseOrder == PartClose) { if (b >= 2) if (AwerageBuyPrice > 0 && Bid >= AwerageBuyPrice) { if (!OrderClose(BuyPriceMaxTic, iStartLots, Bid, iSlippage, clrRed)) Print("OrderClose Error ", GetLastError()); if (!OrderClose(BuyPriceMinTic, BuyPriceMinLot, Bid, iSlippage, clrRed)) Print("OrderClose Error ", GetLastError()); } if (s >= 2) if (AwerageSelPrice > 0 && Ask <= AwerageSelPrice) { if (!OrderClose(SelPriceMinTic, iStartLots, Ask, iSlippage, clrRed)) Print("OrderClose Error ", GetLastError()); if (!OrderClose(SelPriceMaxTic, SelPriceMaxLot, Bid, iSlippage, clrRed)) Print("OrderClose Error ", GetLastError()); } } } } } } void OnDeinit(const int reason) { } bool CheckVolumeValue(double volume) { double min_volume = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MIN); if (volume < min_volume) return (false); double max_volume = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MAX); if (volume > max_volume) return (false); double volume_step = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_STEP); int ratio = (int)MathRound(volume / volume_step); if (MathAbs(ratio * volume_step - volume) > 0.0000001) return (false); return (true); }