Author: Knazev Segey
Price Data Components
Indicators Used
Relative strength indexCommodity channel indexMoving average indicator
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
KSU_martin
ÿþ//+------------------------------------------------------------------+

//|                                                   KSU_martin.mg5 |

//|                                     Copyright 2020, Knazev Segey |

//|                                       http://www.companyname.net |

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

#property copyright "Knazev Segey"

#include <Trade\Trade.mqh>



input bool                start_order=false;           // =0G0BL B>@3>2;N / 70:@KBL A5B:C

input double              start_lot=0.1;               // =0G0;L=K9 ;>B

input double              tp_in_money=10.0;            // ?@>D8B 2 20;NB5

input double              multiplier=1.2;              // <=>68B5;L ;>B0

input double              signal=true;                 // A5B:0: A83=0; / 157 A83=0;0

input int                 range=10;                    // H03 A5B:8

input bool                close_all=true;              // 70:@K20BL 2A5 >@45@0

input bool                close_buy=true;              // 70:@K20BL 2A5 buy >@45@0

input bool                close_sell=true;             // 70:@K20BL 2A5 sell >@45@0





input bool                info=false;                   // 2K2>4 8=D>@<0F88





input ENUM_TIMEFRAMES     cci_timeframe=PERIOD_M1;      // ?5@8>4 3@0D8:0 CCI

input int                 cci_period=14;                // ?5@8>4 CCI

input ENUM_APPLIED_PRICE  cci_price=PRICE_CLOSE;        // B8? F5=K CCI

input int                 cci_level=100;                // 7>=0 CCI



input ENUM_TIMEFRAMES     rsi_timeframe=PERIOD_M1;      // ?5@8>4 3@0D8:0 RSI

input int                 rsi_period=14;                // ?5@8>4 RSI

input ENUM_APPLIED_PRICE  rsi_price=PRICE_CLOSE;        // B8? F5=K RSI

input int                 rsi_level=30;                 // 7>=0 RSI



input bool                use_ma=true;                  // 2:; MA

input ENUM_TIMEFRAMES     ma_timeframe=PERIOD_M1;       // ?5@8>4 3@0D8:0 

input int                 ma_period=5;                  // ?5@8>4 MA

input ENUM_MA_METHOD      ma_metod=MODE_SMA;            // A?>A>1 CA@54=5=8O MA

input ENUM_APPLIED_PRICE  ma_price=PRICE_CLOSE;         // B8? F5=K MA



//-- 3;>10;L=K5 ?5@5<5==K5

ulong             pos_TIC; // ?5@5<5==0O 4;O E@0=5=8O B8:5B0 >@45@>2

string            my_symbol; //?5@5<5==0O 4;O E@0=5=8O A8<2>;0

ENUM_TIMEFRAMES   my_timeframe;  //?5@5<5==0O 4;O E@0=5=8O B09<D@59<0

double            my_point; // ?5@5<5==0O 4;O B5:CI53> @07<5@ ?C=:B0

int               my_digits; // ?5@5<5==0O 4;O :>;8G5AB2> 45AOB8G=KE 7=0:>2 ?>A;5 70?OB>9

CTrade            m_Trade; //AB@C:BC@0 4;O 2K?>;=5=8O B>@3>2KE >?5@0F89



int               Hand_RSI;  // E5=45; RSI

double            Val_RSI[]; // <0AA82 4;O 7=0G5=89 RSI

int               Hand_CCI;  // E5=45; CCI

double            Val_CCI[]; // <0AA82 4;O 7=0G5=89 CCI

int               Hand_MA;   // E5=45; MA

double            Val_MA[];  // <0AA82 4;O 7=0G5=89 MA

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

int OnInit()

  {

   Comment(" ");

   my_symbol=Symbol();          // B5:CI89 A8<2>;

   my_timeframe=PERIOD_CURRENT; // B5:CI89 ?5@8>4

   my_point=Point();            // @07<5@ ?C=:B0

   my_digits=Digits();          // :>;8G5AB2> 45AOB8G=KE 7=0:>2



   if(_Digits == 5 || _Digits == 3)

      my_point *= 10;



   Hand_RSI=iRSI(my_symbol,rsi_timeframe,rsi_period,rsi_price);

   Hand_CCI=iCCI(my_symbol,cci_timeframe,cci_period,cci_price);

   Hand_MA=iMA(my_symbol,ma_timeframe,ma_period,0,ma_metod,ma_price);

   if(Hand_RSI<0 || Hand_CCI<0 || Hand_MA<0)

     {

      Alert("H81:0 ?@8 A>740=88 8=48:0B>@>2 - =><5@ >H81:8: ",GetLastError(),"!!");

      return(INIT_FAILED);

     }

   return(INIT_SUCCEEDED);

  }

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

void OnDeinit(const int reason)

  {

//--- #40;O5< 30D8:C

   Comment(" ");

   ObjectDelete(0,"pr");

   ObjectDelete(0,"or");

   ObjectDelete(0,"to");

//--- A2>1>6405< EM=4;K 8=48:0B>@>2

   IndicatorRelease(Hand_RSI);

   IndicatorRelease(Hand_CCI);

   IndicatorRelease(Hand_MA);

  }

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

void OnTick()

  {

   if(info)

      draw_info();



   static datetime Old_Time;

   datetime New_Time[1];

   bool IsNewBar=false;



   int copied=CopyTime(my_symbol,my_timeframe,0,1,New_Time);

   if(copied>0)

     {

      if(Old_Time!=New_Time[0])

        {

         IsNewBar=true;

         if(MQL5InfoInteger(MQL5_DEBUGGING))

            Print(">2K9 10@",New_Time[0],"AB0@K9 10@",Old_Time);

         Old_Time=New_Time[0];

        }

     }

   else

     {

      Alert("H81:0 :>?8@>20=8O 2@5<5=8, =><5@ >H81:8 =",GetLastError());

      ResetLastError();

      return;

     }

   if(IsNewBar==false)

     {

      return;

     }

//--- >1=>28< AB@C:BC@K, :>B>@K5 1C4CB 8A?>;L7>20BLAO 4;O B>@3>2;8

   MqlTick latest_price;              // 4;O B5:CI8E :>B8@>2>:

   MqlTradeRequest mrequest;          // 4;O >B?@02:8 B>@3>2KE 70?@>A>2

   MqlTradeResult mresult;            // 4;O ?>;CG5=8O @57C;LB0B>2 8A?>;=5=8O B>@3>2KE 70?@>A>2

   MqlRates mrate[];                  // A>45@68B F5=K, >1J5<K 8 A?@54 4;O :064>3> 10@0

   ZeroMemory(mrequest);



   ArraySetAsSeries(mrate,true);

   ArraySetAsSeries(Val_CCI,true);

   ArraySetAsSeries(Val_RSI,true);

   ArraySetAsSeries(Val_MA,true);

//--- >;CG8BL 8AB>@8G5A:85 40==K5 ?>A;54=8E 3-E 10@>2

   if(CopyRates(my_symbol,PERIOD_D1,0,3,mrate)<0)

     {

      Alert("H81:0 :>?8@>20=8O 8AB>@8G5A:8E 40==KE - >H81:0:",GetLastError(),"!!");

      return;

     }

//--- !:>?8@>20BL 8AB>@8G5A:85 40==K5 ?>A;54=8E 7=0G5=89 8=48:0B>@>2 3-E 10@>2

   if(CopyBuffer(Hand_MA,0,0,3,Val_MA)<0 || CopyBuffer(Hand_RSI,0,0,3,Val_RSI)<0 || CopyBuffer(Hand_CCI,0,0,3,Val_CCI)<0)

     {

      Alert("H81:0 :>?8@>20=8O 1CD5@>2 8=48:0B>@>2 - =><5@ >H81:8:",GetLastError());

      return;

     }

   if(info)

      draw_info();

//--- 1LO28< ?5@5<5===K5 4;O >B:@KB8O 8 70:@KB8O >@45@>2

   bool OpenBuy=Val_CCI[1]<-cci_level && Val_RSI[1]<rsi_level;

   bool OpenSell=Val_CCI[1]>cci_level && Val_RSI[1]>100-rsi_level;



   bool CloseBuy=Val_MA[0]<Val_MA[1] && Val_MA[1]<Val_MA[2];

   bool CloseSell=Val_MA[0]>Val_MA[1] && Val_MA[1]>Val_MA[2];

//--- B:@K205< ?5@2K9 >@45@

   if(start_order && OpenBuy  &&  OrdersBuy()==0)

      m_Trade.Buy(start_lot,my_symbol,Bid(),0,0,"Buy");

   if(start_order && OpenSell && OrdersSell()==0)

      m_Trade.Sell(start_lot,my_symbol,Ask(),0,0,"Sell");

//--- B:@K205< A5B:C Buy

   if(signal)

      OpenBuy=true;

   if(OpenBuy && OrdersBuy()>=1)

     {

      if(minOrderBuy()>Ask()+range*my_point || OrdersBuy()==OrdersSell())

         m_Trade.Buy(NormalizeDouble(maxLotBuy()*multiplier,2),my_symbol,Bid(),0,0,"Buy set");

     }

//--- B:@K205< A5B:C Sell

   if(signal)

      OpenSell=true;

   if(OpenSell && OrdersSell()>=1)

     {

      if(maxOrderSell()<Bid()-range*my_point || OrdersBuy()==OrdersSell())

         m_Trade.Sell(NormalizeDouble(maxLotSell()*multiplier,2),my_symbol,Ask(),0,0,"Sell set");

     }

//--- 0:@K205< >@45@0 Buy

   if(close_buy && ProfitBuy()> tp_in_money)

     {

      if(CloseBuy||use_ma!=true)

        {

         for(int i=PositionsTotal()-1; i>=0; i--)

           {

            if(PositionGetSymbol(i)==my_symbol)

               pos_TIC=PositionGetInteger(POSITION_TICKET);

              {

               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)

                  m_Trade.PositionClose(pos_TIC);

              }

           }



        }

     }

//--- 0:@K205< >@45@0 Sell

   if(close_sell && ProfitSell()> tp_in_money)

     {

      if(CloseSell||use_ma!=true)

        {

         for(int i=PositionsTotal()-1; i>=0; i--)

           {

            if(PositionGetSymbol(i)==my_symbol)

               pos_TIC=PositionGetInteger(POSITION_TICKET);

              {

               if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)

                  m_Trade.PositionClose(pos_TIC);

              }

           }



        }

     }

//--- 0:@K205< 2A5 >@45@0

   if(close_all && ProfitBuy()+ProfitSell()>tp_in_money)

     {

      if((ProfitBuy()>ProfitSell() && (CloseBuy||use_ma!=true)) ||

         (ProfitBuy()<ProfitSell() && (CloseSell||use_ma!=true)))

        {

         for(int i=PositionsTotal()-1; i>=0; i--)

           {

            if(PositionGetSymbol(i)==my_symbol)

               pos_TIC=PositionGetInteger(POSITION_TICKET);

            m_Trade.PositionClose(pos_TIC);

           }

        }

     }

//////////

  }

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

//--- F5=0 Ask

double Ask()

  {

   double ask=SymbolInfoDouble(my_symbol,SYMBOL_ASK);

   return(ask);

  }

//--- F5=0 Bid

double Bid()

  {

   double bid=SymbolInfoDouble(my_symbol,SYMBOL_BID);

   return(bid);

  }

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

//--- :>;8G5AB2> >@45@>2 BUY

int OrdersBuy()

  {

   int Orders=0;

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(PositionGetSymbol(i)==my_symbol)

        {

         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)

            Orders++;

        }

   return(Orders);

  }

//--- :>;8G5AB2> >@45@>2 SELL

int OrdersSell()

  {

   int Orders=0;

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(PositionGetSymbol(i)==my_symbol)

        {

         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)

            Orders++;

        }

   return(Orders);

  }

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

//--- <0:A8<0;L=K9 ;>B BUY

double maxLotBuy()

  {

   double lotik=0;

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(PositionGetSymbol(i)==my_symbol)

        {

         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)

           {

            if(lotik<PositionGetDouble(POSITION_VOLUME))

               lotik=PositionGetDouble(POSITION_VOLUME);

           }

        }

   return(lotik);

  }

//--- <0:A8<0;L=K9 ;>B SELL

double maxLotSell()

  {

   double lotik=0;

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(PositionGetSymbol(i)==my_symbol)

        {

         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)

           {

            if(lotik<PositionGetDouble(POSITION_VOLUME))

               lotik=PositionGetDouble(POSITION_VOLUME);

           }

        }

   return(lotik);

  }

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

//--- <8=8<0;L=K9 >@45@ BUY

double minOrderBuy()

  {

   double minOrder=99999999;

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(PositionGetSymbol(i)==my_symbol)

        {

         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)

           {

            if(minOrder>PositionGetDouble(POSITION_PRICE_OPEN))

               minOrder=PositionGetDouble(POSITION_PRICE_OPEN);

           }

        }

   return(minOrder);

  }

//--- <0:A8<0;L=K9 >@45@ SELL

double maxOrderSell()

  {

   double maxOrder=0;

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(PositionGetSymbol(i)==my_symbol)

        {

         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)

           {

            if(maxOrder<PositionGetDouble(POSITION_PRICE_OPEN))

               maxOrder=PositionGetDouble(POSITION_PRICE_OPEN);

           }

        }

   return(maxOrder);

  }

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

//--- ?@>D8B >@45@>2 BUY

double   ProfitBuy()

  {

   double Prof=0;

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(PositionGetSymbol(i)==my_symbol)

        {

         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)

           {

            Prof+=PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP);

           }

        }

   return(Prof);

  }

//--- ?@>D8B >@45@>2 SELL

double   ProfitSell()

  {

   double Prof=0;

   for(int i=PositionsTotal()-1; i>=0; i--)

      if(PositionGetSymbol(i)==my_symbol)

        {

         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)

           {

            Prof+=PositionGetDouble(POSITION_PROFIT)+PositionGetDouble(POSITION_SWAP);

           }

        }

   return(Prof);

  }

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

//--- 2K2>4 8=D>@<0F88

void draw_info()

  {

   string text,text1,text2;

   color col=clrSnow;



   if(ProfitSell()+ProfitBuy()>0)

     {

      text="+"+DoubleToString(ProfitSell()+ProfitBuy(),2);

      col=clrLimeGreen;

     }

   if(ProfitSell()+ProfitBuy()<0)

     {

      text=DoubleToString(ProfitSell()+ProfitBuy(),2);

      col=clrOrangeRed;

     }

   if(ProfitSell()+ProfitBuy()==0)

     {

      text=" ";

      col=clrLimeGreen;

     }

   if(OrdersBuy()+OrdersSell()>0)

      text1="Buy "+DoubleToString(OrdersBuy(),0)+"  Sell "+DoubleToString(OrdersSell(),0);

   else

      text1="5B >@45@>2";

   text2="A53> "+DoubleToString(PositionsTotal(),0);



   ObjectDelete(0,"pr");

   ObjectCreate(0,"pr",OBJ_LABEL,0,0,0);

   ObjectSetInteger(0,"pr",OBJPROP_XDISTANCE,100);

   ObjectSetInteger(0,"pr",OBJPROP_YDISTANCE,60);

   ObjectSetInteger(0,"pr",OBJPROP_CORNER,CORNER_RIGHT_LOWER);

   ObjectSetString(0,"pr",OBJPROP_TEXT,text);

   ObjectSetString(0,"pr",OBJPROP_FONT,"Arial");

   ObjectSetInteger(0,"pr",OBJPROP_FONTSIZE,12);

   ObjectSetInteger(0,"pr",OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);

   ObjectSetInteger(0,"pr",OBJPROP_COLOR,col);



   ObjectDelete(0,"or");

   ObjectCreate(0,"or",OBJ_LABEL,0,0,0);

   ObjectSetInteger(0,"or",OBJPROP_XDISTANCE,110);

   ObjectSetInteger(0,"or",OBJPROP_YDISTANCE,42);

   ObjectSetInteger(0,"or",OBJPROP_CORNER,CORNER_RIGHT_LOWER);

   ObjectSetString(0,"or",OBJPROP_TEXT,text1);

   ObjectSetString(0,"or",OBJPROP_FONT,"Arial");

   ObjectSetInteger(0,"or",OBJPROP_FONTSIZE,10);

   ObjectSetInteger(0,"or",OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);

   ObjectSetInteger(0,"or",OBJPROP_COLOR,clrLimeGreen);



   ObjectDelete(0,"to");

   ObjectCreate(0,"to",OBJ_LABEL,0,0,0);

   ObjectSetInteger(0,"to",OBJPROP_XDISTANCE,100);

   ObjectSetInteger(0,"to",OBJPROP_YDISTANCE,24);

   ObjectSetInteger(0,"to",OBJPROP_CORNER,CORNER_RIGHT_LOWER);

   ObjectSetString(0,"to",OBJPROP_TEXT,text2);

   ObjectSetString(0,"to",OBJPROP_FONT,"Arial");

   ObjectSetInteger(0,"to",OBJPROP_FONTSIZE,10);

   ObjectSetInteger(0,"to",OBJPROP_ANCHOR,ANCHOR_LEFT_UPPER);

   ObjectSetInteger(0,"to",OBJPROP_COLOR,clrIvory);

  }

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

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 ---