Author: © 2019, Alexey Viktorov
Indicators Used
Indicator of the average true rangeMoving average indicator
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
4UJ
ÿþ//+------------------------------------------------------------------+

//|                                                        «4UJ».mq5 |

//|                                          © 2019, Alexey Viktorov |

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

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

#property copyright "© 2019, Alexey Viktorov"

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

#property version   "1.2"

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

//|                                                                  |

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

struct HANDLE

  {

   int               ATR;

   int               MA_ATR;

   int               MA_CHART;

  }

handle;

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

//|                                                                  |

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

struct IND_DATA

  {

   double            ATR[];

   double            MA_ATR[];

   double            MA_CHART[];

  }

ind_date;



#include <Trade\Trade.mqh>

CTrade trade;

MqlRates mqlRates[];

MqlTick mqlTick;



input ENUM_TIMEFRAMES  workingPeriod   = PERIOD_H4;  //  Working time frame

input double           lot             = 0.1;        //  The size of the contract, if Risk = 0

input double           Risk            = 0.0;        //  Risk

input int              take            = 1000;       //  Take profit

input int              stop            = 260;        //  Stop loss

input int              period_ATR      = 7;          //  Indicator Average True Range period

input int              period_MA_ATR   = 30;         //  Indicator average period Average True Range

input int              period_MA_CHART = 48;         //  Indicator Moving Average period

input int              bodyMin         = 10;         //  The minimum size of the candle

sinput int             magicNumber     = 1;          //  Magic number



int NoLoss=10;

double contract,takeProfit,stopLoss,noLoss;

bool atrSignal=false;

datetime timeUP=0,timeDN=0;

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

//|                                                                  |

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

int OnInit()

  {

   TesterHideIndicators(true);

   trade.SetExpertMagicNumber(magicNumber);

   takeProfit=take*_Point;

   stopLoss=stop*_Point;

   noLoss=NoLoss*_Point;

   handle.ATR=iATR(_Symbol,workingPeriod,period_ATR);

   handle.MA_ATR=iMA(_Symbol,workingPeriod,period_MA_ATR,0,MODE_SMA,handle.ATR);

   handle.MA_CHART=iMA(_Symbol,workingPeriod,period_MA_CHART,0,MODE_SMA,PRICE_CLOSE);

//---

   return(INIT_SUCCEEDED);

  }

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

//|                                                                  |

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

void OnTick()

  {

   bool tradeAllowed=TerminalInfoInteger(TERMINAL_TRADE_ALLOWED);

   double margin = 0, freeMargin = AccountInfoDouble(ACCOUNT_MARGIN_FREE);

   if(!SymbolInfoTick(_Symbol,mqlTick))

      return;

   PosModify(tradeAllowed);

//---

   if(newBar(workingPeriod))

     {

      if(CopyRates(_Symbol,PERIOD_H4,0,2,mqlRates)<0) Print(__FUNCTION__," ",GetLastError());

      CopyBuffer(handle.ATR, 0, 0, 3, ind_date.ATR);

      CopyBuffer(handle.MA_ATR, 0, 0, 3, ind_date.MA_ATR);

      CopyBuffer(handle.MA_CHART, 0, 0, 3, ind_date.MA_CHART);

      if(!atrSignal && ind_date.ATR[0]<ind_date.MA_ATR[0] && ind_date.ATR[1]>ind_date.MA_ATR[1])

        {

         timeUP=mqlRates[0].time;

         atrSignal=true;

        }

      if(atrSignal && ind_date.ATR[0]>ind_date.MA_ATR[0] && ind_date.ATR[1]<ind_date.MA_ATR[1])

         timeDN=mqlRates[0].time;

      if(timeDN>timeUP && mqlRates[1].time-timeDN>PeriodSeconds(workingPeriod)*2)

         atrSignal=false;

      if(atrSignal)

        {

         contract=Risk==0 ? CheckContractVolume(lot) : RiskLots(Risk,stop);

         if(mqlTick.bid>ind_date.MA_CHART[2])

           {

            if(mqlRates[0].open-mqlRates[0].close>=bodyMin*_Point)

              {

               if(OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,contract,mqlTick.ask,margin) && freeMargin-margin<=0)

                {

                 Print("Not enough money");

                 return;

                }

               if(tradeAllowed)

                  trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,contract,mqlTick.ask,mqlTick.bid-stopLoss,mqlTick.ask+takeProfit);

               else Alert("... ",contract);

               atrSignal=false;

              }

           }

         if(mqlTick.ask<ind_date.MA_CHART[2])

           {

            if(mqlRates[0].close-mqlRates[0].open>=bodyMin*_Point)

              {

               if(OrderCalcMargin(ORDER_TYPE_SELL,_Symbol,contract,mqlTick.bid,margin) && freeMargin-margin<=0)

                {

                 Print("Not enough money");

                 return;

                }

               if(tradeAllowed)

                  trade.PositionOpen(_Symbol,ORDER_TYPE_SELL,contract,mqlTick.bid,mqlTick.ask+stopLoss,mqlTick.bid-takeProfit);

               else Alert("... ",contract);

               atrSignal=false;

              }

           }

        }

     }

  }

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

//|                                                                  |

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

void PosModify(bool tradeAllowed)

  {

   int posTotal=PositionsTotal();

   if(posTotal>0)

     {

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

        {

         ulong posTicket=PositionGetTicket(i);

         ENUM_POSITION_TYPE posType=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);

         double posOpen=PositionGetDouble(POSITION_PRICE_OPEN);

         double posTP = PositionGetDouble(POSITION_TP);

         double posSL = PositionGetDouble(POSITION_SL);

         if(tradeAllowed)

           {

            switch(posType)

              {

               case POSITION_TYPE_BUY :

                  if(posSL > posOpen || mqlTick.bid-posOpen < stopLoss) return;

                  posSL=posOpen+noLoss;

                  break;

               case POSITION_TYPE_SELL :

                  if(posSL < posOpen || posOpen-mqlTick.ask < stopLoss) return;

                  posSL=posOpen-noLoss;

                  break;

               default:

                  break;

              }

            trade.PositionModify(posTicket,posSL,posTP);

           }

         else

            Alert(_Symbol," posTicket ",posTicket);

        }

     }

  }

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

//|                                                                  |

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

double CheckContractVolume(double volume)

  {

   double v=volume;

   double volumeStep=0;

   volumeStep= SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);

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

   if(fabs(ratio*volumeStep-volume)>0.0000001)

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

  }

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

//|                                                                  |

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

double RiskLots(double risk,int SL)

  {

   double RiskMony,Lot;

   double tickValue=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);

   double margin=SymbolInfoDouble(_Symbol,SYMBOL_MARGIN_INITIAL);

   double FreeMargin=AccountInfoDouble(ACCOUNT_MARGIN_FREE);

   long accountLeverage=AccountInfoInteger(ACCOUNT_LEVERAGE);

   RiskMony=floor(FreeMargin*risk/100);

   Lot=CheckContractVolume(RiskMony*_Point/NormalizeDouble((SL*_Point*tickValue),_Digits));

   return(Lot);

  }

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

//|                                                                  |

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

void OnDeinit(const int reason)

  {

   Comment("");

  }

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

//|                                                                  |

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

bool newBar(ENUM_TIMEFRAMES tf)

  {

   datetime currTime = iTime(_Symbol, tf, 0);

   static datetime timeLastBar;

    bool ret = timeLastBar != currTime;

     if(ret)

      timeLastBar = currTime;

     return(ret);

  }

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

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