How to limit no of signal of the complete chart

The following code is to limit the no of trades per day. But I want to limit the no of trades for the complete chart.

For example I want first 2 signals of the chart from the first bar. In analysis window I don't want to get other signals after 2nd signal.

`// trades limit
N = 2;

Plot( Close, "Close", colorDefault, styleBar );

// identify new day
dn = DateNum();
newDay = dn != Ref( dn,-1);

// buy and sell signals
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

// visualize signals with yellow arrows
PlotShapes(BuyshapeUpArrow, colorYellow, 0, Low);
PlotShapes(Sell
shapeDownArrow, colorYellow, 0, High);

// modify Buy array and allow only first N signals
Buy = Buy AND Sum( Buy, BarsSince( newDay) +1 ) <= N;

// visualize modified signals with green triangles
PlotShapes(Buy*shapeUpTriangle, colorGreen, 0, Low, -24);Code button `

@prabudaisy,

See How to ask a good question.

Also consider using the words "Hi", "Please" and or "Thank-you"!

Instead of just:

I am very sorry sir,

Just I was searching that page only. I used this forum only for learning only, I am extremly sorry for that.

So please, can anybody help me in modifying the following code to get no of limited signals for the whole chart. The code I pasted here is copied from knowledge base, for limiting the no of signal for a new day, but I want to modify the code for the complete chart.

// trades limit
N = 2;

Plot( Close, "Close", colorDefault, styleBar );

// identify new day
dn = DateNum();
newDay = dn != Ref( dn,-1);

// buy and sell signals
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

// visualize signals with yellow arrows
PlotShapes(BuyshapeUpArrow, colorYellow, 0, Low);
PlotShapes(SellshapeDownArrow, colorYellow, 0, High);

// modify Buy array and allow only first N signals
Buy = Buy AND Sum( Buy, BarsSince( newDay) +1 ) <= N;

// visualize modified signals with green triangles
PlotShapes(Buy*shapeUpTriangle, colorGreen, 0, Low, -24);Code button `

Thankyou for very much.

@prabudaisy you can try this modification, but I don't understand your plan. You plan to only take two trades ever, even over many years?

Check this out as I'm typing on an iPad so can not confirm.

// trades limit
N = 2;

// buy and sell signals
Buy = Cross( MACD(), Signal() );
Sell = Cross( Signal(), MACD() );

// modify Buy array and allow only first N signals
Buy = Buy AND Cum( Buy) <= N;
Sell = ExRem(Sell, Buy);

////////////////////////
// Chart
////////////////////////
Plot( Close, "Close", colorDefault, styleBar );
PlotShapes(Buy*shapeUpTriangle, colorGreen, 0, Low, -24);
PlotShapes(Sell*shapeDownTriangle, colorRed, 0, High, 24);
1 Like

Thankyou very much sir,

The code is working. I want to use this code for Index option contracts which has life period of 1 month to 3 months only. My condition is very simple that I want to trade if price crossed on first crossover signal only.

Thanks once again for your kind help.

2 Likes