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.
Size:
Color:
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?
Size:
Color:
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.
Size:
Color:
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?
Size:
Color:
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?
Size:
Color:
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.
Size:
Color:
This should do it -
CODE:
Please log in to see this code.
Size:
Color:
Elegant solution.
Size:
Color: