Whats wrong with my SumSince

Hello all,
I am a complete novice at this, so please be patient with me...in the code below I am trying to limit the number of scale in events...but my back tests show more entries than I have coded for using these lines:
x = SumSince(sellsig1,buysig1);
xsigs = x <= 4; //legs
Buy = IIf(buysig1 AND xsigs, sigScaleIn,0);
Any ideas what I am doing wrong please? Thanks in advance...

//position sizing
maxpos = 30; // maximum number of open positions//
SetOption( "MaxOpenPositions", maxpos);
SetOption("InitialEquity", 100000 ); // set initial equity = 100K
SetPositionSize( 100 / maxpos, spsPercentOfEquity );
Compounding = ParamToggle("Position Sizing", "Equal Fixed Dollar|Equal Fixed Fraction", 1);

//buysignal
buysig1=
C > 42//Optimize("price",2,2,45,5) 
//AND C <1.25
AND C>MA(C,10)
AND C*V>25000 
AND MA(v,50)>250000;

sellsig1 = ApplyStop( stoptypetrailing, stopModePercent, 15, exitatstop=0, volatile = False, ReEntryDelay = 0, ValidFrom = 0, ValidTo = -1, ActivationFloor = 0 );
x = SumSince(sellsig1,buysig1);
xsigs = x <= 4; //legs
Buy = IIf(buysig1 AND xsigs, sigScaleIn,0);

//arrays
pricerange = C > 42; //AND C < 1.25;
mavten = MA(C,10);
volum = C*V;
mavvol = MA(v,50);
closeprice = C;

Filter = buysig1;
AddColumn(mavten,"mavtenday");
AddColumn(volum,"turnover");
AddColumn(mavvol,"Mavvolume");
AddColumn(c,"close");
AddColumn (sigScaleIn, "scalein",0);

Sell = 0;  
Short=0;
Cover=0;

@stevecar144, please check the documentation for ApplyStop() and you'll see that this function returns Nothing, so you cannot use this returned value as a valid condition for the following SumSince() function.

To better understand what happens in our formulas, I always recommend to use the explorations exhaustively. For example, if you add the values ​​of sellsig1 and x to your exploration you could easily see what is cause of the problem.

1 Like

See my note in other thread:

2 Likes

Thanks for the replies.
I have changed the sell condition to something that returns a value....c<ma(c,10). The exit is working fine, but the scaling in is not. It still scales more than the limit of 2 in the code.
I guess I am still not understanding the basics here...??

//position sizing
maxpos = 30; // maximum number of open positions//
SetOption( "MaxOpenPositions", maxpos);
SetOption("InitialEquity", 100000 ); // set initial equity = 100K
SetPositionSize( 100 / maxpos, spsPercentOfEquity );
//Compounding = ParamToggle("Position Sizing", "Equal Fixed Dollar|Equal Fixed Fraction", 1);

//buysignal
buysig1=
C > .25 
AND C < 1.25
AND C>MA(C,10)
AND C*V>25000 
AND MA(v,50)>250000
AND MA(C,10)>Ref(MA(C,10),-3);

sellsig1 = c<MA(c,10);  //Sum(C<O, 6) == 6; // True/false array consec_bearish 

x = SumSince(sellsig1,buysig1);
xsigs = x <= 2; //legs
Buy = IIf(buysig1 , sigScaleIn,0);

//arrays
pricerange = C>.25 AND C < 1.25;
mavten = MA(C,10);
volum = C*V;
mavvol = MA(v,50);
closeprice = C;

Filter = buysig1;
AddColumn(mavten,"mavtenday");
AddColumn(volum,"turnover");
AddColumn(mavvol,"Mavvolume");
AddColumn(c,"close");
AddColumn (sigScaleIn, "scalein",0);

Sell=sellsig1;  
Short=0;
Cover=0;

sigscalein variable is not a counter and it is not an array in addition. It is a constant (-> number) of value of 99998 (always) and serves as flag for trade signals. If you add sigsalein to Addcolumn (as you did) then it always outputs that constant value it holds. (BTW and as aside, you should not use 99998 as value may or may not change in further AB versions. So always use sigscalein.)

As for pyramiding you may read here: https://www.amibroker.com/guide/h_pyramid.html
Another note: Exploration is not a backtester environment.

Thanks fxshrat, please note below the inclusion of "AND xsigs" in the Buy condition. Should this control the counting of positions opened??

sellsig1 = c<MA(c,10);  //Sum(C<O, 6) == 6; // True/false array consec_bearish 

x = SumSince(sellsig1,buysig1);
xsigs = x <= 2; //legs
Buy = IIf(buysig1 AND xsigs, sigScaleIn,0);