Closing position after X hours
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!
Replies
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
breakermind
08 Jun 2014, 20:57
RE:
Hi, look at this:
Regards
@breakermind