Hi,
I am trying to create the stop loss part of the code for my strategy. To just get the principle working I have opted for a simple 150 points below the entry price. However during backtesting it seems to only work for the first trade and then the next entry does not exit till the close out trade action at the end of the backtest.
As the entry price is resetting for each trade i cant see why the nStopLevel isnt doing the same. Do i have to declare it as 0/null in the stop loss part of the code.
Anyone have any ideas?
I am trying to create the stop loss part of the code for my strategy. To just get the principle working I have opted for a simple 150 points below the entry price. However during backtesting it seems to only work for the first trade and then the next entry does not exit till the close out trade action at the end of the backtest.
As the entry price is resetting for each trade i cant see why the nStopLevel isnt doing the same. Do i have to declare it as 0/null in the stop loss part of the code.
Anyone have any ideas?
PHP Code:
var nSignal;
var nTradeEntryPrice = null;
var nStopLevel = null;
var inTrade = null;
var nNewTrade;
function preMain() {
setPriceStudy(true);
setStudyTitle("Donchian");
setCursorLabelName("20 Day High", 0);
setCursorLabelName("10 Day High", 1);
setCursorLabelName("20 day Low", 2);
setCursorLabelName("10 day Low", 3);
setColorPriceBars(true);
setDefaultBarFgColor (Color.blue, 0);
setDefaultBarFgColor (Color.red, 1);
setDefaultBarFgColor (Color.blue, 2);
setDefaultBarFgColor (Color.red, 3);
setDefaultBarThickness(1, 0);
setDefaultBarThickness(1, 1);
setDefaultBarThickness(1, 2);
setDefaultBarThickness(1, 3);
setDefaultPriceBarColor(Color.black);
}
function main() {
var nDonch_Upper = upperDonchian (20);
var nDonch_Upper2 = upperDonchian (10);
var nDonch_Lower = lowerDonchian (20);
var nDonch_Lower2 = lowerDonchian (10);
if (inTrade == null) inTrade = 0;
// identify entry price
if (nNewTrade == 1) {
nTradeEntryPrice = open();
nNewTrade = 0;
inTrade = 1;
nStopLevel = nTradeEntryPrice - 150;
}
// stop loss/profit exit
//LONG
if ((inTrade == 1) && (nSignal > 0)) {
if (low() <= nStopLevel) {
Strategy.doSell("Stop Sell", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nStopLevel);
inTrade = 0;
}
}
//identify entry signal
if (inTrade == 0) {
if (high() >= nDonch_Upper.getValue(-1)) {
nSignal = 1;
nNewTrade = 1;
}
}
//execute trade
if ((nNewTrade == 1) && (nSignal > 0)) {
Strategy.doLong("Long", Strategy.MARKET, Strategy.NEXTBAR);
setPriceBarColor(Color.green);
}
return new Array (nDonch_Upper, nDonch_Lower, nDonch_Upper2, nDonch_Lower2);
}
Comment