Hi.
There is a breakeven transfer function breakevenpoint(). I open three positions, and when the first one is closed by take, this function is activated. That is,
there are two positions left, but this function transfers the stop of only one position to breakeven, the second one remains with the old stop. It seems that everything is
written correctly, but it does not work correctly. What is the error. I would appreciate a hint.
void BreakEvenPoint()
{
if(BreakEven == 0)
return;
for(int i=PositionsTotal()-1; i>=0; i--)
if(iPosition.SelectByIndex(i))
if(iPosition.Symbol() == iSymbol.Name() && iPosition.Magic() == MagicNumber)
{
if(PositionSelect(iSymbol.Name())==true) // there is an open position
{
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
if(iSymbol.Bid() > (iPosition.PriceOpen() + iSprd * iSymbol.Point()))
{
double sl = NormalizeDouble(iPosition.PriceOpen() + iSprd, iSymbol.Digits());
double tp = iPosition.TakeProfit();
if(iPosition.StopLoss() < sl || iPosition.StopLoss()==0.0)
{
//--- modify position
if(iTrade.PositionModify(iSymbol.Name(), sl, tp))
printf("Long position by %s to be modified", iSymbol.Name());
else
{
printf("Error modifying position by %s : '%s'", iSymbol.Name(), iTrade.ResultComment());
printf("Modify parameters : SL=%f,TP=%f", sl, tp);
}
//--- modified and must exit from expert
}
}
}
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
if(iPosition.PriceOpen() > (iSymbol.Ask() - BreakEven * iSymbol.Point()))
{
double sl=NormalizeDouble(iPosition.PriceOpen() - iSprd, iSymbol.Digits());
double tp=iPosition.TakeProfit();
if(iPosition.StopLoss() > sl || iPosition.StopLoss()==0.0)
{
//--- modify position
if(iTrade.PositionModify(iSymbol.Name(),sl,tp))
printf("Short position by %s to be modified", iSymbol.Name());
else
{
printf("Error modifying position by %s : '%s'", iSymbol.Name(), iTrade.ResultComment());
printf("Modify parameters : SL=%f,TP=%f", sl, tp);
}
//--- modified and must exit from expert
}
}
}
}
}
}
crooked
The primer says “To
ensure that you get fresh data about the position, it is recommended to call the positionselect function() immediately
before applying for them. ” I’ll leave it, it won’t get any worse.
In general, he got confused in three birches. For hedged accounts, you need to sort through the pickets. The correct code is below.
void BreakEvenPoint()
{
if(BreakEven == 0)
return;
for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions
if(iPosition.SelectByIndex(i)) // selects the position by index for further access to its properties
if(iPosition.Symbol() == iSymbol.Name() && iPosition.Magic() == MagicNumber)
{
if(PositionSelect(iSymbol.Name())==true)
{
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
if(iSymbol.Bid() > (iPosition.PriceOpen() + BreakEven * iSymbol.Point()))
{
double sl = NormalizeDouble(iPosition.PriceOpen() + iSprd, iSymbol.Digits());
double tp = iPosition.TakeProfit();
ulong iTicket = PositionGetTicket(i);
if(iPosition.StopLoss() < sl || iPosition.StopLoss()==0.0)
{
//--- modify position
if(iTrade.PositionModify(iTicket, sl, tp))
printf("Long position by %s to be modified", iSymbol.Name());
else
{
printf("Error modifying position by %s : '%s'", iSymbol.Name(), iTrade.ResultComment());
printf("Modify parameters : SL=%f,TP=%f", sl, tp);
}
//--- modified and must exit from expert
}
}
}
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
if(iPosition.PriceOpen() > (iSymbol.Ask() - BreakEven * iSymbol.Point()))
{
double sl=NormalizeDouble(iPosition.PriceOpen() - iSprd, iSymbol.Digits());
double tp=iPosition.TakeProfit();
ulong iTicket = PositionGetTicket(i);
if(iPosition.StopLoss() > sl || iPosition.StopLoss()==0.0)
{
//--- modify position
if(iTrade.PositionModify(iTicket,sl,tp))
printf("Short position by %s to be modified", iSymbol.Name());
else
{
printf("Error modifying position by %s : '%s'", iSymbol.Name(), iTrade.ResultComment());
printf("Modify parameters : SL=%f,TP=%f", sl, tp);
}
//--- modified and must exit from expert
}
}
}
}
}
}
barabashkakvn
The second time to select a position is unnecessary.
if(iPosition.SelectByIndex(i))
if(iPosition.Symbol() == iSymbol.Name() && iPosition.Magic() == MagicNumber)
{
if(PositionSelect(iSymbol.Name())==true) // there is an open position