OmnivorousV3DiadCodeBase

Author: Etrid
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reached
0 Views
0 Downloads
0 Favorites
OmnivorousV3DiadCodeBase
ÿþ//+------------------------------------------------------------------+

//|                                             OmnivorousV3Diad.mq5 |

//|                                                            Etrid |

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

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

#property copyright "Etrid"

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

#property version   "3.00"



input  int    InpOffset                      = 100;                                    // Offset of current price for create an order

input  double InpVolume                      = 0.01;                                   // Volume

input  long   InpOrderMagic                  = 0000547;                                // Order magic

input  double InpNeedProfit                  = 2.0;                                    // Minimal profit for close position



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

//| Variab                                                           |

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

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

double        longPrice                      = 0.00;                                   // Price of long limits

double        shortPrice                     = 0.00;                                   // Price of short limits



bool          Failed                         = false;                                  // Cand close all positions





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

//| Expert tick function                                             |

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

void OnTick()

  {

   if(CheckCurrentPeriod() && _Symbol != "XAUUSD" && _Symbol != "EURUSD")

     {

      GetProcess();

     }

  }

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

//| Main action                                                      |

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

void GetProcess()

  {

   datetime date = TimeLocal();

   MqlDateTime MDT;

   TimeToStruct(date,MDT);



   if(GetTotalProfit() > InpNeedProfit || (MDT.hour == 22 && MDT.min == 56))

     {

      CloseAllOrders();



      if(GetOrdersTotal() > 0 && GetPositionsTotal() > 0)

        {

         Failed = true;

        }

     }







   if(Failed)

     {

      CloseAllOrders();



      if(GetOrdersTotal() == 0 && GetPositionsTotal() == 0)

        {

         Failed = false;

        }

     }



   if(GetOrdersTotal() == 0 && GetPositionsTotal() == 0 && Failed == false)

     {

      double point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);                // one pip



      longPrice  = SymbolInfoDouble(_Symbol,SYMBOL_ASK)+InpOffset*point; // price for open long

      shortPrice = SymbolInfoDouble(_Symbol,SYMBOL_ASK)-InpOffset*point; // price for open short



      OrderCreate(ORDER_TYPE_BUY_STOP);

      OrderCreate(ORDER_TYPE_SELL_STOP);

     }

  }

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

//| Check bar to accept on action                                    |

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

bool CheckCurrentPeriod()

  {

   bool CurrentPeriod = false; // new bar



   datetime date = TimeLocal();

   MqlDateTime MDT;

   TimeToStruct(date,MDT);



   int dat[] = {1,6,11,16,21,26,31,36,41,46,51,56};



   for(int i = 0 ; i < ArraySize(dat); i++)

     {

      if(MDT.min == dat[i])

        {

         CurrentPeriod = true;

        }

     }



   if(LastUpDate == IntegerToString(MDT.hour)+":"+IntegerToString(MDT.min))

     {

      CurrentPeriod = false;

     }



   return CurrentPeriod;

  }

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

//| Fix last action time                                             |

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

void FixTime()

  {

   datetime date = TimeLocal();

   MqlDateTime MDT;

   TimeToStruct(date,MDT);



   LastUpDate = IntegerToString(MDT.hour)+":"+IntegerToString(MDT.min);

  }

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

//| Get total profit by positions                                    |

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

double GetTotalProfit()

  {

   double profit = 0;



   if(GetPositionsTotal() > 0)

     {

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

        {

         PositionGetTicket(i);

         if(PositionGetInteger(POSITION_MAGIC) == InpOrderMagic)

           {

            profit += PositionGetDouble(POSITION_PROFIT) - PositionGetDouble(POSITION_SWAP);

           }

        }

     }



   return profit;

  }

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

//|   Close all orders                                               |

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

void CloseAllOrders()

  {

   for(ulong i = 0 ; i < PositionsTotal(); i++)

     {

      if(PositionGetInteger(POSITION_MAGIC) == InpOrderMagic)

        {

         ClosePosition(PositionGetTicket(i));

        }

     }



   for(ulong i = 0 ; i < OrdersTotal(); i++)

     {

      if(OrderGetInteger(ORDER_MAGIC) == InpOrderMagic)

        {

         CloseOrder(OrderGetTicket(i));

        }

     }

  }

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

//| Open order                                                       |

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

void OrderCreate(ENUM_ORDER_TYPE orderType)

  {

   MqlTradeRequest request= {};

   MqlTradeResult  result= {};

   

   double Volume = InpVolume;

   

   Volume = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);

   

   request.action     =  TRADE_ACTION_PENDING;                      

   request.symbol     =  _Symbol;                                      

   request.volume     =  Volume;                                   

   request.deviation  =  50;                                           

   request.magic      =  InpOrderMagic;                                

   request.type       =  orderType;



   long digits=SymbolInfoInteger(_Symbol,SYMBOL_DIGITS);               



   if(orderType==ORDER_TYPE_BUY_STOP)

     {

      // B8? >@45@0

      request.price=NormalizeDouble(longPrice,(int)digits);                 

     }

   else

      if(orderType==ORDER_TYPE_SELL_STOP)

        {

         // B8? >@45@0

         request.price    =NormalizeDouble(shortPrice,(int)digits);           

        }

//--- >B?@02:0 70?@>A0

   if(!OrderSend(request,result))

      PrintFormat("OrderSend error %d",GetLastError());     

//--- 8=D>@<0F8O >1 >?5@0F88

   PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);

  }

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

//| Close position                                                   |

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

void ClosePosition(ulong ticket)

  {

   MqlTradeRequest request;

   MqlTradeResult  result;



   ZeroMemory(request);

   ZeroMemory(result);



   PositionSelectByTicket(ticket);

   string symbol = PositionGetString(POSITION_SYMBOL);

   double volume = PositionGetDouble(POSITION_VOLUME);



   request.action     =  TRADE_ACTION_DEAL;            // actyion type

   request.position   =  ticket;                       // position ticket

   request.symbol     =  symbol;                       // symbol

   request.volume     =  volume;                       // volume

   request.deviation  =  5;                            // deviation

   request.magic      =  InpOrderMagic;                // 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",ticket,symbol);

     }

   else

     {

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

      Failed = true;

     }

  }

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

//| Close order                                                      |

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

void CloseOrder(ulong ticket)

  {

   MqlTradeRequest request= {};

   MqlTradeResult  result= {};



   ulong  magic=OrderGetInteger(ORDER_MAGIC);    

   

   if(magic==InpOrderMagic)

     {

      ZeroMemory(request);

      ZeroMemory(result);

      

      request.action=TRADE_ACTION_REMOVE;                 

      request.order = ticket;             

      

      if(!OrderSend(request,result))

         PrintFormat("OrderSend error %d",GetLastError());  

      

      

      PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);

     }

  }

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

//| Get total orders by magic                                        |

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

int GetOrdersTotal()

  {

   int total = 0;



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

     {

      OrderGetTicket(i);

      if(OrderGetInteger(ORDER_MAGIC) == InpOrderMagic)

        {

         total++;

        }

     }



   return total;

  }

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

//| Get total positions by magic                                     |

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

int GetPositionsTotal()

  {

   int total = 0;



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

     {

      PositionGetTicket(i);

      if(PositionGetInteger(POSITION_MAGIC) == InpOrderMagic)

        {

         total++;

        }

     }



   return total;

  }

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

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