Correlation Pair V1

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

//|                                          Correlation Pair V1.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"



input group    "First symbol"

input string   InpSymbolOneName     = "";     // First used symbol

input string   InpSymbolOneType     = "";     // Long or short

input string   InpSymbolOneVolume   = "";     // Volume for first symbol

input group    "Second symbol"

input string   InpSymbolTwoName     = "";     // Second used symbol

input string   InpSymbolTwoType     = "";     // Long or short

input string   InpSymbolTwoVolume   = "";     // Volume for second symbol



long           OrderMagic  = 0000072;

string         Symbols[]   = {"", ""};      // used symbols

MqlRates       Results[]   = {};

bool           Opened      = false;                                     // worked on this day

double         TotalProfit = 0;                                         // total profit on positions



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

//| Open position on start                                           |

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

void OnInit()

  {

   Symbols[0] = InpSymbolOneName;

   Symbols[1] = InpSymbolTwoName;



   if(InpSymbolOneName != "" && InpSymbolOneType != "" && StringToDouble(InpSymbolOneVolume) != 0 && InpSymbolTwoName != "" && InpSymbolTwoType == "" && StringToDouble(InpSymbolTwoVolume) != 0)

     {

      OpenPos();

     }

   else

     {

      Print("Error! Invalid input data");

     }

  }



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

//|  Close and reopen position if we have profit                     |

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

void OnTick()

  {

   datetime Date = TimeLocal();

   MqlDateTime MDT;

   TimeToStruct(Date,MDT);

   string Min;

   string Sec;



   if(MDT.min < 10)

     {

      Min = "0"+IntegerToString(MDT.min);

     }

   else

     {

      Min = IntegerToString(MDT.min);

     }



   if(MDT.sec < 10)

     {

      Sec = "0"+IntegerToString(MDT.sec);

     }

   else

     {

      Sec = IntegerToString(MDT.sec);

     }



   string Time   = IntegerToString(MDT.hour)+":"+Min+":"+Sec;



   if(Time == "23:50:00"  || Time == "14:50:00" || Time == "18:50:00" || Time == "21:50:00")

     {

      if(Opened == false)

        {

         TotalProfit = 0;

         for(int n=0; n<ArraySize(Symbols); n++)

           {

            CheckCurrency(n);

           }

         Print("Time: ",Time);

         Print("profit: ",TotalProfit);

         if(TotalProfit >= 5)

           {



            ClosePos();

            OpenPos();

            Opened = true;

           }

        }

     }

   else

     {

      Opened = false;

     }

  }



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

//|  Open positions                                                  |

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

void OpenPos()

  {

   uint r1 = OrderOpen(OrderMagic,InpSymbolOneName, StringToDouble(InpSymbolOneVolume), InpSymbolOneType);

   uint r2 = OrderOpen(OrderMagic,InpSymbolTwoName, StringToDouble(InpSymbolTwoVolume), InpSymbolTwoType);

  }

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

//|  Close all positions                                             |

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

void ClosePos()

  {

   for(int n=0; n<ArraySize(Symbols); n++)

     {

      if(PositionSelect(Symbols[n]))

        {

         uint res1 = OrderClose(OrderMagic,n, PositionGetDouble(POSITION_VOLUME), PositionGetInteger(POSITION_TICKET));

         Print("Closed");

        }

     }

  }

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

//|  Set active profit                                               |

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

void CheckCurrency(int x)

  {

   if(PositionSelect(Symbols[x]))

     {

      TotalProfit += PositionGetDouble(POSITION_PROFIT);

     }

  }



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

//|  Open order function                                             |

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

uint OrderOpen(long const magic_number,string symbol, double lot, string ordType)

  {

   MqlTradeRequest request= {0};

   MqlTradeResult  result= {0};



   double price = SymbolInfoDouble(symbol,SYMBOL_ASK);



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

     {

      request.type = ORDER_TYPE_BUY;

     }

   else

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

        {

         price        = SymbolInfoDouble(symbol,SYMBOL_BID);

         request.type = ORDER_TYPE_SELL;

        }





   request.action  =  TRADE_ACTION_DEAL;

   request.magic   =  magic_number;

   request.symbol  =  symbol;

   request.volume  =  lot;

   request.sl      =  0;

   request.tp      =  0;

   request.price   =  price;



   if(OrderSend(request,result))

     {

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

      if(result.retcode==10016)

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

      return result.retcode;

     }

   else

     {

      return 0;

     }

  }



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

//| Close order function                                             |

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

uint OrderClose(long const magic_number,int i, double lots, ulong  position_ticket)

  {

   MqlTradeRequest request;

   MqlTradeResult  result;



   ZeroMemory(request);

   ZeroMemory(result);



   request.action     =  TRADE_ACTION_DEAL;

   request.position   =  position_ticket;

   request.symbol     =  Symbols[i];

   request.volume     =  lots;

   request.deviation  =  5;

   request.magic      =  magic_number;



//--- Set position type

   if(PositionGetInteger(POSITION_TYPE)  ==  ORDER_TYPE_BUY)

     {

      request.price  =  SymbolInfoDouble(Symbols[i],SYMBOL_BID);

      request.type   =  ORDER_TYPE_SELL;

     }

   else

     {

      request.price  =  SymbolInfoDouble(Symbols[i],SYMBOL_ASK);

      request.type   =  ORDER_TYPE_BUY;

     }



   if(OrderSend(request,result))

     {

      PrintFormat("Close #%I64d %s",position_ticket,Symbols[i]);

     }

   else

     {

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

     }



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