Close all positions in a specific chart/symbol only
Close all positions in a specific chart/symbol only
05 Jul 2019, 13:44
Hi, this forum have been great learning help. This ebot I copied from this forum closes all positions in all charts . I want it to close positions in a specific chart only. Like if i have positions in 6 charts, it should only close in the chart where I am running this bot. Will appreciate any help in changing the code.
using System; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class profit : Robot { [Parameter(DefaultValue = 100.0)] public double Parameter { get; set; } protected override void OnTick() { int? a = null; int? b = null; foreach (var position in Positions) a++; foreach (var position in Positions) if (position.Pips > Parameter) b++; if (a == b) foreach (var position in Positions) { ClosePosition(position); } } } }
Replies
YMYMYM
05 Jul 2019, 18:43
RE:
Hi Panagiotis, if I might seek your help again. Will it be possible to add another line so that it also Cencels all Pending Orders in the same symbol/chart at the same time when open positions r closed? I use grid strategy. After your help I am able to csuccessfully closed all open positions in a chart but for the pending orders I have to do it manually. I tried to play around with coding but no success.
Regards
{ foreach (var order in PendingOrders) { CancelPendingOrder(order); }
Hi amarnadeem1,
See below
using System; using cAlgo.API; using System.Linq; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class profit : Robot { [Parameter(DefaultValue = 100.0)] public double Parameter { get; set; } protected override void OnTick() { int? a = null; int? b = null; foreach (var position in Positions) a++; foreach (var position in Positions) if (position.Pips > Parameter) b++; if (a == b) foreach (var position in Positions.Where(x => x.SymbolName == Symbol.Name)) { ClosePosition(position); } } } }Best Regards,
Panagiotis
@YMYMYM
PanagiotisCharalampous
08 Jul 2019, 10:21
Hi amarnadeem1,
Here it is
foreach (var order in PendingOrders.Where(x => x.SymbolCode == Symbol.Name)) { CancelPendingOrder(order); }
Best Regards,
Panagiotis
@PanagiotisCharalampous
YMYMYM
08 Jul 2019, 19:11
RE:
Thanks Panagiotis. It worked exactly as I wanted. Basically closes all open positions and pending orders in a specific chart only. Grateful for help!
using System; using cAlgo.API; using System.Linq; using cAlgo.API.Indicators; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class ClosePositionsOrders : Robot { [Parameter(DefaultValue = 100.0)] public double Parameter { get; set; } protected override void OnTick() { int? a = null; int? b = null; foreach (var position in Positions) a++; foreach (var position in Positions) if (position.Pips > Parameter) b++; if (a == b) foreach (var position in Positions.Where(x => x.SymbolName == Symbol.Name)) { ClosePosition(position); } foreach (var order in PendingOrders.Where(x => x.SymbolCode == Symbol.Name)) { CancelPendingOrder(order); } Stop(); } } }
@YMYMYM
PanagiotisCharalampous
05 Jul 2019, 14:20
Hi amarnadeem1,
See below
Best Regards,
Panagiotis
@PanagiotisCharalampous