I've a trailing stop (long) study based on ATR (which I've attempted to re-write). I'd like to add in the FunctionParameter of choosing between the price low & close (in edit studies), but have not quite got it. I'm new to this, so if anyone could cast an eye over this and point out any glaring errors, I'd be very grateful. It did work until I added the fp3 FunctionParameter then I lost the line on the chart (but without any syntax errors). I've also added a max Stop period of 21 because of the array (21). Is that necessary?
Ps. Alex, I'm working on the posting below. That's my next job & I've started on the tutorials. Cheers
Ps. Alex, I'm working on the posting below. That's my next job & I've started on the tutorials. Cheers
PHP Code:
function preMain() {
setPriceStudy(true);
setStudyTitle("TStopL");
setCursorLabelName("TStopL");
setDefaultBarThickness(1);
setDefaultBarFgColor(Color.red);
setShowTitleParameters(false);
setComputeOnClose();
// Formula Parameters
var fp1 = new FunctionParameter("nATR", FunctionParameter.NUMBER);
fp1.setName("ATR Periods");
fp1.setLowerLimit(1);
fp1.setDefault(13);
var fp2 = new FunctionParameter("nStop", FunctionParameter.NUMBER);
fp2.setName("Stop Periods");
fp2.setLowerLimit(1);
fp2.setUpperLimit(21);
fp2.setDefault(13);
var fp3 = new FunctionParameter("sPrice", FunctionParameter.STRING);
fp3.setName("Price");
fp3.addOption("close");
fp3.addOption("low");
fp3.setDefault("low");
// Study Parameters
var sp1 = new FunctionParameter("nThick", FunctionParameter.NUMBER);
sp1.setName("Thickness");
sp1.setDefault(1);
}
var bEdit = true;
var vATR = null;
var aStopL = new Array(21);
function main(nATR, nStop, sPrice, nThick) {
if (bEdit == true) {
vATR = new ATRStudy(nATR);
setDefaultBarThickness(nThick, 0);
bEdit = false;
}
var nState = getBarState();
if (nState == BARSTATE_NEWBAR) {
aStopL.pop();
aStopL.unshift(0);
}
var ATR = vATR.getValue(ATRStudy.ATR);
if (ATR == null) return;
var p = sPrice();
var vStopL = (p - (2*ATR));
aStopL[0] = vStopL;
var vStopxL = vStopL;
for (var i = 0; i < nStop; i++) {
vStopxL = Math.max(aStopL[i], vStopxL);
}
return vStopxL;
}
Comment