Asking about IF else statement

Hi all

When I followed some code such as below code, the "BarsInTrade" code did not work properly and I attached the screenshot of the analysis page.

I have no idea why it did not work

please let me know how to do.

Thank you so much

_SECTION_BEGIN("Test");
SetChartOptions(0,chartShowArrows|chartShowDates);

//Plot CandleSticks
//Plot(C, "Price", ParamColor( "Color", colorDefault), ParamStyle( "Style", styleCandle, maskPrice ) );
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );  

// Contract specifications
TickSize = 0.25;
PointValue = 50;
RoundLotSize = 0;
PositionSize = MarginDeposit = 1;


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

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

Sell = 0;
BarsInTrade = 0;
for( i = 0; i < BarCount; i++ )
{
	if(Buy[ i ])
	{
	BarsInTrade = 1;
	}
	else Buy[ i ] = 0;
	
   if( BarsInTrade > 0 ) 
     BarsInTrade++;
   
   if( BarsInTrade < 10 AND Sell1[ i ] ) 
    {
       Sell[ i ] = 1;
       BarsInTrade = 0;
   }

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



AddColumn(Buy,"Buy");
AddColumn(BarsInTrade,"BarsInTrade");

BuyPrice = Close;
SellPrice = Close;

Filter = 1;

//Money Management

SetPositionSize( 1, spsValue ) ; // 1 Lakh in each Trade

//TRADE DELAYS
SetTradeDelays(0,0,0,0);


_SECTION_END();

Question

You need to make an array, not scalar

1 Like

Please listen carefully.

  1. Please read here
    It is mandatory rule to apply code tags for inserting code to a post. (@Tomasz has fixed it for you. Please take care next time because it is tiresome (and it again costs time) to correct many posts like that.)

  2. If you copy code then credit to original creator belongs INTO the code
    Please pay some respect to creators instead of just grabbing other peoples work which has COST TIME(!) to create. I do not understand you people at all. Why are so many of you guys (who copy&paste) not bothering?

    The main code is by @Tomasz and is taken from here
    Counting bars in trade resulting in 2 sell exit strategies

    So it costs just little effort to pay some respect

_SECTION_BEGIN( "Test" );
SetChartOptions( 0, chartShowArrows | chartShowDates );

//Plot CandleSticks
//Plot(C, "Price", ParamColor( "Color", colorDefault), ParamStyle( "Style", styleCandle, maskPrice ) );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " + WriteVal( V, 1.0 ) + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

// Contract specifications
TickSize = 0.25;
PointValue = 50;
RoundLotSize = 0;
PositionSize = MarginDeposit = 1;

//Money Management
SetPositionSize( 1, spsValue ) ; // 1 Lakh in each Trade

//TRADE DELAYS
SetTradeDelays( 0, 0, 0, 0 );

/// Original code source
/// https://forum.amibroker.com/t/counting-bars-in-trade-resulting-in-2-sell-exit-strategies/1255/2
/// by Tomasz Janeczko
/// modified at
/// @link https://forum.amibroker.com/t/asking-about-if-else-statement/21832
r = RSI( 2 );
Buy = r < 22; // state form

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

Sell = 0;
BarsInTrade = 0;
for( i = 0; i < BarCount; i++ )
{
    if( Buy[ i ] )
    {
        BarsInTrade = 1;
    }
    else Buy[ i ] = 0;

    if( BarsInTrade > 0 )
        BarsInTrade++;

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

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

AddColumn( Buy, "Buy" );
AddColumn( BarsInTrade, "BarsInTrade" );

BuyPrice = Close;
SellPrice = Close;

Filter = 1;

_SECTION_END();
  1. You assume that BarsIntrade would behave like array outside of loop.
    No, it does not because it is not array variable. It is type "number" (see typeof operator). So you have to store BarsIntrade to array.
_SECTION_BEGIN( "Test" );
SetChartOptions( 0, chartShowArrows | chartShowDates );

//Plot CandleSticks
//Plot(C, "Price", ParamColor( "Color", colorDefault), ParamStyle( "Style", styleCandle, maskPrice ) );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " + WriteVal( V, 1.0 ) + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorDefault ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

// Contract specifications
TickSize = 0.25;
PointValue = 50;
RoundLotSize = 0;
PositionSize = MarginDeposit = 1;

//Money Management
SetPositionSize( 1, spsValue ) ; // 1 Lakh in each Trade

//TRADE DELAYS
SetTradeDelays( 0, 0, 0, 0 );

/// Code source
/// https://forum.amibroker.com/t/counting-bars-in-trade-resulting-in-2-sell-exit-strategies/1255/2
/// by Tomasz Janeczko
r = RSI( 2 );
Buy = r < 22; // state form

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

Sell = 0;
BarsInTrade = arrBarsInTrade = 0;
nbars = 10;
for( i = 0; i < BarCount; i++ ) {
    if( BarsInTrade > 0 )
        BarsInTrade++;
    else if( Buy[ i ] ) 
		BarsInTrade = 1;

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

    if( BarsInTrade >= nbars AND Sell2[ i ] ) {
        Sell[ i ] = 1;
        BarsInTrade = 0;
    }
    
    arrBarsInTrade[ i ] = BarsInTrade;// this is array variable
}

AddColumn( Buy, "Buy" );
AddColumn( Sell, "Sell" );
AddColumn( arrBarsInTrade, "BarsInTrade" );

BuyPrice = Close;
SellPrice = Close;

Filter = 1;

_SECTION_END();
4 Likes

Thank you for ur help

At the next time, I will post the topic with tags or find how to ask the question with proper ways

Even Im still be newbie about the amibroker, Im really appreciated your help

1 Like

Thank you Tomasz

Im still learning coding for amibroker.

Because of ur help, it helps me to improve the coding

Im really appreciated ur help

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.