Libraries: TradeTransactions

 

TradeTransactions:

Access to OnTradeTransaction data anywhere within an application

Author: fxsaber

 
Library - realisation of this idea

OnTradeTransaction, which writes all transactions to a public place (for example, to a resource).

  • The Expert Advisor creates this indicator via iCustom.
  • At any point of execution, the Expert Advisor can see all the transactions that the indicator has written.
  • Thus, there is a huge flexibility.

    For example, you can send a hundred Async orders and wait for their execution without leaving the deep guts and continue executing your code.


    We are talking about requests, of course. And the magic theme has nothing to do with it.


    Although it would be reasonable to have developers execute transaction records into a public array. Without an indicator.

    Of course, a publicly available array of transactions from the moment of Terminal operation by developers would be a logical way to promote trading asynchrony in MT5. But the demand for such a solution is probably close to zero. For now the library closes this gap, if someone needs it, of course.

     

    The value is not in the library, but in such functionality, when there is an archive of all Terminal transactions.

    It becomes possible to write a trading library when you don't need to think about asynchrony, waiting for results, etc. The trading library itself does everything invisibly.


    For example, there is $1K in an account. 100 asynchronous orders Buy USDCHF 1 lot are sent. It is clear that 99 of them will be rejected by the trade server (not Terminal), only the first one will be executed. But before the cancellation and any response from the trade server, the smart trading bible can assume that 100 BUY positions are present.

     
    • В исходниках библиотеки присутствует (ResourceData.mqh) универсальный класс, позволяющий удобно сохранять/читать данные в/из Ресурсах.
    It doesn't work in MT4 because of a bug.
     

    For example, due to partial execution, many positions may be open instead of one. When you need to modify a lot of positions/orders, it becomes very convenient to use the proposed approach.

    You don't need to remember anything on exit from OnTick, you don't need to write OnTradeTransaction and deal with RequestID there - the same way. You write, as usual, only in OnTick. And in it you always have full control over asynchronous transactions anywhere. You get lightning-fast multiple modifications without tambourines - you don't even need to think about asynchrony and its peculiarities.

     
    Interesting idea, thanks. The performance should improve due to code parallelisation, as I understand. Does it work correctly in the tester?
     
    Alexey Navoykov:
    Interesting idea, thanks. The performance should improve due to code parallelisation, as I understand. Does it work correctly in the tester?

    I haven't tried it in the tester. But if multicurrency OnTick works in Tester on the same idea, it should work here too.


    ZЫ But the idea is not in parallelisation. It was in MT4 that we were distributing by trade flows.

     
    // Example of storing/exchanging data via Resources inside the Terminal
    #include <fxsaber\TradeTransactions\ResourceData.mqh> // https://www.mql5.com/en/code/22166
    
    void OnStart()
    {  
      const RESOURCEDATA<int> ResourceINT("::int"); // Resource for exchanging ints. const - as a proof that nothing is written to the class object
      
      int ArrayINT[] = {1, 2, 3};
      int Num = 5;
      
      ResourceINT = ArrayINT;  // The resource stores an array.
      ResourceINT += Num;      // Added another value to the resource.
      ResourceINT += ArrayINT; // Added an array.
      
      int ArrayINT2[];  
      ResourceINT.Get(ArrayINT2); // Read data from the resource.
      ArrayPrint(ArrayINT2);      // Withdrawn: 1 2 3 5 1 2 3
    
      ResourceINT.Free();                // Deleted data from the resource
      Print(ResourceINT.Get(ArrayINT2)); // Check that there is no data: 0
    
      const RESOURCEDATA<MqlTick> ResourceTicks("::Ticks"); // Resource for exchange of ticks. const - as a proof that nothing is written to the class object
      MqlTick Tick;
      
      if (SymbolInfoTick(_Symbol, Tick))
        for (int i = 0; i < 3; i++)
          ResourceTicks += Tick; // Added to the tiki resource
    
      MqlTick Ticks[];
      ResourceTicks.Get(Ticks); // Read data from the resource.
      ArrayPrint(Ticks);        // Withdrawn.
      
      // This is the full name of the resource to be accessed from another programme
      const string NameOut = StringSubstr(MQLInfoString(MQL_PROGRAM_PATH), StringLen(TerminalInfoString(TERMINAL_PATH)) + 5) + "::Ticks";  
      Print(NameOut); // Print the full name of the resource.
      
      const RESOURCEDATA<MqlTick> Resource(NameOut); // Resource for accessing data (read-only) from another programme
      
      MqlTick TicksOut[];
      Resource.Get(TicksOut); // Read data from the resource.
      ArrayPrint(TicksOut);   // Withdrawn.
      
      Resource.Free();   // There is no way to affect read-only resource data.
      Print(_LastError); // ERR_INVALID_PARAMETER - Erroneous parameter when calling a system function.
    }
     
    fxsaber:
    Example of storing/exchanging data via Resources inside the Terminal

    Is it possible to read data that is in the process of being written? I.e. can I read incomplete information?

     

    So far I can't think of any use for this library.

    It is definitely not needed on forex, and there is no need for it on the stock exchange.

    Have you tested this library on the stock exchange?

    In what case do you think it can be useful?

     
    Andrey Khatimlianskii:

    Is it possible to read data that is in the process of being written? I.e. read not complete information?

    You need to experiment. I think that simultaneous writing/reading of a resource works the same way as with global variables, because writing in both cases is creation: ResourceCreate and GlobalVariableSet. The only difference between a resource and a global is that you cannot even theoretically write anything to a resource at the same time. With global - it is possible.


    In fact, reading a resource is getting a piece of data from memory. And if reading has started, writing a resource should not affect it, because writing is allocating another piece of memory. They can hardly overlap because the OS itself will most likely prevent them from doing so. That's why, from my point of view, there should be no load/save conflicts with resources. But of course it is better to ask the developers this question.