Facebook
From RS, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 64
  1. import ta
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. # Set up inputs
  6. src = close
  7. keyvalue = 3
  8. atrperiod = 10
  9. nLoss = keyvalue * ta.average_true_range(high, low, close, atrperiod)
  10.  
  11. # Initialize variables
  12. xATRTrailingStop = np.zeros_like(src)
  13. pos = np.zeros_like(src)
  14.  
  15. # Calculate xATRTrailingStop and position values
  16. for i in range(1, len(src)):
  17.     if src[i] > xATRTrailingStop[i-1]:
  18.         iff_1 = src[i] - nLoss[i]
  19.     else:
  20.         iff_1 = src[i] + nLoss[i]
  21.    
  22.     if src[i] < xATRTrailingStop[i-1] and src[i-1] < xATRTrailingStop[i-1]:
  23.         iff_2 = min(xATRTrailingStop[i-1], src[i] + nLoss[i])
  24.     else:
  25.         iff_2 = iff_1
  26.        
  27.     if src[i] > xATRTrailingStop[i-1] and src[i-1] > xATRTrailingStop[i-1]:
  28.         xATRTrailingStop[i] = max(xATRTrailingStop[i-1], src[i] - nLoss[i])
  29.     else:
  30.         xATRTrailingStop[i] = iff_2
  31.    
  32.     if src[i-1] > xATRTrailingStop[i-1] and src[i] < xATRTrailingStop[i-1]:
  33.         pos[i] = -1
  34.     else:
  35.         pos[i] = pos[i-1]
  36.    
  37.     if src[i-1] < xATRTrailingStop[i-1] and src[i] > xATRTrailingStop[i-1]:
  38.         pos[i] = 1
  39.        
  40. # Plot xATRTrailingStop and buy/sell signals
  41. buy = np.where(np.diff(np.sign(xATRTrailingStop - src)) > 0)[0]
  42. sell = np.where(np.diff(np.sign(src - xATRTrailingStop)) > 0)[0]
  43. xcolor = np.where(pos == -1, 'red', np.where(pos == 1, 'green', 'blue'))
  44.  
  45. plt.plot(xATRTrailingStop, color=xcolor)
  46. plt.scatter(buy, src[buy], color='green', marker='^', s=100)
  47. plt.scatter(sell, src[sell], color='red', marker='v', s=100)
  48.  
  49. plt.show()
  50.  
  51.  
  52. ———— MQL5
  53.  
  54. // Set input parameters
  55. input float KeyVaule = 3.0;
  56. input int ATRPeriod = 10;
  57.  
  58. // Set variables
  59. double xATR, nLoss, xATRTrailingStop, pos;
  60. double src = Close[0];
  61.  
  62. // Set ATR calculation
  63. xATR = iATR(_Symbol, _Period, ATRPeriod);
  64.  
  65. // Set nLoss calculation
  66. nLoss = KeyVaule * xATR;
  67.  
  68. // Set xATRTrailingStop calculation
  69. if (src > iCustom(_Symbol, _Period, "xATRTrailingStop"))
  70.     xATRTrailingStop = src - nLoss;
  71. else if (src < iCustom(_Symbol, _Period, "xATRTrailingStop") &&
  72.          Close[1] < iCustom(_Symbol, _Period, "xATRTrailingStop"))
  73.     xATRTrailingStop = MathMin(iCustom(_Symbol, _Period, "xATRTrailingStop"), src + nLoss);
  74. else if (src > iCustom(_Symbol, _Period, "xATRTrailingStop") &&
  75.          Close[1] > iCustom(_Symbol, _Period, "xATRTrailingStop"))
  76.     xATRTrailingStop = MathMax(iCustom(_Symbol, _Period, "xATRTrailingStop"), src - nLoss);
  77.  
  78. // Set position calculation
  79. if (Close[1] > iCustom(_Symbol, _Period, "xATRTrailingStop") &&
  80.     Close[0] < iCustom(_Symbol, _Period, "xATRTrailingStop"))
  81.     pos = -1;
  82. else
  83.     pos = Close[1] < iCustom(_Symbol, _Period, "xATRTrailingStop") &&
  84.           Close[0] > iCustom(_Symbol, _Period, "xATRTrailingStop") ? 1 : pos[1];
  85.  
  86. // Set color for trailing stop line
  87. color xColor = pos == -1 ? clrRed : pos == 1 ? clrGreen : clrBlue;
  88.  
  89. // Draw trailing stop line on chart
  90. ObjectCreate("xATRTrailingStop", OBJ_TREND, 0, 0, Time[0], iCustom(_Symbol, _Period, "xATRTrailingStop"));
  91. ObjectSet("xATRTrailingStop", OBJPROP_COLOR, xColor);
  92.  
  93. // Buy and sell signals
  94. bool buy = Close[0] > iCustom(_Symbol, _Period, "xATRTrailingStop");
  95. bool sell = Close[0] < iCustom(_Symbol, _Period, "xATRTrailingStop");
  96.  
  97. // Plot buy and sell signals
  98. if (buy)
  99.     ObjectCreate("Buy", OBJ_LABEL, 0, 0, Time[0], High[0]);
  100. else
  101.     ObjectDelete("Buy");
  102. if (sell)
  103.     ObjectCreate("Sell", OBJ_LABEL, 0, 0, Time[0], Low[0]);
  104. else
  105.     ObjectDelete("Sell");
  106.  
  107. // Set buy and sell alerts
  108. if (buy)
  109.     Alert("UT BOT Buy");
  110. if (sell)
  111.     Alert("UT BOT Sell");
  112.  
  113. // Trading logic
  114. if (buy)
  115.     OrderSend(_Symbol, OP_BUY, 0.1, Ask, 0, 0, 0, "UT BOT Buy", 0, 0, clrGreen);
  116. if (
  117.