Author: © 2021, Alexey Viktorov
0 Views
0 Downloads
0 Favorites
DVA_Martin
ÿþ/********************************************************************\

|                                                     DVA_Martin.mq5 |

|                                            © 2021, Alexey Viktorov |

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

\********************************************************************/

#property copyright "© 2021, Alexey Viktorov"

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

#property version   "3.00"



#include <Trade\Trade.mqh>

CTrade trade;

input int     TakeProfit1         = 300;  // ?@>D8B ?5@2>9 ?>78F88

input int     TakeProfit2         = 250;  // ?@>D8B 2B>@>9 ?>78F88

input int     TakeProfit3         = 200;  // ?@>D8B B@5BL59 ?>78F88

input int     TakeProfit4         = 100;  // ?@>D8B G5B25@B>9 8 A;54CNI8E ?>78F89

input int     Step                = 200;  // H03 ?5@2>9 ?>78F88

input int     Delta               = 100;  // 4>102:0 : H03C

// A> 2B>@>9 ?>78F88 C25;8G8205B @0AAB>O=85 ?>A;54CNI8E ?>78F89 =0 25;8G8=C "45;LB0" >B ?@54K4CI53>

input double  Lot                 = 0.03; // ?5@2K9 ;>B >B:@KB8O

input double  MaximalLot          = 0.5;  // <0:A8<0;L=K9 ;>B, :>B>@K9 <K @07@5H05<

input int     MaxTrades           = 9;    // <0:A8<0;L=>5 :>;8G5AB2> ?>78F89 >4=>3> =0?@02;5=8O

input double  MultiplicatorLot    = 1.6;  // C<=>605< ?>A;54CNI85 ?>78F88

input int     Magic               = 123;  // 845=B8D8:0B>@ A>25B=8:0

//--- @5<5==K5 4;O E@0=5=8O A>1@0==>9 8=D>@<0F88

double MaxLot = 0,

       LotBuy = 0,

       LotSell = 0;



/*******************Expert initialization function*******************/

int OnInit()

 {

  trade.SetExpertMagicNumber(Magic);

  MaxLot = contractSize(MaximalLot);

  return(INIT_SUCCEEDED);

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



/************************Expert tick function************************/

void OnTick()

 {

  int posTotal = PositionsTotal();

  double BuyMinPrice = DBL_MAX, //  8=8<0;L=0O F5=0 Buy ?>78F88

         SelMaxPrice = DBL_MIN, //  0:A8<0;L=0O F5=0 Sell ?>78F88

         BuyMinLot = 0,         //  >B A0<>9 =86=59 Buy ?>78F88

         SelMaxLot = 0,         //  >5 A0<>9 25@E=59 Sell ?>78F88

         posPrice = 0.0,

         posLot = 0.0,

         BuyAwerage = 0,

         SelAwerage = 0,

         BuyPrice = 0,

         SelPrice = 0,

         BuyLot = 0,

         SelLot = 0;

  MqlTick tick;

  if(!SymbolInfoTick(_Symbol, tick))

    return;

  int b = 0,

      s = 0;

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

   {

    ulong posTicket = PositionGetTicket(i);

    if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == Magic)

     {

      posPrice = PositionGetDouble(POSITION_PRICE_OPEN);

      posLot = PositionGetDouble(POSITION_VOLUME);

      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)

       {

        b++; // !G8B05< >B:@KBK5 ?>78F88 =0 ?>:C?:C

        if(posPrice < BuyMinPrice)

         {

          BuyMinPrice = posPrice;

          BuyMinLot = posLot;

          BuyPrice += posPrice*posLot;  // 4>1028BL : ?5@5<5==>9 BuyPrice &5=0 >@54@0 1 * ;>B >@45@0 1

          BuyLot += posLot;             // AC<<8@C5< ;>BK

         }

       }

      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)

       {

        s++; // !G8B05< >B:@KBK5 ?>78F88 =0 ?@>406C

        if(posPrice > SelMaxPrice)

         {

          SelMaxPrice = posPrice;

          SelMaxLot = posLot;

          SelPrice += posPrice*posLot;  // 4>1028BL : ?5@5<5==>9 BuyPrice &5=0 >@54@0 1 * ;>B >@45@0 1

          SelLot += posLot;             // AC<<8@C5< ;>BK

         }

       }

     }

   }

  LotBuy = b == 0 ? contractSize(Lot) : fmin(MaxLot, contractSize(BuyMinLot*MultiplicatorLot));

  LotSell = s == 0 ? contractSize(Lot) : fmin(MaxLot, contractSize(SelMaxLot*MultiplicatorLot));

