Author: Copyright 2017, Sergey Pavlov (DC2008)
Indicators Used
Bollinger bands indicator
1 Views
0 Downloads
0 Favorites
bollinger
ÿþ//+------------------------------------------------------------------+

//|                                                    bollinger.mq5 |

//|                                           Copyright 2017, DC2008 |

//|                              http://www.mql5.com/ru/users/dc2008 |

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

#property copyright     "Copyright 2017, Sergey Pavlov (DC2008)"

#property link          "http://www.mql5.com/ru/users/dc2008"

#property version       "1.00"

#property description   "Example bollinger expert"

//---

#include <Trade\Trade.mqh>

//---

MqlTick    last_tick;

CTrade     trade;

//---

input int                  e_bands_period=80;           // ?5@8>4 A:>;L7OI59 A@54=59 

int                        e_bands_shift=0;             // A4283 

input double               e_deviation=3.0;             // :>;-2> AB0=40@B=KE >B:;>=5=89  

input ENUM_APPLIED_PRICE   e_applied_price=PRICE_CLOSE; // B8? F5=K 

//---

double lot=0.01;

bool   on_trade=false;

//--- ?5@5<5==0O 4;O E@0=5=8O EM=4;0 8=48:0B>@0 iBands 

int    handle;

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

//| !B@C:BC@0 A83=0;0                                                |

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

struct sSignal

  {

   bool              Buy;    // A83=0; =0 ?>:C?:C

   bool              Sell;   // A83=0; =0 ?@>406C

  };

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

//| 5=5@0B>@ A83=0;>2                                               |

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

sSignal Buy_or_Sell()

  {

   sSignal res={false,false};

//--- 8=48:0B>@=K5 1CD5@K 

   double         UpperBuffer[];

   double         LowerBuffer[];

   double         MiddleBuffer[];

   ArraySetAsSeries(MiddleBuffer,true);

   CopyBuffer(handle,0,0,1,MiddleBuffer);

   ArraySetAsSeries(UpperBuffer,true);

   CopyBuffer(handle,1,0,1,UpperBuffer);

   ArraySetAsSeries(LowerBuffer,true);

   CopyBuffer(handle,2,0,1,LowerBuffer);

//--- B09<A5@88

   double L[];

   double H[];

   ArraySetAsSeries(L,true);

   CopyLow(_Symbol,_Period,0,1,L);

   ArraySetAsSeries(H,true);

   CopyHigh(_Symbol,_Period,0,1,H);

   if(H[0]>UpperBuffer[0] && L[0]>MiddleBuffer[0])

      res.Sell=true;

   if(L[0]<LowerBuffer[0] && H[0]<MiddleBuffer[0])

      res.Buy=true;

//---

   return(res);

  }

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

//| Expert initialization function                                   |

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

int OnInit()

  {

   Comment("");

//--- A>74048< EM=4; 8=48:0B>@0 

   handle=iBands(_Symbol,_Period,e_bands_period,e_bands_shift,e_deviation,e_applied_price);

   if(handle==INVALID_HANDLE)

      return(INIT_FAILED);

   else

      on_trade=true;

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   if(on_trade)

     {

      sSignal signal=Buy_or_Sell();

      //--- BUY

      if(signal.Buy)

        {

         if(!PositionSelect(_Symbol))

           {

            SymbolInfoTick(_Symbol,last_tick);

            trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,NormalizeDouble(lot,2),last_tick.ask,0,0,"BUY: new position");

           }

         else

           {

            if(PositionGetDouble(POSITION_PROFIT)<0) return;

            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)

              {

               trade.PositionClose(_Symbol);

               SymbolInfoTick(_Symbol,last_tick);

               trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,NormalizeDouble(lot,2),last_tick.ask,0,0,"BUY: reversal");

              }

           }

        }



      //--- SELL

      if(signal.Sell)

        {

         if(!PositionSelect(_Symbol))

           {

            SymbolInfoTick(_Symbol,last_tick);

            trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,NormalizeDouble(lot,2),last_tick.bid,0,0,"SELL: new position");

           }

         else

           {

            if(PositionGetDouble(POSITION_PROFIT)<0) return;

            if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)

              {

               trade.PositionClose(_Symbol);

               SymbolInfoTick(_Symbol,last_tick);

               trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,NormalizeDouble(lot,2),last_tick.bid,0,0,"SELL: reversal");

              }

           }

        }

     }

  }

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

//| Trade function                                                   |

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

void OnTrade()

  {

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

  }

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

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