# Counting bars in trade resulting in 2 sell exit strategies

Hello,

I am struggling to solve the following puzzle. I would like to count the bars while after I bought (and remove signals in between). Based on the bar count I want to let Ami decide on two sell exits. One is if bars intrade is less than 10(days) than sell = RSI(2)>70 and if bars in trade is => 10 than RSI(2)> 90. I have used a code part which was available here and are trying now to make it work without succes. Can anyone help? I think I am writing the loop in the wrong order and I am also not initializing barsintrade on the right spotâ€¦I guess

``````BuyPrice=Close;
SellPrice=Close;

Sell =(barsintrade <10 AND RSI(2) > 72) OR
(barsintrade =10 OR Bars>10 AND RSI(2) > 92)

for( i = 0; i < BarCount; i++ )
{
// if in-trade, then increase bar counter
else

if( Sell[ i ] ) BarsInTrade = 0; // on sell reset barsintrade flag
}
``````

Swen

Moderator comment: Use `[code]` tags as explained in â€śHow to use this siteâ€ť Yet again I had to correct someoneâ€™s post for sole purpose of not reading instructions by the poster

First you need to understand the difference between state and pulse form. BarsSince can be used if signal is in pulse form

``````Buy = Cross( 22, RSI( 2 ) ); // pulse form of buy signal
``````

Secondly, you are doing other mistakes such as using = (assignment) instead of comparison (==).
These are basic mistakes that you need to get rid of otherwise all formulas will be wrong.
https://www.amibroker.com/guide/a_mistakes.html

Thirdly if you are using loop, you donâ€™t need ExRem or BarsSince or anything like that:

``````r = RSI( 2 );
Buy = r < 22; // state form

Sell1 = r > 72; // state form
Sell2 = r > 92; // state form

Sell = 0;
for( i = 0; i < BarCount; i++ )
{

if( BarsInTrade < 10 AND Sell1[ i ] )
{
Sell[ i ] = 1;
}

if( BarsInTrade >= 10 AND Sell2[ i ] )
{
Sell[ i ] = 1;