Local variable to retain its value.

 

Hello.

There is a simple code. The idea is next, trading volume will increase after there was a losing trade to recover losses.

What I am trying to do is that if for example trading volume was 0.01 and there was a loss trade, it becomes 0.02 and resets once there is a winning trade.

The problem is that instead of 0.01, 0.02, 0.04, 0.08, etc. after a series of loss trades, it just stays at 0.02. its value changes only once.

Can someone explain how to retain the changed value?

double RR = (double)I_stopLoss / (double)I_takeProfit;

   datetime from = iTime(Symbol(),PERIOD_W1,1);
   datetime to=TimeCurrent();
//--- request history
   HistorySelect(from,to);

   int totalHistoryDeal = HistoryDealsTotal();

   if(I_recoveryMode == true && totalHistoryDeal > 0)
     {
      ulong historyDealTicket = HistoryDealGetTicket(totalHistoryDeal-1);
      string historyDealSymbol = HistoryDealGetString(historyDealTicket,DEAL_SYMBOL);
      ulong historyDealMagic = HistoryDealGetInteger(historyDealTicket,DEAL_MAGIC);

      if(historyDealMagic == I_magicNumber && historyDealSymbol == Symbol())
        {
         double lastDealProfit = HistoryDealGetDouble(historyDealTicket,DEAL_PROFIT);

         if(lastDealProfit < 0)
           {
            volume = volume * RR;
           }
         if(lastDealProfit > 0)
           {
            volume = volume;
           }
        }
     }
 

From where this code is run ? (from which event handle). What is the initial value of volume and where it is in the code ?

Beside that, you should not rely that the last deal by index is the last deal out.

 
It runs in OnTick function. the initial value of volume is 0.01 it is global variable declared on top
 
Buba Akhrakhadze #:
It runs in OnTick function. the initial value of volume is 0.01 it is global variable declared on top
And where is it reset to 0.01 when needed ?
 
Buba Akhrakhadze #:

I had read your code.

Please think about my question and your answer (your code).

 
Alain Verleyen #:

I had read your code.

Please think about my question and your answer (your code).

Can you explain in more detail what info you need?

 
Buba Akhrakhadze #:

Can you explain in more detail what info you need?

I don't need any info, I'm trying to make you think about your code.

What are you thinking this code is doing ?

            volume = volume;
 
Alain Verleyen #:

I don't need any info, I'm trying to make you think about your code.

What are you thinking this code is doing ?

It changes nothing, You are right it is useless. what I am trying to do is that I declare static variable volume,  this function runs in the OnTick function, and when the condition is true, it continues increasing volume to infinite. I need to modify it so that the action happens only once before for example ticket number is the same, but after the ticket number changes, it will increase only once and not unlimited time.
 
I got the answer. Can you delete this post? Thanks!
 
Buba Akhrakhadze #:
I got the answer. Can you delete this post? Thanks!

There is no reason to delete it and if you have an answer you should provide it.

A forum is not a place to get personal help and then delete it, it's a place to share.

 
Alain Verleyen #:

There is no reason to delete it and if you have an answer you should provide it.

A forum is not a place to get personal help and then delete it, it's a place to share.

I modified only this:

         if(lastDealProfit < 0)
           {
            if(lastdealticket != historyDealTicket) // in where the lastdealticket is a static variable
              {
               volume = volume * RR;
               lastdealticket = historyDealTicket;
              }
Reason: