//+------------------------------------------------------------------+
//|                                                RSI_lvl_close.mq4 |
//|                                    Copyright 2014, Igor Vedeneev |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, Igor Vedeneev"
#property link      ""
#property version   "1.00"
//+------------------------------------------------------------------+
extern bool   AvtoLot      = true;
extern double lot          = 1;
extern double moneyclose   = 0;
extern double risk         = 0.01;
extern int    period_RSI   = 14;
extern int    RSIup        = 65;
extern int    RSIdown      = 35;
extern int    sellclose    = 70;
extern int    buyclose     = 30;
extern double koof         = 1.7;
extern int    magic        = 7557;
//------- Âíåøíèå ïàðàìåòðû ------------------------------------------
extern bool   AllPositions   = True; // Óïðàâëÿòü âñåìè ïîçèöèÿìè
extern bool   ProfitTrailing = False;  // Òðàëèòü òîëüêî ïðîôèò
extern int    TrailingStop   = 50;    // Ôèêñèðîâàííûé ðàçìåð òðàëà
extern int    TrailingStep   = 10;     // Øàã òðàëà
extern bool   UseSound       = True;  // Èñïîëüçîâàòü çâóêîâîé ñèãíàë
extern string NameFileSound  = "expert.wav";  // Íàèìåíîâàíèå çâóêîâîãî ôàéëà
extern string mar = "Ìàðòèíãåéë";
extern bool use_martini = true;
extern double Lot_koef = 1.6;
extern int step = 40;
//+------------------------------------------------------------------+
double pause_if_busy = 0.7; 
int    slippage = 2; //ïðîñêàëüçûâàíèå
string comment = "Shabl"; //êîìåíòàðèé ê îðäåðó
int    MaxAttempts = 4; //ìàêñ êîëè÷åñòâî ïîïûòîê îòêðûòü èëè çàêðûòü îðäåð
double Balans, Sredstva ;
string    txt,txt1;
string    txt2="";
string    txt3="";
string    txt4="";
color col = ForestGreen;
//----
int ord;
//+------------------------------------------------------------------+
int razm1,razm2,razm3,vert1,vert2,vert3,vert4,vert5,vert6;
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int      NObay;
int      NOsell;
double   RSI;
double   dig_lot;
double   min_l, max_l;
double   SL;
int buy,sell,buy_s,sell_s,buy_l,sell_l,Orders_Total;
//+------------------------------------------------------------------+
int init()
{
   min_l=MarketInfo(Symbol(),MODE_MINLOT);
   max_l=MarketInfo(Symbol(),MODE_MAXLOT);
   double n_lot=MarketInfo(Symbol(),MODE_LOTSTEP);
   dig_lot=NormalizeDouble(MathLog(1.0/n_lot)/MathLog(10.0),0);
   return(0);
}
//+------------------------------------------------------------------+
int deinit()
{
//----
  ObjectDelete("Lable");
  ObjectDelete("Lable1");
  ObjectDelete("Lable2");
  ObjectDelete("Lable3");
  ObjectDelete("Lable4");   
//----
  ObjectDelete("profit1");
  ObjectDelete("profit2");
  ObjectDelete("profit3");
  ObjectDelete("profit4");
  ObjectDelete("profit5");
  ObjectDelete("profit6");
  ObjectDelete("profit7");
   return(0);
}
//+------------------------------------------------------------------+
int start()
{
   int    counted_bars=IndicatorCounted();
//----
   Balans = NormalizeDouble( AccountBalance(),2);
  Sredstva = NormalizeDouble(AccountEquity(),2);  
 //-------------
    if (Sredstva >= Balans/6*5) col = DodgerBlue; 
    if (Sredstva >= Balans/6*4 && Sredstva < Balans/6*5)col = DeepSkyBlue;
    if (Sredstva >= Balans/6*3 && Sredstva < Balans/6*4)col = Gold;
    if (Sredstva >= Balans/6*2 && Sredstva < Balans/6*3)col = OrangeRed;
    if (Sredstva >= Balans/6   && Sredstva < Balans/6*2)col = Crimson;
    if (Sredstva <  Balans/5                           )col = Red;
 //-------------
ObjectDelete("Lable2");
ObjectCreate("Lable2",OBJ_LABEL,0,0,1.0);
   ObjectSet("Lable2", OBJPROP_CORNER, 3);
   ObjectSet("Lable2", OBJPROP_XDISTANCE, 20);
   ObjectSet("Lable2", OBJPROP_YDISTANCE, 51);
   txt2=(DoubleToStr(AccountBalance()/100, 2));
   ObjectSetText("Lable2","ÁÀËÀÍÑ     "+txt2+"$",18,"Times New Roman",DodgerBlue);
   //-------------------------   
ObjectDelete("Lable3");
ObjectCreate("Lable3",OBJ_LABEL,0,0,1.0);
   ObjectSet("Lable3", OBJPROP_CORNER, 3);
   ObjectSet("Lable3", OBJPROP_XDISTANCE, 20);
   ObjectSet("Lable3", OBJPROP_YDISTANCE, 31);
   txt3=(DoubleToStr(AccountEquity()/100, 2));
   ObjectSetText("Lable3","ÑÐÅÄÑÒÂÀ     "+txt3+"$",18,"Times New Roman",col);
//-------------------------
ObjectDelete("Lable4");
ObjectCreate("Lable4",OBJ_LABEL,0,0,1.0);
   ObjectSet("Lable4", OBJPROP_CORNER, 3);
   ObjectSet("Lable4", OBJPROP_XDISTANCE, 20);
   ObjectSet("Lable4", OBJPROP_YDISTANCE, 11);
   txt4=(DoubleToStr(AccountProfit()/100, 2));
   ObjectSetText("Lable4","ÏÐÎÑÀÄÊÀ      "+txt4+"$",18,"Times New Roman",col);
 //-------------
   double RSI = iRSI(Symbol(), 0, period_RSI, PRICE_CLOSE, 1);
   double lots;
   int tik=ticket_ord(lots);
   //......................................
   
   double lot1, lot2, lot3, lot4, lot5, lot6, lot7, lot8, lot9, lot10, lot11, lot12, lot13, lot14, lot15, lot16;
   lot1  = lot;
   lot2  = lot1  * koof;
   lot3  = lot2  * koof;
   lot4  = lot3  * koof;
   lot5  = lot4  * koof;
   lot6  = lot5  * koof;
   lot7  = lot6  * koof;   
   lot8  = lot7  * koof;
   lot9  = lot8  * koof;
   lot10 = lot9  * koof;
   lot11 = lot10 * koof;
   lot12 = lot11 * koof;
   lot13 = lot12 * koof;
   lot14 = lot13 * koof;
   lot15 = lot14 * koof;   
   lot16 = lot15 * koof;
   
       
   if (tik == 0)  // îòêðûòü îðäåð
   {
      if (RSI < RSIdown)// âàðèàíò 1
      {
            NObay=OrderSend(Symbol(),OP_BUY,       lot,  Ask,              20, 0, 0, NULL, magic, 0, Red);
         tik=ticket_ord(lots);
      }
      if (RSI > RSIup)//âàðèàíò 1
      {
            NOsell=OrderSend(Symbol(),OP_SELL,     lot,  Bid,              20, 0, 0, NULL, magic, 0, Red);
         
         tik=ticket_ord(lots);
      }
      }
 if(use_martini) martini_f();
   // çàêðûòü îðäåð
   if (AccountProfit() > moneyclose)
{
  for (int i=0; i<OrdersTotal(); i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (AllPositions || OrderSymbol()==Symbol())
     {
   if (tik > 0 && RSI >= sellclose) TrailingPositions() ; 
   if (tik < 0 && RSI <= buyclose)  TrailingPositions() ; 
     }
    }
  }
   
}
   //......................................
   return(0);
}
//+------------------------------------------------------------------+
int ticket_ord(double& lot)
{     // âîçâðàùàåò òèêåò Buy > 0 è Sell < 0 è 0, åñëè íåò îðäåðîâ
   int ticket = 0;
   lot=0;
   for (int trade = OrdersTotal() -1; trade >=0; trade --)
      if (OrderSelect (trade, SELECT_BY_POS, MODE_TRADES))
         if (OrderSymbol()==Symbol() && OrderMagicNumber()== magic)
         {  
            if (OrderType() ==  OP_BUY)
            {
               ticket=OrderTicket();
               lot=OrderLots();
               break;
            }
            if (OrderType() ==  OP_SELL)
            {
               ticket=-OrderTicket();
               lot=OrderLots();
               break;
            }
         }
      
   return(ticket);
}  
void TrailingPositions() {
  double pBid, pAsk, pp;
  pp = MarketInfo(OrderSymbol(), MODE_POINT);
  if (OrderType()==OP_BUY) {
    pBid = MarketInfo(OrderSymbol(), MODE_BID);
    if (!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp) {
      if (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp) {
        ModifyStopLoss(pBid-TrailingStop*pp);
        return;
      }
    }
  }
  if (OrderType()==OP_SELL) {
    pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
    if (!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp) {
      if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) {
        ModifyStopLoss(pAsk+TrailingStop*pp);
        return;
      }
    }
  }
}
//+------------------------------------------------------------------+
//| Ïåðåíîñ óðîâíÿ StopLoss                                          |
//| Ïàðàìåòðû:                                                       |
//|   ldStopLoss - óðîâåíü StopLoss                                  |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStopLoss) {
  bool fm;
  fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
  if (fm && UseSound) PlaySound(NameFileSound);
}
//+------------------------------------------------------------------+
void martini_f()
{
int nor_lot;
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.1) nor_lot=1;
if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.01) nor_lot=2;
CountOpenPositions();
double Mar_Lot_b=lot*buy*Lot_koef;
double Mar_Lot_s=lot*sell*Lot_koef;
   if (OrdersTotal() == 1)  step = 50;
   if (OrdersTotal() == 2)  step = 100;
   if (OrdersTotal() == 3)  step = 150;
   if (OrdersTotal() == 4)  step = 200;
   if (OrdersTotal() == 5)  step = 250;
   if (OrdersTotal() == 6)  step = 300;
   if (OrdersTotal() == 7)  step = 350;
   if (OrdersTotal() == 8)  step = 400;
   if (OrdersTotal() == 9)  step = 450;
   if (OrdersTotal() == 10) step = 500;
   if (OrdersTotal() == 11) step = 550;
   if (OrdersTotal() == 12) step = 600;
   if (OrdersTotal() == 13) step = 650; 
 for(int i=OrdersTotal()-1; i>=0; i--)
 {
   if(OrderSelect(i, SELECT_BY_POS))
   {
   if(OrderMagicNumber()==magic)
   {
   if(OrderSymbol()==Symbol())
   {
   break; 
   } 
   }              
   }
   }
   
   
   
   if(OrderType()==OP_BUY)
   {
   //
   if(Ask<=OrderOpenPrice()-step*Point) 
   {
   int ticket_op_m=-1;
   
   for (int j_op_b = 0; j_op_b < MaxAttempts; j_op_b++)
 {
 while(IsTradeContextBusy()) Sleep(pause_if_busy*1000);
 RefreshRates();
                             
 if(buy>0)  ticket_op_m=OrderSend(Symbol(),OP_BUY,NormalizeDouble(Mar_Lot_b,nor_lot),Ask,slippage,0,0,NULL,magic,0,Green);
 if(ticket_op_m>-1)break;
 }
   
   } 
   }    
        
   if(OrderType()==OP_SELL)
   {
   //
   if(Bid>=OrderOpenPrice()+step*Point) 
   {
   ticket_op_m=-1;
   
    for (int j_op_s = 0; j_op_s < MaxAttempts; j_op_s++)
 {
 while(IsTradeContextBusy()) Sleep(pause_if_busy*1000);
 RefreshRates();
                             
 if(sell>0) ticket_op_m=OrderSend(Symbol(),OP_SELL,NormalizeDouble(Mar_Lot_s,nor_lot),Bid,slippage,0,0,comment,magic,0,OrangeRed);
 if(ticket_op_m>-1)break;
 }
   
   }
   }     
  
 
}
////////////////////////////////////////////////////////////////////////////////////
void CountOpenPositions()
{
buy=0;
sell=0;
buy_s=0;
sell_s=0;
buy_l=0;
sell_l=0;
Orders_Total=0;
for (int i=OrdersTotal()-1; i>=0; i--)
 {
   if(OrderSelect(i, SELECT_BY_POS))
   {
   if(OrderMagicNumber()==magic)
   { 
   if(OrderSymbol()==Symbol())
   {        
   if(OrderType()==OP_BUY)      buy++;
   if(OrderType()==OP_SELL)     sell++; 
   if(OrderType()==OP_BUYSTOP)      buy_s++;
   if(OrderType()==OP_SELLSTOP)     sell_s++; 
   if(OrderType()==OP_BUYLIMIT)      buy_l++;
   if(OrderType()==OP_SELLLIMIT)     sell_l++; 
   } 
   }             
   }
 }
Orders_Total=buy+sell+buy_s+sell_s+buy_l+sell_l;
}
             
            
            
            
Comments