How to get each days max bar number in intraday script
Author: dansmo
Creation Date: 7/26/2011 3:04 AM
profile picture

dansmo

#1
Hi,

I need to code an intraday strategy that has multiple positions open. Positions are only held intraday, so at the last Bar all positions need to be closed. Pretty simple so far.
However, due to liquidtiy issues I´d like to start exit positions prior to the close. If I have for example 5 positions open I want to start exit the oldes position 5 bars before the last bar of the day.
First step would be to determine the bar number of the last bar each trading day. I cannot do this static, since trading hours used to change in my test period. Any suggestions on how to implement this?

Regards,
dansmo
profile picture

Eugene

#2
Hi Daniel,

Here's a way to do it (the largest code block, of course):

Intraday / Multi-Time Frame | Keep from holding positions overnight

The logic is pretty straightforward, so to exit N bars before the close you'd need to modify this line accordingly:
CODE:
Please log in to see this code.


The actual market close time can be determined through the MarketInfo.CloseTimeNative property (set either in your data provider or in Market Manager).
profile picture

dansmo

#3
Hi Eugene,

your solution does need a market close time.
The problem is that in a multi-year backtest the close time is not always the same, i.e due to overall change in market hours, holidays etc.

I found this code in WealthSCript QuickRef:

CODE:
Please log in to see this code.


However, this will also only work if the number of bars is constant, which is not the case.
profile picture

Eugene

#4
You can check the SpecialHours first to see if the day is a shortened trading session.

Or as long a multi-year backtest is considered, peek ahead with IsLastBarOfDay, subtract the number of bars (e.g. 5), and come with the right bar to exit.
profile picture

dansmo

#5
QUOTE:
peek ahead with IsLastBarOfDay

That´s probably the way to go. Looping though all bars and create a <Date, LastBar> Dictionary.
profile picture

dansmo

#6
How would you find the oldest active position at a specified bar? As this is a multiple positions strategy all my exits are handled with an positions-loop.
I am thinking in this direction:
CODE:
Please log in to see this code.

How would you approach this?
profile picture

Eugene

#7
Position.EntryBar?
profile picture

dansmo

#8
Haha. Good answer.
Does ActivePositions list all active positions at the current bar?
How can I get a list of those positions open at the current bar? Do I need to create it myself?

That was my try but it did not work:
CODE:
Please log in to see this code.


So the only way would be to have another positions loop to create a list of active positions for the current bar and iterate to find the oldest?
profile picture

Eugene

#9
The oldest Position is:
CODE:
Please log in to see this code.
profile picture

dansmo

#10
So this list is "synchronized" to this bar already?
profile picture

Eugene

#11
Yep, you can use it in a loop. Only a Position with .Active == true will make its way to the ActivePositions.
profile picture

dansmo

#12
Good to know. Somehow I thought that this only works for the last bar. So I tried to program something that is already there....now it works fine.
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).