Thư viện lớp vẽ phiên bản Python (Tương thích 2/3)

Tác giả:Giấc mơ nhỏ, Ngày: 2017-04-10 17:20:26
Tags:Công cụPythonBiểu đồ

Điều này đơn giản hóa logic của đường vẽ biểu đồ chính sách, bạn có thể trực tiếp gọi các hàm đã được gói.

  • Hỗ trợ vẽ nhiều đường
  • Hỗ trợ K-thread
  • Hỗ trợ biểu tượng cờ nhỏ
  • Bạn cũng có thể thêm hỗ trợ nhiều hình ảnh hơn.

Chức năng tương đương với phiên bản JS

Chuyển sang phiên bản JS

Nếu bạn có bất kỳ câu hỏi nào, hãy liên hệ với 359706687


# Python 2/3 兼容版本
import time
chart = None
series = []
labelIdx = {}
preBarTime = 0
preFlagTime = 0
preDotTime = {}

cfg = {
    "tooltip" : {
        "xDateFormat" : "%Y-%m-%d %H:%M:%S, %A"
    },
    "legend" : {
        "enabled" : True
    },
    "plotOptions" : {
        "candlestick" : {
            "color" : "#d75442",
            "upColor" : "#6ba583"
        }
    },
    "rangeSelector" : {
        "buttons" : [{
            "type" : "hour",
            "count" : 1,
            "text" : "1h",
        }, {
            "type" : 'hour',
            "count" : 3,
            "text" : "3h"
        }, {
            "type" : "hour",
            "count" : 8,
            "text" : "8h"
        }, {
            "type" : "all",
            "text" : "All"
        }],
        "selected" : 2,
        "inputEnabled" : True
    },
    "series" : series,
}

def GetCfg():
    global cfg
    return cfg

# 画水平线
def PlotHLine(value = None, label = None, color = None, style = None):
    global cfg, chart
    if ("yAxis" in cfg) == False :
        cfg.setdefault("yAxis", {"plotLines" : []})
    elif ("plotLines" in cfg["yAxis"]) == False :
        cfg["yAxis"].setdefault("plotLines", [])
    
    obj = {
        "value" : value,
        "color" : color or "red",
        "width" : 2,
        "dashStyle" : style or "Solid",
        "label" : {
            "text" : label or "",
            "align" : "center"
        }
    }
    found = False
    for i in range(len(cfg["yAxis"]["plotLines"])) : 
        if cfg["yAxis"]["plotLines"][i]["label"]["text"] == label : 
            cfg["yAxis"]["plotLines"][i] = obj
            found = True
    if not found :
        cfg["yAxis"]["plotLines"].append(obj)
    if not chart :
        chart = Chart(cfg)
        chart.update(cfg)    # 更新图表
    else :
        chart.update(cfg)

# 画K线
def PlotRecords(records, title = None):
    global labelIdx, series, preBarTime, chart
    if not chart :
        chart = Chart(cfg)
        chart.reset()
    if ("candlestick" in labelIdx) == False : 
        cfg["__isStock"] = True
        seriesIdx = len(series)
        series.append({
            "type" : "candlestick",
            "name" : "" if title == None else title,
            "id" : "primary",
            "data" : []
            })
        chart.update(cfg)
        labelIdx.setdefault("candlestick", seriesIdx)
    else :
        seriesIdx = labelIdx["candlestick"]
    if isinstance(records, dict) and ("Time" in records) == True :
        Bar = records
        if Bar["Time"] == preBarTime :
            chart.add(seriesIdx, [Bar["Time"], Bar["Open"], Bar["High"], Bar["Low"], Bar["Close"]], -1)
        elif Bar["Time"] > preBarTime : 
            preBarTime = Bar.Time
            chart.add(seriesIdx, [Bar["Time"], Bar["Open"], Bar["High"], Bar["Low"], Bar["Close"]])    
    else :
        for i in range(len(records)) :
            if records[i]["Time"] == preBarTime :
                chart.add(seriesIdx, [records[i]["Time"], records[i]["Open"], records[i]["High"], records[i]["Low"], records[i]["Close"]], -1)
            elif records[i]["Time"] > preBarTime :
                preBarTime = records[i]["Time"]
                chart.add(seriesIdx, [records[i]["Time"], records[i]["Open"], records[i]["High"], records[i]["Low"], records[i]["Close"]])
    return chart

# 画指标线
def PlotLine(label, dot, Ntime = None):
    global labelIdx, chart, series, preDotTime
    if not chart :
        cfg.setdefault("xAxis", {
            "type" : "datetime"
            })
        chart = Chart(cfg)
        chart.reset()
    if (label in labelIdx) == False :
        seriesIdx = len(series)
        preDotTime.setdefault(str(seriesIdx), 0)
        labelIdx[label] = seriesIdx
        series.append({
            "type" : "line",
            "yAxis" : 0,
            "showInLegend" : True,
            "name" : label,
            "data" : [],
            "tooltip" : {"valueDecimals" : 5}
            })
        chart.update(cfg)
    else :
        seriesIdx = labelIdx[label]
    if Ntime == None :
        Ntime = _N(time.time() * 1000, 0)
    if preDotTime[str(seriesIdx)] != Ntime :
        preDotTime[str(seriesIdx)] = Ntime
        chart.add(seriesIdx, [Ntime, dot])
    else :
        chart.add(seriesIdx, [Ntime, dot], -1)
    return chart

