learning mql5

2 hours wasted because of one missing file close..... file functions.......

any number of platforms can read the same common file but only 1 at a time...... each platform must close the file for the next to read......

in the case of trade copiers where many platforms are copying trades of one, each must close the common file.....

otherwise the daisy chain stops......h
//-----

MQL5:
               }  //---- while(!FileIsEnding(file))
 
               FileClose(file);
 
               }  //---- if(file != INVALID_HANDLE)
 
               }  //---- if(Mode == MODE_SLAVE)
//-------

Screenshot 2026-02-15 191458.png
 
MqlTradeRequest ......h

MQL5:
//--- declare and initialize the trade request and result of trade request
 
   MqlTradeRequest request={};   // <--------  {};
   MqlTradeResult  result={};    // <-------- {};
 
been coding a personal trade copier...... just noticed something that i had not considered......

if using a trade copier and you place a trade in the master on a symbol that is not listed as show in other slave platform, the other platforms will automatically add that symbol and so they can place the trade......

if the symbols are listed as show, they will show in the market watch..... if hide, they will not.......

had thought if the symbol was not listed as show in the platform the trade would not be placed...... that was my plan anyway......

plans change....

kinda got in a jam today...... i now realize the master terminal should close the trades first....... well before the others....... such as the master close at 50$...... and the others at 1000$ or something......... this gets that particular ticket out of the common folder file...... otherwise you run the risk of a having a trade close in a slave terminal and then reopen another trade because the common folder file has not yet been rewritten......

some did just that today at the worst possible time........h
//-----

Screenshot 2026-02-20 115635.png
 
Last edited:
been coding a personal trade copier...... just noticed something that i had not considered......
//-----
//-----

after studing on it, for protection.....

now think there should be a limiting condition based on max positions....... so nothing gets written, read or orders copied if the open trades have reached a preset maximum......

also, number of contracts should be set as an input in the copier and not just replicate the master orders contract volume..... this protects a fat finger event......

also think the file in the common folder that is being written to and read, should be deleted in the deint if master chart is closed or expert removed.....

you must specify (file_common).....

and the master terminal should be specified and not chosen by enum mode...... this eliminates the possibility of two masters......
//-----

some gee whiz info.......

this computer has 20 metatrader platforms..... when looking at the path C:\Users\hayse\AppData\Roaming\MetaQuotes\Terminal\xxxxxxxxxxxxx

you can see every platform but it's difficult to know which is which.....

just noticed in each is a text document named origin....... click on that and you can see the platforms shortcut name.......h
//------



MQL5:
void OnDeinit(const int reason)
  {
//---
   FileDelete(FILE_NAME, FILE_COMMON);                                  //--- <must specify file_common  >
   EventKillTimer();
  }
 
 
 
     if(PositionsTotal() < maxpositions)                                                    //----- <overide  to limit positions>
      { 
//--------------------------------------------------------------------------------------
 
 
     if(AccountInfoInteger(ACCOUNT_LOGIN) == master)        //---  <specify in inputs such as    input long master      = ;  >
     {
 
   //-------------------------------------------------------------------------------------
 
 
               }  //--- for(int i = PositionsTotal()-1; i >= 0; i--) 
 
               contracts = 1;                                          //-----  <might prevent fat finger event>
 
              if(arr.SearchFirst(ticket) < 0) 
                {
                if(type == POSITION_TYPE_BUY)
                  {
                   trade.Buy(contracts*multiple,sym,0,stoploss,profittarget,IntegerToString(ticket));
                  }


Screenshot 2026-02-24 212259.png
 
Last edited:
after studing on it, for protection.....
//----

need to add expertremove once the open positions count reaches maximum allowed......

twice now, something went wrong...... had worked perfectly for over 5 days with thousands of trades copied in 14 accounts......

across all accounts combined, took probably 50 losses ... some in the 1000's of dollars...... most were a few hundred.....

still unclear on what is causing the problems......

trying to assure same thing don't happen for the third time......

or at least limit the cost.......h

//-------

MQL5:
    string whichmode = "Account is slave ";
 
    if(AccountInfoInteger(ACCOUNT_LOGIN) == master)   {whichmode = "Account is master ";}
 
    Comment(whichmode);
 
   if(PositionsTotal() >=  maxpositions) {FileDelete(FILE_NAME, FILE_COMMON); ExpertRemove();}
 
   if(PositionsTotal() < maxpositions)
      {  
 
   if(AccountInfoInteger(ACCOUNT_LOGIN) == master)    //---  specify in inputs//----   input long master      = ;
     {
 
Is. It available for free?
//---

hey enrich...... i have never and will never charge anything for coding.....

i'm still learning mq5..... the copier i'm writing has some issues i have not yet worked out.... had a whole pack of losses this week because of it......

it also needs some checks and balances just in case......

it's not something which can be trusted.......

even i don't trust it fully.....h
 
  • 👍
Reactions: EnrichWave
ctradepositionclose......

If individual positions are allowed (ACCOUNT_MARGIN_MODE_RETAIL_HEDGING), multiple positions can be open for one symbol. In this case, PositionClose will close a position with the lowest ticket.

yes, individual positions are allowed....

yes, multiple positions can be open for one symbol.....

PositionClose will close a position with the lowest ticket....... and then what.....

need to test and see just what that is saying......h
 
......h
//----

MQL5:
    if(sparam == "Sell 6J_M")
    {
 
    string text= " all clear";
 
    if(tradexist("6J_M",0)) {text = " warning buys exist";}  // idea is to verify hedge....
 
    int Sell6N = MessageBox("Sell 6J_M"+text,"0",MB_OKCANCEL);   
 
    if(Sell6N != IDOK) return;
 
    if(tradexist("6J_M",0)) {text = " did you read the warning moron warning buys exist";}  // idea is to verify hedge....   
 
    if(tradexist("6J_M",0)) return;  //------- for dumb hayseeds that can't read warning labels
 
 
    Alert(sparam+ " was pressed  "+ AccountInfoString(ACCOUNT_NAME));
 
    trade.Sell(contracts,"6J_M",0,0,0,NULL);
 
    }