Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Miscellaneous
Uses files from the file systemIt writes information to fileIt reads information from a file
0 Views
0 Downloads
0 Favorites
Copier_Pos
ÿþ//+------------------------------------------------------------------+

//|                                                   Copier_Pos.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

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

#property version   "1.6"



#include <Trade\Trade.mqh>

#include <Trade\symbolInfo.mqh>



CTrade        atrade;

CSymbolInfo   asymbol;



input string pair = "1";  //Symptom pairing

enum copier_mode

  {

   master,

   _slave,

  };

input copier_mode _mode=0;  // Working _mode

input  double    Lots=1;



int  filehandle;

string   Type="A", tempT="B", Type_Pos="A";

double Volume_M=0,Volume_S=0, tempM=0, Sl=1, Tp=1;

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

//|                                                                  |

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

int OnInit()

  {

   EventSetMillisecondTimer(300);

   RefreshRates();

   ObjectsDeleteAll(0,0,OBJ_LABEL);

      if(ObjectFind(0,"_mode"))

     {

      ObjectDelete(0,"_mode");

      if(!ObjectCreate(0,"_mode",OBJ_LABEL,0,0,0))

        {

         Print(__FUNCTION__,

               ": =5 C40;>AL A>740BL B5:AB>2CN <5B:C! >4 >H81:8 = ",GetLastError());

         return(0);

        }

      ObjectSetInteger(0,"_mode",OBJPROP_XDISTANCE,10);

      ObjectSetInteger(0,"_mode",OBJPROP_YDISTANCE,10);

      ObjectSetInteger(0,"_mode",OBJPROP_COLOR,Blue);

      ObjectSetString(0,"_mode",OBJPROP_TEXT,EnumToString(_mode)+":"+pair);

      ObjectSetInteger(0,"_mode",OBJPROP_FONTSIZE,10);

     }

   return(INIT_SUCCEEDED);

  }

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

//|                                                                  |

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

void OnDeinit(const int reason)

  {

   ObjectsDeleteAll(0,0,OBJ_LABEL);

   FileDelete("OBMEN.csv",FILE_COMMON);

   EventKillTimer();

   return;

  }

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

//|                                                                  |

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

void OnTick()

  {



  }

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

//|                                                                  |

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

