Loading ...

基于期货基本面分析的交易策略

Author: Hukybo, Date: 2019-08-09 11:04:50
Tags: Python

在期货交易市场,无论是私募机构还是普通散户,争论技术分析和基本面分析谁好谁坏,从来就没有停止过。技术分析者认为价格已经包含了一切,相信未来价格还以趋势方式演变,只关心图表上价格行为本身的变化,判断它能卖多少钱。基本面分析者认为真正价值最终将会反映在价格上,并不需要关心短期价格走势,更多的是分析影响价格背后的因素,判断它值多少钱。 点击阅读更多内容




import requests
from bs4 import BeautifulSoup
import time
import datetime
import json

diff_data = reserve_data = receipt_data = 0

def to_timestamp(date_str):
    time_array = time.strptime(date_str + " 00:00:00", "%Y-%m-%d %H:%M:%S")
    return int(round(time.mktime(time_array) * 1000))

def date_arr(year, month, day):
    begin, end = datetime.date(year, month, day), datetime.date.today()
    arr = []
    for i in range((end - begin).days + 1):
        day = begin + datetime.timedelta(days=i)
        arr.append([str(day).replace('-', ''), str(day),
                    day.weekday() + 1, to_timestamp(str(day))])
    return arr

def spot_futures_diff_data(date, futures_name):
    global diff_data
    url = f"http://www.100ppi.com/sf2/day-{date}.html"
    try:
        url_text = requests.get(url).text
    except BaseException:
        return int(diff_data)
    soup = BeautifulSoup(url_text, "html5lib")
    if len(soup.select("#fdata")) > 0:
        results = soup.select("#fdata")[0]
        for i in results.find_all('tr'):
            if len(i.find_all('td', text=futures_name)) > 0:
                data = i.find_all('font')[0].text
                if data is not None:
                    diff_data = data
    return int(diff_data)

def spot_data(date, futures_name, url_type, types):
    global reserve_data, receipt_data
    data_type = reserve_data if types == 'WHSTOCKS' else receipt_data
    url = f'http://www.shfe.com.cn/data/dailydata/{date}{url_type}.dat'
    try:
        url_text = requests.get(url).text
    except BaseException:
        return data_type
    total = count = 0
    if url_text[0] == '{':
        for i in json.loads(url_text)['o_cursor']:
            if futures_name in i['VARNAME']:
                if '合计' not in i['WHABBRNAME']:
                    if '总计' not in i['WHABBRNAME']:
                        try:
                            inventory = int(i[types])
                        except BaseException:
                            return data_type
                        if inventory > 0:
                            total, count = total + inventory, count + 1
        if count > 0:
            data_type = int(total / count)
    return data_type

def main():
    cfgA = {
        "extension": {"layout": 'single', "col": 4, "height": "500px"},
        "title": {"text": "基差图表"},
        "series": [{"name": "基差", "data": []}]
    }
    cfgB = {
        "extension": {"layout": 'single', "col": 4, "height": "500px"},
        "title": {"text": "库存图表"},
        "series": [{"name": "库存", "data": []}]
    }
    cfgC = {
        "extension": {"layout": 'single', "col": 4, "height": "500px"},
        "title": {"text": "仓单图表"},
        "series": [{"name": "仓单", "data": []}]
    }
    LogReset()
    chart = Chart([cfgA, cfgB, cfgC])
    chart.reset()
    for i in date_arr(2018, 1, 1):
        diff = spot_futures_diff_data(i[1], '天然橡胶')
        reserve = spot_data(i[0], '天然橡胶', 'weeklystock', 'WHSTOCKS')
        receipt = spot_data(i[0], '天然橡胶', 'dailystock', 'WRTWGHTS')
        if diff != 0 and reserve != 0 and receipt != 0:
            chart.add(0, [i[3], diff])
            chart.add(1, [i[3], reserve])
            chart.add(2, [i[3], receipt])
            chart.update([cfgA, cfgB, cfgC])
            time.sleep(1)
            Log(f'基差:{diff} 库存:{reserve} 仓单:{receipt} 日期:{i[1]}')


Related

More

solarhe2006 报错没有bs4怎么处理

Hukybo win+r >>> 输入cmd >>> 点击确定 >>> 输入pip install beautifulsoup4 >>> 回车

a410999923 报错没有bs4怎么处理

Hukybo 百度一下安装bs4