реклама
Бургер менюБургер меню

Алексей Боровков – 10 роботов для автоматической торговли на Форекс (страница 5)

18

{

if(PositionGetSymbol(i) == _Symbol && PositionGetInteger(POSITION_MAGIC) == MagicNumber)

{

ulong ticket = PositionGetTicket(i);

if(ticket <= 0) continue;

double currentSL = PositionGetDouble(POSITION_SL);

double currentPrice = PositionGetDouble(POSITION_PRICE_CURRENT);

double openPrice = PositionGetDouble(POSITION_PRICE_OPEN);

long positionType = PositionGetInteger(POSITION_TYPE);

// Для длинной позиции (BUY)

if(positionType == POSITION_TYPE_BUY)

{

double newSL = currentPrice – TrailingStop * _Point;

if(newSL > currentSL && newSL > openPrice)

{

ModifyStopLoss(ticket, newSL);

}

}

// Для короткой позиции (SELL)

else if(positionType == POSITION_TYPE_SELL)

{

double newSL = currentPrice + TrailingStop * _Point;

if(newSL < currentSL && newSL < openPrice)

{

ModifyStopLoss(ticket, newSL);

}

}

}

}

}

//+–+

//| Модификация стоп-лосса |

//+–+

void ModifyStopLoss(ulong ticket, double newStopLoss)

{

MqlTradeRequest request;

MqlTradeResult result;

ZeroMemory(request);

ZeroMemory(result);

request.action = TRADE_ACTION_SLTP;

request.position = ticket;

request.symbol = _Symbol;

request.sl = newStopLoss;

request.magic = MagicNumber;

// Сохраняем текущий тейк-профит

request.tp = PositionGetDouble(POSITION_TP);

if(OrderSend(request, result))

{

if(result.retcode == TRADE_RETCODE_DONE)

{

Print("Стоп-лосс модифицирован. Новый SL: ", newStopLoss);

}

}

}

//+–+

//| Функция для логов |

//+–+

void PrintStatus()

{

double fastMA[1], slowMA[1], adxValue[1];

if(CopyBuffer(fastMAHandle, 0, 0, 1, fastMA) < 1) return;

if(CopyBuffer(slowMAHandle, 0, 0, 1, slowMA) < 1) return;

if(CopyBuffer(adxHandle, 0, 0, 1, adxValue) < 1) return;

Print("Текущие значения: FastMA=", fastMA[0], " SlowMA=", slowMA[0], " ADX=", adxValue[0]);

}