//+------------------------------------------------------------------+ //| Test2.mq4 | //| | //| | //+------------------------------------------------------------------+ #property copyright "" #property link "" extern double Lots = 0.1; extern int TakeProfit = 130; extern int StopLoss = 22; extern int BarCount = 10; extern int HourStart = 10; extern int HourEnd = 23; extern int Slippage = 10; extern int Indent = 5; extern int Magic = 777; extern string comment = "Test2"; extern bool ProfitTrailing = TRUE; extern double TrailingStop = 28.0; extern double TrailingStep = 8.0; extern int MagicNumber = 777; double minprice, maxprice, SL, TP, gd_100; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { gd_100 = Point; if (Digits == 5 || Digits == 3) gd_100 = 10.0 * gd_100; TakeProfit *=10; StopLoss *=10; Slippage *=10; Indent *=10; return (0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { if (TrailingStop > 0.0 && OrdersTotal() > 0) RealTrailOrder(); if (HourEnd == TimeHour(TimeCurrent())) { DeleteOrders(); } minprice = NormalizeDouble(GetMinPrice(), Digits); maxprice = NormalizeDouble(GetMaxPrice(), Digits); if (HourStart == TimeHour(TimeCurrent())&&CountBuy()==0&&CountBuyLimit()==0&&CountBuyStop()==0 &&CountSell()==0&&CountSellLimit()==0&&CountSellStop()==0) { SL=NormalizeDouble(maxprice - StopLoss*Point+Indent*Point, Digits); TP=NormalizeDouble(maxprice + TakeProfit*Point+Indent*Point, Digits); OrderSend(Symbol(),OP_BUYSTOP,Lots,maxprice+Indent*Point,Slippage,SL,TP,comment,Magic,0,Blue); SL=NormalizeDouble(minprice + StopLoss*Point-Indent*Point, Digits); TP=NormalizeDouble(minprice - TakeProfit*Point-Indent*Point, Digits); OrderSend(Symbol(),OP_SELLSTOP,Lots,minprice-Indent*Point,Slippage,SL,TP,comment,Magic,0,Red); } return(0); } //+------------------------------------------------------------------+ double GetMinPrice() { double dLow=1000000, dPrice; for (int i=1; i<=BarCount; i++) { dPrice=iLow(Symbol(), 0, i); if (dPrice dHigh) dHigh=dPrice; } return(dHigh); } //+------------------------------------------------------------------+ int CountBuy() { int count=0; for (int trade = OrdersTotal()-1;trade>=0;trade--) { OrderSelect(trade,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { if (OrderType()==OP_BUY) count++; } } return(count); } //+------------------------------------------------------------------+ int CountSell() { int count=0; for (int trade = OrdersTotal()-1;trade>=0;trade--) { OrderSelect(trade,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { if (OrderType()==OP_SELL) count++; } } return(count); } //+------------------------------------------------------------------+ int CountBuyLimit() { int count=0; for (int trade = OrdersTotal()-1;trade>=0;trade--) { OrderSelect(trade,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { if (OrderType()==OP_BUYLIMIT) count++; } } return(count); } //+------------------------------------------------------------------+ int CountSellLimit() { int count=0; for (int trade = OrdersTotal()-1;trade>=0;trade--) { OrderSelect(trade,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { if (OrderType()==OP_SELLLIMIT) count++; } } return(count); } //+------------------------------------------------------------------+ int CountBuyStop() { int count=0; for (int trade = OrdersTotal()-1;trade>=0;trade--) { OrderSelect(trade,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { if (OrderType()==OP_BUYSTOP) count++; } } return(count); } //+------------------------------------------------------------------+ int CountSellStop() { int count=0; for (int trade = OrdersTotal()-1;trade>=0;trade--) { OrderSelect(trade,SELECT_BY_POS,MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { if (OrderType()==OP_SELLSTOP) count++; } } return(count); } //+------------------------------------------------------------------+ void DeleteOrders() { for (int trade = OrdersTotal()-1; trade>=0; trade--) { OrderSelect(trade, SELECT_BY_POS, MODE_TRADES); if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { OrderDelete(OrderTicket(), Black); } } } //+------------------------------------------------------------------+ void RealTrailOrder() { double l_ord_open_price_20; double l_ord_stoploss_28; double l_price_36; double ld_0 = MarketInfo(Symbol(), MODE_STOPLEVEL) * Point / gd_100; double ld_8 = MathMax(TrailingStop, ld_0); for (int l_pos_16 = OrdersTotal() - 1; l_pos_16 >= 0; l_pos_16--) { if (OrderSelect(l_pos_16, SELECT_BY_POS, MODE_TRADES) == TRUE) { if (OrderMagicNumber() == MagicNumber || MagicNumber < 0 && OrderSymbol() == Symbol()) { l_ord_open_price_20 = OrderOpenPrice(); l_ord_stoploss_28 = OrderStopLoss(); while (IsTradeContextBusy()) Sleep(500); RefreshRates(); if (OrderType() == OP_BUY) { l_price_36 = ND(Bid - ld_8 * gd_100); if (Bid > l_ord_open_price_20 + ld_8 * gd_100 || !ProfitTrailing && l_price_36 >= l_ord_stoploss_28 + TrailingStep * gd_100 && ld_8 * gd_100 > ld_0 * gd_100) { if (!OrderModify(OrderTicket(), OrderOpenPrice(), l_price_36, OrderTakeProfit(), 0, Blue)) if (!IsOptimization()) Print("BUY OrderModify Error " + GetLastError()); } } if (OrderType() == OP_SELL) { l_price_36 = ND(Ask + ld_8 * gd_100); if (Ask < l_ord_open_price_20 - ld_8 * gd_100 || !ProfitTrailing && l_price_36 <= l_ord_stoploss_28 - TrailingStep * gd_100 || l_ord_stoploss_28 == 0.0 && ld_8 * gd_100 > ld_0 * gd_100) { if (!OrderModify(OrderTicket(), OrderOpenPrice(), l_price_36, OrderTakeProfit(), 0, Red)) if (!IsOptimization()) Print("Sell OrderModify Error " + GetLastError()); } } } } } } double ND(double ad_0) { return (NormalizeDouble(ad_0, Digits)); }