Py学习  »  Python

Python编程学动量自动化:Delta周期循环主图指标公式

量化对冲套利 • 11 月前 • 344 次点击  

朋友们大家好今天来学习这个Delta周期循环主图通达信指标算法,注意本指标源码用于通达信,友情情提示:代码较多,一定要复制全。本文所述模型算法仅限学术探讨,指标公式作为知识免费分享,"基于开源数据集的理论推演",仅用于学习交流。

图片
图片
风险提示:本指标仅供技术研究与学习交流使用。市场具有高度不确定性,任何基于本指标的决策都需要自行承担风险,不构成任何投资建议。

01 指标图例


图一

图二



02 Py源码


下方代码仅用于学习交流使用。

import pandas as pdimport numpy as npimport mplfinance as mpffrom datetime import datetime, timedeltadef calculate_lunar_indicators(df):    """    计算农历相关技术Delta周期循环主图指标公式    """    # 常量定义    CSR = 3    synodic_month = 29.53058782  # 朔望月周期    # 计算日期序列 (从固定起点开始的天数)    base_date = pd.Timestamp('1899-12-30')  # 兼容Excel日期系统    df['RIC'] = (df.index - base_date).days + 1    # 核心指标计算    df['IS'] = ((df['RIC'] + 3) / synodic_month).astype(int)    fractional_part = (df['RIC'] + CSR) / synodic_month - np.floor((df['RIC'] + CSR) / synodic_month)    df['YYS'] = fractional_part * synodic_month    df['NLRI'] = np.where(df['YYS'] 1, np.round(df['YYS'] + synodic_month), np.floor(df['YYS']))    df['NUM'] = df['IS'] % 4    # 相位变化检测    df['NLRI_prev'] = df['NLRI'].shift(1)    df['ISCOLOR'] = ((df['NLRI_prev'] >= df['NLRI']) | (df['NLRI'] == 1)).astype(int)    # 江恩日计算    condition = (df['ISCOLOR'] == 1) & (df['NUM'] == 2)    df['江恩日'] = condition[::-1].cumsum()[::-1] - condition[::-1].cumsum()[::-1].where(~condition).ffill().fillna(0) + 1    df['江恩日'] = df['江恩日'].where(condition, 0)    # 基准线计算    df['基准'] = (df['RIC'] + 2) / synodic_month + 0.0016    df['ISFIRSTA'] = df['基准'].astype(int)    df['SCOLORA'] = ((df['基准'].shift(1) < df['ISFIRSTA']) &                    (df['基准'] + 0.1 >= df['ISFIRSTA'])) | (df['基准'] == df['ISFIRSTA'])    # 相位分类    phase = (df['基准'] + 0.1).astype(int) % 4    df['A0'] = (df['ISCOLOR'] == 1) & (phase == 1)    df['A1'] = (df['ISCOLOR'] == 1) & (phase == 2)    df['A2'] = (df['ISCOLOR'] == 1) & (phase == 3)    df['A3'] = (df['ISCOLOR'] == 1) & (phase == 0)    return dfdef plot_lunar_chart(df, start_date=None, end_date=None):    """    绘制带有农历指标的K线图    """    # 截取指定时间范围    if start_date or end_date:        start = start_date or df.index[0]        end = end_date or df.index[-1]        plot_df = df.loc[start:end].copy()    else:        plot_df = df.copy()    # 创建绘图对象    apds = [        # 江恩日标记        mpf.make_addplot(plot_df['江恩日'], panel=1, color='purple', ylabel='江恩日'),    ]    # 创建彩色标记    colors = {        0'yellow',        1

'red',        2'green',        3'white'    }    # 添加相位标记    markers = []    for num, color in colors.items():        condition = (plot_df['ISCOLOR'] == 1) & (plot_df['NUM'] == num)        markers.append(mpf.make_addplot(            plot_df['close'].where(condition),             type='scatter'            marker='o',            markersize=50,            color=color,            alpha=0.3        ))    apds.extend(markers)    # 绘制图表    mpf.plot(plot_df,              type='candle'             addplot=apds,             style='charles',             title='农历周期分析',             ylabel='价格',             volume=False,             figratio=(128),             show_nontrading=False)# ===================== 使用示例 =====================if __name__ == "__main__":    # 1. 准备数据 (这里用模拟数据,实际使用时替换为真实数据)    dates = pd.date_range(start='2023-01-01', end='2024-01-01', freq='D')    data = {        'open': np.random.uniform(100200len(dates)),        'high': np.random.uniform(200300len(dates)),        'low': np.random.uniform(50100len(dates)),        'close': np.random.uniform(100200len(dates)),        'volume': np.random.randint(1000050000len(dates))    }    df = pd.DataFrame(data, index=dates)    # 2. 计算指标    df = calculate_lunar_indicators(df)    # 3. 可视化 (显示最后90天)    end_date = df.index[-1]    start_date = end_date - timedelta(days=90)    plot_lunar_chart(df, start_date=start_date)



学习分享



学术交流:我们专注于市场的学术交流与研究包括自用指标零滞后均线最速曲线,以及自动化程序量化学习。

分享知识:田都元帅,敬神常在,爱人如己,我为人人,与人为善,分享快乐!


Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/183063