希望祠.同舟.量化.侦云取势势能研究中心

 找回密码
 立即注册

《周易.系辞》

君子藏器于身,待时而动!

《吕氏.春秋》

君子谋时而动,顺势而为!

《晏子·霸业因时而生》

识时务者为俊杰,通机变者为英豪!

《周易.系辞》

天之所助,吉无不利,天之所助者.顺天者也!

《史记.太史公自序》
天之势,浩浩汤汤,顺之者昌,逆之者亡!

《寒窑赋》

天不得时,日月无光.地 不得时,草木不长.

《周易.系辞》

刚柔者,立本者也!变通者,识时者也!

圣人云:

君子务本,本立而道生!

 《势胜学》

不知势,无以为人也.势易而未觉,必败焉!

《周易·系辞上传》:

富有之谓大业,日新之谓盛德!

《礼记.大学》:
格物致知,正心诚意,修身齐家!

《礼记.中庸》:

凡事预则立,不预则废!

查看: 1491|回复: 0

[【优秀推荐】曲线完美收益中等] rsi上涨继续,下跌继续源码

[复制链接]

773

主题

838

帖子

3021

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3021

最佳新人活跃会员热心会员推广达人荣誉管理副站长之印官方管理员常务

admin 发表于 2026-2-22 09:45:59 | 显示全部楼层 |阅读模式
//+------------------------------------------------------------------+
//|                                               RSIOMA_Final.mq4   |
//|                                  Copyright © 2025, EarnForex.com |
//|        Chinese Label & Cooldown Modification by AI Assistant     |
//|        Based on indicator by Kalenzo bartlomiej.gorski@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2025, EarnForex.com"
#property link      "https://www.earnforex.com"
#property version   "2.01_Final_CN"
#property strict

#property description "RSIOMA显示一个基于MA相对强弱构建的RSI。"
#property description "核心信号: '续涨' 和 '续跌' 已在K线上标注。"

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 clrBlue
#property indicator_type1  DRAW_LINE
#property indicator_width1 3
#property indicator_label1 "RSIOMA"
#property indicator_color2 clrRed
#property indicator_type2  DRAW_HISTOGRAM
#property indicator_label2 "下跌趋势"
#property indicator_color3 clrGreen
#property indicator_type3  DRAW_HISTOGRAM
#property indicator_label3 "上涨趋势"
#property indicator_color4 clrMagenta
#property indicator_type4  DRAW_HISTOGRAM
#property indicator_label4 "顶部反转预警"
#property indicator_color5 clrDodgerBlue
#property indicator_type5  DRAW_HISTOGRAM
#property indicator_label5 "底部反转预警"
#property indicator_color6 clrBlueViolet
#property indicator_type6  DRAW_LINE
#property indicator_label6 "MA of RSIOMA"

#property indicator_minimum -10
#property indicator_maximum 100

// --- 中文标注输入参数 ---
input string  GroupName               = "--- 中文K线标注设置 ---";
input bool    EnableChineseLabels     = true;         // 启用中文K线标注
// vvvvvvvvvv  这里是您要求的修改  vvvvvvvvvv
input string  UpContinueText          = "续涨";       // 上涨继续文字
input color   UpContinueColor         = clrLimeGreen;   // 上涨文字颜色
input string  DownContinueText        = "续跌";       // 下跌继续文字
// ^^^^^^^^^^  这里是您要求的修改  ^^^^^^^^^^
input color   DownContinueColor       = clrRed;         // 下跌文字颜色
input int     LabelDistance           = 20;           // 标签与K线的距离(点)
input int     SignalCooldown          = 10;           // 信号冷却K线数 (N根内只显示一次)
// --- 原始输入参数 ---
input string  OriginalGroupName       = "--- 原始指标参数 ---";
input int     RSIOMA                  = 14;           // RSIOMA 周期
input ENUM_MA_METHOD RSIOMA_MODE      = MODE_EMA;     // RSIOMA 模式
input ENUM_APPLIED_PRICE RSIOMA_PRICE = PRICE_CLOSE;  // RSIOMA 应用价格
input int     Ma_RSIOMA               = 21;           // MA of RSIOMA 周期
input ENUM_MA_METHOD Ma_RSIOMA_MODE   = MODE_EMA;     // MA of RSIOMA 模式
input int     BuyTrigger              = 20;
input int     SellTrigger             = 80;
input int     MainTrendLong           = 50;
input int     MainTrendShort          = 50;
input bool    MainAlerts              = false;
input bool    AuxiliaryAlerts         = false;
input bool    EnableNativeAlerts      = false;
input bool    EnableEmailAlerts       = false;
input bool    EnablePushAlerts        = false;
input bool    EnableSoundAlerts       = false;
input string  SoundFile               = "alert.wav";
input ENUM_TIMEFRAMES TimeFrame        = PERIOD_CURRENT; // 时间周期

