
等级: 新手上路
- 注册:
- 2025-2-10
- 曾用名:
|
麻烦把以下程序的符号修正
# -*- 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);#
|
|