Close All Async
Close All Async
15 Feb 2017, 23:31
Hi, I need to close many positions quickly.
So I use async :
foreach (var position in Positions.FindAll(Label)) { ClosePositionAsync(position); }
But how to be sure that all positions are closed before to resume the script ? And eventually close all remaining opened positions.
I tried this without success
while (Positions.Count > 0) { foreach (var position in Positions.FindAll(Label)) { ClosePositionAsync(position); } RefreshData(); }
Thank you.
Replies
mytraderadvisor
16 Feb 2017, 19:38
Thank you Lucian.
But I would still appreciate some help from anyone to write the right code ;-)
@mytraderadvisor
Jiri
16 Feb 2017, 20:13
Not sure if it's best solution but it's working. It might get looped if market is closed or you lost the internet connection - you should add a check if there is an error and break the loop.
using cAlgo.API; using cAlgo.API.Internals; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class SampleCloseAllPositionsAsync : Robot { [Parameter(DefaultValue = 100, MinValue = 1)] public int NumberOfPositions { get; set; } protected override void OnStart() { for (int i = 0; i < NumberOfPositions; i++) { ExecuteMarketOrderAsync(TradeType.Buy, Symbol, Symbol.VolumeMin); } } protected override void OnBar() { CloseAllPositionsAsync(); Stop(); } private void CloseAllPositionsAsync() { int lastIndex = Positions.Count - 1; for (int index = 0; index <= lastIndex; index++) { if (index != lastIndex) { ClosePositionAsync(Positions[index]); } else { ClosePosition(Positions[index]); CheckIfAllPositionsAreClosed(); } } } private void CheckIfAllPositionsAreClosed() { if (Positions.Count > 0) { CloseAllPositionsAsync(); } } protected override void OnStop() { Print(Positions.Count); } } }
@Jiri
Jiri
16 Feb 2017, 20:30
This should do, haven't tested though.
using cAlgo.API; using cAlgo.API.Internals; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class SampleCloseAllPositionsAsync : Robot { [Parameter(DefaultValue = 100, MinValue = 1)] public int NumberOfPositions { get; set; } protected override void OnStart() { for (int i = 0; i < NumberOfPositions; i++) { ExecuteMarketOrderAsync(TradeType.Buy, Symbol, Symbol.VolumeMin); } } protected override void OnBar() { CloseAllPositionsAsync(); Stop(); } private void CloseAllPositionsAsync() { int lastIndex = Positions.Count - 1; for (int index = 0; index <= lastIndex; index++) { if (index != lastIndex) { ClosePositionAsync(Positions[index]); } else { TradeResult result = ClosePosition(Positions[index]); if (result.IsSuccessful) { if (Positions.Count > 0) { CloseAllPositionsAsync(); } } else { if (result.Error == ErrorCode.MarketClosed) { // market closed } else if (result.Error == ErrorCode.Disconnected) { // disconnected } else { // other error } } } } } protected override void OnStop() { Print(Positions.Count); } } }
@Jiri
mytraderadvisor
16 Feb 2017, 23:03
Thank you so much Tmc :-)
Backtest Ok. so going live demo test...
Now I need to find a way to save variables in case cAlgo reboot, like MT4 global variables...but it is another story ;-)
@mytraderadvisor
... Deleted by UFO ...