How to get the balance of a set of positions?

 
Hi!
I have a question about a hotly debated subject, but I still don't have a solution to my problem, so I would be grateful for suggestions on how to get a solution for my case.

I have a strategy in an EA that uses Martingale, and therefore doubles the lot when the previous operation is at loss... the first operation is created from a signal, if it reaches the take, it ends there. If the second operation gets a take, the cycle ends and the EA waits for a new signal and starts again.

So far so good... but I would like to monitor every tick, during a position that has been "doubled", the balance since the initial position of this cycle, the objective would be that from "X" "doubles" the EA terminates the operation when the overall balance reached 0 x 0...

I could go on storing the balances monitoring the result of the positions via OnTradeTransaction, but I would like to obtain such data by querying the order history, since if the EA is removed and reloaded or has its inputs changed during its execution, the data in variables would be reset, generating data inconsistency.

To find a day's balance, for example, just scroll through the history and analyze the DEAL_ENTRY_OUT type positions, see your profit and everything is fine. But here I only need to check a set of positions within the day.

I structured the position comments to help me in this control, but as in the position histories, the DEAL_ENTRY_IN type records do not have profit data, so far I haven't had a solution for my case. The comment is structured with an element that identifies the step (each position within a cycle, starting with a sign and doubling "N" times), and another that identifies the cycle (the set of steps).

I am grateful for suggestions that might lead me to develop a way to get the overall balance of the cycle.

obs.: I hope I have expressed myself so that you understand the issue, and sorry for my "English".

Thanks!
Files:
 
Show your attempt if you need coding help.
 
Alain Verleyen #:
Show your attempt if you need coding help.

When explaining my doubt I ended up thinking of an alternative... go through the history of positions until I find the record of step 1, using the POSITION_COMMENT in DEAL_ENTRY_IN. And while this does not happen, add the DEAL_PROFIT of the DEAL_ENTRY_OUT records. It worked, I got the result I needed...

I'll share the query code here and anyone who has a better idea or consideration to contribute...

Thanks!


Examples of values registered in the comment field, to help understand the code:

Cycle 32154, with 3 steps:

S-1-32154

S-2-32154

S-3-32154


//+------------------------------------------------------------------+
//| Function                                                         |
//+------------------------------------------------------------------+
double getTotalProfitOfCycle()
  {
   HistorySelect(0,TimeCurrent());

   string symbol;
   long   entry;
   long   magic;
   ulong  ticket         = 0;
   double totalProfit    = 0;
   bool   gotCycleProfit = false;

   
for(int i = HistoryDealsTotal()-1; i>=0; i--) 
     {
      if((ticket=HistoryDealGetTicket(i))>0)
        {
         symbol   = HistoryDealGetString(ticket,DEAL_SYMBOL);
         entry    = HistoryDealGetInteger(ticket,DEAL_ENTRY);
         magic    = HistoryDealGetInteger(ticket,DEAL_MAGIC);

         if(symbol==Symbol() && entry==DEAL_ENTRY_IN && magic == mgNumber)
           {
            // check if it is still running data from the current cycle...
            string strArrStep[];
            StringSplit(HistoryDealGetString(ticket,DEAL_COMMENT) ,StringGetCharacter("-",0),strArrStep);
            if(ArraySize(strArrStep) >= 3)
              {
               // if we reach step 1 of the current cycle, it should stop adding profit
               // cycleKey -> global variable that contains the identifier of the cycle that is in POSITION_COMMENT
               if((int)strArrStep[1] == 1 && (int)strArrStep[2] == cycleKey)
                 {
                  gotCycleProfit = true;
                 }
              }
           }

         if(!gotCycleProfit && symbol==Symbol() && entry==DEAL_ENTRY_OUT && magic == mgNumber)
           {
            totalProfit += HistoryDealGetDouble(ticket,DEAL_PROFIT);
           }
        }
     }

   return totalProfit;
  }
 
Hello friend, using the martingale is never a good choice, most traders prefer to actually lower the risk when things are looking difficult, just an advice, think about the big picture and living to trade another day, martingale puts more psychological preasure and is the nearest thing to revenge trading, if its a lost, take the lost and move on, just an advice.
 
Luis Garcia #:
Hello friend, using the martingale is never a good choice, most traders prefer to actually lower the risk when things are looking difficult, just an advice, think about the big picture and living to trade another day, martingale puts more psychological preasure and is the nearest thing to revenge trading, if its a lost, take the lost and move on, just an advice.

I understand and thank you for your concern, but the question is just technical. Martingale in this case is part of the strategy and is not used indiscriminately, but strategically, for sure the use of this technique without criteria is the end for any trader.


Thanks!

Reason: