Close only Aggregated Positions
Close only Aggregated Positions
06 Jun 2016, 10:48
I was happy that I got my Close-Position-Bot working, but sadly after testing it live today I discovered, that olthough it works, it Closes ALL my positions.
My code so far:
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
var position = args.Position;
Print("Position closed with {0} profit", position.GrossProfit);
// otherwise profit > 0 so close all pending orders
foreach (PendingOrder o in PendingOrders)
CancelPendingOrder(o);
// and close all open positions
foreach (Position p in Positions)
ClosePosition(p);
Stop();
}
I suspect I can't do it with Label, because if my other bot runs on multiple Charts, they all get the same Label.
So I have to do it with Symbol somehow, or better yet, if there is a command for an aggregated closeAll.
Just like when you do it manually.
Replies
jeremy.vostik
06 Jun 2016, 15:44
PID is a Little tricky I believe. In case of multiple positions. It is certainly way out of my coding skill reach to do that.
I was thinking something like
get the currentSymbol.
var positionB = PositionsFind(lable, currentSymbol, TradeType.Buy)
var positionS = PositionsFind(lable, currentSymbol, TradeType.Sell)
foreach (Position p in Positions)
if(position.Symbol == currentSymbol)
{
ClosePosition(p);
}
Something along that line. But I don't know how to do it :(
@jeremy.vostik
tradermatrix
05 Aug 2016, 19:53
RE:
jeremy.vostik said:
I was happy that I got my Close-Position-Bot working, but sadly after testing it live today I discovered, that olthough it works, it Closes ALL my positions.
My code so far:
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
}
private void OnPositionsClosed(PositionClosedEventArgs args) { var position = args.Position; Print("Position closed with {0} profit", position.GrossProfit); if (position.Label != "your label"|| position.SymbolCode != Symbol.Code) return; // otherwise profit > 0 so close all pending orders foreach (PendingOrder o in PendingOrders) CancelPendingOrder(o); // and close all open positions foreach (Position position in Positions.FindAll("your label", Symbol)) ClosePosition(); Stop(); }can be in this style
@tradermatrix
kricka
06 Jun 2016, 13:31
Try using PID
Maybe you could use the PID instead of the label in your case. Retrieve the PID and save it in a variable. That will distinguish it if you have a lot of positions with the same label name.
The Market Order 3.0 uses the label name in search for the position and history information, but if you have several positions with the same label name it will treat all those positions the same. That is why you can't place an order with a label that is already in an open position.
@kricka