Measuring Time Between Opening and Closing a Trade

Created at 12 Dec 2017, 10:52
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
AR

aronbara664

Joined 29.10.2017

Measuring Time Between Opening and Closing a Trade
12 Dec 2017, 10:52


Hi guys,

 

Can anyone please help with measuring the time between opening and closing a trade on a backtest?

I tried using a Stopwatch and a Timer, but none of them worked - even though I'm sure the solution to this problem really is using a Timer. I have wasted countless of hours on this task already.

 

If anyone of you could give me some hints, that'd be great! 

 

Thank you in advance :-)


@aronbara664
Replies

ap11
12 Dec 2017, 14:26

RE:

Hi aronbara664,

In backtesting you can use Server.Time or just Time property (which is the same) to get current backtesting time.

Kind Regards,
Andrey

aronbara664 said:

Hi guys,

 

Can anyone please help with measuring the time between opening and closing a trade on a backtest?

I tried using a Stopwatch and a Timer, but none of them worked - even though I'm sure the solution to this problem really is using a Timer. I have wasted countless of hours on this task already.

 

If anyone of you could give me some hints, that'd be great! 

 

Thank you in advance :-)

 


@ap11

aronbara664
12 Dec 2017, 15:21

Hi there,

 

Thank you for the quick response!

I have a little bit of a more complicated problem though.

I'll try to give you some context.

My robot utilizes onbar to open a position. So the opening hour is always an even number like 12:00, 13:00 etc... this is not a problem.

However, my robot closes positions in 3 different ways.

1) If price crosses 200MA, CLOSE - Hourly - This is based on a function

2) If we made +80 pip profit, CLOSE - Checked every Minute - This is based on a function

3) If position hits STOPLOSS, CLOSE - This is NOT based on a function.

 

So in short when I close a trade thanks to my CloseAllPos() function, I can get both the Open and Close dates and I can do something with them.

BUT when a position just hits the predetermined SL, (20 pips), I can't find a way to get the CLOSE time at that EXACT moment.

 

I know this issue is a bit more complex, so my apologies. I guess what I really want to know is, how can I get the CLOSE time at any given point in time? For example if my SL is hit.

 

 


@aronbara664

Stokes Bay
12 Dec 2017, 16:08

Does each trade not have a trade number to which you can refer? 

Or, positions held, account balance, etc will change when a trade is closed, could you use that?

Sorry, I don't know the calgo language.


@Stokes Bay

ap11
12 Dec 2017, 16:50

aronbara664,

You can subscribe to Positions.Close event.

Also you can use HistoricalTrade to calculate how long position was opened. It has both EntryTime and ClosingTime.

Please see example below:

protected override void OnStart()
{
    Positions.Closed += Positions_Closed;
}

private void Positions_Closed(PositionClosedEventArgs args)
{
    var position = args.Position;

    // Using Server.Time
    var time1 = Time - position.EntryTime;

    // Using HistoricalTrade
    var historicalTrade = History.FirstOrDefault(h => h.PositionId == position.Id);
    var time2 = historicalTrade.ClosingTime - historicalTrade.EntryTime;
}

Kind Regards,
Andrey


@ap11

aronbara664
12 Dec 2017, 19:26

Thanks, this will help!


@aronbara664