Generic Class Library - bugs, description, questions, usage and suggestions - page 16

 
fxsaber:

Interesting. And here's a question. I didn't like the current implementation, so I tweaked it. It's crooked, of course. How do I get the original bible?

Here - from 1702.

Files:
Generic.zip  44 kb
 
Artyom Trishkin:

Here - from 1702

Thank you! I'll take the question to the developers. Since I'm a bit of a straightener...

 

Example 2: Trading multiple Expert Advisors on a net account

Net positioning is a headache for those who trade more than one Expert Advisor on the same symbol at the same time. To deal with situations where both EAs are in a hedge, but must understand that the absence of a net position does not actually mean they are not in the market, generates a complex code. One solution is to calculate the contribution of each expert to the total position. To do this, we need to analyze the entire history and calculate the number of contracts attributable to each unique indicator. If the number is 0.0, the expert is out of the market, if it is negative, the expert holds a short position, if it is positive - a long position. In fact, it's very easy to do, if we use CHashMap, for that it's enough to decompose all trades by magic orders summing up their volume. I have sketched a very simplified code (prototype) below:

//+------------------------------------------------------------------+
//|                                                 NettoByMagic.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#include <Generic\HashMapGen.mqh>

CHashMap<ulong, double> PositionsByMagic;
int prev_deals = 0;
//+------------------------------------------------------------------+
//| Добавляет новый объем и его меджик                               |
//+------------------------------------------------------------------+
void AddVolume(ulong magic, double volume)
{
   double cur_volume = 0.0;
   if(PositionsByMagic.TryGetValue(magic, cur_volume))
      PositionsByMagic.TrySetValue(magic, cur_volume+volume);
   else
      PositionsByMagic.Add(magic, volume);
}
//+------------------------------------------------------------------+
//| Добавляет новые сделки в словарь                                 |
//+------------------------------------------------------------------+
void ParseDeals()
{
   HistorySelect(0, TimeCurrent());
   for(int i = prev_deals; i < HistoryDealsTotal(); i++)
   {
      ulong ticket = HistoryDealGetTicket(i);
      if(HistoryDealGetString(ticket, DEAL_SYMBOL)!= Symbol())
         continue;
      ENUM_DEAL_TYPE deal_type = (ENUM_DEAL_TYPE)HistoryDealGetInteger(ticket, DEAL_TYPE);
      double volume = 0.0;
      if(deal_type == DEAL_TYPE_BUY)
         volume = HistoryDealGetDouble(ticket, DEAL_VOLUME);
      else if(deal_type == DEAL_TYPE_SELL)
         volume = HistoryDealGetDouble(ticket, DEAL_VOLUME)*(-1);
      else
         continue;
      ulong magic = HistoryDealGetInteger(ticket, DEAL_MAGIC);
      AddVolume(magic, volume);
   }
   prev_deals = HistoryDealsTotal();
}
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnTick()
{
   ParseDeals();
   for(int i = 0, k = 0; i < PositionsByMagic.Count(); i++)
   {
      ulong magic = PositionsByMagic.GetKeyAt(i);
      double valume = PositionsByMagic.GetValueAt(i);
      if(k == 10)
         break;
   }
}
//+------------------------------------------------------------------+

Attention! To simplify, the code only calculates the net volume for the current symbol. Also CHashMap in current implementation doesn't contain enumeration iterators, that's why I've improvised such iterators. The modified CHashMap is shown in the attachment.

Files:
HashMapGen.mqh  25 kb
 
fxsaber:

Is the speed of the tester important for trading? If yes, then HashMap also affects trading, because it increases the speed of development and execution of the TS.

Tester, optimization and trading are different things.

One and the same idea is traded and optimized, but the implementation can differ dramatically, this is also undeniable.But a specific example of a task where it is necessary to use these efficient algorithms for data storage and extraction, at least for the optimizer, since for trading, someone can give ?

 
Alexey Oreshkin:

If I wanted to give a concrete example of a task, where it is necessary to use these efficient algorithms for data storage and retrieval, at least for the optimizer, if not for trading, can someone give me ?

Forum on trading, automated trading systems and strategy testing

Generic classes library - bugs, description, issues, usage features and suggestions

fxsaber, 2017.12.08 22:46

For a more realistic tester case (2000 trades and 1,000,000 single history accesses) the result looks like this

2017.12.05 00:00:00   Time[Print(SumProfit(Deals,GetDealProfitFull))] = 122969
2017.12.05 00:00:00   Time[SetHashMap()] = 816
2017.12.05 00:00:00   4829800340.792288
2017.12.05 00:00:00   Time[Print(SumProfit(Deals,GetDealProfitHashClear))] = 23852
2017.12.05 00:00:00   Time[HistorySelect(0,INT_MAX)] = 1
2017.12.05 00:00:00   4829800340.792288
2017.12.05 00:00:00   Time[Print(SumProfit(Deals,GetDealProfitClear))] = 114427

Almost 100ms savings per pass! If, say, we do Optimization for 10,000 full passes, the Hash variant will end up 15 minutes faster.

 
Vasiliy Sokolov:

Example 2: Multiple Expert Advisors Trading on a Net Account

Forgot
prev_deals = HistoryDealsTotal();


A good example! Indeed, it is convenient.

 
fxsaber:
Forgot

Good example! Really handy.

Corrected.

 

There are bikes that are easier to build yourself than to figure out the nuances of using someone else's and depending on them.

Generic is not that kind of bike. You can't write it quickly and correctly. It's just a matter of completing it.

 
fxsaber:

Great theoretical example! In practice, has anyone ever operated thousands of trades?

p.s. I'm not trying to prove that it's crap and nobody needs it. I'm trying to understand the value for real trading. I'm not a theorist at all, but a pure practitioner.

 
Alexey Oreshkin:

In practice, has anyone ever operated thousands of transactions?

On Forts every first.