#!usr/bin/env python3
#-*- coding:utf-8 -*-
"""@author: yanqiong@file: futures_spot_spreads.py@create_on: 2019/9/27@description: 演示如何使用 Tqsdk 计算期货和现货价差,并使用 GUI 界面展示除了 Tqsdk 还需要提前安装的工具包 :PySimpleGUI (https://pysimplegui.readthedocs.io/en/latest/)matplotlib (https://matplotlib.org/)mplcursors (https://mplcursors.readthedocs.io/en/stable/index.html)"""
import sys
import math
import asyncio
import numpy as np
import pandas as pd
import PySimpleGUI as sg
import webbrowser
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import mplcursors
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from tqsdk import TqApi
DataLength = 40 # 显示最近10日价差
IndexList = np.arange(DataLength)
# ------------------------------- TqSdk Helper Code -------------------------------
links = {
"_LINK_": "https://www.shinnytech.com/blog/futures-spot-spreads/",
"_TITLE_": "https://www.shinnytech.com/tianqin/#utm_source=pc-customer&utm_medium=banner&utm_campaign=little-instrument"
}
def open_link (url = 'https://www.shinnytech.com/tianqin/'):
webbrowser.open_new(url)
def get_product_name (product_id):
return product_id.split(' - ')[1]
def get_quote_sn (symbol):
return symbol.split('.')[1]
# ------------------------------- Tqsdk 业务代码 -------------------------------
loop = asyncio.get_event_loop()
api = TqApi(loop=loop)
# 现货、期货合约对应列表
SymbolDict = {
"al - 铝": ["SSWE.ALH", "KQ.m@SHFE.al", "orange"],
"cu - 铜": ["SSWE.CUH", "KQ.m@SHFE.cu", "deepskyblue"],
"ni - 镍": ["SSWE.NIH", "KQ.m@SHFE.ni", "red"],
"pb - 铅": ["SSWE.PBH", "KQ.