void OnTimer()

  {

   if(!RefreshRates())

     {

      return;

     }



   if(EnumToString(_mode)=="master")  // K1>@ @568<0 A>25B=8:0

     {

      if(PositionSelect(_Symbol))     // K18@05< ?>78F8N =0 3@0D8:5  "master"

        {

         Type= EnumToString((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE));                //?@545;O5< B8? ?>78F88, BUY 8;8 SELL

         EnumToString((ENUM_POSITION_PROPERTY_DOUBLE)PositionGetDouble(POSITION_VOLUME,Volume_M)); //?@545;O5< >1JQ<

        }

      else

        {

         Type="A";              //  A;8 >B:@KB>9 ?>78F88 =5B, 70?>;=O5< ?CABK< 7=0G5=85< (<=5 =5 =@02OBAO ?CABK5, ?>MB><C O =0?8A0; "")

         Volume_M=0;

        }



      if(Type!=tempT || Volume_M!=tempM)  // @>25@O5< =0 87<5=5=85 ?>78F89.

        {

         filehandle=FileOpen(pair+"_OBMEN.csv",FILE_WRITE|FILE_SHARE_WRITE|FILE_CSV|FILE_COMMON); //B:@K205< D09; 4;O 70?8A8

         if(filehandle!=INVALID_HANDLE)

           {

            FileSeek(filehandle,0,SEEK_SET);         // 5@5<5I05< D09;>2K9 C:070B5;L 2 =0G0;> D09;0 (=5 7=0N <>65B 1KBL MB> 8 =5 =C6=>)

            FileWrite(filehandle, Type, Volume_M);   // 0?8AL B8?0 ?>78F88 8 >1LQ<0

            FileFlush(filehandle);                   // 5 7=0N 70G5<, => 3>2>@OB 25IL =C6=0O

           }

         FileClose(filehandle);                      // 0:@K205< D09;

         tempT=Type;                                 // 0?8AK205< B5:CI89 B8? ?>78F88 8 >1LQ<

         tempM=Volume_M;

        }

     }



   if(EnumToString(_mode)=="_slave")    // K1>@ @568<0 A>25B=8:0

     {

      filehandle=FileOpen(pair+"_OBMEN.csv",FILE_READ|FILE_SHARE_READ|FILE_CSV|FILE_COMMON);   //B:@K205< D09; 4;O GB5=8O

      if(filehandle!=INVALID_HANDLE)

        {

         FileSeek(filehandle,0,SEEK_SET);                                // 5@5<5I05< D09;>2K9 C:070B5;L 2 =0G0;> D09;0

         Type = FileReadString(filehandle);                              // 'B5=85 B8?0  ?>78F88   A   "master"

         Volume_M = StringToDouble(FileReadString(filehandle));          // 'B5=85 ?>:070=89  >1LQ<0 A "master"

        }

      FileClose(filehandle);                                             // 0:@K205< D09;



      if(PositionSelect(_Symbol))                                        // K18@05< ?>78F8N =0 3@0D8:5  "slave"

        {

         Type_Pos=EnumToString((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)); // ?@545;O5< B8? ?>78F88, BUY 8;8 SELL

         PositionGetDouble(POSITION_VOLUME,Volume_S);                                  // ?@545;O5< >1JQ<

        }

      else

        {

         Type_Pos="A";     // A;8 >B:@KB>9 ?>78F88 =5B, 70?>;=O5< ?CABK< 7=0G5=85< (<=5 =5 =@02OBAO ?CABK5, ?>MB><C O =0?8A0; "")

         Volume_S=0;

        }



      if(Volume_M>Volume_S/Lots)   // A;8 =0 "master" AGQB5 5ABL >B:@KB0O ?>78F8O, 0 =0 "slave" AGQB5 =5B, >B:@K205< ?>78F8N

        {

         if(Type=="POSITION_TYPE_BUY")

           {

            OpenBuy((Volume_M-Volume_S/Lots)*Lots);

            return;

           }

         else

           {

            OpenSell((Volume_M-Volume_S/Lots)*Lots);

            return;

           }

        }



      if(Volume_M<Volume_S/Lots)   // A;8 =0 "slave" AGQB5 5ABL >B:@KB0O ?>78F8O, 0 =0 "master" AGQB5 C65 =5B, 70:@K205< ?>78F8N

        {

         if(Type_Pos=="POSITION_TYPE_BUY")

           {

            OpenSell((Volume_S/Lots-Volume_M)*Lots);

            return;

           }

         else

           {

            OpenBuy((Volume_S/Lots-Volume_M)*Lots);

            return;

           }

        }



      if(Volume_S>0 && Type_Pos!=Type)   // A;8 ?>78F88 >B:@KBK 8 =0 "slave" AGQB5 8 =0 "master", => B8? ?>78F88 @07=K9, 45;05< @525@A

        {

         if(Type=="POSITION_TYPE_BUY")

            OpenBuy((Volume_S/Lots+Volume_M)*Lots);

         else

            OpenSell((Volume_S/Lots+Volume_M)*Lots);

        }

     }

  }

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

//| B:@K205< Buy ?>78F8N                                            |

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

void OpenBuy(double lots)

  {

   double  sl=0,tp=0;

   sl = asymbol.NormalizePrice(sl);

   tp = asymbol.NormalizePrice(tp);



   if(atrade.Buy(lots,asymbol.Name(),asymbol.Ask(), sl, tp,"OBMEN.csv"))

     {

      if(atrade.ResultDeal()==0)

        {

         Print("5 C40;>AL >B:@KBL >@45@ =0 ?>:C?:C1;;",asymbol.Ask());

        }

     }

   else

     {

      Print("5 C40;>AL >B:@KBL >@45@ =0 ?>:C?:C2;;",asymbol.Ask());

     }

  }

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

//| B:@K205< Sell ?>78F8N                                           |

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

void OpenSell(double lots)

  {

   double  sl=0,tp=0;

   sl = asymbol.NormalizePrice(sl);

   tp = asymbol.NormalizePrice(tp);



   if(atrade.Sell(lots,asymbol.Name(),asymbol.Bid(),sl,tp,"OBMEN.csv"))

     {

      if(atrade.ResultDeal()==0)

        {

         Print("5 C40;>AL >B:@KBL >@45@ =0 ?@>406C1;;",asymbol.Bid());

        }

     }

   else

     {

      Print("5 C40;>AL >B:@KBL >@45@ =0 ?@>406C2;;",asymbol.Bid());

     }

  }

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

//| 1=>2;5=85 :>B8@>2>:                                             |

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

bool RefreshRates()

  {

   if(!asymbol.RefreshRates())

     {

      Print("=5 C40;>AL >1=>28BL :>B8@>2:8!");

      return(false);

     }



   if(asymbol.Ask()==0 || asymbol.Bid()==0)

      return(false);

   return(true);

  }



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

//                                                                   |

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