Compare assets in position score

Hello everyone.

I have programmed a rotational system that buys the 5 strongest assets on a watchlist.

I would like to incorporate the condition that if any asset has less ROC than the asset "TLT" directly buy "TLT".

I don't know how to make it compare the assets of a watchlist with a specific asset before starting to do a ranking by force.

Can someone help me please?


// SMARTBETA

SetBacktestMode(backtestRotational);

SetOption( "InitialEquity", 8000 );
NumberHeld=5;
SetOption("WorstRankHeld",NumberHeld);
SetOption("MaxOpenPositions",NumberHeld);
PositionSize=-100/5;


SetTradeDelays( 0, 0, 0, 0 );
BuyPrice=SellPrice=close;

TimeFrameSet(inMonthly);

RO3 = Optimize ("ROC3",3, 1, 12, 1);
ROC3= ROC (C,RO3);


// Corte mayor a 0.
Corte=0;

Corte = Optimize("Corte",0,0,100,25);
Score=IIf(ROC3<Corte,0,Max (ROC3,0));

//PositionScore
PositionScore=Score;

Nombre=FullName();
Filter=ROC3;

First your use of TimeFrame functions is incorrect.
Please read the docs carefully. There are orange boxes on purpose.

Secondly I do not see reason to use TimeFrame functions in your upper code. Simply set to monthly interval in analysis settings.


Possibly like so (as aside: I have commented time frame functions).

/// @link https://forum.amibroker.com/t/compare-assets-in-position-score/23329/2
/// @link https://www.amibroker.com/kb/2014/10/20/foreign-timeframeset/
SetBacktestMode(backtestRotational);

Corte = Optimize("Corte",0,0,100,25);
RO3 = Optimize ("ROC3",3, 1, 12, 1);

SetOption("MaxOpenPositions",5);
PositionSize=-100/5;

SetForeign("TLT");
//TimeFrameSet(inMonthly);
	rcTLT = ROC(C,RO3);
RestorePriceArrays();
//rcTLT = TimeFrameExpand(rcTLT, inMonthly);

//TimeFrameSet(inMonthly);
	ROC3 = ROC(C,RO3);
//TimeFrameRestore();
//ROC3 = TimeFrameExpand(ROC3, inMonthly);

TLTdirect = (ROC3>rcTLT) * 1e7; 

Score = (ROC3>Corte) * (10000+ROC3);
Score = Score + TLTdirect;
// Or
//Score = Max(Score, TLTdirect);

//PositionScore
PositionScore=Score;
2 Likes

Hello fxshrat,

First of all, thank you for your help and advice.

I have modified the code somewhat and I would like to ask you 2 last questions if it is not too much trouble.

I would like the system to buy the 5 strongest assets but if any are weaker than TLT the system will buy in those GLD positions. I don't know how to do it and I would really appreciate it if you could help me.

I would also like to know what is the meaning of this operation that you put me in the code

TLTdirect = (ROC3> rcTLT) * 1e7;

since I don't understand that multiplication by 1e7:

I copy you the modified code.Thank you very much in advance.

RO3 = Optimize ("ROC3",3, 1, 12, 1);

totalPositions = 5;
SetOption("MaxOpenPositions", totalPositions);
SetOption("AllowPositionShrinking", True );
SetPositionSize(100 / totalPositions, spsPercentOfEquity);

Setoption("AllowSameBarExit",False);

//Definición de variables y configuracion sistema rotacional
SetTradeDelays( 0, 0, 0, 0 );
BuyPrice=SellPrice=close;

SetForeign("TLT");
rcTLT = ROC(C,RO3);
RestorePriceArrays();

SetForeign("GLD");
rcGLD = ROC(C,RO3);
RestorePriceArrays();

BEDays = 2;
m = Month();

FirstOfMonth = m !=Ref(m,-1);

Buydate = Ref ( FirstOfMonth,BEDays); 
SellDate = Ref( FirstOfMonth, BEDays + 1); 

//rcTLT = TimeFrameExpand(rcTLT, inMonthly);

ROC3 = ROC(C,RO3);
//TimeFrameRestore();
//ROC3 = TimeFrameExpand(ROC3, inMonthly);

TLTdirect = (ROC3>rcTLT) * 1e7; 

GLDdirect = iIF(TLTdirect, rcGLD, 0);


// Score
Score = 10000+ROC3;
Score = Score + GLDdirect;
// Or
//Score = Max(Score, TLTdirect);


//PositionScore
PositionScore= Score;

Buy = buydate;
Sell = Selldate;

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.