Hello all. Sorry for my bad English

I have a problem with backtesting my code. I use Donchian Channel strategy and try to use some Martingale method for position sizing.

Starting with 1 share, if i have a loss than use 2 shares, if loss again than 3 shares and so on untill profit trade or when i get 6 loss in row. Than return to trade 1 share.

The problem i have in backtest. Marking REDs the problem sizing. In both cases we have to use 1 shares instead of 2 in first case and 3 in second case.

This is my code:

```
SetBarsRequired(sbrAll, sbrAll);
ps = 1; //quantity of shares in the beginning
//------------system------------
DonchianUpper = HHV(Ref(H,-1), 15);
DonchianLower = LLV(Ref(L,-1), 15);
DonchianMiddle = (DonchianUpper+DonchianLower)/2;
Buy = Cross(High, DonchianUpper) ;
Short = Cross(DonchianLower, Low) ;
Sell = DonchianMiddle-20 > Low;
Cover = DonchianMiddle+20 < High;
BuyPrice=max(DonchianUpper, O);
SellPrice=min(DonchianMiddle-20,O);
ShortPrice=min(DonchianLower, O);
CoverPrice=max(DonchianMiddle+20,O);
//---------end of system--------------
Buy = ExRem(Buy,Sell);Sell = ExRem(Sell,Buy);Short = ExRem(Short,Cover);Cover = ExRem(Cover,Short);
res = ValueWhen(Sell, SellPrice) - ValueWhen(Buy, BuyPrice); // result for long
res1 = ValueWhen(Short, ShortPrice) - ValueWhen(Cover, CoverPrice); // result for short
losess = 0;
for (i = 1; i < BarCount; i++)
{
if(Sell[i])
{
if ( res[i] < 0 )
losess++;
else
losess = 0;
if(losess == 0)
ps[i] = 1;
else if(losess == 1)
ps[i] = 2;
else if(losess == 2)
ps[i] = 3;
else if(losess == 3)
ps[i] = 4;
else if(losess == 4)
ps[i] = 5;
else if(losess == 5)
ps[i] = 6;
else if(losess >= 6)
ps[i] = 1;
}
else
if(Cover[i])
{
if ( res1[i] < 0 )
losess++;
else
losess = 0;
if(losess == 0)
ps[i] = 1;
else if(losess == 1)
ps[i] = 2;
else if(losess == 2)
ps[i] = 3;
else if(losess == 3)
ps[i] = 4;
else if(losess == 4)
ps[i] = 5;
else if(losess == 5)
ps[i] = 6;
else if(losess >= 6)
ps[i] = 1;
}
else
ps[i] = ps[i-1];
}
SetPositionSize(ps, spsShares);
```

and screen of trades:

i think some problems with my BuyPrice, ShortPrice, CoverPrice, SellPrice. Because if i use simple system with crossing of 2 MA with BuyPrice=ShortPrice=CoverPrice=SellPrice=O, than i havnt any bugs...

Can anyone help me? Thank you in advance