How to Buy the top ranked 10 stocks when condition is satisfied

Hi,

I am trying to figure out how to buy inside the top 20 stocks, selected by coppock, the 10 stocks that satisfied the Parabolic SAR condition. The code:

PosQty = 10;										//Number of max stocks to buy
SetOption("MaxOpenPositions", PosQty);				//Max open positions
PositionSize = 100/PosQty;							//100% of portfloio between the max stocks to buy
SetPositionSize( PositionSize, spsPercentOfEquity );//x% of current portfolio equity

// Main Trading Strategy
WMAPeriods	=Param("WMA periods",10,2,200,1,0);
ROC1Periods =Param("ROC1 periods",14,2,200,1,0);
ROC2Periods =Param("ROC2 periods",11,2,200,1,0);
Coppock=WMA((ROC(C,ROC1Periods)+ROC(C,ROC2Periods)),WMAPeriods);

PositionScore = coppock;
Filter = Status("stocknum") <= 20; // Top 20 stocks

//Buy or Sell Criteria
BuySignal  = (Close >= SAR(0.002,02));
SellSignal = (SAR(0.002,02) > Close);

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

I am rookie,
Thanks for all,
Iaki

Read the manual:
https://www.amibroker.com/guide/h_portfolio.html

Read about "MaxOpenPositions" setting in the manual
https://www.amibroker.com/f?setoption

In short you define PositionScore and "MaxOpenPositions" and
AmiBroker automatically would buy top scored positions according to the PositionScore variable upto MaxOpenPositions.

The "Filter" line is not needed (should be removed)

1 Like