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();
}