How to keep from trading on last bar of the day in
Author: gfparker
Creation Date: 12/31/2008 12:23 PM
profile picture

gfparker

#1
I'm trying to code an intraday strategy that doesn't hold any positions overnight. In order to do this I need to keep the buy limit order from being placed on the bar before the last bar of the day, since if the order gets executed, I can't get out of it on the next bar. I'm using WL5 with autotrading so I can't use SellAtClose orders (not supported). I wrote the following code to flag the next to last bar of the day, and the script runs fine until the last bar of the chart where it throws the following exception.

Runtime error: Index was out of range. Must be non-negative and less than the size of the collection.
at WealthLab.Strategies.MyStrategy.Execute()
at WealthLab.Bars.IsLastBarOfDay(Int32 bar)
etc.....

Any ideas on how to fix this problem or why it's happening?

CODE:
Please log in to see this code.
profile picture

Eugene

#2
QUOTE:
why it's happening?

It happens because you're going out of range with Bars.IsLastBarOfDay(bar + 1).
QUOTE:
how to fix this problem

CODE:
Please log in to see this code.

Here's the corrected code:
CODE:
Please log in to see this code.
profile picture

gfparker

#3
Your solution works fine for back testing, but if you try to use it on a 5 minute chart in a real time (streaming mode) you don't appear to be processing the current bar, always the next to last bar so you can't enter buy signals based on the most recent bars information. You'll note the following strategy works fine in back testing, but in realtime, it never generates an alert.
CODE:
Please log in to see this code.
profile picture

gfparker

#4
Any ideas on how to get around this problem?
profile picture

Cone

#5
Here's a re-write with some nifty functions that I'll eventually add to Community.Components.

The slider for "Today Close" allows a convenient switch to adjust for a short day's close at 1pm.

CODE:
Please log in to see this code.
profile picture

gfparker

#6
Thanks Cone, that's an interesting approach. So I would just use the slider to manually enter the market close time for that day. I don't suppose there is any way to automate the detection of early market closes, is there?
profile picture

Cone

#7
QUOTE:
I don't suppose there is any way to automate the detection of early market closes
Actually there is, but why complicate it? You only need to click the slider when you're trading on days that have an early close.

Note that the backtest part of the strategy already determines an early close from IsLastBarOfDay in the "else if" condition in the exit logic.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).