//---

  if(b == 0 || (b < MaxTrades && BuyMinPrice-tick.ask >= (Step+Delta*b)*_Point))

    openPos(ORDER_TYPE_BUY, LotBuy, tick.ask);

  if(s == 0 || (s < MaxTrades && tick.bid-SelMaxPrice >= (Step+Delta*s)*_Point))

    openPos(ORDER_TYPE_SELL, LotSell, tick.bid);

//--- ?>AG8B05< <0B5<0B8G5A:85 D>@<C;K A@54=8E F5=

  switch(b)

   {

    case 0 :

      return;

    case 1 :

      BuyAwerage = BuyPrice/BuyLot+TakeProfit1*_Point;

      break;

    case 2 :

      BuyAwerage = BuyPrice/BuyLot+TakeProfit2*_Point;

      break;

    case 3 :

      BuyAwerage = BuyPrice/BuyLot+TakeProfit3*_Point;

      break;

    default:

      BuyAwerage = BuyPrice/BuyLot+TakeProfit4*_Point;

      break;

   }

  switch(s)

   {

    case 0 :

      return;

    case 1 :

      SelAwerage = SelPrice/SelLot-TakeProfit1*_Point;

      break;

    case 2 :

      SelAwerage = SelPrice/SelLot-TakeProfit2*_Point;

      break;

    case 3 :

      SelAwerage = SelPrice/SelLot-TakeProfit3*_Point;

      break;

    default:

      SelAwerage = SelPrice/SelLot-TakeProfit4*_Point;

      break;

   }

  normalizePrice(BuyAwerage); // @>872545< @0AG5B F5=K TP Buy

  normalizePrice(SelAwerage); // @>872545< @0AG5B F5=K TP Sell

//---

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

   {

    ulong posTicket = PositionGetTicket(i);

    if(PositionGetString(POSITION_SYMBOL) == _Symbol && PositionGetInteger(POSITION_MAGIC) == Magic)

     {

      double posTP = PositionGetDouble(POSITION_TP);

      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)

       {

        if(fabs(posTP-BuyAwerage) > _Point && tick.ask < BuyAwerage) // A;8 B59: =5 @025= B@51C5<>9 F5=5

          if(!trade.PositionModify(posTicket, 0.0, BuyAwerage))

            Print("H81:0 ", __LINE__);

       }

      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)

       {

        if(fabs(posTP-SelAwerage) > _Point && tick.bid > SelAwerage) // A;8 B59: =5 @025= B@51C5<>9 F5=5

          if(!trade.PositionModify(posTicket, 0.0, SelAwerage))

            Print("H81:0 ", __LINE__);

       }

     }

   }

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



/********************************************************************/

void openPos(ENUM_ORDER_TYPE type, double lot, double price)

 {

  trade.CheckVolume(_Symbol, lot, price, type);

  if(trade.CheckResultMarginFree() <= 0.0)

   {

    Print("Not enough money for ", EnumToString(type), " ", lot, " ", _Symbol, " Error code=", trade.CheckResultRetcode());

    return;

   }

  if(!trade.PositionOpen(_Symbol, type, lot, price, 0.0, 0.0))

    Print(trade.ResultRetcode());

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



/********************************************************************/

double contractSize(double volume, string symbol = NULL)

 {

  symbol = symbol == NULL ? _Symbol : symbol;

  double v = volume;

  double volumeStep = SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP);

  v = round(volume/volumeStep)*volumeStep;

  double minLot = SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN);

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

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



/********************************************************************/

double normalizePrice(double &price, string symbol = NULL)

 {

  symbol = symbol == NULL ? _Symbol : symbol;

  double tickSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);

  price = round(price/tickSize)*tickSize;

  return(tickSize);

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



/******************Expert deinitialization function******************/

void OnDeinit(const int reason)

 {

  Comment("");

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



/***********************OnChartEvent function************************

void OnChartEvent(const int id,         // 845=B8D8:0B>@ A>1KB8O

                  const long& lparam,   // ?0@0<5B@ A>1KB8O B8?0 long

                  const double& dparam, // ?0@0<5B@ A>1KB8O B8?0 double

                  const string& sparam  // ?0@0<5B@ A>1KB8O B8?0 string

                 )

 {

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



/*********************TradeTransaction function**********************

void OnTradeTransaction(const MqlTradeTransaction& trans,

                        const MqlTradeRequest& request,

                        const MqlTradeResult& result)

 {

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

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