Example #1
0
File: T.py Project: icharm/quantc
def draw(df, xf, yf, tf, title='New Stock Chart'):
    '''
    Draw hight stock chart by pandas.DataFrame
    Args:
        df: dataFrame
        xf: field name of dataFrame to x coordinate, the value of x must be unique datetime.
        yf: field name of dataFrame to y coordinate.
        tf: field name of dataFrame to line title.
    '''
    uniques = df[tf].unique()
    # log.debug(str(uniques))
    c = Highstock()
    for unique in uniques:
        sel_df = df[df[tf] == unique]
        sel_df = sel_df.loc[:, [xf, yf]]
        xyl = xy(sel_df)
        c.add_data_set(xyl, series_type='line', name=unique)

    options = {
        'title': {
            'text': title
        },
    }
    c.set_dict_options(options)
    c.save_file()
    log.info('Successful completion of drawing.')
Example #2
0
# -*- coding: utf-8 -*-
"""
Highstock Demos
Compare multiple series: http://www.highcharts.com/stock/demo/compare
"""

from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader

H = Highstock()

names = ["MSFT", "AAPL", "GOOG"]

for name in names:
    data_url = "http://www.highcharts.com/samples/data/jsonp.php?filename=" + name.lower() + "-c.json&callback=?"
    data = jsonp_loader(data_url, sub_d=r"(\/\*.*\*\/)")

    H.add_data_set(data, "line", name)


