【超短线指标】超短线千万不要贪心

| 发表于 昨天 20:22 | 显示全部楼层 |复制链接
  1. //+------------------------------------------------------------------+
  2. //|                                      远山Ai量化--超级趋势变色版.mq4 |
  3. //|                                             Copyright 2025, 远山Ai量化 |
  4. //|                                             https://www.yuanshan.asia |
  5. //+------------------------------------------------------------------+
  6. #property copyright "远山Ai量化--制作于2026.9.01(点击此文字访问百款指标网站)"
  7. #property link      "https://www.yuanshan.asia"
  8. #property version   "1.00"
  9. #property description "Ai量化编程,2小时学习,1分钟做策略/指标,微信:_yshan001(备注来意)"
  10. #property description ""
  11. #property description "远山Ai量化-超级趋势变色版 是一款判断当前趋势的指标,并根据趋势改变K线颜色"
  12. #property indicator_chart_window
  13. #property indicator_buffers 11  // 增加了4个用于K线颜色的缓冲区
  14. // 默认颜色和样式(可通过代码动态修改)
  15. #property indicator_color1 clrLime     // 绿色上升趋势
  16. #property indicator_color2 clrYellow   // 黄色下降趋势
  17. #property indicator_color3 clrLime     // 绿色买入信号
  18. #property indicator_color4 clrYellow   // 黄色卖出信号
  19. #property indicator_color5 clrLime     // 绿色上升趋势点
  20. #property indicator_color6 clrYellow   // 黄色下降趋势点
  21. #property indicator_color7 clrRed      // K线上涨颜色
  22. #property indicator_color8 clrBlue     // K线下跌颜色
  23. #property indicator_color9 clrRed      // K线上涨颜色(实体)
  24. #property indicator_color10 clrBlue    // K线下跌颜色(实体)
  25. #property indicator_color11 clrGray    // 中性K线颜色
  26. // 枚举:信号箭头类型
  27. enum ENUM_SIGNAL_TYPE
  28. {
  29.    SIGNAL_CIRCLE = 0,       // 圆点
  30.    SIGNAL_ARROW_UPDOWN = 1, // 空心上下箭头
  31.    SIGNAL_ARROW_FILLED = 2, // 实心上下箭头
  32.    SIGNAL_ARROW_RIGHT = 3,  // 实心向右箭头
  33.    SIGNAL_THUMB = 4,        // 大拇指
  34.    SIGNAL_SUN = 5           // 太阳
  35. };
  36. // 枚举:趋势线样式
  37. enum ENUM_TREND_STYLE
  38. {
  39.    STYLE_LINES = 0,        // 仅线段
  40.    STYLE_LINES_DOTS = 1,   // 线段+圆点
  41.    STYLE_DOTS = 2          // 仅圆点
  42. };
  43. // 缓冲区声明
  44. double TrendUp[];          // 上升趋势线
  45. double TrendDown[];        // 下降趋势线
  46. double BuySignal[];        // 买入信号
  47. double SellSignal[];       // 卖出信号
  48. double TrendUpDots[];      // 上升趋势圆点
  49. double TrendDownDots[];    // 下降趋势圆点
  50. double TrendBuffer[];      // 趋势方向缓冲区(用于计算)
  51. // K线颜色缓冲区
  52. double UpBar[];           // 上涨影线
  53. double DownBar[];         // 下跌影线
  54. double UpCandle[];        // 上涨实体
  55. double DownCandle[];      // 下跌实体
  56. // 指标参数
  57. string TimeLabel = "══════ 指标到期时间 ══════";  // ▼ 指标到期信息
  58. string ExpiryInfo = "2025年8月1日";            // [只读]指标到期时间
  59. // 超级趋势计算参数
  60. extern string ParamLabel1 = "══════ 趋势计算参数 ══════"; // ▼ 趋势计算参数
  61. extern int    ATR周期 = 10;         // ATR周期
  62. extern double 乘数系数 = 3.0;        // ATR乘数
  63. // 信号显示参数
  64. extern string ParamLabel2 = "══════ 信号显示参数 ══════"; // ▼ 信号显示参数
  65. extern bool   显示信号 = true;       // 显示买卖信号
  66. extern ENUM_SIGNAL_TYPE 信号类型 = SIGNAL_ARROW_RIGHT; // 信号类型
  67. extern int    信号大小 = 1;          // 信号大小
  68. // 趋势线参数
  69. extern string ParamLabel3 = "══════ 趋势线参数 ══════"; // ▼ 趋势线参数
  70. extern ENUM_TREND_STYLE 趋势线样式 = STYLE_LINES; // 趋势线样式
  71. extern int    线条粗细 = 1;          // 趋势线粗细
  72. extern int    点大小 = 1;            // 趋势点大小
  73. extern color  上升颜色 = clrLime;    // 上升趋势颜色(绿色)
  74. extern color  下降颜色 = clrYellow;  // 下降趋势颜色(黄色)
  75. // K线样式参数
  76. extern string ParamLabel4 = "══════ K线样式参数 ══════"; // ▼ K线样式参数
  77. extern color  上涨K线颜色 = clrBlue;   // 上涨K线颜色
  78. extern color  下跌K线颜色 = clrRed;  // 下跌K线颜色
  79. extern int    K线影线宽度 = 1;        // K线影线宽度
  80. extern int    K线实体宽度 = 3;        // K线实体宽度
  81. // 文字标签参数
  82. extern string ParamLabel5 = "══════ 文字标签参数 ══════"; // ▼ 文字标签参数
  83. extern bool   显示文字 = false;      // 显示文字标签
  84. extern string 买入文字 = "买入";     // 买入文字
  85. extern string 卖出文字 = "卖出";     // 卖出文字
  86. extern int    文字大小 = 10;         // 文字大小
  87. extern color  买入颜色 = clrLime;    // 买入颜色(绿色)
  88. extern color  卖出颜色 = clrYellow;  // 卖出颜色(黄色)
  89. extern int    文字偏移 = 10;         // 文字偏移量(点)
  90. // 全局变量
  91. datetime expiry_date = D'2099.08.01 00:00';  // 到期时间
  92. bool is_expired = false;
  93. int g_text_size;
  94. int buy_arrow_code, sell_arrow_code;
  95. //+------------------------------------------------------------------+
  96. //| 设置K线颜色函数                                                   |
  97. //+------------------------------------------------------------------+
  98. void SetCandleColor(bool isUpTrend, int i)
  99. {
  100.    // 重置所有缓冲区为空值
  101.    UpBar = EMPTY_VALUE;
  102.    DownBar = EMPTY_VALUE;
  103.    UpCandle = EMPTY_VALUE;
  104.    DownCandle = EMPTY_VALUE;
  105.    if(isUpTrend)
  106.    {
  107.       // 上涨K线
  108.       UpBar = High;
  109.       UpCandle = MathMax(Open, Close);
  110.       DownBar = Low;
  111.       DownCandle = MathMin(Open, Close);
  112.    }
  113.    else
  114.    {
  115.       // 下跌K线
  116.       DownBar = High;
  117.       DownCandle = MathMax(Open, Close);
  118.       UpBar = Low;
  119.       UpCandle = MathMin(Open, Close);
  120.    }
  121. }
  122. //+------------------------------------------------------------------+
  123. //| 自定义指标初始化函数                                              |
  124. //+------------------------------------------------------------------+
  125. int init()
  126. {
  127.    // 格式化到期时间显示
  128.    int yr = TimeYear(expiry_date);
  129.    int mn = TimeMonth(expiry_date);
  130.    int dy = TimeDay(expiry_date);
  131.    ExpiryInfo = StringConcatenate(yr, "年",
  132.                            (mn < 10 ? "0" : ""), mn, "月",
  133.                            (dy < 10 ? "0" : ""), dy, "日");
  134.    // 清除所有标签
  135.    ObjectsDeleteAll(0, OBJ_TEXT);
  136.    // 根据信号类型选择箭头代码
  137.    switch(信号类型)
  138.    {
  139.       case SIGNAL_CIRCLE:       // 圆点
  140.          buy_arrow_code = 159;
  141.          sell_arrow_code = 159;
  142.          break;
  143.       case SIGNAL_ARROW_UPDOWN: // 空心上下箭头
  144.          buy_arrow_code = 241;  // 空心上箭头
  145.          sell_arrow_code = 242; // 空心下箭头
  146.          break;
  147.       case SIGNAL_ARROW_FILLED: // 实心上下箭头
  148.          buy_arrow_code = 233;  // 实心上箭头
  149.          sell_arrow_code = 234; // 实心下箭头
  150.          break;
  151.       case SIGNAL_ARROW_RIGHT:  // 实心向右箭头
  152.          buy_arrow_code = 232;  // 向右箭头(买入和卖出)
  153.          sell_arrow_code = 232; // 向右箭头(买入和卖出)
  154.          break;
  155.       case SIGNAL_THUMB:        // 大拇指
  156.          buy_arrow_code = 67;   // 大拇指向上
  157.          sell_arrow_code = 68;  // 大拇指向下
  158.          break;
  159.       case SIGNAL_SUN:          // 太阳
  160.          buy_arrow_code = 77;   // 太阳符号
  161.          sell_arrow_code = 77;  // 太阳符号
  162.          break;
  163.       default:                  // 默认使用圆点
  164.          buy_arrow_code = 159;
  165.          sell_arrow_code = 159;
  166.          break;
  167.    }
  168.    // 初始化指标缓冲区
  169.    IndicatorBuffers(11);
  170.    // 设置趋势线缓冲区
  171.    SetIndexBuffer(0, TrendUp);
  172.    SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 线条粗细, 上升颜色);
  173.    SetIndexLabel(0, "上升趋势");
  174.    SetIndexEmptyValue(0, EMPTY_VALUE);
  175.    SetIndexBuffer(1, TrendDown);
  176.    SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 线条粗细, 下降颜色);
  177.    SetIndexLabel(1, "下降趋势");
  178.    SetIndexEmptyValue(1, EMPTY_VALUE);
  179.    SetIndexBuffer(2, BuySignal);
  180.    SetIndexStyle(2, DRAW_ARROW, STYLE_SOLID, 信号大小, 买入颜色);
  181.    SetIndexArrow(2, buy_arrow_code);
  182.    SetIndexLabel(2, "买入信号");
  183.    SetIndexEmptyValue(2, EMPTY_VALUE);
  184.    SetIndexBuffer(3, SellSignal);
  185.    SetIndexStyle(3, DRAW_ARROW, STYLE_SOLID, 信号大小, 卖出颜色);
  186.    SetIndexArrow(3, sell_arrow_code);
  187.    SetIndexLabel(3, "卖出信号");
  188.    SetIndexEmptyValue(3, EMPTY_VALUE);
  189.    SetIndexBuffer(4, TrendUpDots);
  190.    SetIndexStyle(4, DRAW_ARROW, STYLE_SOLID, 点大小, 上升颜色);
  191.    SetIndexArrow(4, 159);
  192.    SetIndexLabel(4, "上升趋势点");
  193.    SetIndexEmptyValue(4, EMPTY_VALUE);
  194.    SetIndexBuffer(5, TrendDownDots);
  195.    SetIndexStyle(5, DRAW_ARROW, STYLE_SOLID, 点大小, 下降颜色);
  196.    SetIndexArrow(5, 159);
  197.    SetIndexLabel(5, "下降趋势点");
  198.    SetIndexEmptyValue(5, EMPTY_VALUE);
  199.    // 设置K线颜色缓冲区
  200.    SetIndexBuffer(6, UpBar);
  201.    SetIndexStyle(6, DRAW_HISTOGRAM, STYLE_SOLID, K线影线宽度, 上涨K线颜色);
  202.    SetIndexLabel(6, "上涨影线");
  203.    SetIndexEmptyValue(6, EMPTY_VALUE);
  204.    SetIndexBuffer(7, DownBar);
  205.    SetIndexStyle(7, DRAW_HISTOGRAM, STYLE_SOLID, K线影线宽度, 下跌K线颜色);
  206.    SetIndexLabel(7, "下跌影线");
  207.    SetIndexEmptyValue(7, EMPTY_VALUE);
  208.    SetIndexBuffer(8, UpCandle);
  209.    SetIndexStyle(8, DRAW_HISTOGRAM, STYLE_SOLID, K线实体宽度, 上涨K线颜色);
  210.    SetIndexLabel(8, "上涨实体");
  211.    SetIndexEmptyValue(8, EMPTY_VALUE);
  212.    SetIndexBuffer(9, DownCandle);
  213.    SetIndexStyle(9, DRAW_HISTOGRAM, STYLE_SOLID, K线实体宽度, 下跌K线颜色);
  214.    SetIndexLabel(9, "下跌实体");
  215.    SetIndexEmptyValue(9, EMPTY_VALUE);
  216.    // 趋势方向缓冲区
  217.    SetIndexBuffer(10, TrendBuffer);
  218.    SetIndexLabel(10, NULL);
  219.    // 初始化文字大小
  220.    g_text_size = (文字大小 <= 0) ? 8 : 文字大小;
  221.    return(0);
  222. }
  223. //+------------------------------------------------------------------+
  224. //| 自定义指标迭代函数                                                |
  225. //+------------------------------------------------------------------+
  226. int start()
  227. {
  228.    // 检查指标是否已过期
  229.    if(TimeCurrent() > expiry_date && !is_expired)
  230.    {
  231.       is_expired = true;
  232.       Alert("【指标策略已到期】\n请联系远山Ai量化团队\n微信:_yshan001");
  233.       return(0);
  234.    }
  235.    if(is_expired) return(0);
  236.    int counted_bars = IndicatorCounted();
  237.    int limit, i;
  238.    string objName;
  239.    datetime next_bar_time;
  240.    // 检查可能的错误
  241.    if(counted_bars < 0) return(-1);
  242.    // 最后计算的K线将被重新计算
  243.    if(counted_bars > 0) counted_bars--;
  244.    limit = Bars - counted_bars;
  245.    if(limit > Bars) limit = Bars;
  246.    // 初始化数组
  247.    ArrayInitialize(TrendUp, EMPTY_VALUE);
  248.    ArrayInitialize(TrendDown, EMPTY_VALUE);
  249.    ArrayInitialize(BuySignal, EMPTY_VALUE);
  250.    ArrayInitialize(SellSignal, EMPTY_VALUE);
  251.    ArrayInitialize(TrendUpDots, EMPTY_VALUE);
  252.    ArrayInitialize(TrendDownDots, EMPTY_VALUE);
  253.    double up[], down[], atr[], medianPrice[];
  254.    ArrayResize(up, Bars);
  255.    ArrayResize(down, Bars);
  256.    ArrayResize(atr, Bars);
  257.    ArrayResize(medianPrice, Bars);
  258.    // 首先计算所有K线的ATR和中值价格
  259.    for(i = 0; i < Bars; i++)
  260.    {
  261.       atr = iATR(NULL, 0, ATR周期, i);
  262.       medianPrice = (High + Low) / 2;
  263.       up = medianPrice + (乘数系数 * atr);
  264.       down = medianPrice - (乘数系数 * atr);
  265.    }
  266.    // 确定首个计算的K线的趋势方向
  267.    int firstBar = Bars - 1;
  268.    if(Close[firstBar] > up[firstBar])
  269.       TrendBuffer[firstBar] = 1;  // 上升趋势
  270.    else if(Close[firstBar] < down[firstBar])
  271.       TrendBuffer[firstBar] = -1; // 下降趋势
  272.    else
  273.       TrendBuffer[firstBar] = 1;  // 默认上升趋势
  274.    // 主计算循环
  275.    for(i = Bars - 2; i >= 0; i--)
  276.    {
  277.       // 趋势逻辑判断
  278.       if(Close > up[i+1])
  279.       {
  280.          TrendBuffer = 1; // 上升趋势
  281.       }
  282.       else if(Close < down[i+1])
  283.       {
  284.          TrendBuffer = -1; // 下降趋势
  285.       }
  286.       else
  287.       {
  288.          TrendBuffer = TrendBuffer[i+1]; // 保持前一根K线的趋势
  289.       }
  290.       // 设置K线颜色
  291.       SetCandleColor(TrendBuffer == 1, i);
  292.       // 更新上下轨
  293.       if(TrendBuffer == 1) // 上升趋势
  294.       {
  295.          // 如果是上升趋势,保持上轨不变,调整下轨
  296.          if(down < down[i+1])
  297.             down = down[i+1];
  298.          // 根据趋势线样式绘制
  299.          if(趋势线样式 == STYLE_LINES || 趋势线样式 == STYLE_LINES_DOTS)
  300.             TrendUp = down; // 绘制线段
  301.          if(趋势线样式 == STYLE_LINES_DOTS || 趋势线样式 == STYLE_DOTS)
  302.             TrendUpDots = down; // 绘制圆点
  303.          // 趋势变化检测
  304.          if(TrendBuffer[i+1] == -1)
  305.          {
  306.             // 趋势从下降转为上升,添加买入信号
  307.             BuySignal = down;
  308.             // 显示文字标签
  309.             if(显示文字)
  310.             {
  311.                objName = "Buy" + TimeToStr(Time);
  312.                next_bar_time = i > 0 ? Time[i-1] : Time + (Time - Time[i+1]);
  313.                ObjectCreate(objName, OBJ_TEXT, 0, next_bar_time, down - 文字偏移 * Point);
  314.                ObjectSetText(objName, 买入文字, g_text_size, "Arial", 买入颜色);
  315.                ObjectSet(objName, OBJPROP_ANCHOR, ANCHOR_TOP);
  316.             }
  317.          }
  318.       }
  319.       else // 下降趋势
  320.       {
  321.          // 如果是下降趋势,保持下轨不变,调整上轨
  322.          if(up > up[i+1])
  323.             up = up[i+1];
  324.          // 根据趋势线样式绘制
  325.          if(趋势线样式 == STYLE_LINES || 趋势线样式 == STYLE_LINES_DOTS)
  326.             TrendDown = up; // 绘制线段
  327.          if(趋势线样式 == STYLE_LINES_DOTS || 趋势线样式 == STYLE_DOTS)
  328.             TrendDownDots = up; // 绘制圆点
  329.          // 趋势变化检测
  330.          if(TrendBuffer[i+1] == 1)
  331.          {
  332.             // 趋势从上升转为下降,添加卖出信号
  333.             SellSignal = up;
  334.             // 显示文字标签
  335.             if(显示文字)
  336.             {
  337.                objName = "Sell" + TimeToStr(Time);
  338.                next_bar_time = i > 0 ? Time[i-1] : Time + (Time - Time[i+1]);
  339.                ObjectCreate(objName, OBJ_TEXT, 0, next_bar_time, up + 文字偏移 * Point);
  340.                ObjectSetText(objName, 卖出文字, g_text_size, "Arial", 卖出颜色);
  341.                ObjectSet(objName, OBJPROP_ANCHOR, ANCHOR_BOTTOM);
  342.             }
  343.          }
  344.       }
  345.    }
  346.    return(0);
  347. }
  348. //+------------------------------------------------------------------+
  349. //| 指标释放函数                                                      |
  350. //+------------------------------------------------------------------+
  351. int deinit()
  352. {
  353.    // 清除所有标签
  354.    ObjectsDeleteAll(0, OBJ_TEXT);
  355.    return(0);
  356. }
  357. //+------------------------------------------------------------------+
复制代码
微信图片_20260912202143_270_2.jpg
微信图片_20260912202144_271_2.jpg
微信图片_20260912202144_272_2.jpg
微信图片_20260912202144_272_2.jpg
微信图片_20260912202143_270_2.jpg
微信图片_20260912202144_271_2.jpg
举报

评论 使用道具

精彩评论1

ijlpyg
CC
| 发表于 昨天 22:33 | 显示全部楼层
还是不错的,看起来有进步的
举报

点赞 评论 使用道具

发新帖
EA交易
您需要登录后才可以评论 登录 | 立即注册