I have a script which tests 4 different strategies on an instrument. It is possible to have several positions open (and to close them on each bar).
In the attached screen shots on 1/6/2005, I am trying to close 2 outstanding open positions and open and close a different position. The graphic representation on my graph shows what the script is trying to do, whereas the Strategy Analyser screen shot shows that the position is not being successfully closed.
I have attached my code below. I'm not sure why this doesn't work.
In the attached screen shots on 1/6/2005, I am trying to close 2 outstanding open positions and open and close a different position. The graphic representation on my graph shows what the script is trying to do, whereas the Strategy Analyser screen shot shows that the position is not being successfully closed.
I have attached my code below. I'm not sure why this doesn't work.
PHP Code:
function main() {
if (openstrats.length > 0) {
do {
var tmp = openstrats.shift();
var tmpPos = arguments[tmp];
var stillopenstrat = new Array();
switch(aStrategies[tmp][0]){
case "a" :
vStrat_result = a(arguments);
break;
case "b" :
vStrat_result = b(arguments);
break;
case "c" :
vStrat_result = c(arguments);
break;
case "d" :
vStrat_result = d(arguments);
break;
default :
debugPrint("no entry ");
}
arguments[tmp] = vStrat_result;
if (arguments[tmp] == tmpPos) {
var t = stillopenstrat.unshift(tmp);
}
}
while (openstrats.length > 0);
openstrats = stillopenstrat;
}
for (x = 0; x < aList.length; x++) {
switch(aStrategies[x][0]){
case "a" :
vStrat_result = a(arguments);
break;
case "b" :
vStrat_result = b(arguments);
break;
case "c" :
vStrat_result = c(arguments);
break;
case "d" :
vStrat_result = d(arguments);
default :
debugPrint("no entry ");
}
arguments[x] = vStrat_result;
}
if (arguments[x] == 1) {
var t = openstrats.push(x);
}
if (vStrat_result[3] == 1) {
x = aList.length;
}
}
return null;
}
function postMain() {
}
function a(arguments) {
var vTraded = 0;
if (there is a position that should be closed) {
Strategy.doCover("a", Strategy.MARKET, Strategy.THISBAR, Strategy.DEFAULT, 0);
vStrat_price=0;
vStrat_stop_price=0;
vPos --;
}
else if (there is a position that could be stopped) {
Strategy.setStop(vStrat_stop_price);
if (high() > vStrat_stop_price) {
vStrat_price=0;
vStrat_stop_price=0;
vPos --;
}
}
else if (a short trigger exists) {
Strategy.doShort("a", Strategy.MARKET, Strategy.THISBAR, Strategy.DEFAULT, 0);
Strategy.setStop(stop_price);
vStrat_price = position price;
vStrat_stop_price = stop price;
vTraded = 1;
}
return new Array(vPos, vStrat_price, vStrat_stop_price, vTraded);
}
function b(arguments) {
var vTraded = 0;
if (there is a position that should be closed) {
Strategy.doCover("b", Strategy.MARKET, Strategy.THISBAR, Strategy.DEFAULT, 0);
vStrat_price=0;
vStrat_stop_price=0;
vPos --;
}
else if (there is a position that could be stopped) {
Strategy.setStop(vStrat_stop_price);
if (high() > vStrat_stop_price) {
vStrat_price=0;
vStrat_stop_price=0;
vPos --;
}
}
else if (a short trigger exists) {
Strategy.doShort("b", Strategy.MARKET, Strategy.THISBAR, Strategy.DEFAULT, 0);
Strategy.setStop(stop_price);
vStrat_price = position price;
vStrat_stop_price = stop price;
}
vTraded = 1;
}
return new Array(vPos, vStrat_price, vStrat_stop_price, vTraded);
}
Comment