Help with coding trading session

tmac

Member
Joined
Jul 5, 2024
Posts
8
Likes
4
I have an indicator that shows my trading setups. I only want to show these during a trading session - perhaps a few hours in the morning. I want the start and end time of the trading session to be configurable. I know I can use a TimeFrame to set this, but I can't figure out how to check is the candlesticks are within this window.

In TradingView, I was able to do something like this:
// InSession() returns 'true' when current bar is inside session time
InSession(sessionTime, sessionTimeZone=syminfo.timezone) =>
not na(time(timeframe.period, sessionTime, sessionTimeZone))
...
// Set input variables for open range time and trading time
tradeHours = input.session("0730-1030", "Trade Session", group = "Sessions")
timeZone = input.string("America/Denver", title = "Time Zone", group = "Sessions")
...
// Set flag for in trading session
tradeSession = InSession(tradeHours, timeZone)

Not sure how to do something equivalent in Motivewave.

Any help/tips greatly appreciated.
 
You can use getStartTime on the bar: https://www.motivewave.com/sdk/javadoc/com/motivewave/platform/sdk/common/Bar.html#getStartTime()

I used the following in one of my studies which may be helpful for what you are trying to do. The session window is defined using a Timeframe descriptor, this function answers if the index passed to it is within the window and gives a boolean response. I have a lunch window ignore part too which is currently commented out:

Java:
public boolean isInTimeFrame(int index, DataContext dataCtx) { //defines session window

var settings = getSettings();
var timeframe = settings.getTimeFrame(TIMEFRAME);

DataSeries dSeries = dataCtx.getDataSeries();

boolean withinTime = true;

long midnight = Util.getMidnightEST(dSeries.getStartTime()); //gets midnight EST near current bar in milliseconds
long start = midnight + timeframe.getStartTime();
long end = midnight + timeframe.getEndTime();
long lunchStart = midnight+12L*60L*60L*1000L;
long lunchEnd = midnight+13L*60L*60L*1000L;

int startIndex = dSeries.findIndex(start);
int endIndex = dSeries.findIndex(end);
int lunchStartIndex = dSeries.findIndex(lunchStart);
int lunchEndIndex = dSeries.findIndex(lunchEnd);

if (settings.getTimeFrame(TIMEFRAME).isEnabled()
&& (index < startIndex || index > endIndex)
)
{
withinTime = false;
}

// if (settings.getBoolean(Inputs.SHIFT) == true
// && index >= lunchStartIndex
// && index <= lunchEndIndex
// )
// {
// withinTime = false;
// }

return withinTime;

}
 
Last edited:
Top