import ta import numpy as np import matplotlib.pyplot as plt # Set up inputs src = close keyvalue = 3 atrperiod = 10 nLoss = keyvalue * ta.average_true_range(high, low, close, atrperiod) # Initialize variables xATRTrailingStop = np.zeros_like(src) pos = np.zeros_like(src) # Calculate xATRTrailingStop and position values for i in range(1, len(src)): if src[i] > xATRTrailingStop[i-1]: iff_1 = src[i] - nLoss[i] else: iff_1 = src[i] + nLoss[i] if src[i] < xATRTrailingStop[i-1] and src[i-1] < xATRTrailingStop[i-1]: iff_2 = min(xATRTrailingStop[i-1], src[i] + nLoss[i]) else: iff_2 = iff_1 if src[i] > xATRTrailingStop[i-1] and src[i-1] > xATRTrailingStop[i-1]: xATRTrailingStop[i] = max(xATRTrailingStop[i-1], src[i] - nLoss[i]) else: xATRTrailingStop[i] = iff_2 if src[i-1] > xATRTrailingStop[i-1] and src[i] < xATRTrailingStop[i-1]: pos[i] = -1 else: pos[i] = pos[i-1] if src[i-1] < xATRTrailingStop[i-1] and src[i] > xATRTrailingStop[i-1]: pos[i] = 1 # Plot xATRTrailingStop and buy/sell signals buy = np.where(np.diff(np.sign(xATRTrailingStop - src)) > 0)[0] sell = np.where(np.diff(np.sign(src - xATRTrailingStop)) > 0)[0] xcolor = np.where(pos == -1, 'red', np.where(pos == 1, 'green', 'blue')) plt.plot(xATRTrailingStop, color=xcolor) plt.scatter(buy, src[buy], color='green', marker='^', s=100) plt.scatter(sell, src[sell], color='red', marker='v', s=100) plt.show() ———— MQL5 // Set input parameters input float KeyVaule = 3.0; input int ATRPeriod = 10; // Set variables double xATR, nLoss, xATRTrailingStop, pos; double src = Close[0]; // Set ATR calculation xATR = iATR(_Symbol, _Period, ATRPeriod); // Set nLoss calculation nLoss = KeyVaule * xATR; // Set xATRTrailingStop calculation if (src > iCustom(_Symbol, _Period, "xATRTrailingStop")) xATRTrailingStop = src - nLoss; else if (src < iCustom(_Symbol, _Period, "xATRTrailingStop") && Close[1] < iCustom(_Symbol, _Period, "xATRTrailingStop")) xATRTrailingStop = MathMin(iCustom(_Symbol, _Period, "xATRTrailingStop"), src + nLoss); else if (src > iCustom(_Symbol, _Period, "xATRTrailingStop") && Close[1] > iCustom(_Symbol, _Period, "xATRTrailingStop")) xATRTrailingStop = MathMax(iCustom(_Symbol, _Period, "xATRTrailingStop"), src - nLoss); // Set position calculation if (Close[1] > iCustom(_Symbol, _Period, "xATRTrailingStop") && Close[0] < iCustom(_Symbol, _Period, "xATRTrailingStop")) pos = -1; else pos = Close[1] < iCustom(_Symbol, _Period, "xATRTrailingStop") && Close[0] > iCustom(_Symbol, _Period, "xATRTrailingStop") ? 1 : pos[1]; // Set color for trailing stop line color xColor = pos == -1 ? clrRed : pos == 1 ? clrGreen : clrBlue; // Draw trailing stop line on chart ObjectCreate("xATRTrailingStop", OBJ_TREND, 0, 0, Time[0], iCustom(_Symbol, _Period, "xATRTrailingStop")); ObjectSet("xATRTrailingStop", OBJPROP_COLOR, xColor); // Buy and sell signals bool buy = Close[0] > iCustom(_Symbol, _Period, "xATRTrailingStop"); bool sell = Close[0] < iCustom(_Symbol, _Period, "xATRTrailingStop"); // Plot buy and sell signals if (buy) ObjectCreate("Buy", OBJ_LABEL, 0, 0, Time[0], High[0]); else ObjectDelete("Buy"); if (sell) ObjectCreate("Sell", OBJ_LABEL, 0, 0, Time[0], Low[0]); else ObjectDelete("Sell"); // Set buy and sell alerts if (buy) Alert("UT BOT Buy"); if (sell) Alert("UT BOT Sell"); // Trading logic if (buy) OrderSend(_Symbol, OP_BUY, 0.1, Ask, 0, 0, 0, "UT BOT Buy", 0, 0, clrGreen); if (