// Indicator buffers:
double RSIBuffer[];
double PosBuffer[];
double NegBuffer[];
double bdn[], bup[]; // bdn - Red(下跌), bup - Green(上涨)
double sdn[], sup[]; // sdn - Magenta(顶部预警), sup - DodgerBlue(底部预警)
double marsioma[];

// Global variables:
string   short_name;
string   obj_prefix; // 对象唯一前缀,用于清理
datetime LastAlertTime = D'01.01.1970';

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
{
    SetIndexBuffer(0, RSIBuffer);
    SetIndexBuffer(1, bdn);
    SetIndexBuffer(2, bup);
    SetIndexBuffer(3, sdn);
    SetIndexBuffer(4, sup);
    SetIndexBuffer(5, marsioma);
    SetIndexBuffer(6, PosBuffer);
    SetIndexBuffer(7, NegBuffer);

    SetIndexDrawBegin(0, RSIOMA);
    SetIndexDrawBegin(1, RSIOMA);
    SetIndexDrawBegin(2, RSIOMA);
    SetIndexDrawBegin(3, RSIOMA);
    SetIndexDrawBegin(4, RSIOMA);
    SetIndexDrawBegin(5, RSIOMA);

    short_name = StringConcatenate("RSIOMA(", RSIOMA, ")");
    IndicatorShortName(short_name);
    IndicatorSetInteger(INDICATOR_DIGITS, 2);

    // 创建唯一的对象前缀,防止不同图表/周期的对象冲突
    obj_prefix = short_name + "_" + (string)ChartID() + "_";
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
    // 清理本指标实例创建的所有图表对象
    ObjectsDeleteAll(0, obj_prefix);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    if(Bars <= RSIOMA) return(0);

    int limit;
    if(prev_calculated == 0 || rates_total < prev_calculated)
    {
        limit = rates_total - RSIOMA - 2;
    }
    else
    {
        limit = rates_total - prev_calculated;
    }

    for(int i = limit; i >= 0; i--)
    {
        double sumn = 0.0, sump = 0.0;
        double rel, negative, positive;

        double ccma = iMA(Symbol(), TimeFrame, RSIOMA, 0, RSIOMA_MODE, RSIOMA_PRICE, i);
        double ppma = iMA(Symbol(), TimeFrame, RSIOMA, 0, RSIOMA_MODE, RSIOMA_PRICE, i + 1);
        rel = ccma - ppma;

        if(rel > 0) sump = rel;
        else sumn = -rel;

        positive = (PosBuffer[i + 1] * (RSIOMA - 1) + sump) / RSIOMA;
        negative = (NegBuffer[i + 1] * (RSIOMA - 1) + sumn) / RSIOMA;

        PosBuffer = positive;
        NegBuffer = negative;

        bup = 0; bdn = 0; sdn = 0; sup = 0;

        if(negative != 0.0)
        {
            RSIBuffer = 100.0 - 100.0 / (1.0 + positive / negative);

            if(RSIBuffer > MainTrendLong) bup = -10;
            if(RSIBuffer < MainTrendShort) bdn = -10;
            if(RSIBuffer < BuyTrigger && RSIBuffer > RSIBuffer[i + 1]) sup = -10;
            if(RSIBuffer > SellTrigger && RSIBuffer < RSIBuffer[i + 1]) sdn = -10;
        }
        else
        {
            RSIBuffer = 0.0;
        }
    }

    for(int i = limit; i >= 0; i--)
    {
        marsioma = iMAOnArray(RSIBuffer, 0, Ma_RSIOMA, 0, Ma_RSIOMA_MODE, i);
    }

    // --- 中文标注和冷却逻辑 ---
    if(EnableChineseLabels)
    {
      static int upCooldown = 0;
      static int downCooldown = 0;

      int check_limit = rates_total - prev_calculated;
      if (prev_calculated == 0) check_limit = rates_total - 2;

      for(int i = check_limit; i >= 1; i--)
      {
         // 每一根K线都减少冷却计数器
         if(upCooldown > 0) upCooldown--;
         if(downCooldown > 0) downCooldown--;

         // 信号1: 上涨继续 (前一根洋红 -> 当前绿色) 并且冷却结束
         if(sdn[i+1] != 0 && bup != 0 && upCooldown == 0)
         {
            string obj_name = obj_prefix + "UpCont_" + (string)Time;
            DrawLabel(obj_name, i, High + LabelDistance * _Point, UpContinueText, UpContinueColor, ANCHOR_LEFT);
            // 重置冷却计数器
            upCooldown = SignalCooldown;
         }

         // 信号2: 下跌继续 (前一根浅蓝 -> 当前红色) 并且冷却结束
         if(sup[i+1] != 0 && bdn != 0 && downCooldown == 0)
         {
            string obj_name = obj_prefix + "DownCont_" + (string)Time;
            DrawLabel(obj_name, i, Low - LabelDistance * _Point, DownContinueText, DownContinueColor, ANCHOR_RIGHT);
            // 重置冷却计数器
            downCooldown = SignalCooldown;
         }
      }
    }

    // --- 原始警报逻辑 (保持不变) ---
    if(Time[0] > LastAlertTime)
    {
        int candle = 1; // 检查已收盘的K线
        string Text;
        if (MainAlerts)
        {
            if ((RSIBuffer[candle] < SellTrigger) && (RSIBuffer[candle + 1] >= SellTrigger))
            {
                Text = "RSIOMA: " + Symbol() + " - " + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()), 7) + " - Crossed " + IntegerToString(SellTrigger) + " from above.";
                IssueAlerts(Text);
            }
            if ((RSIBuffer[candle] > BuyTrigger) && (RSIBuffer[candle + 1] <= BuyTrigger))
            {
                Text = "RSIOMA: " + Symbol() + " - " + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()), 7) + " - Crossed " + IntegerToString(BuyTrigger) + " from below.";
                IssueAlerts(Text);
            }
        }
        if (AuxiliaryAlerts)
        {
            if ((sdn[candle] != 0) && (sdn[candle + 1] == 0))
            {
                Text = "RSIOMA: " + Symbol() + " - " + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()), 7) + " - Bearish reversal imminent.";
                IssueAlerts(Text);
            }
            if ((sup[candle] != 0) && (sup[candle + 1] == 0))
            {
                Text = "RSIOMA: " + Symbol() + " - " + StringSubstr(EnumToString((ENUM_TIMEFRAMES)Period()), 7) + " - Bullish reversal imminent.";
                IssueAlerts(Text);
            }
        }
    }

    return(rates_total);
}

