Simple_advisor

Author: © 2023, Alexey ViktoroV
Price Data Components
Series array that contains the highest prices of each barSeries array that contains the lowest prices of each bar
Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
Simple_advisor
ÿþ/********************************************************************\

|                                                 Simple advisor.mq5 |

|                                            © 2023, Alexey ViktoroV |

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

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

#property copyright "© 2023, Alexey ViktoroV"

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

#property version   "1.20"

#define zz " ***** "

#include <Trade\Trade.mqh>

CTrade trade;

//---

input string    flatStart = "14:00";    // =0G0;> «D;5B0»

input string    flatStop  = "14:30";    // :>=5F «D;5B0»

input string    endRun    = "20:00";    // 7025@H5=85 @01>BK

input uint      Indent    = 35;         //  >BABC? 2 ?C=:B0E

input double    lot       = 0.01;       // @07<5@ :>=B@0:B0

input uint      eaID      = 13;         // <038: A>25B=8:0

//---

datetime timeStart,

         timeStop,

         runStop;

int ticketBuy = 0,

    ticketSell = 0,

    bars;

double contract = 0.0,

       freeMargin = 0.0,

       indent = 0.0;

bool ordOpen = false;

//---

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

int OnInit()

 {

  double volume_min = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MIN),

         volume_max = SymbolInfoDouble(_Symbol, SYMBOL_VOLUME_MAX);

  contract = lot < volume_min ? volume_min : lot > volume_max ? volume_max : lot;

  indent = Indent*_Point;

  trade.SetExpertMagicNumber(eaID);

  if(PositionSelect(_Symbol))

   {

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

     {

      ulong posTicket = PositionGetTicket(i);

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

        ordOpen = true;

     }

   }

  for(int i = OrdersTotal(); i-- > 0;)

   {

    ulong ordTicket = OrderGetTicket(i);

    if(OrderGetInteger(ORDER_MAGIC) == eaID && OrderGetString(ORDER_SYMBOL) == _Symbol)

      ordOpen = true;

   }

  return(INIT_SUCCEEDED);

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



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

void OnTick()

 {

  MqlTick tick;

  SymbolInfoTick(_Symbol, tick);

  double flatHigh,

         flatLow,

         tp,

         price,

         spred = (tick.ask-tick.bid)*2,

         margin = 0.0;

  if(NewBar(PERIOD_D1))

   {

    CloseDelete();

    timeStart   = StringToTime(flatStart);

    timeStop    = StringToTime(flatStop);

    runStop     = StringToTime(endRun);

    ordOpen     = false;

    freeMargin  = AccountInfoDouble(ACCOUNT_MARGIN_FREE);

   }

  if(!ordOpen && TimeCurrent() > timeStop)

   {

    ordOpen = true;

    int shift = Bars(_Symbol, PERIOD_M5, timeStop, timeStart),

        indexHigh = iHighest(_Symbol, PERIOD_M5, MODE_HIGH, shift),

        indexLow = iLowest(_Symbol, PERIOD_M5, MODE_LOW, shift);

    flatHigh = iHigh(_Symbol, PERIOD_M5, indexHigh);

    flatLow  = iLow(_Symbol, PERIOD_M5, indexLow);

    datetime timeHigh = iTime(_Symbol, PERIOD_M5, indexHigh),

             timeLow = iTime(_Symbol, PERIOD_M5, indexLow);

    tp = flatHigh-flatLow+indent;

    price = NormalizeDouble(fmax(flatHigh+indent, tick.ask+spred), _Digits);

    trade.CheckVolume(_Symbol, contract, price, ORDER_TYPE_BUY);

    margin = trade.CheckResultMargin();

    if(trade.CheckResultRetcode() == 0 && freeMargin > margin)

     {

      trade.BuyStop(contract, price, _Symbol, NormalizeDouble(flatLow-indent, _Digits), NormalizeDouble(flatHigh+tp, _Digits));

      freeMargin -= margin;

     }

    else

      Print(trade.CheckResultRetcodeDescription());

    //---

    price = NormalizeDouble(fmin(flatLow-indent, tick.bid-spred), _Digits);

    trade.CheckVolume(_Symbol, contract, price, ORDER_TYPE_BUY);

    margin = trade.CheckResultMargin();

    if(trade.CheckResultRetcode() == 0 && freeMargin > margin)

     {

      trade.SellStop(contract, price, _Symbol, NormalizeDouble(flatHigh+indent, _Digits), NormalizeDouble(flatLow-tp, _Digits));

      freeMargin -= margin;

     }

    else

      Print(trade.CheckResultRetcodeDescription());

   }

  if(ordOpen)

   {

    if(TimeCurrent() >= runStop)

     {

      CloseDelete();

     }

   }

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

void CloseDelete()

 {

  for(int i = OrdersTotal(); i-- > 0;)

   {

    ulong ordTicket = OrderGetTicket(i);

    if(OrderGetInteger(ORDER_MAGIC) == eaID && OrderGetString(ORDER_SYMBOL) == _Symbol)

      trade.OrderDelete(ordTicket);

   }

  if(PositionSelect(_Symbol))

   {

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

     {

      ulong posTicket = PositionGetTicket(i);

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

        trade.PositionClose(posTicket);

     }

   }

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



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

void OnDeinit(const int reason)

 {

//---

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



/**************************NewBar function***************************/

bool NewBar(ENUM_TIMEFRAMES tf)

 {

  static datetime nt = 0;

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

  if(tm == 0)

    return false;

  if(tm != nt)

   {

    nt = tm;

    return true;

   }

  return false;

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

Comments