dynamic lotsize not working. help!

 
void OnTick()
{
auto.s5 = 0;
   for(int k = total0 - 1; k >= 0; k--)
     {
      if(position.SelectByIndex(k))
        {
         tk = position.Ticket();
         if(tk <= 0 || tk != position.Ticket())
            break;
         int otyp = position.Type();
         if(otyp != POSITION_TYPE_SELL && otyp != POSITION_TYPE_BUY)
            continue;
         auto.ltv = position.Volume();
         auto.pft = position.Profit();
         auto.sw = position.Swap();
         auto.ot = position.Time();
         auto.net = auto.pft - auto.iMinimalProfit + auto.sw;
         auto.net += ((auto.net > -auto.xcm) ? auto.xcm : -auto.iMinimalProfit);
         if(otyp == POSITION_TYPE_SELL)
           {
            auto.s++;
            auto.s2 += (auto.net > 0) ? 1 : 0;
            if(position.Symbol() == _Symbol)
              {
               auto.s4 += 1;
               if(auto.ltv >= iStartLots)
                 {
                  auto.s5=auto.s5+1;
                  if(position.PriceOpen() > auto.selMxg)
                     auto.selMxg = position.PriceOpen();
                  if(position.PriceOpen() < auto.selMng)
                     auto.selMng = position.PriceOpen();
                 }
              }
           }
}}

         if(auto.openNewTrade(sigBuy, sigSel, auto.b5, auto.s5) >= true)
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool AutoA3m::openNewTrade(bool buySignal, bool selSignal, int totalBuy, int totalSel)//, int buyCount, int sellCount)
  {
   if(selSignal)
     {
      static double h0 = TF1_RSO[0];
      static double h01 = TF2_RSO[0];
      if(trend[0] == sel || trend[1] == sel) //trend[3]==sel ||
         if(h01 < TF2_RSO[1] - 3 || h0 < TF1_RSO[1] - 3)
           {
            new3htfSel = Time[0];
            ccount = 5;
            LastLot[sel] = totalSel<1? iStartLots : totalSel * iStartLots2;
            osd();
            Last[sel] = bid;
            if(!trade.Sell(NormalizeDouble(LastLot[sel], 2)))
              {
               LastError = GetLastError();
               Print("OrderSend error #", LastError);
               if(testing != false && LastError == 10019 && LastError != 0)
                  ExpertRemove();
               new3htfSel = iTime(NULL, PERIOD_M6, 0);
               ResetLastError();
              }
            if(!buySignal)
               return true;
           }
     }
   if(buySignal)
      new3htfBuy = Time[0];

   return false;
  }

My issue is that my lotsize should be dynamic:

            LastLot[sel] = totalSel<1? iStartLots : totalSel * iStartLots2;

but when backtesting -- ALL Trades are istartlots, not istartlots2. Why not? i am confused!

There are always several trades open, all sell trades, so s5 variable is above 1 -- VERY OFTEN, so i do not understand why the line above is not making new lotsize to be "totalsel * istartlots2!

NOTE that i think i included all the code and highlighted the count of open sell trades and code how i count them. I hope someone can tell me what i have done wrong.

============

I am willing to post all files for the ea, however, i have not done so in this post due to there being 5 attached files -- besides the ea file.

Files:
 
Michael Charles Schefe:

My issue is that my lotsize should be dynamic:

but when backtesting -- ALL Trades are istartlots, not istartlots2. Why not? i am confused!

There are always several trades open, all sell trades, so s5 variable is above 1 -- VERY OFTEN, so i do not understand why the line above is not making new lotsize to be "totalsel * istartlots2!

NOTE that i think i included all the code and highlighted the count of open sell trades and code how i count them. I hope someone can tell me what i have done wrong.

============

I am willing to post all files for the ea, however, i have not done so in this post due to there being 5 attached files -- besides the ea file.

You need to reference PositionsTotal().

Forum on trading, automated trading systems and testing trading strategies

PositionsTotal()

Miguel Angel Vico Alba, 2017.03.07 08:51


You have never had a doubt? That lucky...

By the way...issue resolved.

PosCounter=0;

      for(int i=PositionsTotal()-1; i>=0; i--)
        {
         string CounterSymbol=PositionGetSymbol(i);

         if(Symbol()==CounterSymbol && m_position.Magic()==MagicNumber)
           {
            PosCounter+=1;



 
Ryan L Johnson #:

You need to reference PositionsTotal().


i changed the line below 

   for(int k = total0 - 1; k >= 0; k--)

to

   for(int k = PositionsTotal() - 1; k >= 0; k--)

lotsize of all trades in backtest are still same size of iStartLots.

Thanks for the advice tho. Keep em comin.

 
Michael Charles Schefe #:

i changed the line below 

to

lotsize of all trades in backtest are still same size of iStartLots.

Thanks for the advice tho. Keep em comin.

The following code is a bit of a blunt instrument from my complex GBPJPY bot that uses GlobalVariables to communicate with external files but it works. Just replace the GV's with your own standard variables─the math is the same (minus denotes a lot reduction).

   if(PositionsTotal() > 0)
    {
     tradeSize = NormalizeDouble(GlobalVariableGet("Last Traded Lot Size") - GlobalVariableGet("Last Traded Lot Size") * PercentLotDecrease * 0.01, 2);
    }
   else
    {
     tradeSize = NormalizeDouble(LotSize, 2);
    }

And to get the previous lot size, something like:

Forum on trading, automated trading systems and testing trading strategies

How to use PositionGetDouble(POSITION_VOLUME) correctly?

nicholish en, 2020.04.15 22:46

This is good but you shouldn't compare the strings, and instead you should get the actual position's symbol using PositionInfoString. Better yet, use CPositionInfo...

#include <trade/trade.mqh>

double netPosition(string symbol) {
   CPositionInfo pos;
   double net_volume = 0.0;
   for (int i=PositionsTotal()-1; i>=0; --i)
      if (pos.SelectByIndex(i) && pos.Symbol() == symbol)
         net_volume += pos.PositionType() == POSITION_TYPE_BUY ? pos.Volume() : -pos.Volume();
   return net_volume;
}

You should get your sell lot volume following your:

if(otyp == POSITION_TYPE_SELL)
 

Apparently, the condition if(auto.ltv >= iStartLots) only occurs on the first trade. Try updating s5 before this condition:

auto.s5 = auto.s5 + 1;

if(auto.ltv >= iStartLots)
{
    if(position.PriceOpen() > auto.selMxg)
        auto.selMxg = position.PriceOpen();
    if(position.PriceOpen() < auto.selMng)
        auto.selMng = position.PriceOpen();
}
 
Ryan L Johnson #:

The following code is a bit of a blunt instrument from my complex GBPJPY bot that uses GlobalVariables to communicate with external files but it works. Just replace the GV's with your own standard variables─the math is the same (minus denotes a lot reduction).

And to get the previous lot size, something like:

You should get your sell lot volume following your:

please check the use of my struct. thanks. ie the file attachment on the op message.

my dual lotsize(s) work when i put my variables and bools on global enviro, but breaks when i add them to the struct.

 
Michael Charles Schefe #:

please check the use of my struct. thanks. ie the file attachment on the op message.

my dual lotsize(s) work when i put my variables and bools on global enviro, but breaks when i add them to the struct.

I've never used structs in that way. At a mere guess, maybe you can't equate auto.s5 to itself plus one. I would try storing auto.s5 to a "placeholder" variable, and then equating auto.s5 to that "placeholder" variable plus one.
 
Ryan L Johnson #:
I've never used structs in that way. At a mere guess, maybe you can't equate auto.s5 to itself plus one. I would try storing auto.s5 to a "placeholder" variable, and then equating auto.s5 to that "placeholder" variable plus one.

didnt work either, but thansks for your suggestion. I have now removed the class.

I was clearly abusing the use of class. its been so many years since i did real coding that i have clearly forgotten something!

thanks again.

 
Michael Charles Schefe #:

didnt work either, but thansks for your suggestion. I have now removed the class.

I was clearly abusing the use of class. its been so many years since i did real coding that i have clearly forgotten something!

thanks again.

You're most welcome.

On a related note, former Moderator Fernando posted elsewhere that he always prefers to use standard code elements instead of OOP code elements wherever possible.

 
Ryan L Johnson #:

You're most welcome.

On a related note, former Moderator Fernando posted elsewhere that he always prefers to use standard code elements instead of OOP code elements wherever possible.

Yeah i did think of that. maybe the libraries arent updated after mt5 was, but coding with em is still working otherwise...AND is faster to code with the shortened commands, so that is why i chose to simply remove the full class and go back to global enviro use, as opposed to coding via the "long winded method".
 
Michael Charles Schefe #:
Yeah i did think of that. maybe the libraries arent updated after mt5 was, but coding with em is still working otherwise...AND is faster to code with the shortened commands, so that is why i chose to simply remove the full class and go back to global enviro use, as opposed to coding via the "long winded method".
TBH, I actually prefer to use standard code elements too. I haven't tested the comparative efficiencies of standard versus OOP, but I've always assumed that standard code is faster than accessing external include/library files. Maybe someone else can chime in here and give us the hard facts.