Closing position after X hours

Created at 07 Jun 2014, 13:51
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!
MA

Marin0ss

Joined 07.06.2014

Closing position after X hours
07 Jun 2014, 13:51


Hi,

I have a question regarding automatic closure of an open position after X hours. As I do not have any coding experience I hope you can help me out.

I want an open position automatically closed after, say 2 days after opening time. Not caring if it is in profit or not.

I have come along following code here: (example closing order if profit > 0)

foreach (var position in Positions)
            {
                if (position.GrossProfit > 1)
                {
                    ClosePosition(position);
                }
            }

I want to make an variabel so the closing time can be set in settings, I got this worked out, it is now set to 2 (which I believe, as nothing else is configured 2 hours)

[Parameter("Max time position open", DefaultValue = 2, MinValue = 0)]
        public int MaxTimeOpen { get; set; }

Please give me some guidelines, or the solution to adjust the code so it will work correctly:

Code below is placed in OnTick() section, got it working for GrossProfit sample.

foreach (var position in Positions)
            {
                if (!MaxTimeOpen......) //here I am stuck; what's next? server.time .... < or >???
                {
                    ClosePosition(position);
                }

Thanks in advance!


@Marin0ss
Replies

breakermind
08 Jun 2014, 20:57

RE:

Hi, look at this:

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class z : Robot
    {

        [Parameter("Max time position open(hours)", DefaultValue = 2, MinValue = 1)]
        public int MaxTimeOpen { get; set; }

        protected override void OnStart()
        {
            // Put your initialization logic here
            Print("Start robot: ", Server.Time.Hour);
            if (Server.Time.Hour == 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, 10000, "", 100, 100);
            }
        }

        protected override void OnTick()
        {
            // Put your core logic here
            Print("OnTick: ", Server.Time.Hour);

            foreach (var position in Positions)
            {
                Print(position.EntryTime.TimeOfDay);
                int posOpenTimeHours = position.EntryTime.Hour + MaxTimeOpen;
                int posOpenTimeMinutes = position.EntryTime.Minute;

                if (posOpenTimeHours > 24)
                {
                    posOpenTimeHours = posOpenTimeHours - 24;
                }

                Print("Pos open time: ", posOpenTimeHours);

                if (Server.Time.Hour > posOpenTimeHours && Server.Time.Minute > posOpenTimeMinutes)
                {
                    ClosePosition(position);
                }
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Regards


@breakermind

Marin0ss
09 Jun 2014, 16:56

RE: RE:

Hi, Thank you very much! I left out te part in OnStart, as I don't want an order to be opened. (this will not cause any problems for the rest of the code?)

I would also like to Backtest this, this is not possible because of use of Server.Time; how must the code look to make this work?

I tried to replace Server.Time for MarketSeries.OpenTime.LastValue but this won't work.

 


@Marin0ss

Spotware
10 Jun 2014, 09:10

RE: RE: RE:

Marin0ss said:

Hi, Thank you very much! I left out te part in OnStart, as I don't want an order to be opened. (this will not cause any problems for the rest of the code?)

I would also like to Backtest this, this is not possible because of use of Server.Time; how must the code look to make this work?

I tried to replace Server.Time for MarketSeries.OpenTime.LastValue but this won't work.

 

Server.Time is emulated in backtesting, therefore you can backtest such cBot. Please do not forget to change TimeZones.UTC to the desired timezone.


@Spotware

breakermind
12 Jun 2014, 09:50

RE: RE: RE: RE:

Hi Marin0ss it works ???

Why is this robot works once again dont,

what is wrong with baktestem in spotware cAlgo -- support why ???

Regards

 

Spotware said:

Marin0ss said:

Hi, Thank you very much! I left out te part in OnStart, as I don't want an order to be opened. (this will not cause any problems for the rest of the code?)

I would also like to Backtest this, this is not possible because of use of Server.Time; how must the code look to make this work?

I tried to replace Server.Time for MarketSeries.OpenTime.LastValue but this won't work.

 

Server.Time is emulated in backtesting, therefore you can backtest such cBot. Please do not forget to change TimeZones.UTC to the desired timezone.

 


@breakermind

Spotware
12 Jun 2014, 11:08

Why is this robot works once again dont,

what is wrong with baktestem in spotware cAlgo -- support why ???

Please try to be more specific. We cannot answer to not clear question.


@Spotware