Long Waiting

Author: Etrid
Orders Execution
It Closes Orders by itself It automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
Long Waiting
ÿþ//+------------------------------------------------------------------+

//|                                                 Long Waiting.mq5 |

//|                                                            Etrid |

//|                              https://www.mql5.com/ru/users/etrid |

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

#property copyright "Etrid"

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

#property version   "1.00"

//---

#property description "Expert every day opening long position for CurrentPeriod active if haven't opened position"

#property description "In the end day if we have profit then we will fix position and open new. If we haven't profit the we nothing do"

#property description "InpCoefficient - if set on 100 and deposit is 1000, Volume will be 1000/100 = 10 * 0.01 = 0.1"

//--- input parameters

input long   InpOrderMagic                = 0000072;  // Order magic

input int    InpCoefficient               = 100;      // Volume coeficient

input double InpMaximumVolume             = 0.01;     // Maximum volume

input double InpMinimumProfit             = 0.5;      // Minimum profit for fixation

input bool   InpClosePositionVersionTwo   = false;    // if true then position will be fixed 23:00 else if false then in 18:00

//--- other parameters

double Balance          = AccountInfoDouble(ACCOUNT_BALANCE);  // Account balance

double MicroVolumes     = MathFloor(Balance/InpCoefficient);   // Number of microlots

double Volume           = 0.01*MicroVolumes;                   // Total volume

string LastUpDate       = "00:00";                             // Last time script action (actions with positions)

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

//|  Main action                                                     |

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

void OnTick()

  {

//---

   Balance = AccountInfoDouble(ACCOUNT_BALANCE);

   if(Balance < InpCoefficient)

     {

      Balance = InpCoefficient;

     }

   MicroVolumes = MathFloor(Balance/InpCoefficient);



   if(SymbolInfoDouble(_Symbol,SYMBOL_TRADE_LIQUIDITY_RATE) * Volume > Balance)

     {

      MicroVolumes = MathFloor(Balance/(SymbolInfoDouble(_Symbol,SYMBOL_TRADE_LIQUIDITY_RATE) * Volume));

     }



   Volume = 0.01*MicroVolumes;



   if(Volume > InpMaximumVolume)

     {

      Volume = InpMaximumVolume;

     }



//---

   bool CurrentPeriod = false; // started new bar for CurrentPeriod period



   datetime Date = TimeLocal();

   MqlDateTime MDT;

   TimeToStruct(Date,MDT);

//---

   if(Period() == PERIOD_D1)

     {

      int CurrentHour = 18; // Current hour for working



      if(InpClosePositionVersionTwo)

        {

         CurrentHour = 23;

        }



      if(MDT.hour == CurrentHour)

        {

         CurrentPeriod = true;

        }



      if(LastUpDate == IntegerToString(MDT.mon)+":"+IntegerToString(MDT.day))

        {

         CurrentPeriod = false;

        }



      if(CurrentPeriod && _Symbol != "XAUUSD")

        {

         if(PositionSelect(_Symbol))

           {

            //--- close and reopen position if we have profit

            if(PositionGetDouble(POSITION_PROFIT) > InpMinimumProfit)

              {

               OrderClose(InpOrderMagic, PositionGetDouble(POSITION_VOLUME), PositionGetInteger(POSITION_TICKET));

               OrderOpen(InpOrderMagic, Volume, "long");

               LastUpDate = IntegerToString(MDT.mon)+":"+IntegerToString(MDT.day);

              }

           }

         else //--- open position if we haven't positions

           {

            OrderOpen(InpOrderMagic, Volume, "long");

            LastUpDate = IntegerToString(MDT.mon)+":"+IntegerToString(MDT.day);

           }

        }

     }

   else

     {

      Print("Error: Undefinded CurrentPeriod timeframe");

     }

//---

  }



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

//| Open order                                                       |

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

void OrderOpen(long const magic_number, double OpenVolume, string OrderType)

  {

//---

   MqlTradeRequest request= {0};

   MqlTradeResult  result= {0};



   double price = 0;



//---

   if(OrderType == "long") // long tp

     {

      price        = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

      request.type = ORDER_TYPE_BUY;

     }

   else

      if(OrderType == "short") // short tp

        {

         price        = SymbolInfoDouble(_Symbol,SYMBOL_BID);

         request.type = ORDER_TYPE_SELL;

        }

//---

   Print("volume ",OpenVolume);

   request.action  =  TRADE_ACTION_DEAL;    // set market order

   request.magic   =  magic_number;

   request.symbol  =  _Symbol;

   request.volume  =  OpenVolume;

   request.sl      =  0;

   request.tp      =  0;

   request.price   =  price;                // open price (market)

//---

   if(OrderSend(request,result))

     {

      Print(__FUNCTION__,":",result.comment);

      if(result.retcode==10016)

         Print(result.bid,result.ask,result.price);

     }

   else

     {

      PrintFormat("Error for opening %s  With code %d",_Symbol,result.retcode);

     }

  }



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

//|   Close order                                                    |

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

void OrderClose(long const magic_number, double Volumes, ulong  position_ticket)

  {

//---

   MqlTradeRequest request;

   MqlTradeResult  result;



   ZeroMemory(request);

   ZeroMemory(result);



//---



   request.action     =  TRADE_ACTION_DEAL;            // actyion type

   request.position   =  position_ticket;              // position ticket

   request.symbol     =  _Symbol;                      // symbol

   request.volume     =  Volumes;                      // volume

   request.deviation  =  5;                            // deviation

   request.magic      =  magic_number;                 // MagicNumber position



//---

   if(PositionGetInteger(POSITION_TYPE)  ==  ORDER_TYPE_BUY)

     {

      request.price  =  SymbolInfoDouble(_Symbol,SYMBOL_BID);

      request.type   =  ORDER_TYPE_SELL;

     }

   else

     {

      request.price  =  SymbolInfoDouble(_Symbol,SYMBOL_ASK);

      request.type   =  ORDER_TYPE_BUY;

     }

//---

   if(OrderSend(request,result))

     {

      PrintFormat("Close #%I64d %s",position_ticket,_Symbol);

     }

   else

     {

      PrintFormat("Error for closing #%I64d %s  With code %d",position_ticket,_Symbol,result.retcode);

     }

  }

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

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