Author: Viktorov
Orders Execution
It automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategyChecks for the total of open orders
Indicators Used
Relative strength index
0 Views
0 Downloads
0 Favorites
Izza_RSI
ÿþ//+------------------------------------------------------------------+

//|                                                     Izza_RSI.mq4 |

//|                                                         Viktorov |

//|                     https://www.mql5.com/ru/users/alexeyvik/news |

//+------------------------------------------------------------------+

/*

0@0<5B@K A>25B=8:0

1J5< ?>78F88 CAB0=02;8205BAO 02B><0B8G5A:8, 0,01 ;>B =0 :064K5 1000;

maxSL   CAB0=02;8205BAO 2 @CG=CN, 2 ?C=:B0E;



1. E>4 =0 @K=>: =0 ?>:C?:C 5A;8 8=48:0B>@ RSI 2KH5 50% =0 G0A>2>< B09<D@5<5 8 2K?>;=O5BAO CA;>285 A @07<5@>< AB>? ;>A0 A<.?.3.

2. E>4K >ACI5AB2;O5BAO >48= @07 2 45=L.  45=L <>65B 1KBL >4=0 A45;:0 =0 ?>:C?:C, 0 B0:65 >4=0 A45;:0 =0 ?@>406C.

3. A;8 @07<5@ AB>? ;>A0 ?>;CG05BAO 1>;LH5 SLmax B> 83=>@8@C5< A83=0; =0 2E>4. !B>? ;>A @025= F5=K <8=8<C< ?@54K4CI53> 4=52=>3> 10@0 - 10 ?C=:B>2.

4. "59: ?@>D8B @025= B@5E:@0B=><C @07<5@C AB>? ;>A0.

5. 5@5=>A 2 157C1KB>: 5A;8 F5=0 ?@>H;0 @0AAB>O=85 2 ?>;>68B5;L=CN AB>@>=C =5 <5=LH5 G5< @07<5@ AB>? ;>A0.

6. A;8 8<55BAO >B:@KB0O ?>78F8O 8 =0 A;54CNI89 45=L ?>;CG8; A83=0; 4;O 2E>40 B> >B@010BK205< A;54CNI85 A83=0;K.

7. > ?@>4060< 2A5 CA;>28O >1@0B=> 75@:0;L=K5.

*/

#property copyright "Viktorov"

#property link      "https://www.mql5.com/ru/users/alexeyvik/news"

#property version   "1.00"

#property strict



input bool                 auto        =  true; // Auto lot

input double               lot         =  0.01; // Contract size 1,000 waluty Deposit

input ENUM_TIMEFRAMES      tfRSI;               // TimeFrame indicator RSI

input int                  perRsi      =  14;   // Period indicator RSI

input ENUM_APPLIED_PRICE   appledRSI;           // To apply to

input int                  maxSL       =  50;   // The maximum allowable StopLoss

input int                  indent      =  10;   // Indent from the extreme to StopLoss

sinput int                 magic       =  0;    // Magic number



double pipsMaxSL  // Size StopLoss

     , pipsTP     // Size TakeProfit

     , pipsOOE    // Size >BABC?0

     , contpact;  // Size ;>B0 >@45@0

datetime newDay = 0;



//+------------------------------------------------------------------+

//|                 Expert initialization function                   |

//+------------------------------------------------------------------+

int OnInit()

  {

   int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS);

   pipsOOE    = digits%2 == 0 ? indent*_Point : indent*_Point*10;

   pipsMaxSL  = digits%2 == 0 ? maxSL*_Point : maxSL*_Point*10;

   pipsTP     = pipsMaxSL*3;

   return(INIT_SUCCEEDED);

  }



//+------------------------------------------------------------------+

//|                     Expert tick function                         |

//+------------------------------------------------------------------+

