how to make different buy condtions for pos==0 and pos>0

#1
Boys, why do you think this code doesn't work?

HTML:
Pos = 0; 
EntryPrice = 0; 

Buy = C < WMA( C, 300 ) ; 
Sell = C >= WMA( C, 300 );// 

for ( i = 0; i < BarCount; i++ ) 
{ 
    if ( Buy[i] AND Pos==0) 
    { 
        EntryPrice = BuyPrice[i]; 
        pos == 1; // position became greater than 0. stop buy at this point. but the system continues to buy. what is wrong?
    } 
    else if (pos == 1) 
    { 
        Buy[i] = 0; 
    } 

}
 

trash

Well-Known Member
#2
Code:
BuyPrice = ...;

Buy = C < WMA( C, 300 ) ; 
Sell = C >= WMA( C, 300 );// 

Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
 

KelvinHand

Well-Known Member
#3
Boys, why do you think this code doesn't work?

HTML:
Pos = 0; 
EntryPrice = 0; 

Buy = C < WMA( C, 300 ) ; 
Sell = C >= WMA( C, 300 );// 

for ( i = 0; i < BarCount; i++ ) 
{ 
    if ( Buy[i] AND Pos==0) 
    { 
        EntryPrice = BuyPrice[i]; 
        pos == 1; // position became greater than 0. stop buy at this point. but the system continues to buy. what is wrong?
    } 
    else if (pos == 1) 
    { 
        Buy[i] = 0; 
    } 

}
When Loop i =0; buy and Pos=0, then EntryPrice = BuyPrice[0],
you set Pos = 1;

When Loop 2 onward, Pos is alway 1 then the condition test on (Pos==0) is always false regardless of Buy is true or false

The next condition test (Pos==1) will always set the Buy to false;

However your EntryPrice will always remember the buyprice at loop=0;
when loop is at long time ago.
 

trash

Well-Known Member
#4
Simply use ExRem if you want to remove excessive signals on chart.
In Analysis - Backtest ExRem is not required.

Here is AFL version of ExRem (not needed, use built-in one)

Code:
/*
ExRem is *very* simple function it just matches Buy with corresponding Sell
and removes extra signals between. It is NOT necessary for backtesting because
backtester takes care of it by itself, see http://www.amibroker.com/guide/h_portfolio.html

Here is how ExRem is internally coded (note that this is AFL equivalent,
internal function is written in C++ )

Best regards,
Tomasz Janeczko
amibroker.com
*/

function ExRemAFL( BuyTable, SellTable )
{
	up = False;
	result = False;

	for( i = 0; i < BarCount; i++ )
	{
		result[ i ] = False;

		if( ! up && BuyTable[ i ] )
		{
			up = True;
			result [ i ] = True; // generate True (1) impulse on buy
		}

		if( up && SellTable [ i ] )
		{
			up = False; // reset the flag on first matching sell
		}
	}

	return result;
}
And here is the difference between ExRem and Flip

B -> Buy
S -> Sell
1 -> True
0 -> False

 
Last edited: