Hi,
I have performed a quick search on here and the KnowledgeBase for a guide/pointers on the "correct" way to structure/indent code. Unfortunately with no luck. Does anyone have any suggestions?
Also in the following code;
1) Will nTradeEntryPrice obtain the purchase price of my trade?
2) And I want to use the atr value, (var atrLevel) at the point when the purchase is made, as the basis of my stop loss point. Is this calculated in the correct section of the code?
Thanks for any help in advance.
-----------------------------------------------------------------------------------
I have performed a quick search on here and the KnowledgeBase for a guide/pointers on the "correct" way to structure/indent code. Unfortunately with no luck. Does anyone have any suggestions?
Also in the following code;
1) Will nTradeEntryPrice obtain the purchase price of my trade?
2) And I want to use the atr value, (var atrLevel) at the point when the purchase is made, as the basis of my stop loss point. Is this calculated in the correct section of the code?
Thanks for any help in advance.
-----------------------------------------------------------------------------------
PHP Code:
var vDonch = null;
var vDonch2 = null;
var nSignal;
var nTradeEntryPrice;
var nStopLevel;
var atrLevel;
var inTrade;
var nNewTrade;
function preMain() {
setPriceStudy(true);
setStudyTitle("Donchian");
setCursorLabelName("20 Day High", 0);
setCursorLabelName("20 Day Low", 1);
setCursorLabelName("10 day High Exit", 2);
setCursorLabelName("10 day Low Exit", 3);
setColorPriceBars(true);
setDefaultBarFgColor (Color. blue, 0);
setDefaultBarFgColor (Color. red, 1);
setDefaultBarFgColor (Color. green, 2);
setDefaultBarFgColor (Color. green, 3);
setDefaultBarThickness(1, 0);
setDefaultBarThickness(1, 1);
setDefaultBarThickness(1, 2);
setDefaultBarThickness(1, 3);
setDefaultPriceBarColor(Color.black);
}
function main() {
if (vDonch == null) vDonch = new DonchianStudy (20,0);
if (vDonch2 == null) vDonch2 = new DonchianStudy (10,0);
// identify entry price
if (nNewTrade == 1) {
nTradeEntryPrice = open();
nNewTrade = 0;
atrLevel = atr(20);
inTrade = 1;
if (Strategy.isLong() == true) setPriceBarColor(Color.green);
if (Strategy.isShort() == true) setPriceBarColor(Color.red);
}
// stop loss/profit exit
if (inTrade == 1 && nSignal > 0) {
if (low() <= (nTradeEntryPrice - nStopLevel)) {
Strategy.doSell("Stop Sell", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nStopLevel);
inTrade = 0;
}
}
else if (low() <= vDonch2.getValue(DonchianStudy.LOWER, -1)) {
var longExit = vDonch2.getValue(DonchianStudy.LOWER, -1);
if (open() < longExit) longExit = open(); {
Strategy.doSell("10 day low",Strategy.LIMIT,Strategy.THISBAR,Strategy.ALL,longExit);
inTrade = 0;
}
}
if (inTrade == 1 && nSignal < 0) {
if (high() >= (nTradeEntryPrice + nStopLevel)) {
Strategy.doCover("Cover",Strategy.LIMIT, Strategy,THISBAR,Strategy.ALL,nStopLevel);
inTrade = 0;
}
}
else if (high() >= vDonch2.getValue(DonchianStudy.UPPER, -1)) {
var shortExit = vDonch2.getValue(DonchianStudy.UPPER, -1);
if (open() > shortExit) shortExit = open(); {
Strategy.doSell("10 day low",Strategy.LIMIT,Strategy.THISBAR,Strategy.ALL,shortExit);
inTrade = 0;
}
}
//identify entry signal
if (inTrade == 0) {
if (high() >= vDonch.getValue(DonchianStudy.UPPER, -1)) {
nSignal = 1;
nNewTrade = 1;
}
}
if (inTrade == 0) {
if (low() <= vDonch.getValue(DonchianStudy.LOWER, -1)) {
nSignal = -1;
nNewTrade = 1;
}
}
//execute trade
if ((nNewTrade == 1) && (nSignal > 0)) {
Strategy.doLong("Long", Strategy.MARKET, Strategy.NEXTBAR);
nStopLevel = nTradeEntryPrice - (atrLevel * 2);
}
if ((nNewTrade == 1) && (nSignal < 0)) {
Strategy.doShort("Short", Strategy.MARKET, Strategy.NEXTBAR);
nStopLevel = nTradeEntryPrice + (atrLevel * 2);
}
return new Array
(vDonch.getValue (DonchianStudy.UPPER),
vDonch.getValue (DonchianStudy.LOWER),
vDonch2.getValue (DonchianStudy.UPPER),
vDonch2.getValue (DonchianStudy.LOWER));
}
Comment