options = {
    "rangeSelector": {"selected": 4},
    "yAxis": {
        "labels": {
            "formatter": "function () {\
                            return (this.value > 0 ? ' + ' : '') + this.value + '%';\
                        }"
        },
        "plotLines": [{"value": 0, "width": 2, "color": "silver"}],
    },
    "plotOptions": {"series": {"compare": "percent"}},
Example #3
0
    def plot(self,
             title=False,
             width=600,
             height=300,
             load_js=None,
             js_sources=[]):
        chart_height = (height + 50) * 1 - 50 + 200
        H = Highstock(width=width, height=chart_height)
        groupingUnits = [
            ['hour', [60]],
        ]

        with open(
                os.path.join(os.path.dirname(os.path.abspath(__file__)),
                             "files/load.jinja")) as f:
            load_tempate = jinja2.Template(f.read())

        options = {
            'rangeSelector': {
                'buttons': [{
                    'type': 'minute',
                    'count': 180,
                    'text': '3h'
                }, {
                    'type': 'minute',
                    'count': 360,
                    'text': '6h'
                }, {
                    'type': 'minute',
                    'count': 720,
                    'text': '12h'
                }, {
                    'type': 'minute',
                    'count': 60 * 24,
                    'text': '1d'
                }, {
                    'type': 'minute',
                    'count': 60 * 48,
                    'text': '2d'
                }, {
                    'type': 'second',
                    'count': 1,
                    'text': '1s'
                }, {
                    'type': 'second',
                    'count': 10,
                    'text': '10s'
                }],
                'selected':
                4,
                'inputEnabled':
                False
            },
            'plotOptions': {
                'series': {
                    'turboThreshold:': 0,
                    'dataGrouping': {
                        'enabled': False,
                    },
                    'animation': False
                }
            },
            'navigator': {
                'enabled': True,
                'height': 20
            },
            'chart': {
                'animation': False,
            },
            'legend': {
                'enabled': True,
            },
            'yAxis': [],
            'title': {
                'text': '',
                'style': {
                    'display': 'none'
                }
            },
        }
        if title:
            options['title'] = {'text': title}

        if load_js:
            load_js = load_tempate.render(symbols=self.symbols)
            options['event']['load'] = load_js

        symbol_data = {}
        for idx, (symbol, data) in enumerate(self.symbols):
            symbol_data[symbol] = data

            options['yAxis'].append({
                'labels': {
                    'align': 'right',
                    'x': -3
                },
                'title': {
                    'text': 'y'
                },
                'lineWidth': 2,
                'offset': 0,
            })

            H.add_data_set(data, 'line', symbol, dataGrouping={})

        H.set_dict_options(options)
        H.add_JSscript(self.theme, "head")
        for js_source in js_sources:
            H.add_JSsource(js_source)
        return H
Highstock Demos
Flags marking events: http://www.highcharts.com/stock/demo/flags-general
"""

"""
This example generates the same chart as flags-general.py
But instead of copying from the website, the dataset is queried direcly using jsonp_loader
The json doc from the url is not in the correct format (lack of quotes), so the sub_d and sub_by parameters
are used to fix the problem.
"""

import datetime
from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader

H = Highstock()

data_url = "http://www.highcharts.com/samples/data/jsonp.php?filename=usdeur.json&callback=?"
data = jsonp_loader(
    data_url, sub_d=r"(Date\.UTC\(([0-9]+,[0-9]+,[0-9]+)(,[0-9]+,[0-9]+,[0-9]+)?(,[0-9]+)?\))", sub_by=r'"\1"'
)  # data from url is not in right json format

data2 = [
    {"x": datetime.datetime(2015, 6, 8), "title": "C", "text": "Stocks fall on Greece, rate concerns; US dollar dips"},
    {
        "x": datetime.datetime(2015, 6, 12),
        "title": "D",
        "text": "Zimbabwe ditches 'worthless' currency for the US dollar ",
    },
    {"x": datetime.datetime(2015, 6, 19), "title": "E", "text": "US Dollar Declines Over the Week on Rate Timeline"},
    {
        second = [(s.days * 24 * 60 * 60 + s.seconds) * 1000 for s in second]

    else:
        if isinstance(date, str):
            date = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
        elif isinstance(date, datetime.date):
            date = datetime.datetime.combine(date,
                                             datetime.datetime.min.time())
        date = date + datetime.timedelta(minutes=1)
        second = date - datetime.datetime(1970, 1, 1, 0, 0, 0)
        second = (second.days * 24 * 60 * 60 + second.seconds) * 1000

    return second


chart = Highstock()

data = Load.FinData(dataset='TaiwanStockPrice',
                    select='2330',
                    date='2018-10-10')

print(' change type of dataframe to highcharts ')
data['date'] = date2millisecond(data['date'])
list_data = []
for i in range(len(data)):
    tem = [
        int(data.loc[i, 'date']),
        float(data.loc[i, 'max']),
        float(data.loc[i, 'min'])
    ]
    list_data.append(tem)
Example #6
0
# -*- coding: utf-8 -*-
"""
Highstock Demos
OHLC: http://www.highcharts.com/stock/demo/ohlc
"""

from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlc.json&callback=?'
data = jsonp_loader(data_url, sub_d=r'(\/\*.*\*\/)')

H.add_data_set(
    data,
    'ohlc',
    'AAPL Stock Price',
    dataGrouping={
        'units': [
            [
                'week',  # unit name
                [1]  # allowed multiples
            ],
            ['month', [1, 2, 3, 4, 6]]
        ]
    })

options = {
    'rangeSelector': {
        'selected': 2
    },
Example #7
0
but use jsonp_loader from highstock_helper instead of add_data_from_jsonp

jsonp_loader(url, prefix_regex=r'^(.*\()', suffix_regex=r'(\);)$', sub_d=None, sub_by='')
jsonp_loader is to request (JSON) data from a server in a different domain (JSONP) 
and covert to python readable data. 
    1. url is the url (https) where data is located
    2. "prefix_regex" and "suffix_regex" are regex patterns used to 
        remove JSONP specific prefix and suffix, such as callback header: "callback(" and end: ");", 
    3. "sub_d" is regex patterns for any unwanted string in loaded json data (will be replaced by sub_by). 
    4. "sub_by" is the string to replace any unwanted string defined by sub_d
For function coverstion, such as Data.UTC to datetime.datetime, please check JSONPDecoder
"""

from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?'
data = jsonp_loader(
    data_url,
    sub_d=r'(\/\*.*\*\/)')  # to remove the comment in json doc from the url

options = {
    'rangeSelector': {
        'selected': 1
    },
    'title': {
        'text': 'AAPL Stock Price'
    },
}
def pplot_from_df(df, kind='line', stock=False, y_axes=None, debug=False, **kwargs):
    """

    :param df:    (pd.DataFrame): Column names become series names
    :param kind:   (str or list): Use list of same length as number of cols for multiple plot types
                                 (e.g. ['line_w_col', 'column'])
    :param stock:
    :param y_axes:
    :param debug:         (bool): Pretty prints options json
    :param kwargs:            (): Plotting options such as resolution, labels, color, etc.
    :return:
    """

    # If series given instead of DataFrame, convert:
    df = pd.DataFrame(df)

    # If no columns, do nothing:
    if df.shape[1] == 0:
        print("nothing to plot...")
        return

    # Create a color list for each line to plot:
    if 'colors' not in kwargs:
        kwargs['colors'] = map(
            lambda val: val.get_hex_l(),
            list(Color('#1492FB').range_to(Color('#27662A'), df.shape[1]))
        )

    # Set y axis
    if y_axes is None:
        y_axes = [1] * df.shape[1]
    else:
        assert (len(y_axes) == df.shape[1]), "y_axes must be list of same length as dataframe columns"
        # Should also assert something about the values in this list... later

    # Create highcharts_df object:
    # (Use height and width if provided):
    if stock:
        hc_visualization = Highstock(**{key: kwargs.pop(key) for key in ['width', 'height'] if key in kwargs})
    else:
        hc_visualization = Highchart(**{key: kwargs.pop(key) for key in ['width', 'height'] if key in kwargs})

    # Set timeseries encoding automatically:
    if df.index.is_all_dates:
        # NOTE *** AFTER monthes, I finally figured out that certain types of option settings
        # were the cause of time not encoding properly.  Basically, customer jinja-js like {x.point} functions
        # need to be created carefully with timestamps in mind.  Also, data must be encoded as 32 bit datetime.
        # (e.g. df.index.map(lambda dt: dt.to_datetime()), or [(datetime.datetime(2016,9,28), $val), ...]
        hc_visualization.set_options('xAxis', {'type': 'datetime'})

    # Set options (chart customization):
    options = get_highchart_options(**kwargs)

    if kind == 'line':
        for col_idx, col in enumerate(df.columns):
            hc_visualization = _highcharts_add_data_set(hc_visualization, df[col], col, kind, y_axes[col_idx])

    elif kind == 'bar' or kind == 'column':
        # Add bar categories (dataframe index):
        options['xAxis']['categories'] = list(df.index)

        # Add grouped column/bar data:
        for col_idx, col in enumerate(df.columns):
            hc_visualization = _highcharts_add_data_set(hc_visualization, df[col], col, kind, y_axes[col_idx])

    elif isinstance(kind, list):

        # Make sure the kind list is the right size:
        assert len(kind) == df.shape[1], "list kind must be the same length as df"

        # If columns in the list, map to line_w_col:
        if 'bar' in kind or 'column' in kind:
            kind = map(lambda a_kind: ("%s_w_col" % a_kind) if a_kind == 'line' else a_kind, kind)

        # Add categories:
        options['xAxis']['categories'] = list(df.index.values)

        # Add grouped data:
        for col_idx, col in enumerate(df.columns):
            hc_visualization = _highcharts_add_data_set(hc_visualization, df[col], col, kind[col_idx], y_axes[col_idx])

    # Set options:
    hc_visualization.set_dict_options(options)

    # Print options json if debug mode is on:
    if debug:
        pp = pprint.PrettyPrinter(indent=1)
        pp.pprint(options)

    return hc_visualization
# -*- coding: utf-8 -*-
"""
Highstock Demos
OHLC: http://www.highcharts.com/stock/demo/ohlc
"""

from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlc.json&callback=?'
data = jsonp_loader(data_url, sub_d = r'(\/\*.*\*\/)')

H.add_data_set(data, 'ohlc', 'AAPL Stock Price', dataGrouping = {
                    'units' : [[
                        'week', # unit name
                        [1] # allowed multiples
                    ], [
                        'month',
                        [1, 2, 3, 4, 6]
                    ]]
                })

options = {
    'rangeSelector' : {
        'selected' : 2
    },

    'title' : {
        'text' : 'AAPL Stock Price'
    },
# -*- coding: utf-8 -*-
"""
Highstock Demos
1.7 million points with async loading: http://www.highcharts.com/stock/demo/lazy-loading
"""

"""
This example generates a candlestick chart, which updates (async loading) when a different time period is selected
by the navigation bar due to the large dataset.

Due to the update, this chart requires JS function in the beginning and xAxis.events options.
"""

from highcharts import Highstock
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/from-sql.php?callback=?'
H.add_data_from_jsonp(data_url, 'json_data', 'candlestick', dataGrouping = {'enabled': False})

script = """json_data = [].concat(json_data, [[Date.UTC(2011, 9, 14, 19, 59), null, null, null, null]]);"""
H.add_JSscript(script, 'head')

H.add_navi_series_from_jsonp() # not really useful, but it shows in highstock demo

options = {
    'chart' : {
        'zoomType': 'x'
    },

    'navigator' : {
        'adaptToUpdatedData': False,
Example #11
0
# -*- coding: utf-8 -*-
"""
Highstock Demos
Compare multiple series: http://www.highcharts.com/stock/demo/compare
"""

from highcharts import Highstock
from highcharts.highstock.highstock_helper import json_loader
H = Highstock()

names = ['MSFT', 'AAPL', 'GOOG']

for name in names:
    data_url = 'http://www.highcharts.com/samples/data/' + name.lower(
    ) + '-c.json'
    data = json_loader(data_url)

    H.add_data_set(data, 'line', name)

options = {
    'rangeSelector': {
        'selected': 4
    },
    'yAxis': {
        'labels': {
            'formatter':
            "function () {\
                            return (this.value > 0 ? ' + ' : '') + this.value + '%';\
                        }"
        },
        'plotLines': [{
Example #12
0
# -*- coding: utf-8 -*-
"""
Highstock Demos
1.7 million points with async loading: http://www.highcharts.com/stock/demo/lazy-loading
"""
"""
This example generates a candlestick chart, which updates (async loading) when a different time period is selected
by the navigation bar due to the large dataset.

Due to the update, this chart requires JS function in the beginning and xAxis.events options.
"""

from highcharts import Highstock
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/from-sql.php?callback=?'
H.add_data_from_jsonp(data_url,
                      'json_data',
                      'candlestick',
                      dataGrouping={'enabled': False})

script = """json_data = [].concat(json_data, [[Date.UTC(2011, 9, 14, 19, 59), null, null, null, null]]);"""
H.add_JSscript(script, 'head')

H.add_navi_series_from_jsonp(
)  # not really useful, but it shows in highstock demo

options = {
    'chart': {
        'zoomType': 'x'
    },
Example #13
0
def creatChart(klines, symbol):
    filename = "%s_dayk" % symbol
    ohlc = []
    volume = []
    for line in klines:
        ohlc.append([
            line[0],
            #open
            float(line[1]),
            #high
            float(line[2]),
            # low
            float(line[3]),
            # close
            float(line[4])
        ])
        volume.append([line[0], float(line[5])])

    H = Highstock()

    groupingUnits = [['day', [1]]]

    options = {
        'rangeSelector': {
            'selected': 4
        },
        'chart': {
            'zoomType': 'x',
            'reflow': True,
            'height': 900
        },
        'title': {
            'text': '%s' % (symbol)
        },
        'subtitle': {
            'text': filename
        },
        'yAxis': [{
            'labels': {
                'align': 'right',
                'x': -3
            },
            'title': {
                'text': 'OHLC'
            },
            'height': '60%',
            'lineWidth': 2
        }, {
            'labels': {
                'align': 'right',
                'x': -3
            },
            'title': {
                'text': 'Volume'
            },
            'top': '65%',
            'height': '35%',
            'offset': 0,
            'lineWidth': 2
        }],
    }

    H.add_data_set(ohlc,
                   'candlestick',
                   symbol,
                   dataGrouping={'units': groupingUnits})
    H.add_data_set(volume,
                   'column',
                   'Volume',
                   yAxis=1,
                   dataGrouping={'units': groupingUnits})

    H.set_dict_options(options)

    H.save_file(filename)

    print("===--- Completed ---===")
# -*- coding: utf-8 -*-
"""
Highstock Demos
Flags marking events: http://www.highcharts.com/stock/demo/flags-general
"""

import datetime
from highcharts import Highstock
H = Highstock()

data = [[datetime.datetime(2013, 6, 2), 0.7695],
        [datetime.datetime(2013, 6, 3), 0.7648],
        [datetime.datetime(2013, 6, 4), 0.7645],
        [datetime.datetime(2013, 6, 5), 0.7638],
        [datetime.datetime(2013, 6, 6), 0.7549],
        [datetime.datetime(2013, 6, 7), 0.7562],
        [datetime.datetime(2013, 6, 9), 0.7574],
        [datetime.datetime(2013, 6, 10), 0.7543],
        [datetime.datetime(2013, 6, 11), 0.7510],
        [datetime.datetime(2013, 6, 12), 0.7498],
        [datetime.datetime(2013, 6, 13), 0.7477],
        [datetime.datetime(2013, 6, 14), 0.7492],
        [datetime.datetime(2013, 6, 16), 0.7487],
        [datetime.datetime(2013, 6, 17), 0.7480],
        [datetime.datetime(2013, 6, 18), 0.7466],
        [datetime.datetime(2013, 6, 19), 0.7521],
        [datetime.datetime(2013, 6, 20), 0.7564],
        [datetime.datetime(2013, 6, 21), 0.7621],
        [datetime.datetime(2013, 6, 23), 0.7630],
        [datetime.datetime(2013, 6, 24), 0.7623],
        [datetime.datetime(2013, 6, 25), 0.7644],
# -*- coding: utf-8 -*-
"""
Highstock Demos
52,000 points with data grouping: http://www.highcharts.com/stock/demo/data-grouping
"""

from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/jsonp.php?filename=large-dataset.json&callback=?'
data = jsonp_loader(data_url, sub_d=r'(\/\*.*\*\/)')

H.add_data_set(data['data'],
               'line',
               'Temperature',
               pointStart=data['pointStart'],
               pointInterval=data['pointInterval'],
               tooltip={
                   'valueDecimals': 1,
                   'valueSuffix': u'°C'
               })

options = {
    'chart': {
        'zoomType': 'x'
    },
    'rangeSelector': {
        'buttons': [{
            'type': 'day',
            'count': 3,
# -*- coding: utf-8 -*-
"""
Highstock Demos
Two panes, candlestick and volume: http://www.highcharts.com/stock/demo/candlestick-and-volume
"""
from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?'
data = jsonp_loader(data_url, sub_d = r'(\/\*.*\*\/)')

ohlc = []
volume = []
groupingUnits = [
['week', [1]], 
['month', [1, 2, 3, 4, 6]]
]

for i in range(len(data)):
    ohlc.append(
        [
        data[i][0], # the date
        data[i][1], # open
        data[i][2], # high
        data[i][3], # low
        data[i][4]  # close
        ]
        )
    volume.append(
        [
# -*- coding: utf-8 -*-
"""
Highstock Demos
Intraday area: http://www.highcharts.com/stock/demo/intraday-area
"""
from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader
from datetime import datetime
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/jsonp.php?filename=new-intraday.json&callback=?'
data = jsonp_loader(data_url, sub_d = r'(\/\*.*\*\/)')

H.add_data_set(data, 'area', 'AAPL', gapSize = 5,
                tooltip = {
                    'valueDecimals': 2
                },
                fillColor = {
                    'linearGradient' : {
                        'x1': 0,
                        'y1': 0,
                        'x2': 0,
                        'y2': 1
                    },
                    'stops' : [
                        [0, 'Highcharts.getOptions().colors[0]'],
                        [1, 'Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get("rgba")']
                    ]
                },
                threshold =  None)
# -*- coding: utf-8 -*-
"""
Highstock Demos
52,000 points with data grouping: http://www.highcharts.com/stock/demo/data-grouping
"""

from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/jsonp.php?filename=large-dataset.json&callback=?'
data = jsonp_loader(data_url, sub_d = r'(\/\*.*\*\/)')

H.add_data_set(data['data'], 'line', 'Temperature', pointStart = data['pointStart'],
                pointInterval = data['pointInterval'],
                tooltip = {
                    'valueDecimals': 1,
                    'valueSuffix': u'°C'
                })

options = {
    'chart': {
        'zoomType': 'x'
    },

    'rangeSelector': {

        'buttons': [{
            'type': 'day',
            'count': 3,
            'text': '3d'
    1. data_src is the url (https) for the dataset
    2. data_name is the variable name of dataset. This name is used for javascript environment (not in python)
    3. series_type( default: "line") is the type of plot this dataset will be presented
    4. kwargs are for parameters in series or plotOptions 
        (for detail please ref to highcharts API: http://api.highcharts.com/highcharts#)

In most examples, add_data_from_jsonp method is used to show a similar practice in Highstock Demos 

The following example is from Highstock Demos
Single line series: http://www.highcharts.com/stock/demo/basic-line
"""

from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader

H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?'
H.add_data_from_jsonp(data_url,
                      'json_data',
                      'line',
                      'AAPL',
                      tooltip={'valueDecimals': 2})

options = {
    'rangeSelector': {
        'selected': 1
    },
    'title': {
        'text': 'AAPL Stock Price'
    },
# -*- coding: utf-8 -*-
"""
Highstock Demos
Flags marking events: http://www.highcharts.com/stock/demo/flags-general
"""

import datetime
from highcharts import Highstock
H = Highstock()

data = [
[datetime.datetime(2013,6,2),0.7695],
[datetime.datetime(2013,6,3),0.7648],
[datetime.datetime(2013,6,4),0.7645],
[datetime.datetime(2013,6,5),0.7638],
[datetime.datetime(2013,6,6),0.7549],
[datetime.datetime(2013,6,7),0.7562],
[datetime.datetime(2013,6,9),0.7574],
[datetime.datetime(2013,6,10),0.7543],
[datetime.datetime(2013,6,11),0.7510],
[datetime.datetime(2013,6,12),0.7498],
[datetime.datetime(2013,6,13),0.7477],
[datetime.datetime(2013,6,14),0.7492],
[datetime.datetime(2013,6,16),0.7487],
[datetime.datetime(2013,6,17),0.7480],
[datetime.datetime(2013,6,18),0.7466],
[datetime.datetime(2013,6,19),0.7521],
[datetime.datetime(2013,6,20),0.7564],
[datetime.datetime(2013,6,21),0.7621],
[datetime.datetime(2013,6,23),0.7630],
[datetime.datetime(2013,6,24),0.7623],
but use jsonp_loader from highstock_helper instead of add_data_from_jsonp

jsonp_loader(url, prefix_regex=r'^(.*\()', suffix_regex=r'(\);)$', sub_d=None, sub_by='')
jsonp_loader is to request (JSON) data from a server in a different domain (JSONP) 
and covert to python readable data. 
    1. url is the url (https) where data is located
    2. "prefix_regex" and "suffix_regex" are regex patterns used to 
        remove JSONP specific prefix and suffix, such as callback header: "callback(" and end: ");", 
    3. "sub_d" is regex patterns for any unwanted string in loaded json data (will be replaced by sub_by). 
    4. "sub_by" is the string to replace any unwanted string defined by sub_d
For function coverstion, such as Data.UTC to datetime.datetime, please check JSONPDecoder
"""

from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?'
data = jsonp_loader(data_url, sub_d = r'(\/\*.*\*\/)') # to remove the comment in json doc from the url

options = {
    'rangeSelector' : {
            'selected' : 1
        },

    'title' : {
        'text' : 'AAPL Stock Price'
    },
}

H.add_data_set(data, 'line', 'AAPL', tooltip = {
Example #22
0
def hc_candlestick_and_volume(data, title, theme=None):
    H = Highstock()

    ohlc = []
    volume = []
    groupingUnits = [['week', [1]], ['month', [1, 2, 3, 4, 6]]]

    for i in range(len(data)):
        ohlc.append([
            data[i][0],  # the date
            data[i][1],  # open
            data[i][2],  # high
            data[i][3],  # low
            data[i][4]  # close
        ])
        volume.append([
            data[i][0],  # the date
            data[i][5]  # the volume
        ])

    options = {
        'rangeSelector': {
            'selected': 1
        },
        'title': {
            'text': title
        },
        'yAxis': [{
            'labels': {
                'align': 'right',
                'x': -3
            },
            'title': {
                'text': 'OHLC'
            },
            'height': '60%',
            'lineWidth': 2
        }, {
            'labels': {
                'align': 'right',
                'x': -3
            },
            'title': {
                'text': 'Volume'
            },
            'top': '65%',
            'height': '35%',
            'offset': 0,
            'lineWidth': 2
        }],
    }

    H.add_data_set(ohlc,
                   'candlestick',
                   'OHLC',
                   dataGrouping={'units': groupingUnits})

    H.add_data_set(volume,
                   'column',
                   'Volume',
                   yAxis=1,
                   dataGrouping={'units': groupingUnits})

    H.set_dict_options(options)

    return hc_plot(H, title, theme)
Example #23
0
# -*- coding: utf-8 -*-
"""
Highstock Demos
Plot bands on Y axis: http://www.highcharts.com/stock/demo/yaxis-plotbands
"""

import datetime
from highcharts import Highstock
H = Highstock()

data = [[datetime.datetime(2013, 6, 2), 0.7695],
        [datetime.datetime(2013, 6, 3), 0.7648],
        [datetime.datetime(2013, 6, 4), 0.7645],
        [datetime.datetime(2013, 6, 5), 0.7638],
        [datetime.datetime(2013, 6, 6), 0.7549],
        [datetime.datetime(2013, 6, 7), 0.7562],
        [datetime.datetime(2013, 6, 9), 0.7574],
        [datetime.datetime(2013, 6, 10), 0.7543],
        [datetime.datetime(2013, 6, 11), 0.7510],
        [datetime.datetime(2013, 6, 12), 0.7498],
        [datetime.datetime(2013, 6, 13), 0.7477],
        [datetime.datetime(2013, 6, 14), 0.7492],
        [datetime.datetime(2013, 6, 16), 0.7487],
        [datetime.datetime(2013, 6, 17), 0.7480],
        [datetime.datetime(2013, 6, 18), 0.7466],
        [datetime.datetime(2013, 6, 19), 0.7521],
        [datetime.datetime(2013, 6, 20), 0.7564],
        [datetime.datetime(2013, 6, 21), 0.7621],
        [datetime.datetime(2013, 6, 23), 0.7630],
        [datetime.datetime(2013, 6, 24), 0.7623],
        [datetime.datetime(2013, 6, 25), 0.7644],
    add dataset from the data_src using jsonp. It is converted to jquery function "$.getJSON" in javascript environment
    1. data_src is the url (https) for the dataset
    2. data_name is the variable name of dataset. This name is used for javascript environment (not in python)
    3. series_type( default: "line") is the type of plot this dataset will be presented
    4. kwargs are for parameters in series or plotOptions 
        (for detail please ref to highcharts API: http://api.highcharts.com/highcharts#)

In most examples, add_data_from_jsonp method is used to show a similar practice in Highstock Demos 

The following example is from Highstock Demos
Single line series: http://www.highcharts.com/stock/demo/basic-line
"""

from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?'
H.add_data_from_jsonp(data_url, 'json_data', 'line', 'AAPL', tooltip = {
    'valueDecimals': 2
    }
)

options = {
    'rangeSelector' : {
            'selected' : 1
        },

    'title' : {
        'text' : 'AAPL Stock Price'
    },
Example #25
0
# -*- coding: utf-8 -*-
"""
Highstock Demos
Area range: http://www.highcharts.com/stock/demo/arearange
"""
from highcharts import Highstock
from highcharts.highstock.highstock_helper import json_loader

H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/range.json'
data = json_loader(data_url)

H.add_data_set(data, 'arearange', 'Temperatures')

options = {
    'rangeSelector': {
        'selected': 2
    },
    'title': {
        'text': 'Temperature variation by day'
    },
}

H.set_dict_options(options)

H.htmlcontent
# -*- coding: utf-8 -*-
"""
Highstock Demos
Plot bands on Y axis: http://www.highcharts.com/stock/demo/yaxis-plotbands
"""

import datetime
from highcharts import Highstock
H = Highstock()

data = [
[datetime.datetime(2013,6,2),0.7695],
[datetime.datetime(2013,6,3),0.7648],
[datetime.datetime(2013,6,4),0.7645],
[datetime.datetime(2013,6,5),0.7638],
[datetime.datetime(2013,6,6),0.7549],
[datetime.datetime(2013,6,7),0.7562],
[datetime.datetime(2013,6,9),0.7574],
[datetime.datetime(2013,6,10),0.7543],
[datetime.datetime(2013,6,11),0.7510],
[datetime.datetime(2013,6,12),0.7498],
[datetime.datetime(2013,6,13),0.7477],
[datetime.datetime(2013,6,14),0.7492],
[datetime.datetime(2013,6,16),0.7487],
[datetime.datetime(2013,6,17),0.7480],
[datetime.datetime(2013,6,18),0.7466],
[datetime.datetime(2013,6,19),0.7521],
[datetime.datetime(2013,6,20),0.7564],
[datetime.datetime(2013,6,21),0.7621],
[datetime.datetime(2013,6,23),0.7630],
[datetime.datetime(2013,6,24),0.7623],
# -*- coding: utf-8 -*-
"""
Highstock Demos
Intraday area: http://www.highcharts.com/stock/demo/intraday-area
"""
from highcharts import Highstock
from highcharts.highstock.highstock_helper import json_loader
from datetime import datetime
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/new-intraday.json'
data = json_loader(data_url)

H.add_data_set(
    data,
    'area',
    'AAPL',
    gapSize=5,
    tooltip={'valueDecimals': 2},
    fillColor={
        'linearGradient': {
            'x1': 0,
            'y1': 0,
            'x2': 0,
            'y2': 1
        },
        'stops':
        [[0, 'Highcharts.getOptions().colors[0]'],
         [
             1,
             'Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get("rgba")'
Example #28
0
    def plot(self,
             title=False,
             width=600,
             height=300,
             load_js=None,
             js_sources=[]):
        chart_height = (height +
                        50) * self.number_of_candlestick_charts - 50 + 200
        H = Highstock(width=width, height=chart_height)
        groupingUnits = [
            ['hour', [60]],
        ]

        with open(
                os.path.join(os.path.dirname(os.path.abspath(__file__)),
                             "files/load.jinja")) as f:
            load_tempate = jinja2.Template(f.read())

        options = {
            'rangeSelector': {
                'buttons': [{
                    'type': 'minute',
                    'count': 180,
                    'text': '3h'
                }, {
                    'type': 'minute',
                    'count': 360,
                    'text': '6h'
                }, {
                    'type': 'minute',
                    'count': 720,
                    'text': '12h'
                }, {
                    'type': 'minute',
                    'count': 60 * 24,
                    'text': '1d'
                }, {
                    'type': 'minute',
                    'count': 60 * 48,
                    'text': '2d'
                }, {
                    'type': 'second',
                    'count': 1,
                    'text': '1s'
                }, {
                    'type': 'second',
                    'count': 10,
                    'text': '10s'
                }],
                'selected':
                4,
                'inputEnabled':
                False
            },
            'plotOptions': {
                'series': {
                    'turboThreshold:': 0,
                    'dataGrouping': {
                        'enabled': False,
                    },
                    'animation': False,
                },
                'scatter': {
                    'turboThreshold:': 0,
                    'marker': {
                        'radius': 5,
                        'symbol': 'circle',
                        'states': {
                            'hover': {
                                'enabled': True,
                                'lineColor': 'rgb(100,100,100)'
                            }
                        }
                    },
                    'tooltip': {
                        'pointFormat': '{series.name} x:{point.x}, y:{point.y}'
                        #'pointFormatter': """
                        #function () { debugger; }
                        #"""
                    },
                    'states': {
                        'hover': {
                            'marker': {
                                'enabled': False
                            }
                        }
                    },
                },
            },
            'navigator': {
                'enabled': True,
                'height': 20
            },
            'chart': {
                'animation': False,
            },
            'title': {
                'text': '',
                'style': {
                    'display': 'none'
                }
            },
            'legend': {
                'enabled': True,
            },
            'yAxis': []
        }
        if title:
            options['title'] = {'text': title}

        if load_js:
            load_js = load_tempate.render(symbols=self.symbols)
            options['event']['load'] = load_js

        idx = -1
        current_y = -height
        for chart_type, symbol, data, is_new_chart in self.symbols:
            if is_new_chart:
                idx += 1
                current_y += height + 50
                options['yAxis'].append({
                    'labels': {
                        'align': 'right',
                        'x': -3
                    },
                    'title': {
                        'text': 'y'
                    },
                    'top': '%dpx' % (current_y),
                    'height': '%dpx' % (height),
                    'lineWidth': 2,
                    'offset': 0,
                })

            if chart_type == 'candlestick':
                H.add_data_set(data,
                               chart_type,
                               symbol,
                               yAxis=idx,
                               dataGrouping={})
            elif chart_type == 'line':
                H.add_data_set(data,
                               chart_type,
                               symbol,
                               yAxis=idx,
                               dataGrouping={})
            elif chart_type == 'scatter':
                H.add_data_set(data,
                               chart_type,
                               symbol,
                               yAxis=idx,
                               dataGrouping={},
                               turboThreshold=0)

        H.set_dict_options(options)
        H.add_JSscript(self.theme, "head")
        for js_source in js_sources:
            H.add_JSsource(js_source)
        return H
def hc_candlestick_and_volume(data, title, theme=None):
    H = Highstock()

    ohlc = []
    volume = []
    groupingUnits = [
        ['week', [1]],
        ['month', [1, 2, 3, 4, 6]]
    ]

    for i in range(len(data)):
        ohlc.append(
            [
            data[i][0], # the date
            data[i][1], # open
            data[i][2], # high
            data[i][3], # low
            data[i][4]  # close
            ]
            )
        volume.append(
            [
            data[i][0], # the date
            data[i][5]  # the volume
            ]
        )

    options = {
        'rangeSelector': {
            'selected': 1
        },

        'title': {
            'text': title
        },

        'yAxis': [{
            'labels': {
                'align': 'right',
                'x': -3
            },
            'title': {
                'text': 'OHLC'
            },
            'height': '60%',
            'lineWidth': 2
        }, {
            'labels': {
                'align': 'right',
                'x': -3
            },
            'title': {
                'text': 'Volume'
            },
            'top': '65%',
            'height': '35%',
            'offset': 0,
            'lineWidth': 2
        }],
    }

    H.add_data_set(ohlc, 'candlestick', 'OHLC', dataGrouping = {
        'units': groupingUnits
    })

    H.add_data_set(volume, 'column', 'Volume', yAxis = 1, dataGrouping = {
        'units': groupingUnits
    })

    H.set_dict_options(options)

    return hc_plot(H, title, theme)
Example #30
0
# -*- coding: utf-8 -*-
"""
Highstock Demos
Two panes, candlestick and volume: http://www.highcharts.com/stock/demo/candlestick-and-volume
"""
from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader
H = Highstock()

data_url = 'http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-ohlcv.json&callback=?'
data = jsonp_loader(data_url, sub_d=r'(\/\*.*\*\/)')

ohlc = []
volume = []
groupingUnits = [['week', [1]], ['month', [1, 2, 3, 4, 6]]]

for i in range(len(data)):
    ohlc.append([
        data[i][0],  # the date
        data[i][1],  # open
        data[i][2],  # high
        data[i][3],  # low
        data[i][4]  # close
    ])
    volume.append([
        data[i][0],  # the date
        data[i][5]  # the volume 
    ])

options = {
    'rangeSelector': {
Example #31
0
# -*- coding: utf-8 -*-
"""
Highstock Demos
Area range: http://www.highcharts.com/stock/demo/arearange
"""
from highcharts import Highstock
from highcharts.highstock.highstock_helper import jsonp_loader

H = Highstock()

data_url = "http://www.highcharts.com/samples/data/jsonp.php?filename=range.json&callback=?"
data = jsonp_loader(data_url, sub_d=r"(\/\*.*\*\/)")

H.add_data_set(data, "arearange", "Temperatures")

options = {"rangeSelector": {"selected": 2}, "title": {"text": "Temperature variation by day"}}

H.set_dict_options(options)

H.htmlcontent
    def plot(self, plt_type='all'):

        self.getsecID(sec=1)

        if not exist:
            return self.etf + ' ceased trading'

        #這邊廢code很多QQ
        #為避免error,先創一堆空的dict(相信這邊有更好的寫法XD)

        nav_data = dict()
        price_data = dict()
        volume_data = dict()
        nav_data[self.etf] = dict()
        price_data[self.etf] = dict()
        volume_data[self.etf] = dict()

        if plt_type == 'all':
            nav_data = self.data(data_type='nav')
            price_data = self.data(data_type='price')
            volume_data = self.data(data_type='volume')
        elif plt_type == 'nav':
            nav_data = self.data(data_type='nav')
        elif plt_type == 'price':
            price_data = self.data(data_type='price')
        else:
            return 'Sorry, plt_type should be nav, price or all'

        #用highchart裡的hichstock畫圖
        H = Highstock()

        nav_high_data = [[
            datetime.strptime(i, "%Y-%m-%d"),
            nav_data[list(nav_data.keys())[0]][i]
        ] for i in nav_data[list(nav_data.keys())[0]]]
        price_high_data = [[
            datetime.strptime(i, "%Y-%m-%d"),
            price_data[list(price_data.keys())[0]][i]
        ] for i in price_data[list(price_data.keys())[0]]]
        volume_high_data = [[
            datetime.strptime(i, "%Y-%m-%d"),
            volume_data[list(volume_data.keys())[0]][i]
        ] for i in volume_data[list(volume_data.keys())[0]]]

        if plt_type == 'all':
            H.add_data_set(price_high_data,
                           'line',
                           'price',
                           id='dataseries',
                           color='#969696',
                           tooltip={'valueDecimals': 4})
            H.add_data_set(nav_high_data,
                           'line',
                           'nav',
                           id='dataseries',
                           tooltip={'valueDecimals': 4})
            H.add_data_set(volume_high_data,
                           'column',
                           'Volume',
                           yAxis=1,
                           color='#969696')
        elif plt_type == 'nav':
            H.add_data_set(nav_high_data,
                           'line',
                           'nav',
                           id='dataseries',
                           tooltip={'valueDecimals': 4})
        elif plt_type == 'price':
            H.add_data_set(price_high_data,
                           'line',
                           'price',
                           id='dataseries',
                           color='#969696',
                           tooltip={'valueDecimals': 4})

        options = {
            'rangeSelector': {
                'selected': 4
            },
            'title': {
                'text': self.etf.upper()
            },
            'tooltip': {
                'style': {
                    'width': '200px'
                },
                'shared':
                True,
                'pointFormat':
                '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b><br/>',
            },
            'yAxis': [{
                'labels': {
                    'align': 'right',
                    'x': -3
                },
                'title': {
                    'text': 'USD'
                },
                'height': '60%',
                'lineWidth': 0.5
            }, {
                'labels': {
                    'align': 'right',
                    'x': -3
                },
                'title': {
                    'text': 'Volume'
                },
                'top': '65%',
                'height': '35%',
                'offset': 0,
                'lineWidth': 2
            }],
            #'plotOptions': {'series': {'compare': 'percent'}},
        }

        H.set_dict_options(options)

        return H
Example #33
0
def main(argv):
    from highcharts import Highstock  # 暂时这样避免其他脚本 import 出错
    unittest()

    opts, args = parse_options(argv)
    #exchanges = ('binance', 'huobipro', 'okex', 'zb')
    #symbol = 'qtum/btc'
    #sdt = '2018-01-20 18-00-00'
    #edt = '2018-01-20 18-59-00'
    #folder = r'C:\Users\Eph\Desktop\CC\trade\ccxtbot\CCMarkets\data'
    exchanges = args
    symbol = opts.get('symbol')
    sdt = opts.get('start')
    edt = opts.get('stop')
    folder = opts.get('folder')
    prec = int(opts.get('prec', 8))
    fname = opts.get('file', 'StockChart')
    verbose = opts.get('verbose', False)
    if not exchanges or not symbol or not sdt or not edt or not folder:
        usage(argv[0])
        return 2

    options = {
        'chart': {
            'height': '50%', # default None
        },
        'title': {
            'text': 'Price Difference',
        },
        "tooltip": {
            "xDateFormat": "%Y-%m-%d %H:%M:%S %A",  # NOTE: BUG, 设置不生效
            'pointFormat': '<span style="color:{point.color}">' + '\\u25CF'.decode('unicode-escape') + \
                           '</span> {series.name}: <b>{point.y}</b><br/>',
            'padding': 1,   # default 8
        },
        "legend": {
            "enabled": True
        },
        "xAxis": {
            "type": "datetime"
        },
        'yAxis': {
            'title': {
                'text': '价格',
            },
            'opposite': True,   # False 表示在左边显示,默认值为 True
        },
         # 这是K线图的属性,不画K线图的话不需要
        "plotOptions": {
            "candlestick": {
                "color": "#d75442",
                "upColor": "#6ba583"
            }
        },
        "rangeSelector": {
            "buttons": [
                {
                    "type"  : "hour",
                    "count" : 1,
                    "text"  : "1h",
                },
                {
                    "type"  : 'hour',
                    "count" : 3,
                    "text"  : "3h"
                },
                {
                    "type"  : "hour",
                    "count" : 6,
                    "text"  : "6h"
                },
                {
                    "type"  : "hour",
                    "count" : 12,
                    "text"  : "12h"
                },
                {
                    "type"  : "all",
                    "text"  : "All"
                }
            ],
            #"selected": 2,  # 默认选择的索引号,从0开始,默认值为 undefined
            "inputEnabled": True,
            "inputBoxWidth": 150, # default 90
            'inputDateFormat': '%Y/%m/%d %H:%M:%S',
            'inputEditDateFormat': '%Y/%m/%d %H:%M:%S',
        },
    }

    chart = Highstock()
    chart.set_options('global', {'timezoneOffset': -8 * 60})

    chart.set_dict_options(options)
    #chart.set_options('chart', {'height': None})
    chart.set_options(
        'tooltip',
        {
            #'pointFormat': '<span style="color:{point.color}">' + '\\u25CF'.decode('unicode-escape') + \
            #'</span> {series.name}: <b>{point.y:.%df}</b><br/>' % prec,
            'valueDecimals': prec,
            'valueSuffix':
            ' ' + symbol.replace('/', '_').upper().split('_')[1],
        })

    ers = []
    stats = {}
    for exchange in exchanges:
        inst = ExchangeRecords(exchange, symbol, sdt, edt, folder)
        ers.append(inst)
        stats[exchange] = {}
        stats[exchange]['records_count'] = 0

    chart.set_options('subtitle', {'text': ers[0].symbol.replace('_', '/')})

    # 'binance': {'Buy': x, 'Sell': y}
    all_records = {}
    loop_count = 0
    # 标记哪个交易所已经拿完数据了
    # {exchange: True/False, ...}
    ctl = {}
    for inst in ers:
        ctl[inst.exchange] = False

    while True:
        all_true = True
        for v in ctl.itervalues():
            if not v:
                all_true = False
                break
        if ctl and all_true:
            break

        loop_count += 1
        for inst in ers:
            record = inst.get_one_record()
            if record is None:
                ctl[inst.exchange] = True
                continue

            records_dict = all_records.setdefault(inst.exchange, {})
            if not record:
                #ctl[inst.exchange] = True
                continue
            stats[inst.exchange]['records_count'] += 1

            # 不需要小数点
            t = int(int(float(record[0])) * 1000)
            #t += 3600*1000*8 # GMT+8
            buy = records_dict.setdefault('buy', [])
            buy.append([t, float(record[1])])
            sell = records_dict.setdefault('sell', [])
            sell.append([t, float(record[2])])
            last = records_dict.setdefault('last', [])
            last.append([t, float(record[3])])

    if verbose:
        print('loop_count:', loop_count)
        Log(json.dumps(stats, indent=4, sort_keys=True))
    for exchange in exchanges:
        records_dict = all_records[exchange]
        chart.add_data_set(records_dict['buy'],
                           series_type='line',
                           name='%s buy' % exchange)
        chart.add_data_set(records_dict['sell'],
                           series_type='line',
                           name='%s sell' % exchange)
        chart.add_data_set(records_dict['last'],
                           series_type='line',
                           name='%s last' % exchange)
    # 后缀名值 .html
    chart.save_file(filename=fname)
    Log('Successfully write to the file:', fname + '.html')