Closing a position after time

Created at 19 Oct 2013, 23:50
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!
OL

Old Account

Joined 14.10.2013

Closing a position after time
19 Oct 2013, 23:50


Hi

How do i make a position close after a set amount of hours?

Thanks


@Old Account
Replies

Kate
20 Oct 2013, 00:03

The only thing you can do now is to implement robot n cAlgo that will automatically close your position.


@Kate

Cerunnos
20 Oct 2013, 00:45

RE:

MRSV said:

Hi

How do i make a position close after a set amount of hours?

Thanks

Hi, you could use a timer. Have not tested it but it should work.

using System.Timers;
...
private Position pos;
private readonly Timer _timer = new Timer();

 protected override void OnPositionOpened(Position openedPosition)
        {
        pos = openedPosition;        
        _timer.Elapsed += OnTimedEvent;
        _timer.Interval = 3  * 3600 * 1000; // Timer will tick every Interval (milliseconds)-> in 3 hours close position  
        _timer.Enabled = true; 
        _timer.Start(); 
        } 

 private void OnTimedEvent(object sender, ElapsedEventArgs e)        
        {  
       Trade.Close(pos);  
       _timer.Stop();       
        } 
protected override void OnPositionClosed(Position position)
        {
        pos = null;            
        }


@Cerunnos

Kate
21 Oct 2013, 16:59

I'd suggest to check the time in OnTick() method: it's more simple and more safe.


@Kate