Hi,
I'm just trying to work out a standard way to see if I should trade, related to the time of day. i.e. I don't want to open any trades too soon after the open, at lunchtime, or too close to the end of the day, and I want to close off at the end of day. This is the way I am doing it at the moment, but I was just wondering if anyone else has a simpler or more efficient method.
Thanks
mmillar
In this example I can trade between 9.00am and 12.00noon, then 2.00pm and 4.00pm. Any open positions are closed at 4.30pm.
...
// Setup the trading hours
var TRADE_TIME_FLAG = "OFF";
var TRADE_TIME_HOUR_AM_ON = 9;
var TRADE_TIMS_MINS_AM_ON = 0;
var TRADE_TIME_HOUR_AM_OFF = 12;
var TRADE_TIME_MINS_AM_OFF = 0;
var TRADE_TIME_HOUR_PM_ON = 14;
var TRADE_TIME_MINS_PM_ON = 0;
var TRADE_TIME_HOUR_PM_OFF = 16;
var TRADE_TIME_MINS_PM_OFF = 0;
var TRADE_TIME_HOUR_CLOSE = 16;
var TRADE_TIME_MINS_CLOSE = 30;
...
function main() {
...
var vTime = getValue("Time");
if (vTime == null) return;
var sHour = vTime.getHours();
if (sHour == null) return;
var sMins = vTime.getMinutes();
if (sMins == null) return;
...
// Determine if we are in allowed trading hours. Set the trading flag.
if (((sHour >= TRADE_TIME_HOUR_AM_ON) && (sHour <= TRADE_TIME_HOUR_AM_OFF))
|| ((sHour >=TRADE_TIME_HOUR_PM_ON) && (sHour <= TRADE_TIME_HOUR_PM_OFF))) {
TRADE_TIME_FLAG = "ON";
} else
TRADE_TIME_FLAG = "OFF";
// If trade flag is ON and other conditions are met you can trade.
...
// End of Day - make sure all positions are closed
if (sHour == TRADE_TIME_HOUR_CLOSE && sMins == TRADE_TIME_MINS_CLOSE)
if (Strategy.isShort())
Strategy.doCover("EOD Close", Strategy.CLOSE, Strategy.THISBAR);
else if (Strategy.isLong())
Strategy.doSell("EOD Close", Strategy.CLOSE, Strategy.THISBAR);
...
}
I'm just trying to work out a standard way to see if I should trade, related to the time of day. i.e. I don't want to open any trades too soon after the open, at lunchtime, or too close to the end of the day, and I want to close off at the end of day. This is the way I am doing it at the moment, but I was just wondering if anyone else has a simpler or more efficient method.
Thanks
mmillar
In this example I can trade between 9.00am and 12.00noon, then 2.00pm and 4.00pm. Any open positions are closed at 4.30pm.
...
// Setup the trading hours
var TRADE_TIME_FLAG = "OFF";
var TRADE_TIME_HOUR_AM_ON = 9;
var TRADE_TIMS_MINS_AM_ON = 0;
var TRADE_TIME_HOUR_AM_OFF = 12;
var TRADE_TIME_MINS_AM_OFF = 0;
var TRADE_TIME_HOUR_PM_ON = 14;
var TRADE_TIME_MINS_PM_ON = 0;
var TRADE_TIME_HOUR_PM_OFF = 16;
var TRADE_TIME_MINS_PM_OFF = 0;
var TRADE_TIME_HOUR_CLOSE = 16;
var TRADE_TIME_MINS_CLOSE = 30;
...
function main() {
...
var vTime = getValue("Time");
if (vTime == null) return;
var sHour = vTime.getHours();
if (sHour == null) return;
var sMins = vTime.getMinutes();
if (sMins == null) return;
...
// Determine if we are in allowed trading hours. Set the trading flag.
if (((sHour >= TRADE_TIME_HOUR_AM_ON) && (sHour <= TRADE_TIME_HOUR_AM_OFF))
|| ((sHour >=TRADE_TIME_HOUR_PM_ON) && (sHour <= TRADE_TIME_HOUR_PM_OFF))) {
TRADE_TIME_FLAG = "ON";
} else
TRADE_TIME_FLAG = "OFF";
// If trade flag is ON and other conditions are met you can trade.
...
// End of Day - make sure all positions are closed
if (sHour == TRADE_TIME_HOUR_CLOSE && sMins == TRADE_TIME_MINS_CLOSE)
if (Strategy.isShort())
Strategy.doCover("EOD Close", Strategy.CLOSE, Strategy.THISBAR);
else if (Strategy.isLong())
Strategy.doSell("EOD Close", Strategy.CLOSE, Strategy.THISBAR);
...
}
Comment