Need help to Buy only when Moving Avg. cross and only after stochastic cross

_SECTION_BEGIN("Price");
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() ); 
_SECTION_END();


_SECTION_BEGIN("Stochastic And Moving Avg.");

periods = Param( "Periods", 15, 1, 200, 1 );
Ksmooth = Param( "%K avg", 3, 1, 200, 1 );
//Plot( StochK( periods , Ksmooth), "%K"+_PARAM_VALUES(), ParamColor( "%K color", colorCycle ), ParamStyle("%K style") );
K = StochK( periods , Ksmooth);
Dsmooth = Param( "%D avg", 3, 1, 200, 1 );
D = StochD( periods , Ksmooth, DSmooth );


x = EMA(Close, 10);
y = EMA(Close, 5);
Plot(x,"",colorBlue,styleLine);
Plot(y,"",colorGreen,styleLine);
Buy=Sell=Short=Cover=Null;

 
Buy1 = Cross(y,x);
Buy2= Cross(K,D); 
Short1 = Cross(x,y);
Short2= Cross(D,K);  


Buy= Buy1 AND Buy2;
Short = Short1 AND Short2;

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

PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);                      
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);                      
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
PlotShapes(IIf(Sell AND Short, shapeNone, IIf(Sell,shapeStar,shapeNone)),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover AND Buy, shapeNone, IIf(Cover, shapeStar, shapeNone)),colorRed, 0, L, Offset=-40);


_SECTION_END();

@sacghule welcome to the AmiBroker forum. To get a good answer you need to write a good question. By only using the title as your question you have not explained in any detail what you are trying to accomplish and what you have tried that has failed.

When learning how to write your code it is useful to "debug" your code, for example analyze what your calculations and variables are creating. Below are a few Exploration lines added to your code that may help you find a problem or solution.

/////////////////////////////////
// Explore to debug
////////////////////////////////
Filter = 1; /* all symbols and quotes accepted */
AddColumn( x, "EMA(Close, 10)" );
AddColumn( y, "EMA(Close, 5)" );
AddColumn( K, "K" );
AddColumn( D, "D" );
// adding some color for easy identification
AddColumn( Buy1, "Buy1", 1.0, colorDefault, IIf( Buy1, colorGreen, colorDefault ) );
AddColumn( Buy2, "Buy2", 1.0, colorDefault, IIf( Buy2, colorGreen, colorDefault ) );

Produces an output like this,
image

As you currently have this coded, you will only create a BUY on a bar that has both Buy1 AND Buy2 true. That means on the same bar you are looking for the moving averages to cross and the Stochastic's to cross.

Two useful forum threads for you to read,

And for your specific problem it will be worth your time to review the difference between "STATE" and "IMPULSE" signals. Although that info is all over this forum and the internet you can get started here,

Once again welcome to AmiBroker and Good luck!

3 Likes