This is supposed to be a "simple" macd crossover with a trailing stop. I can't figure out why it's entering more than one trade at a time. I've used the same code in other strategies a long time ago without any problems but having problems now.
var vMACD4_13 = new MACDStudy(4, 13, 1, "Close", false);
var vMACD28_91 = new MACDStudy(28, 91, 7, "Close", false);
var vLastAlert = -1;
var vtime;
var nStopPrice;
var nLastRawTime = 0;
var BLBarOffset = 0;
function preMain() {
setColorPriceBars(true);
setPriceStudy(true);
setStudyTitle("MACD XOVER-BT2b8");
}
function main() {
vtime = (getHour()*100)+ getMinute();
if (getValue("rawtime", 0) != nLastRawTime) { // Bar Counter function.
nLastRawTime = getValue("rawtime",0);
BLBarOffset += 1;
}
if (Strategy.isInTrade()) { // Draw Current Stop Level on Chart.
drawShapeRelative(4, nStopPrice, Shape.DIAMOND, null, Color.green, Shape.ONTOP, BLBarOffset+"lFSX");
}
if (vMACD4_13.getValue(MACDStudy.MACD) > 0) //Color bars.
setPriceBarColor(Color.RGB(0,0,255));
else if (vMACD4_13.getValue(MACDStudy.MACD) < 0)
setPriceBarColor(Color.RGB(255,0,0));
if ((Strategy.isLong() == true) && (vtime >= 1600)) { //EOD Long Exit
Strategy.doSell("EOD Exit", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
drawShapeRelative(0, high()+1, Shape.SQUARE, "", Color.RGB(0,0,255), Shape.LEFT);
}
if ((Strategy.isShort() == true) && (vtime >= 1600)) { //EOD Short Exit
Strategy.doCover("EOD Exit", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
drawShapeRelative(0, low()-1, Shape.SQUARE, "", Color.RGB(255,0,0), Shape.LEFT);
}
if ((Strategy.isLong() == true) && (low() <= nStopPrice)) { //Exit long if stop hit.
Strategy.doSell("LongStop", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
drawTextRelative(0, high()+.75, "StopXt", Color.RGB(0,0,0), Color.RGB(255,255,255), Text.ONTOP, "Arial", 8, BLBarOffset+"tag");
}
if ((Strategy.isShort() == true) && (high() >= nStopPrice)) { //Exit short if stop hit.
Strategy.doCover("ShortStop", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
drawTextRelative(0, low()-.75, "StopXt", Color.RGB(0,0,0), Color.RGB(255,255,255), Text.ONTOP, "Arial", 8, BLBarOffset+"tag");
}
if (Strategy.isLong() == true) { // Adjust trailing stop....
if (vMACD4_13.getValue(MACDStudy.MACD) > 0 &&
vMACD4_13.getValue(MACDStudy.MACD, -1) < 0)
nStopPrice = (lowest(7, low())-.25);
}
if (Strategy.isShort() == true) { // Adjust trailing stop....
if (vMACD4_13.getValue(MACDStudy.MACD) < 0 &&
vMACD4_13.getValue(MACDStudy.MACD, -1) > 0)
nStopPrice = (highest(7, high())+.25);
}
if (!Strategy.isLong()) { // Go long if not in already...
if (vMACD4_13.getValue(MACDStudy.MACD) > 0 &&
vMACD4_13.getValue(MACDStudy.MACD, -1) < 0 &&
vMACD28_91.getValue(MACDStudy.MACD) > 0 &&
vtime > 930 &&
vtime < 1600 ) {
Strategy.doLong("Trend Long", Strategy.CLOSE, Strategy.THISBAR);
drawShapeRelative(0, low()-1, Shape.UPARROW, "", Color.RGB(0,0,255), Shape.LEFT);
nStopPrice = (lowest(7, low())-.25);
}
}
return;
}
var vMACD4_13 = new MACDStudy(4, 13, 1, "Close", false);
var vMACD28_91 = new MACDStudy(28, 91, 7, "Close", false);
var vLastAlert = -1;
var vtime;
var nStopPrice;
var nLastRawTime = 0;
var BLBarOffset = 0;
function preMain() {
setColorPriceBars(true);
setPriceStudy(true);
setStudyTitle("MACD XOVER-BT2b8");
}
function main() {
vtime = (getHour()*100)+ getMinute();
if (getValue("rawtime", 0) != nLastRawTime) { // Bar Counter function.
nLastRawTime = getValue("rawtime",0);
BLBarOffset += 1;
}
if (Strategy.isInTrade()) { // Draw Current Stop Level on Chart.
drawShapeRelative(4, nStopPrice, Shape.DIAMOND, null, Color.green, Shape.ONTOP, BLBarOffset+"lFSX");
}
if (vMACD4_13.getValue(MACDStudy.MACD) > 0) //Color bars.
setPriceBarColor(Color.RGB(0,0,255));
else if (vMACD4_13.getValue(MACDStudy.MACD) < 0)
setPriceBarColor(Color.RGB(255,0,0));
if ((Strategy.isLong() == true) && (vtime >= 1600)) { //EOD Long Exit
Strategy.doSell("EOD Exit", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
drawShapeRelative(0, high()+1, Shape.SQUARE, "", Color.RGB(0,0,255), Shape.LEFT);
}
if ((Strategy.isShort() == true) && (vtime >= 1600)) { //EOD Short Exit
Strategy.doCover("EOD Exit", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
drawShapeRelative(0, low()-1, Shape.SQUARE, "", Color.RGB(255,0,0), Shape.LEFT);
}
if ((Strategy.isLong() == true) && (low() <= nStopPrice)) { //Exit long if stop hit.
Strategy.doSell("LongStop", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
drawTextRelative(0, high()+.75, "StopXt", Color.RGB(0,0,0), Color.RGB(255,255,255), Text.ONTOP, "Arial", 8, BLBarOffset+"tag");
}
if ((Strategy.isShort() == true) && (high() >= nStopPrice)) { //Exit short if stop hit.
Strategy.doCover("ShortStop", Strategy.CLOSE, Strategy.THISBAR, Strategy.DEFAULT);
drawTextRelative(0, low()-.75, "StopXt", Color.RGB(0,0,0), Color.RGB(255,255,255), Text.ONTOP, "Arial", 8, BLBarOffset+"tag");
}
if (Strategy.isLong() == true) { // Adjust trailing stop....
if (vMACD4_13.getValue(MACDStudy.MACD) > 0 &&
vMACD4_13.getValue(MACDStudy.MACD, -1) < 0)
nStopPrice = (lowest(7, low())-.25);
}
if (Strategy.isShort() == true) { // Adjust trailing stop....
if (vMACD4_13.getValue(MACDStudy.MACD) < 0 &&
vMACD4_13.getValue(MACDStudy.MACD, -1) > 0)
nStopPrice = (highest(7, high())+.25);
}
if (!Strategy.isLong()) { // Go long if not in already...
if (vMACD4_13.getValue(MACDStudy.MACD) > 0 &&
vMACD4_13.getValue(MACDStudy.MACD, -1) < 0 &&
vMACD28_91.getValue(MACDStudy.MACD) > 0 &&
vtime > 930 &&
vtime < 1600 ) {
Strategy.doLong("Trend Long", Strategy.CLOSE, Strategy.THISBAR);
drawShapeRelative(0, low()-1, Shape.UPARROW, "", Color.RGB(0,0,255), Shape.LEFT);
nStopPrice = (lowest(7, low())-.25);
}
}
return;
}
Comment