Navigation:Home > Content >

SR-Scalper.mq4

Time: 2017-02-26 | Download file:SR-Scalper.mq4

//+------------------------------------------------------------------+
//|                                                   SR-Scalper.mq4 |
//|                                 Copyright © 2013, Russian trades |
//|                                              http://rutrade.info |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, Russian trades"
#property link      "http://rutrade.info"

//+------------------------------------------------------------------+
extern double LotsFor3000  = 0.5;
//+------------------------------------------------------------------+
extern int    HourStart     = 0;
extern int    HourEnd       = 3;
extern int    CloseHour     = 7;
extern int    MaPeriod      = 8;
extern int    MaMethod      = 0;
//+------------------------------------------------------------------+
extern int    Difference   = 5;
extern int    TakeProfit   = 15;
extern int    StopLoss     = 70;
extern int    Magic        = 201302061;
extern string comment      = "SR-Scalper v.1";
extern int    Slippage     = 5;
//+------------------------------------------------------------------+

double ma, price, lots, SL, TP, diff;
bool   MoveDn, MoveUp;
int    trades,
       ticket = 0;

double YesterdayHigh;
double YesterdayLow;
double YesterdayClose;
double Pivot,S1,S2,S3,R1,R2,R3;
      
double WeekHigh;
double WeekLow;
double WeekClose;
double WeekPivot,WS1,WS2,WS3,WR1,WR2,WR3;
   
double MonthHigh;
double MonthLow;
double MonthClose;
double MonthPivot,MS1,MS2,MS3,MR1,MR2,MR3;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
   if (Digits == 5 || Digits == 3)
   {
      TakeProfit   *= 10;
      Difference   *= 10;
      TakeProfit   *= 10;
      StopLoss     *= 10;
   }
   
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{

   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   
   trades = CountTrades();

   // удаляем в конце дня все ордера, если они имеют профит      
   if (trades > 0 && Profit() > 0 && TimeHour(TimeCurrent()) >= CloseHour)
   {
      Print("Delete working : end of the day!");
      DeleteWorking();
   }

   if (CountTrades() == 0)
   {
      CalcLevels();
      SetOrder();
   }

   return(0);
}
//---------------------------------------------------------------------------------------------------------
void SetOrder()
{
   string comment = "";
   
   if (IsTradeHours())
   {
      MoveUp = (iMA(Symbol(), PERIOD_D1, MaPeriod, 0, MaMethod, PRICE_CLOSE, 1) > iMA(Symbol(), PERIOD_D1, MaPeriod, 0, MaMethod, PRICE_CLOSE, 2));
      MoveDn = (iMA(Symbol(), PERIOD_D1, MaPeriod, 0, MaMethod, PRICE_CLOSE, 1) < iMA(Symbol(), PERIOD_D1, MaPeriod, 0, MaMethod, PRICE_CLOSE, 2));

      diff   = MathAbs(Bid - MonthPivot)/Point;
      if(diff > 0 && diff <= Difference && MoveUp)
      {
         Print("Sell order, because we are near month pivot line, DIFF = " + diff);
         OrderSell();
         return;
      }   

      diff   = MathAbs(Bid - WeekPivot)/Point;
      if(diff > 0 && diff <= Difference && MoveUp)
      {
         Print("Sell order, because we are near week pivot line, DIFF = " + diff);
         OrderSell();
         return;
      }   
      
      diff = MathAbs(Bid - MR1/Point);
      if(diff > 0 && diff <= Difference && MoveUp)
      {
         Print("Sell order, because we are near MR1 line, DIFF = " + diff);
         OrderSell();
         return;
      }   

      diff = MathAbs(Bid - MR2)/Point;
      if(diff > 0 && diff <= Difference && MoveUp)
      {
         Print("Sell order, because we are near MR2 line, DIFF = " + diff);
         OrderSell();
         return;
      }   
      
      diff = MathAbs(Bid - MR3)/Point;
      if(diff > 0 && diff <= Difference && MoveUp)
      {
         Print("Sell order, because we are near MR3 line, DIFF = " + diff);
         OrderSell();
         return;
      }   
      
      diff = MathAbs(Bid - WR1)/Point;
      if(diff > 0 && diff <= Difference && MoveUp)
      {
         Print("Sell order, because we are near WR1 line, DIFF = " + diff);
         OrderSell();
         return;
      }   
      
      diff = MathAbs(Bid - WR2)/Point;
      if(diff > 0 && diff <= Difference && MoveUp)
      {
         Print("Sell order, because we are near WR2 line, DIFF = " + diff);
         OrderSell();
         return;
      }   
      
      diff = MathAbs(Bid - WR3)/Point;
      if(diff > 0 && diff <= Difference && MoveUp)
      {
         Print("Sell order, because we are near WR3 line, DIFF = " + diff);
         OrderSell();
         return;
      }   

//-------------------

      diff   = MathAbs(Bid - MonthPivot)/Point;
      if(diff > 0 && diff <= Difference && MoveDn)
      {
         Print("Buy order, because we are near month pivot line, DIFF = " + diff);
         Print ("Month pivot : " + WeekPivot);
         OrderBuy();
         return;
      }   

      diff   = MathAbs(Bid - WeekPivot)/Point;
      if(diff > 0 && diff <= Difference && MoveDn)
      {
         Print("Buy order, because we are near week pivot line, DIFF = " + diff);
         Print ("Week pivot : " + WeekPivot);
         OrderBuy();
         return;
      }   
      
      diff = MathAbs(Bid - WS1)/Point;
      if(diff > 0 && diff <= Difference && MoveDn)
      {
         Print("Buy order, because we are near WS1 line, DIFF = " + diff);
         Print ("WS1 : " + WS1);
         OrderBuy();
         return;
      }   

      diff = MathAbs(Bid - WS2)/Point;
      if(diff > 0 && diff <= Difference && MoveDn)
      {
         Print("Buy order, because we are near WS2 line, DIFF = " + diff);
         Print ("WS2 : " + WS2);
         OrderBuy();
         return;
      }   
      
      diff = MathAbs(Bid - WS3)/Point;
      if(diff > 0 && diff <= Difference && MoveDn)
      {
         Print("Buy order, because we are near WS3 line, DIFF = " + diff);
         Print ("WS3 : " + WS3);
         OrderBuy();
         return;
      }   
      
      diff = MathAbs(Bid - MS1)/Point;
      if(diff > 0 && diff <= Difference && MoveDn)
      {
         Print("Buy order, because we are near MS1 line, DIFF = " + diff);
         Print ("MS1 : " + MS1);
         OrderBuy();
         return;
      }   
      
      diff = MathAbs(Bid - MS2)/Point;
      if(diff > 0 && diff <= Difference && MoveDn)
      {
         Print("Buy order, because we are near MS2 line, DIFF = " + diff);
         Print ("MS2 : " + MS2);
         OrderBuy();
         return;
      }   
      
      diff = MathAbs(Bid - MS3)/Point;
      if(diff > 0 && diff <= Difference && MoveDn)
      {
         Print("Buy order, because we are near MS3 line, DIFF = " + diff);
         Print ("MS3 : " + MS3);
         OrderBuy();
         return;
      }   
   }
}
//+------------------------------------------------------------------+
bool OrderSell()
{
   ticket = OrderSend(Symbol(), OP_SELL, GetLots(), Bid, Slippage, 0, 0, comment, Magic, 0, Red);
   if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
   {
       SL = ND(OrderOpenPrice() + StopLoss * Point);
       TP = ND(OrderOpenPrice() - TakeProfit * Point);
       Print("Bid : " + Bid + ", SL : " + SL + ", TP : " + TP);
       OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0);
   }
   return (true);
}
//+------------------------------------------------------------------+
bool OrderBuy()
{
   ticket = OrderSend(Symbol(), OP_BUY, GetLots(), Ask, Slippage, 0, 0, comment, Magic, 0, Blue);
   if (OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
   {
       SL = ND(OrderOpenPrice() - StopLoss * Point);
       TP = ND(OrderOpenPrice() + TakeProfit * Point);
       Print("Bid : " + Bid + ", SL : " + SL + ", TP : " + TP);
       OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0);
   }
   return (true);
}
//+------------------------------------------------------------------+
bool IsTradeHours()
{
   if(TimeHour(TimeCurrent()) > HourStart && (TimeHour(TimeCurrent()) < HourEnd))
   return (true);
   else return (false);
}
//---------------------------------------------------------------------------------------------------------
int Profit()
{
   double prof = 0;
   for(int i=OrdersTotal()-1; i>=0; i--)
   {
     OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
     if(OrderMagicNumber()==Magic && OrderSymbol() == Symbol())
     {
        if (OrderType()==OP_BUY)  prof += (Bid - OrderOpenPrice())/Point*OrderLots()*10;   
        if (OrderType()==OP_SELL) prof += (OrderOpenPrice()-Ask) /Point*OrderLots()*10;
     }
   }
 
 return(prof);  
}
//---------------------------------------------------------------------------------------------------------
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) continue;
      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) continue;
      if (OrderType() == OP_SELL) count++;
   }
   return (count);
}
//---------------------------------------------------------------------------------------------------------
void DeleteWorking()
{
   for (int i=OrdersTotal()-1; i>=0; i--)
   {
      if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) break;
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue;
      Print("DeleteWorking... Symbol : " + Symbol());
      if (OrderType()==OP_BUY  ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
      if (OrderType()==OP_SELL ) OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
   }
}
//---------------------------------------------------------------------------------------------------------
int CountTrades()
{
   return (CountBuy() + CountSell()); 
}
//---------------------------------------------------------------------------------------------------------
double ND(double value)
{
   return (NormalizeDouble(value, Digits));
}
//---------------------------------------------------------------------------------------------------------
double GetLots() 
{
   double clots;
   clots = AccountBalance()    / 3000.0 * LotsFor3000;
   clots = MathMax(clots, MarketInfo(Symbol(), MODE_MINLOT));
   clots = MathMin(clots, MarketInfo(Symbol(), MODE_MAXLOT));
   clots = NormalizeDouble(clots, 2);

   return (clots);
}
//---------------------------------------------------------------------------------------------------------
int CalcLevels()
{
   // Daily
   YesterdayHigh  = iHigh (Symbol(), PERIOD_D1, 1);
   YesterdayLow   = iLow  (Symbol(), PERIOD_D1, 1);
   YesterdayClose = iClose(Symbol(), PERIOD_D1, 1);
   
   Pivot = ((YesterdayHigh + YesterdayLow + YesterdayClose)/3);

   R1 = (2*Pivot)-YesterdayLow;
   S1 = (2*Pivot)-YesterdayHigh;

   R2 = Pivot+(R1-S1);
   S2 = Pivot-(R1-S1);
   
   R3 = (YesterdayHigh + (2*(Pivot-YesterdayLow)));
   S3 = (YesterdayLow - (2*(YesterdayHigh-Pivot)));  

   // Weekly
   
   WeekHigh  = iHigh (Symbol(), PERIOD_W1, 1);
   WeekLow   = iLow  (Symbol(), PERIOD_W1, 1);
   WeekClose = iClose(Symbol(), PERIOD_W1, 1);

   WeekPivot = ((WeekHigh + WeekLow + WeekClose)/3);

   WR1 = (2*WeekPivot)-WeekLow;
   WS1 = (2*WeekPivot)-WeekHigh;

   WR2 = WeekPivot+(WR1-WS1);
   WS2 = WeekPivot-(WR1-WS1);

   WS3 = (WeekLow - (2*(WeekHigh-WeekPivot)));
   WR3 = (WeekHigh + (2*(WeekPivot-WeekLow)));
   

   MonthHigh  = iHigh (Symbol(), PERIOD_MN1, 1);
   MonthLow   = iLow  (Symbol(), PERIOD_MN1, 1);
   MonthClose = iClose(Symbol(), PERIOD_MN1, 1);

   MonthPivot = ((MonthHigh + MonthLow + MonthClose)/3);

   MR1 = (2*MonthPivot)-MonthLow;
   MS1 = (2*MonthPivot)-MonthHigh;

   MR2 = MonthPivot+(MR1-MS1);
   MS2 = MonthPivot-(MR1-MS1);

   MS3 = (MonthLow - (2*(MonthHigh-MonthPivot)));
   MR3 = (MonthHigh + (2*(MonthPivot-MonthLow)));

   return(0);
}
//---------------------------------------------------------------------------------------------------------

Recommend