Hello,
I wrote a function that closes half (or whatever) the position, however, after the partial close, it closes the second half, as well and I'm not sure why.
I've read some code from Google but I couldn't find anything useful, I'll continue googling but I've already discovered there are great people here so I hope I'll get an answer either way.
[EDIT]
As more trades are opened, this function needs to run over and over again to make sure the new trades get partially closed, as well, not just the initial trades.
This is the function
And this is how it's called
I thought that once the position size is reduced than it's done with, no? Maybe I should re-enable the "TradeComment" as the Comment gets changed to "from xxxxx" and not the initial TradeComment?
Thanks in advance.
I wrote a function that closes half (or whatever) the position, however, after the partial close, it closes the second half, as well and I'm not sure why.
I've read some code from Google but I couldn't find anything useful, I'll continue googling but I've already discovered there are great people here so I hope I'll get an answer either way.
[EDIT]
As more trades are opened, this function needs to run over and over again to make sure the new trades get partially closed, as well, not just the initial trades.
This is the function
Inserted Code
//+------------------------------------------------------------------+ //| Partial Trade Profit | //+------------------------------------------------------------------+ void PartialTradeProfit() { int totOrders=0; bool ok=true; for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; // if(OrderSymbol()==Symbol() && (OrderMagicNumber()==MagicNumber)&& (OrderComment()==TradeComment) && (OrderType()==OP_BUY || OrderType()==OP_SELL)) if(OrderSymbol()==Symbol() && (OrderMagicNumber()==MagicNumber)&& (OrderType()==OP_BUY || OrderType()==OP_SELL)) { if ((OrderProfit() + OrderCommission() + OrderSwap()) >= PartialTP) { int type=OrderType(); switch(type) { case OP_BUY: ok=OrderClose(OrderTicket(),PartialLots,Bid,3,clrBlue); break; case OP_SELL: ok=OrderClose(OrderTicket(),PartialLots,Ask,3,clrRed); break; } } if(!ok) { if (TryAgain < MaxTries) { Sleep(3000); TryAgain++; PartialTradeProfit(); } Alert("Close failed on "+OrderSymbol()+": due to error: "+IntegerToString(GetLastError())+". TryAgain: "+(string)TryAgain); } else { TryAgain = 1; } } } } //+------------------------------------------------------------------+
And this is how it's called
Inserted Code
//+------------------------------------------------------------------+ //| Expert OnTick function | //+------------------------------------------------------------------+ void OnTick() { if (MarketInfo(Symbol(), MODE_SPREAD) < MaxSpread) { DoMagic(); } } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Do Magic function | //+------------------------------------------------------------------+ void DoMagic() { if (NotDisabled) { if (IsNewBar()) { if (DoPartialClose) { PartialTradeProfit(); } OpenExtraTrades(); AllThingies(); IsFirstieUpDown(); if (ReverseFractalsEntries || TrendFractalsEntries) { FractalThingies(); } } } } //+------------------------------------------------------------------+
I thought that once the position size is reduced than it's done with, no? Maybe I should re-enable the "TradeComment" as the Comment gets changed to "from xxxxx" and not the initial TradeComment?
Thanks in advance.