void OnTick()

  {

   int ticket=0;

   static MqlRates mqlRates[1],dayRates[1];

   double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID)

               , ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

   static bool openBuy=false

             , openSell=false;

   if(newBar(PERIOD_D1,newDay))

     {

      if(CopyRates(_Symbol,PERIOD_D1,1,1,dayRates)<0)

         return;

      openBuy=true;

      openSell=true;

      double depo=AccountBalance()<1000 ? 1 : ceil((AccountBalance()-999)/1000);

      contpact=auto ? contractSize(lot*depo) : contractSize(lot);

     }

   double rsi=iRSI(_Symbol,tfRSI,perRsi,appledRSI,0);

   if(CopyRates(_Symbol,PERIOD_D1,0,1,mqlRates)<=0)

      return;



   if(openBuy && rsi>50)

     {

      double stopBuy=fmin(dayRates[0].low,mqlRates[0].low)

                     ,ds=ask-stopBuy+pipsOOE

         , ordSL = NormalizeDouble(stopBuy-pipsOOE, _Digits)

         , ordTP = ask+ds*3

      ;

      if(ds>pipsMaxSL)

         return;

      if((ticket=OrderSend(_Symbol,OP_BUY,contpact,ask,100,0,0,NULL,magic))<0)

         return;

      if(OrderSelect(ticket,SELECT_BY_TICKET))

         bool res=OrderModify(ticket,OrderOpenPrice(),fmin(ordSL,NormalizeDouble(OrderOpenPrice()-(Ask-Bid)*3,_Digits)),ordTP,OrderExpiration());

      openBuy=false;

     }



   if(openSell && rsi<50)

     {

      double stopSell=fmax(dayRates[0].high,mqlRates[0].high)

                      ,ds=stopSell-bid+pipsOOE

         , ordSL = NormalizeDouble(stopSell+pipsOOE, _Digits)

         , ordTP = bid-ds*3

      ;

      if(ds>pipsMaxSL)

         return;

      if((ticket=OrderSend(_Symbol,OP_SELL,contpact,bid,100,0,0,NULL,magic))<0)

         return;

      if(OrderSelect(ticket,SELECT_BY_TICKET))

         bool res=OrderModify(ticket,OrderOpenPrice(),fmax(ordSL,NormalizeDouble(OrderOpenPrice()+(Ask-Bid)*3,_Digits)),ordTP,OrderExpiration());

      openSell=false;

     }

   int ordersTotal=OrdersTotal();

   bool res=false;

   if(ordersTotal>0)

     {

      for(int i=0; i<ordersTotal; i++)

        {

         res=false;

         if(OrderSelect(i,SELECT_BY_POS) && OrderSymbol()==_Symbol && OrderMagicNumber()==magic)

           {

            ticket=OrderTicket();

            int orderType=OrderType();

            double orderOpenPrice=OrderOpenPrice()

                                  ,orderStopLoss=OrderStopLoss();

            if(orderType==OP_BUY && orderStopLoss<orderOpenPrice && NormalizeDouble(bid-orderOpenPrice-pipsMaxSL,_Digits)>=0)

              {

               res=OrderModify(ticket,orderOpenPrice,NormalizeDouble(orderOpenPrice+5*_Point,_Digits),OrderTakeProfit(),OrderExpiration());

               if(!res) Print("ticket ",ticket," orderType ",orderType," orderOpenPrice ",orderOpenPrice

                     ," bid-orderOpenPrice-pipsMaxSL ",NormalizeDouble(bid-orderOpenPrice-pipsMaxSL,_Digits));

              }

            if(orderType==OP_SELL && orderStopLoss>orderOpenPrice && NormalizeDouble(orderOpenPrice-pipsMaxSL-ask,_Digits)>=0)

              {

               res=OrderModify(ticket,orderOpenPrice,NormalizeDouble(orderOpenPrice-5*_Point,_Digits),OrderTakeProfit(),OrderExpiration());

               if(!res) Print("ticket ",ticket," orderType ",orderType," orderOpenPrice ",orderOpenPrice

                     ," orderOpenPrice-pipsMaxSL-ask ",NormalizeDouble(orderOpenPrice-pipsMaxSL-ask,_Digits));

              }

           }

        }

     }

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

bool newBar(ENUM_TIMEFRAMES timeframe,datetime  &tOld)

  {

   int copy; static MqlRates newBarRates[1];

   do

     {

      copy=CopyRates(_Symbol,timeframe,0,1,newBarRates);

      Sleep(80);

     }

   while(copy>0 && IsStopped());

   datetime tNew=newBarRates[0].time;

   bool ret=tOld!=tNew;

   if(ret)

      tOld=tNew;

   return(ret);

  }

//+------------------------------------------------------------------+

double contractSize(double volume)

{

   double v = volume;

   double volumeStep = 0;

   volumeStep = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_STEP);

   int ratio = (int)floor(volume/volumeStep);

   v = ratio*volumeStep;

   double minLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN);

   double maxLot = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);

   return((v < minLot ? minLot : v > maxLot ? maxLot : v));

}/*******************************************************************/

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---