associating a counter with a specific position
Author: mkalayog
Creation Date: 3/8/2010 12:20 PM
profile picture

mkalayog

#1
I'm looking for some help in associating a counter with a given position rather than having it be a global counter. Specifically, I'd like to have a time-based exit (say, after 5 days) for an intraday multiple-position strategy; if I use "if( Bars.IsLastBarOfDay(bar)==true ) Counter++;", reset the Counter to 0 after a Buy; and use "if(Counter>5) SellAtMarket();", the counter is perpetually zero-ing out every time I enter a new position and therefore keeping most of my positions open. So I think I need to associate the counter with a specific position? Any direction would be greatly appreciated.
profile picture

Eugene

#2
Sounds like it would complicate things. What if you simply:

1) Start counting Days after first opened Position,
2) When a new Position is created, save the Day counter state in its Tag property,
3) Keep comparing the difference between the current Day with the counter from your last entered Position's .Tag property,
4) Reset the counter when all positions have been exited?
profile picture

Cone

#3
5) Why do you need a counter?

CODE:
Please log in to see this code.

That's after 5 bars... if you really meant 5 days, then there's a little more work to do, but you still don't need a counter.
profile picture

Eugene

#4
Re: if you really meant 5 days -> "...(say, after 5 days) for an intraday..."

In an intraday strategy, how do you reliably tell that 5 days have passed without a counter?
profile picture

mkalayog

#5
yes I meant 5 days (not bars)-

If the consensus is that I need a counter and that I can save the counter state in the position's Tag property when the position is created: can you give me a code example of how I'd do that in a multi-position strategy? p is inaccessible d/t its protection level in the buy section so I can't seem to do p.Tag = Counter after a buy, and LastActivePosition.Tag is probably not the right way to go given that this is multiple-positions?
profile picture

Eugene

#6
QUOTE:
p is inaccessible d/t its protection level in the buy section so I can't seem to do p.Tag = Counter after a buy, and LastActivePosition.Tag is probably not the right way to go given that this is multiple-positions?

Here's a shortcut for the last created active position:
CODE:
Please log in to see this code.
profile picture

Cone

#7
This should do it -

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

Eugene

#8
Elegant solution.
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).