Loading ...

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

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


'''backtest
start: 2019-01-01 00:00:00
end: 2019-08-09 00:00:00
period: 1d
exchanges: [{"eid":"Futures_CTP","currency":"FUTURES"}]
'''


import requests
import re
from bs4 import BeautifulSoup
import time
import datetime
import random
import json
import threading


# 请求头文件
request_headers = {
    'Connection': 'keep-alive',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'accept-encoding': 'gzip, deflate',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
}


# ip代理
proxies = {
    'http': 'http://182.35.82.128:9999', 'https': 'https://182.35.82.128:9999'
}


diff_data = 0
reserve_data = 0
receipt_data = 0
profit_data = 0

# 日期转时间戳
def to_timestamp(date_str):
    times = date_str + " 00:00:00"
    time_array = time.strptime(times, "%Y-%m-%d %H:%M:%S")
    return int(round(time.mktime(time_array) * 1000))


# 返回日期数组
def date_arr(year, month, day):
    begin = datetime.date(year, month, day)
    end = 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, headers=request_headers).text  # , proxies=proxies
    except BaseException:
        print('获取基差数据失败')
        return int(diff_data)
    soup = BeautifulSoup(url_text, "html5lib")
    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_reserve_data(date, futures_name):
    global reserve_data
    url = f'http://www.shfe.com.cn/data/dailydata/{date}weeklystock.dat'
    try:
        url_text = requests.get(url, headers=request_headers).text
    except BaseException:
        print('获取库存数据失败')
        return reserve_data
    total = 0
    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'] and '总计' not in i['WHABBRNAME']:
                    try:
                        inventory = int(i['WHSTOCKS'])
                    except BaseException:
                        return reserve_data
                    if inventory > 0:
                        total = total + inventory
                        count = count + 1
        if count > 0:
            reserve_data = int(total / count)
    return reserve_data


# 获取仓单
def spot_receipt_data(date, futures_name):
    global receipt_data
    url = f'http://www.shfe.com.cn/data/dailydata/{date}dailystock.dat'
    try:
        url_text = requests.get(url, headers=request_headers).text
    except BaseException:
        print('获取库存数据失败')
        return receipt_data
    total = 0
    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'] and '总计' not in i['WHABBRNAME']:
                    try:
                        inventory = int(i['WRTWGHTS'])
                    except BaseException:
                        return receipt_data
                    if inventory > 0:
                        total = total + inventory
                        count = count + 1
        if count > 0:
            receipt_data = int(total / count)
    return receipt_data


# 虚拟利润
def get_profit_data():
    global profit_data
    profit_data = profit_data + random.randint(-100, 150)
    return profit_data


def main():
    # threading.Thread(target=spot_futures_diff_arr, args=('天然橡胶',)).start()
    # threading.Thread(target=commodity_inventory_arr, args=('天然橡胶',)).start()
    # threading.Thread(target=commodity_warehouse_receipt_arr, args=('天然橡胶',)).start()

    cfgA = {
    "extension": {
        "layout": 'single',
        "col": 6,
        "height": "500px",
    },
    "title" : {
        "text" : "基差图表"
    },
    "xAxis" : {
        "type" : "datetime"
    }, 
    "series" : [{
        "name" : "基差", 
        "data" : [],        
    }]
    }
    cfgB = {
        "extension": {
            "layout": 'single',
            "col": 6,
            "height": "500px",
        },
        "title" : {
            "text" : "库存图表"
        },
        "xAxis" : {
            "type" : "datetime"
        }, 
        "series" : [{
            "name" : "库存", 
            "data" : [],        
        }]
    }
    cfgC = {
        "extension": {
            "layout": 'single',
            "col": 6,
            "height": "500px",
        },
        "title" : {
            "text" : "仓单图表"
        },
        "xAxis" : {
            "type" : "datetime"
        }, 
        "series" : [{
            "name" : "仓单", 
            "data" : [],        
        }]
    }
    cfgD = {
        "extension": {
            "layout": 'single',
            "col": 6,
            "height": "500px",
        },
        "title" : {
            "text" : "利润图表"
        },
        "xAxis" : {
            "type" : "datetime"
        }, 
        "series" : [{
            "name" : "利润", 
            "data" : [],        
        }]
    }
    LogReset()
    chart = Chart([cfgA, cfgB, cfgC, cfgD])
    chart.reset()
    for i in date_arr(2018, 1, 1):
        diff = spot_futures_diff_data(i[1], '天然橡胶')
        reserve = spot_reserve_data(i[0], '天然橡胶')
        receipt = spot_receipt_data(i[0], '天然橡胶')
        profit = get_profit_data()
        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.add(3, [i[3], profit])
            chart.update([cfgA, cfgB, cfgC, cfgD])
            time.sleep(1)

            Log(f'基差:{diff} 库存:{reserve} 仓单:{receipt} 日期:{i[1]}')





Related

More