Skip to content

Instantly share code, notes, and snippets.

@Aqzhyi
Created June 11, 2021 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aqzhyi/41858534857135e74822c50f468b8d62 to your computer and use it in GitHub Desktop.
Save Aqzhyi/41858534857135e74822c50f468b8d62 to your computer and use it in GitHub Desktop.
mt5 mql5 Positions Trailing Stop
#define TicketPositionsForEach(ticket, i) \
HistorySelect(0, TimeCurrent()); \
ulong ticket = PositionGetTicket(0); \
int po_total = PositionsTotal(); \
for (int i = 1; i <= po_total; i++, ticket = PositionGetTicket(i - 1))
input string _____185512940 = "_____自動交易功能:移動止損停利第一階段"; // -
input bool SLSTOn_inp = false; // 啟用?(作用於所有倉位)
input int SLSTStepStartAt_inp = 40; // 當點數高於點數時,開始第一步步幅移動, 0=停用
input int SLSTStep_inp = 15; // 每步幅移動點數, 0=停用
//+------------------------------------------------------------------+
//| 移動止損 檢查所有倉位
//+------------------------------------------------------------------+
void SLSTPostions()
{
if (IsTradeWait() || !SLSTOn_inp || SLSTStepStartAt_inp == 0 || SLSTStep_inp == 0) {
return;
}
TicketPositionsForEach(ticket, i) {
TicketPosStruct pos = TicketPosGet(ticket);
if (pos.pointNow >= SLSTStepStartAt_inp) {
double posMultiple = MathFloor(MathDiv(pos.pointNow, SLSTStep_inp)) - 1;
int posOffsetPoints = pos.isBuy ? (SLSTStep_inp) : -(SLSTStep_inp);
double posPriceSLNew = SymbolInfoGetPointsToPrice(posOffsetPoints * posMultiple, pos.priceOpen, pos.symbol);
if (DoubleToString(pos.priceSL, 5) == DoubleToString(posPriceSLNew, 5)) {}
else if (posPriceSLNew == pos.priceSL) {}
else if (pos.isBuy && posPriceSLNew < pos.priceSL) {}
else if (!pos.isBuy && posPriceSLNew > pos.priceSL) {}
else {
Print(""
+ "移動止損功能第1階段" + DIV
+ "商品" + pos.symbol + DIV
+ "票號" + pos.pointNow + DIV
+ "此倉位點數=" + pos.pointNow + DIV
+ "開倉價格=" + (string)(pos.priceOpen) + DIV
+ "舊鎖價格=" + (string)(pos.priceSL) + DIV
+ "新鎖價格=" + posPriceSLNew + DIV
);
SoundPlay(SLSTSoundFilename_inp, SLSTSoundOn_inp);
ctrade.PositionModify(ticket, posPriceSLNew, 0.0);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment