hey guys, i havent been here in a while but ive got a renewed interest in writing some programs to backtest, but im a bit rusty and my code isnt working quite right, everything appears to be working EXCEPT the doLong and doShort events, and i dont know why, could someone take a look and help me out please?
PHP Code:
var ValidTime = false
var Counter = 0
var H = 0
var L = 0
var State = 0
function preMain() {
setPriceStudy(true);
setColorPriceBars(true);
setStudyTitle("4 hours");
setCursorLabelName("4 hours",0)
setPlotType(PLOTTYPE_FLATLINES, 0)
}
function main() {
//ONLY ALLOWS THE EFS TO ENTER A TRADE BETWEEN 22:00 AND 03:59
ValidTime = false
if (getHour() >= 22) ValidTime = true
if (getHour() < 4) ValidTime = true
// DEFINES "H" AS THE GREEN BAR ON THE CHART, AND "L" AS THE RED BAR ON THE CHART
if (getHour() >= 18 && getHour() < 22) {
if (high() > high(-1)) H = high()
else H = high(-1)
if (low() < low(-1)) L = low()
else L = low(-1)
}
//THIS IS JUST DRAWING THE LINES ON THE CHART
if (getHour() == 20) {
drawLineRelative(0, H, 3, H, PS_SOLID, 4, Color.RGB(0,128,0), Counter + 6)
drawLineRelative(0, L, 3, L, PS_SOLID, 4, Color.RGB(128,0,0), Counter + 7)
drawLineRelative(-1, H, -1, L, PS_SOLID, 4, Color.RGB(255,0,255), Counter)
drawLineRelative(0, H, 0, L, PS_SOLID, 4, Color.RGB(255,0,255), Counter + 1)
drawLineRelative(-1, H, 0, H, PS_SOLID, 4, Color.RGB(255,0,255), Counter + 2)
drawLineRelative(-1, L, 0, L, PS_SOLID, 4, Color.RGB(255,0,255), Counter + 3)
drawLineRelative(-1, H, 0, L, PS_SOLID, 4, Color.RGB(255,0,255), Counter + 4)
drawLineRelative(-1, L, 0, H, PS_SOLID, 4, Color.RGB(255,0,255), Counter + 5)
Counter = Counter + 8
}
//ENTERS TRADE AT THE PRICE "H" OR "L" WHEN THE PRICE CROSSES ONE OF THE TWO LINES WITHIN "VALIDTIME"
if(ValidTime == true) {
if (high() >= H ) State = 1
if (high() >= H ) Strategy.doLong("", Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, H)
if (low() <= L ) State = -1
if (low() <= L ) Strategy.doShort("", Strategy.LIMIT, Strategy.THISBAR, Strategy.DEFAULT, L)
}
//AFTER THE VALID TIME IS OVER, THE TRADE CLOSES
if (ValidTime == false) {
if (State == 1) {
State = 0
Strategy.doSell("", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, close())
}
if (State == -1) {
State = 0
Strategy.doCover("", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, close())
}
}
//THIS COLORS THE BAR GREEN IF IT IS CURRENTLY LONG AND RED IF IT IS CURRENTLY SHORT
setPriceBarColor(Color.RGB(128,128,128))
if (Strategy.isLong == true) setPriceBarColor(Color.RGB(0,255,0))
if (Strategy.isShort == true) setPriceBarColor(Color.RGB(255,0,0))
}
Comment