Sell using a param value

Trying to code an exit that uses values that can be set up in Param. I have tried it several ways and can't get it to work. Can I get a REAL afl coder to show me where I went wrong? I have Connors RSI defined correctly. The buy side is working correctly.
Did not include that code here, for brevity.

CRSI = ConnorsRSI(C,3,2,100);
// Initialize signal arrays
Buy = Sell = Short = Cover = 0;

nLongShort = Param("Trade Direction: 1=Long 2=Short", 1, 1, 2, 1);
nExitID = Param("Exit: CRSI: 1=50 2=60 3=70 4=80 5=90",3,1,5,1);

switch(nExitID)
{
	case 1:
		Sell = CRSI >= 50;
		SellName = "CRSI > 50";
		break;
	case 2:
		Sell = CRSI >= 60;
		SellName = "CRSI > 60";
		break;
	case 3:
		Sell = CRSI >= 70;
		SellName = "CRSI > 70";
		break;
	case 4:
		Sell = CRSI >= 80;
		SellName = "CRSI > 80";
		break;
	case 5:
		Sell = CRSI >= 90;
		SellName = "CRSI > 90";
		break;
}

Buy = nLongShort==1 AND rsiVol_vx - Ref(rsiVol_vx, -1) >= nSpikeThreshold AND !Sell;

Sell = Close = nExitID;


It does not work because you are re-declaring Sell variable at the bottom.
Also your last Sell line is not proper one (assigning value to Close array).

Anyway, you do not need switch statement.
It is much simpler (besides initialization is not required in that code) :

CRSI = RSI();// ConnorsRSI(C,3,2,100);

nLongShort = Param("Trade Direction: 1=Long 2=Short", 1, 1, 2, 1);
nExitID = Param("Exit: CRSI: 1=50 2=60 3=70 4=80 5=90",3,1,5,1)-1;
mat = MxFromString("[50;60;70;80;90]");
threshold = mat[nExitID][0];

Buy = nLongShort==1 AND (rsiVol_vx - Ref(rsiVol_vx, -1)) >= nSpikeThreshold;

Sell = CRSI >= threshold;
SellName = "CRSI >= " + threshold;

Alternative for Sell line

Sell = Cross(CRSI, threshold);
1 Like

That's it !! Works like a charm.
Thanks for your help.

Oop's. There is a snag. I can't change the Param values. I get Error 10. Array subscript out of range. You must not access array elements outside 0. It refers to : threshold = mat[nExitID][0]; When I change the Param value for nExitID the code breaks.

The code I posted with that params will not break.
You have 5 elements and param values going from 1 to 5 subtracted by 1 to get matrix indexes (0 to 4);
So it will not break (it will break if you change matrix size or change param values to values smaller 1 or greater 5). You changed either matrix to different size or params values without knowing what you do.

Anyway...

CRSI = RSI();// ConnorsRSI(C,3,2,100);

mat = MxFromString("[50;60;70;80;90]");

nLongShort = Param("Trade Direction: 1=Long 2=Short", 1, 1, 2, 1);
nExitID = Max(Param("Exit: CRSI: 1=50 2=60 3=70 4=80 5=90",3,1,5,1)-1, 0);
nExitID = Min(nExitID, MxGetSize(mat,0)-1);
threshold = mat[nExitID][0];

Buy = nLongShort==1 AND (rsiVol_vx - Ref(rsiVol_vx, -1)) >= nSpikeThreshold;

Sell = CRSI >= threshold;
SellName = "CRSI >= " + threshold;

Here is better/shorter code without matrix and switch and stuff... just using ParamList.

CRSI = RSI();// ConnorsRSI(C,3,2,100);

nLongShort = Param("Trade Direction: 1=Long 2=Short", 1, 1, 2, 1);
threshold = StrToNum(ParamList("CRSI-Threshold","50,60,70,80,90", 2));

Buy = nLongShort==1 AND (rsiVol_vx - Ref(rsiVol_vx, -1)) >= nSpikeThreshold;

Sell = CRSI >= threshold;
SellName = "CRSI >= " + threshold;

Click Apply button not Save.

That shorter code is perfect. Very clean and working just as I wanted. Sooo Cool.

I sincerely thank you for taking the time.

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