//+------------------------------------------------------------------+
//| Helper functions                                                 |
//+------------------------------------------------------------------+
void IssueAlerts(string text)
{
    if (EnableNativeAlerts) Alert(text);
    if (EnableEmailAlerts) SendMail("RSIOMA Alert", text);
    if (EnablePushAlerts) SendNotification(text);
    if (EnableSoundAlerts) PlaySound(SoundFile);
    LastAlertTime = Time[0];
}

void DrawLabel(string obj_name, int bar, double price, string text, color clr, ENUM_ANCHOR_POINT anchor)
{
    if(ObjectFind(0, obj_name) < 0)
    {
        ObjectCreate(0, obj_name, OBJ_TEXT, 0, Time[bar], price);
        ObjectSetString(0, obj_name, OBJPROP_TEXT, text);
        ObjectSetInteger(0, obj_name, OBJPROP_COLOR, clr);
        ObjectSetInteger(0, obj_name, OBJPROP_FONTSIZE, 10);
        ObjectSetString(0, obj_name, OBJPROP_FONT, "Arial");
        ObjectSetInteger(0, obj_name, OBJPROP_ANCHOR, anchor);
        ObjectSetInteger(0, obj_name, OBJPROP_SELECTABLE, false);
    }
}
//+------------------------------------------------------------------+


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /5 下一条

人生者,生存也。
生存所需,财(钱)官(权)也。
财自食伤(子孙福德爻)生,
印星生身荫护,官财印全,适合人间生存也。
若命缺财,缺官,则失去生存权。
一旦脱离父母养育,将天天面临,
从绝望中醒来,又从绝望中睡去。来去如烟!

Archiver|小黑屋|希望祠.同舟.量化

GMT+8, 2026-4-2 06:24 , Processed in 0.021365 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表