Position.ClosedTime
Position.ClosedTime
14 Jun 2015, 11:01
Hi
I'm trying to code a simple Martingale-type bot that doubles the position but only if it hits the S/L within a certain time of opening. In a bot that is running this is easy with:
private void OnPositionsClosed(PositionClosedEventArgs args)
var position = args.Position;
TimeSpan positionAge = DateTime.Now - position.EntryTime;
int positionAgeHours = positionAge.Days * 24 + positionAge.Hours;
if (positionAgeHours < 24)
// do all sorts of amazing stuff
However when I try to backtest this doesn't do any amazing stuff at all because it pulls in the current time and gets a horrible negative number into that positionAge variable.
Is there some way I can get the time that the position closed into this so I can backtest. I've tried putting in:
int index = MarketSeries.Close.Count;
and then changing the positionAge calc to:
TimeSpan positionAge = MarketSeries.OpenTime[index] - position.EntryTime;
but MarketSeries.OpenTime[index] gives me 1/01/0001 12:00:00 AM which is pretty useless.
Any ideas?
Thanks
Rhett
Replies
RhettG
16 Jun 2015, 01:30
Thank you that works.
I'm pretty sure I tried that over the weekend and it didn't do any amazing stuff at all. Is there some reason it wouldn't work over the weekend? I guess the more feneral question is "Is there some reason the results from backtesting would be different over the weekend when the markets are offline to during the week?"
Thanks
(p.s. position.closedtime would be way more logical but I'm not going to use up a vote for it. When your devs are completely bored and have nothing to do they can think of it.)
@RhettG
Spotware
15 Jun 2015, 15:30
Dear Trader,
You can use the function Server.Time instead of the DateTime.Now. It returns the server time when the data was historized.
@Spotware