Unwanted Short signals occur inside a function

Hi everyone,
I tried to combine buy and short with cover and sell. here the detail :
xxx
I have difficulty because the short signal appears not as I expected

yyy

Short Signal should appear after Buy Signal occurs. (does not appear if there is NO Buy Signal ) and finished after Sell / Cover occurs. I hope you all can help me.

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

procedure BuyProfitLossStop(pab1, pab2, pab3 ) {
	/// code sources:
	/// @link https://forum.amibroker.com/t/sell-price-buying-price-x/1358/29
	/// @link https://tinyurl.com/y74e8k56
	/// derived from:
	/// @link https://www.amibroker.com/kb/
	/// (commercial use prohibited!)
	global Buy, Sell, Short, Cover, BuyPrice, SellPrice, ShortPrice, CoverPrice;
	local i, tgt_level, stp_level, Short_level;
	local BuyPrice_array1, SellSignal1, BuyPrice_array2, SellSignal2,BuyPrice_array3, Short_Signal1 ;
	tgt_level = stp_level = Short_level=Cover_Level1=Cover_level2=  0; 
	BuyPrice_array1 = BuyPrice_array2 = BuyPrice_array3 =Short_array4=Short_Array5=  Null;

	for ( i = 0; i < BarCount; i++ ) {
		//
		if ( Buy[ i ] && tgt_level == 0 && stp_level == 0) {			
			tgt_level 	= BuyPrice[ i ] * 1.03;
			stp_level 	= BuyPrice[ i ] * 0.97;
			Short_level = BuyPrice[ i ] * 0.99;
		} else Buy[ i ] = 0;
		//
		if ( tgt_level > 0 )
			BuyPrice_array1[ i ] = tgt_level;			
		if ( stp_level > 0 )
			BuyPrice_array2[ i ] = stp_level;
		if ( Short_level > 0 )
			BuyPrice_array3[ i ] = Short_level ;
		
		
		// 
		Short_Signal1		=  L[ i ] < Short_level;
		if ( Short_Signal1 && Short_level > 0 ) {
			Short[ i ] 		= 1;   
			ShortPrice[ i ] = Min(O[ i ], Short_level); 
			Short_level 	= 0;
		}
			 	
		// 
		SellSignal2 		= Cover_signal2 = L[ i ] < stp_level;	
		if ( SellSignal2 && Cover_signal2 && stp_level > 0 ) {
			Sell[ i ] 		= Cover[ i ] = 1;   
			SellPrice[ i ]  = Cover[ i ] = Min(O[ i ], stp_level); 
			tgt_level 		= stp_level = 0;
		} 			
		// 
		SellSignal1 		=Cover_signal1 =  H[ i ] > tgt_level;	
		if ( SellSignal1 && Cover_signal1&&  tgt_level > 0 ) {
			Sell[ i ] 		= Cover[ i ] = 1;   
			SellPrice[ i ] 	= Cover[ i ] = Max(O[ i ],tgt_level);
			tgt_level 		= stp_level = 0;
		}						
		//
		
		
	}
	VarSet(pab1, BuyPrice_array1);
	VarSet(pab2, BuyPrice_array2);
	
	VarSet(pab3, BuyPrice_array3);
	
}

Buy = Cross( MACD(), Signal() );
BuyPrice = Close;
Sell = Short = Cover = 0;

BuyProfitLossStop("tgt_arr", "stp_arr", "Short_x");

Plot( Short_x, "Short_x", colorYellow );
Plot( tgt_arr, "tgt_arr", colorgreen );
Plot( stp_arr, "stp_arr", colorRed );


color1 = IIf( Buy, colorWhite, colorYellow );
y = IIf( Buy, BuyPrice, SellPrice );
PlotShapes( (Buy + Sell) * shapeSmallCircle, color1, layer = 0, y, offset = 0);
PlotShapes(IIf(short, shapeSmallCircle, shapeNone), colorBlue, 0, c, 0)

Thank you for your attention

You owe me a beer...

procedure BuyShortProfitLossStop(pab1, pab2, pab3 ) {
	/// code sources:
	/// @link https://forum.amibroker.com/t/sell-price-buying-price-x/1358/29
	/// @link https://forum.amibroker.com/t/unwanted-short-signals-occur-inside-a-function/19019/2
	/// @link https://tinyurl.com/y74e8k56
	/// derived from:
	/// @link https://www.amibroker.com/kb/
	/// (commercial use prohibited!)
	global Buy, Sell, Short, Cover, BuyPrice, SellPrice, ShortPrice, CoverPrice;
	local i, tgt_level, stp_level, BuyPrice_array1,BuyPrice_array2;
	local ExitSignal1, ExitSignal2, Short_level,Short_Signal1;
	tgt_level = stp_level = Short_level = Short_flag =0;
	BuyPrice_array1 = BuyPrice_array2 = ShortPrice_array1 = Null;
	for ( i = 0; i < BarCount; i++ ) {
		//
		if ( Buy[ i ] && tgt_level == 0 && stp_level == 0) {	
			tgt_level 	= BuyPrice[ i ] * 1.03;
			stp_level 	= BuyPrice[ i ] * 0.97;
			Short_level = BuyPrice[ i ] * 0.99;
		} else Buy[ i ] = 0;
	
		Short_Signal1		= L[ i ] < Short_level;
		if ( Short_Signal1 && Short_level > 0 ) {
			Short[ i ] 		= 1; 
			ShortPrice[ i ] = Min(O[ i ], Short_level);
			Short_level 	= 0; 
			Short_flag        = 1;
		} else Short[ i ] = 0;
		// 
		ExitSignal1 		= L[ i ] < stp_level;
		if ( ExitSignal1 && stp_level > 0 ) {
			Sell[ i ] 		= 1;   
			SellPrice[ i ]  = Min(O[ i ], stp_level); 
			//
			if ( Short_flag > 0 ) {
				Cover[ i ] = 1;
				CoverPrice[ i ] = SellPrice[ i ]; 
				Short_flag  = 0;
			}
			tgt_level 		= stp_level = 0;
			Short_level 	= 0; 			
		} 	
		// 
		ExitSignal2 		= H[ i ] > tgt_level;	
		if ( ExitSignal2 && tgt_level > 0 ) {
			Sell[ i ] 		=  1;   
			SellPrice[ i ] 	= Max(O[ i ],tgt_level);
			//
			if ( Short_flag > 0 ) {
				Cover[ i ] = 1;
				CoverPrice[ i ] = SellPrice[ i ]; 
				Short_flag  = 0;
			}
			tgt_level 		= stp_level = 0;
			Short_level 	= 0; 
		}	
		//
		if ( tgt_level > 0 )
			BuyPrice_array1[ i ] = tgt_level;
		if ( stp_level > 0 )
			BuyPrice_array2[ i ] = stp_level;
		if ( Short_level > 0 )
			ShortPrice_array1[ i ] = Short_level;
		// 
	}

	VarSet(pab1, BuyPrice_array1);
	VarSet(pab2, BuyPrice_array2);
	VarSet(pab3, ShortPrice_array1);
}

Buy = Cross(MACD(), Signal());
BuyPrice = Close;
Sell = Short = Cover = 0;
BuyShortProfitLossStop("tgt_arr", "stp_arr", "Short_x");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

Plot( Short_x, "Short_x", colorYellow );
Plot( tgt_arr, "tgt_arr", colorgreen );
Plot( stp_arr, "stp_arr", colorRed );

color1 = IIf( Buy, colorWhite, colorYellow );
y = IIf( Buy, BuyPrice, SellPrice );
PlotShapes( (Buy + Sell) * shapeSmallCircle, color1, layer = 0, y, offset = 0);
PlotShapes(Short*shapeSmallCircle, colorBlue, 0, ShortPrice, 0);
PlotShapes(Cover*shapeSmallCircle, colorviolet, 0, CoverPrice, 0);

13

4 Likes

You owe me a beer...

:smile: yeah why not, i will remember it.
Thanks a lot

Hi @fxshrat
f42c338d7ea85b057a4ae376b5020e3ea2979530

I'm sorry about the lack of description,I have edited it, .:sweat_smile:

I need your help please, from the new picture I plan to make a re-buy or re-short ( second buy after first buy ) using sigscalein Buy [ i ]= sigScaleIn;

procedure BuyShortProfitLossStop(pab1, pab2, pab3 ) {
/// code sources:
	/// @link https://forum.amibroker.com/t/sell-price-buying-price-x/1358/29
	/// @link https://forum.amibroker.com/t/unwanted-short-signals-occur-inside-a-function/19019/2
	/// @link https://tinyurl.com/y74e8k56
	/// derived from:
	/// @link https://www.amibroker.com/kb/
	/// (commercial use prohibited!)
	
	global Buy, Sell, Short, Cover, 
			BuyPrice, SellPrice, ShortPrice, CoverPrice;
	local i, tgt_level, stp_level, Buy_2_level, 
			BuyPrice_array1, BuyPrice_array2;
	local ExitSignal1, ExitSignal2, 
			Short_level,Short_Signal1, Short_signal1, Buy2;
	tgt_level = stp_level = Short_level = Short_flag = Buy_2_level = Buy2_flag = 0;
	BuyPrice_array1 = BuyPrice_array2 = ShortPrice_array1 = Null;
	
	for ( i = 0; i < BarCount; i++ ) {
		//Buy1 
		if ( Buy[ i ] && tgt_level == 0 && stp_level == 0) {	
			tgt_level 	= BuyPrice[ i ] * 1.03;
			stp_level 	= BuyPrice[ i ] * 0.97;
			Short_level = BuyPrice[ i ] * 0.99;
			Buy_2_level = BuyPrice[ i ] ;
		} else Buy[ i ] = 0;
		
		//Short1
		Short_Signal1		= L[ i ] < Short_level;
		if ( Short_Signal1 && Short_level > 0 ) {
			Short[ i ] 		= 1; 
			ShortPrice[ i ] = Min(O[ i ], Short_level);
			Short_level 	= 0; 
			Short_flag      = 1;
		} else Short[ i ]   = 0;
		
		//Buy2
		Buy2    	=  H [ i ] > Buy_2_level;
		if (Buy2 [ i ]	&& Buy_2_level > 0) {
		Buy [ i ]	= sigScaleIn;
		BuyPrice	= Max(O[ i ],Buy_2_level);
		Buy_2_level = 0;
		Buy2_flag   =  1 ;
		} else Buy[ i ] = 0;
		

		// sell
		ExitSignal1 		= L[ i ] < stp_level;
		if ( ExitSignal1 && stp_level > 0 ) {
			Sell[ i ] 		= 1;   
			SellPrice[ i ]  = Min(O[ i ], stp_level); 
			Buy2_flag       =  0 ;
			
			//Cover
			if ( Short_flag > 0 ) {
				Cover[ i ] = 1;
				CoverPrice[ i ] = SellPrice[ i ]; 
				Short_flag  = 0;
			}
			tgt_level 		= stp_level = 0;
			Short_level 	= 0; 			
		} 	
		// sell
		ExitSignal2 		= H[ i ] > tgt_level;	
		if ( ExitSignal2 && tgt_level > 0 ) {
			Sell[ i ] 		=  1;   
			SellPrice[ i ] 	= Max(O[ i ],tgt_level);
			Buy2_flag       =  0 ;
			
			//Cover
			if ( Short_flag > 0 ) {
				Cover[ i ] = 1;
				CoverPrice[ i ] = SellPrice[ i ]; 
				Short_flag  = 0;
			}
			tgt_level 		= stp_level = 0;
			Short_level 	= 0; 
		}	
		//
		if ( tgt_level > 0 )
			BuyPrice_array1[ i ] = tgt_level;
		if ( stp_level > 0 )
			BuyPrice_array2[ i ] = stp_level;
		if ( Short_level > 0 )
			ShortPrice_array1[ i ] = Short_level;
		// 
	}

	VarSet(pab1, BuyPrice_array1);
	VarSet(pab2, BuyPrice_array2);
	VarSet(pab3, ShortPrice_array1);
}

Buy = Cross(MACD(), Signal());
BuyPrice = Close;
Sell = Short = Cover = 0;

SetPositionSize( 100, spsShares ); 
SetPositionSize( 400, spsShares * ( Buy == sigScaleIn ) ); // scale in 4x share from buy1


BuyShortProfitLossStop("tgt_arr", "stp_arr", "Short_1");

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

//Plot( Short_1, "Short_1", colorYellow );
Plot( tgt_arr, "tgt_arr", colorgreen );
Plot( stp_arr, "stp_arr", colorRed );

color1 = IIf( Buy, colorWhite, colorYellow );
y = IIf( Buy, BuyPrice, SellPrice );
PlotShapes( (Buy + Sell) * shapeSmallCircle, color1, layer = 0, y, offset = 0);
PlotShapes(Short*shapeSmallCircle, colorBlue, 0, ShortPrice, 0);
PlotShapes(Cover*shapeSmallCircle, colorYellow, 0, CoverPrice, 0)

Thanks a lot

I would check for cross up/down instead of just violation.
Also you would have to enter only at first occurring scale signal (otherwise you would get multiple scales). Also you would have to check for not scale-in at initial buy (and possibly add some delay to give room). Also you possibly don't want to get initial short and long/short scales at exit bar.
(And not forgetting resets)

Below is code output taking all that into consideration.

13

1 Like

I'm sorry for My explanation because it's still lacking in detail,
so SigScaleIn_Buy (or the 2nd Buy after the 1st Buy) will only appear AFTER short (first short signal ) occurs.

1

2

as long as buy levels and short levels occur, we will add up until a sell / cover signal occurs and reset all position, so how to prevent sigscalein happening before the short occurs ?

Thank you very much!:smiley:

You can do that via flags and (initial) scale counter.
13

1 Like