# 画标记
def PlotFlag(time, text, title, shape = "", color = ""):
    global chart, cfg, labelIdx, preFlagTime
    if not chart :
        chart = Chart(cfg)
        chart.reset()
    label = "flag"
    if (label in labelIdx) == False : 
        seriesIdx = len(series)
        labelIdx[label] = seriesIdx
        series.append({
            "type" : "flags",
            "onSeries" : "primary",
            "data" : []
            })
        chart.update(cfg)
    else :
        seriesIdx = labelIdx[label]
    obj = {
        "x" : time,
        "color" : color,
        "shape" : shape,
        "title" : title,
        "text" : text
    }
    if preFlagTime != time : 
        preFlagTime = time
        chart.add(seriesIdx, obj)
    else :
        chart.add(seriesIdx, obj, -1)
    return chart

# 设置图表标题
def PlotTitle(title, chartTitle = None):
    global cfg
    if ("subtitle" in cfg) == True : 
        cfg["subtitle"] = {"text" : title}
    else :
        cfg.setdefault("subtitle", {"text" : title})
    if chartTitle != None :
        if (title in cfg) == True :
            cfg["title"] = {"text" : chartTitle}
        else :
            cfg.setdefault("title", {"text" : chartTitle})
    if chart :
        chart.update(cfg)

# 导出函数
ext.GetCfg = GetCfg
ext.PlotHLine = PlotHLine
ext.PlotRecords = PlotRecords
ext.PlotLine = PlotLine
ext.PlotFlag = PlotFlag
ext.PlotTitle = PlotTitle

# 测试代码
def main():
    isFirst = True
    while True:
        records = exchange.GetRecords()
        if records and len(records) > 0 :
            ext.PlotRecords(records, "BTC")
            if isFirst :
                ext.PlotFlag(records[-1]["Time"], "Start", "S")
                isFirst = False
                ext.PlotHLine(records[-1]["Close"], "Close")
        ticker = exchange.GetTicker()
        if ticker :
            ext.PlotLine("Last", ticker.Last)
            ext.PlotLine("buy", ticker.Buy + 10)
            ext.PlotTitle("Last" + str(ticker.Last))
        Sleep(60000)



Có liên quan

Thêm nữa

churchillxyXin chào, khi tôi gọi PlotFlag, sau khi làm mới trang, tôi thường mất một lá cờ đã được vẽ, có giải pháp cho vấn đề này không?

churchillxyTôi có thể tập hợp một số đường k vào một biểu đồ không? Ví dụ: đường k của btc + đường k của ETH

m0606Có vẻ như là một thư viện lớp vạch tương thích với Python hai phần ba...

lwc87Giáo viên, bạn có hướng dẫn sử dụng hàm mẫu này không?

haorenBạn có thể tìm thấy một biểu đồ tùy chỉnh không? Tập hợp một số đường k vào một biểu tượng; ví dụ: đường giá BTC + đường giá LTC + đường giá ETH + đường giá tổng hợp đa đồng tiền, và như vậy, chỉ có một vài đường trên một biểu đồ, giống như một vài đường giá đồng trên biểu đồ K, tất cả đều hiển thị trên cùng một biểu đồ

LogaCảm ơn!

Giấc mơ nhỏVâng, một số tham số không thể truyền sai.

churchillxyVấn đề đã được giải quyết, số lượng tiêu đề là hạn chế, văn bản có thể phong phú, không bị lộn xộn, cảm ơn.

Giấc mơ nhỏMột dấu hiệu đã được vẽ, được cho là không nên bị mất, đã được vẽ, nếu bạn không thay đổi nó, nó sẽ không thay đổi, nó là cố định.

Giấc mơ nhỏTrên biểu đồ, bạn chỉ đánh dấu số lượng dữ liệu hiển thị trên mỗi trang FMZ, nếu một biểu đồ có 100.000 dữ liệu, bạn đặt ở đây để hiển thị 10.000 dữ liệu trên mỗi trang, 100.000 dữ liệu sẽ hiển thị 10 trang riêng biệt.

churchillxyNếu tôi có dữ liệu vượt quá giới hạn này, tôi sẽ hiển thị nó như thế nào? /upload/asset/245f7442dfa3212019b47.png

Giấc mơ nhỏBạn có thể sử dụng thư viện đường vẽ đa biểu đồ mới nhất.

Giấc mơ nhỏTrong mẫu, hàm main là một ví dụ sử dụng, bạn có thể xem.

Giấc mơ nhỏCảm ơn sự hỗ trợ của bạn!