金字塔决策交易系统

 找回密码
 

微信登录

微信扫一扫,快速登录

搜索
查看: 768|回复: 1

老师好,提示符号不正确

[复制链接]

1

主题

1

帖子

1

积分

Rank: 1

等级: 新手上路

注册:
2025-2-10
曾用名:
发表于 2025-2-10 18:26 | 显示全部楼层 |阅读模式
麻烦把以下程序的符号修正

# -*- coding: utf-8 -*-
import numpy as np;
import pandas as pd;

def calculate_fractal(data):#
    class Fractal:
        def __init__(self, index, price, type_):#
            self.index = index;  # K线位置
            self.price = price;  # 价格
            self.type = type_;   # 类型(顶分型/底分型)

    fractals = [];#

    def process_containing(k1, k2):#
        if k1['high'] >= k2['high'] and k1['low'] <= k2['low']:#
            return {'high':k1['high'], 'low':k1['low']};#
        elif k2['high'] >= k1['high'] and k2['low'] <= k1['low']:#
            return {'high':k2['high'], 'low':k2['low']};#
        return None;#

    merged_data = [];#
    current = data[0];#
    for i in range(1, len(data)):#
        merged = process_containing(current, data[i]);#
        if merged:#
            current = merged;#
        else:#
            merged_data.append(current);#
            current = data[i];#
    merged_data.append(current);#

    for i in range(1, len(merged_data)-1):#
        prev = merged_data[i-1];#
        curr = merged_data[i];#
        next_ = merged_data[i+1];#

        if (curr['high'] > prev['high'] and
            curr['high'] > next_['high'] and
            (prev['high'] < curr['high'] or next_['high'] < curr['high'])):#
            fractals.append(Fractal(i, curr['high'], 'top'));#

        elif (curr['low'] < prev['low'] and
              curr['low'] < next_['low'] and
              (prev['low'] > curr['low'] or next_['low'] > curr['low'])):#
            fractals.append(Fractal(i, curr['low'], 'bottom'));#

    return fractals;#

def generate_strokes(fractals):#
    strokes = [];#
    last_fractal = None;#
    for f in fractals:#
        if not last_fractal:#
            last_fractal = f;#
            continue;#

        if (f.type == 'top' and last_fractal.type == 'bottom' and
            f.price > last_fractal.price * 1.002):  # 至少0.2%波动 #
            strokes.append(('up', last_fractal.index, f.index));#
            last_fractal = f;#
        elif (f.type == 'bottom' and last_fractal.type == 'top' and
              f.price < last_fractal.price * 0.998):#
            strokes.append(('down', last_fractal.index, f.index));#
            last_fractal = f;#
    return strokes;#

def find_zhongshu(strokes, data):#
    overlaps = [];#
    window_size = 5;  # 默认扫描窗口 #

    for i in range(len(strokes)-3):#
        seg1 = strokes[i];#
        seg2 = strokes[i+1];#
        seg3 = strokes[i+2];#

        ranges = [
            (min(data[seg1[1]:seg1[2]]['low']), max(data[seg1[1]:seg1[2]]['high'])),
            (min(data[seg2[1]:seg2[2]]['low']), max(data[seg2[1]:seg2[2]]['high'])),
            (min(data[seg3[1]:seg3[2]]['low']), max(data[seg3[1]:seg3[2]]['high']))
        ];#

        overlap_low = max(r[0] for r in ranges);#
        overlap_high = min(r[1] for r in ranges);#
        if overlap_low < overlap_high:#
            overlaps.append((overlap_low, overlap_high));#

    return overlaps;#

def plot_indicator(context):#
    data = context.get_data(500);  # 获取500根K线数据 #

    fractals = calculate_fractal(data);#
    strokes = generate_strokes(fractals);#
    zhongshu = find_zhongshu(strokes, data);#

    if zhongshu:#
        latest_zhongshu = zhongshu[-1];#
        context.plot_line(latest_zhongshu[0], '支撑边界', color='green');#
        context.plot_line(latest_zhongshu[1], '压力边界', color='red');#

    if len(zhongshu) >= 2:#
        if zhongshu[-1][0] > zhongshu[-2][1]:#
            context.plot_text("趋势方向:▲", color='red');#
        elif zhongshu[-1][1] < zhongshu[-2][0]:#
            context.plot_text("趋势方向:▼", color='green');#

@indicator(title='缠论结构分析', overlay=False)
def run(context):#
    plot_indicator(context);#

回复

使用道具 举报

44

主题

1万

帖子

1万

积分

Rank: 8Rank: 8

等级: 超级版主

注册:
2021-5-18
曾用名:
发表于 2025-2-11 09:13 | 显示全部楼层
抱歉不提供python转换服务
金字塔提供一对一VIP专业技术指导服务,技术团队实时响应您的日常使用问题与策略编写。联系电话:021-20339086
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 微信登录

本版积分规则

手机版|小黑屋|上海金之塔信息技术有限公司 ( 沪ICP备13035422号 )

GMT+8, 2025-7-17 05:31 , Processed in 0.209359 second(s), 27 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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