1 Star 0 Fork 0

wdy/photo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
dataint.py 5.98 KB
一键复制 编辑 原始数据 按行查看 历史
wdy 提交于 2021-08-29 00:41 . 第一次提交 dat
# with open(file_path, "rt") as f:
# reader = csv.DictReader(f)
# for item in reader:
# db_bar.symbol = symbol
# db_bar.exchange = exchange.value
# db_bar.datetime = datetime.strptime(
# item[datetime_head], datetime_format
# )
# db_bar.interval = interval.value
# db_bar.volume = item[volume_head]
# db_bar.open_price = item[open_head]
# db_bar.high_price = item[high_head]
# db_bar.low_price = item[low_head]
# db_bar.close_price = item[close_head]
# db_bar.vt_symbol = vt_symbol
# db_bar.gateway_name = "DB"
#
# db_bar.replace()
# #----------
# from vnpy.app.csv_ loader.engine import CsvLoaderEngine
# from vnpy.event import EventEngine
# from vnpy.trader.constant import Exchange, Interval
# from vnpy.trader.engine import MainEngine
# import os
# symbol_list=os.listdir('D:/data/')
# #print(symbol_list)
# a=CsvLoaderEngine(MainEngine,EventEngine)
# for symbol in symbol_list:
# print(symbol[:-4])
# a.load(
# file_path="D:/data/"+symbol,
# symbol=symbol[:-4],
# exchange=Exchange.OANDA,
# interval=Interval.DAILY,
# datetime_head="time",
# open_head="o",
# high_head="h",
# low_head="l",
# close_head="c",
# volume_head="v",
# datetime_format="%Y-%m-%dT%H:%M:%S.000000000Z")
#-----------
''' def dt_trun(t0):
# # t0 = imported_data['datetime'][0]
# # t0
# t1 = datetime.strptime(t0, "%Y-%m-%d %H:%M:%S")
# t1
# t2= datetime.strftime( t1,"%Y/%m/%d %H:%M:%S")
# t2
#
# # dt_format = '%Y%m%d %H:%M:%S'
# # t3 = datetime.strptime('2015/04/16 09:16:00', "%Y/%m/%d %H:%M:%S" )
# # t3
# return t2
# #
# # imported_data['datetime']
# datetime_format = '%Y%m%d %H:%M:%S'
# # dt_trun(imported_data['datetime'])
'''
from vnpy.trader.database.initialize import init_sql
from vnpy.trader.database.database import Driver
settings={
"database": "database.db",
"host": "localhost",
"port": 3306,
"user": "root",
"password": "",
"authentication_source": "admin"
}
sqlite_manager = init_sql(driver=Driver.SQLITE, settings=settings)
from vnpy.trader.constant import (Exchange, Interval)
import pandas as pd
from vnpy.trader.database import database_manager
from vnpy.trader.object import (BarData,TickData)
from datetime import datetime
import time
# 封装函数
def move_df_to_mongodb(imported_data:pd.DataFrame): # ,collection_name:str
bars = []
start = None
count = 0
# utc_8 = timezone(timedelta(hours=8))
# datetime = row.datetime.replace(tzinfo=utc_8)
for row in imported_data.itertuples():
bar = BarData(
symbol=row.symbol,
exchange=row.exchange,
datetime=row.datetime,
interval=row.interval,
volume=row.volume,
open_price=row.open,
high_price=row.high,
low_price=row.low,
close_price=row.close,
open_interest=row.open_interest,
gateway_name="DB",
)
bars.append(bar)
# do some statistics
count += 1
if not start:
start = bar.datetime
end = bar.datetime
# insert into database
database_manager.save_bar_data(bars) # , collection_name
database_manager.save_bar_data()
print(f'Insert Bar: {count} from {start} - {end}')
if __name__ == "__main__":
# 读取需要入库的csv文件,该文件是用gbk编码
imported_data = pd.read_csv('D:/VNPYProject/CU2201.csv',encoding='gbk')
imported_data = imported_data[['datetime','open', 'high', 'low', 'close','volume']]
# 将csv文件中 `市场代码`的 SC 替换成 Exchange.SHFE SHFE
# imported_data['市场代码'] = Exchange.SHFE
# 增加一列数据 `inteval`,且该列数据的所有值都是 Interval.MINUTE
# imported_data['interval'] = Interval.MINUTE
# 明确需要是float数据类型的列
# float_columns =imported_data.columns
# # for i in range(0,len(float_columns)):
# for col in float_columns:
# imported_data[col] = imported_data[col].astype('float')
# 明确时间戳的格式s
# %Y/%m/%d %H:%M:%S 代表着你的csv数据中的时间戳必须是 2020/05/01 08:32:30 格式
datetime_format = '%Y%m%d %H:%M:%S'
# imported_data['datetime'] = pd.to_datetime(imported_data['datetime'],format=datetime_format)
# xt = [dt_trun(i) for i in imported_data['datetime']]
# imported_data['datetime'] = pd.to_datetime(xt)
# 因为没有用到 成交额 这一列的数据,所以该列列名不变
# imported_data.columns = ['exchange','symbol','datetime','open','high','low','close','volume','成交额','open_interest','interval']
imported_data.columns = ['datetime','open','high','low','close','volume']
imported_data['exchange'] =Exchange.CFFEX
imported_data['interval'] = Interval.MINUTE
imported_data['open_interest'] = 0
imported_data['symbol'] ='IC500'
move_df_to_mongodb(imported_data)
from vnpy.database.mongodb import MongodbDatabase
from datetime import datetime
from vnpy.trader.constant import Exchange,Interval
from vnpy.trader.database import database_manager
import matplotlib.pyplot as plt
# Load history data
bars =database_manager.load_bar_data(
symbol="rb2201",
exchange=Exchange.SHFE,
interval=Interval.MINUTE,
start=datetime(2021, 8, 1),
end=datetime(2021, 8, 26)
)
# Generate x, y
y = []
for bar in bars:
close_price = bar.close_price
y.append(close_price)
x = list(range(1,len(y)+1))
# Show foto
plt.figure(figsize=(40, 20))
plt.plot(x, y)
plt.show()
from vnpy.trader.database import database_manager
import vnpy.trader.database.database_mogodb
from urllib.request import urlopen
url = 'https://www.baidu.com/'
for line in urlopen(url):
line = line.decode('utf-8') # Decoding the binary data to text.
for i in line: # look for Eastern Time
print(i,end="")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wdy999/photo.git
git@gitee.com:wdy999/photo.git
wdy999
photo
photo
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385