[60 量化交易] 对股票进行量化交易 买入最便宜的股票一年后再卖出看结果

import pymongo
from datetime import datetime, timedelta
import csv

# 打开数据库
mongo_client = pymongo.MongoClient('mongodb://localhost:27017/')
# 若没有Database 和Collection,则会自动创建
mongo_db = mongo_client['SinaData']
# # 若没有Database 和Collection,则会自动创建
mongo_collection = mongo_db['SinaAllStockName']


def INDEcreas(date, dayNUM):
    date = datetime.strptime(date, "%Y-%m-%d")
    modified_date = date + timedelta(days=dayNUM)
    datetime.strftime(modified_date, "%Y-%m-%d")
    dateINDE = modified_date.strftime("%Y-%m-%d")
    return dateINDE


def FindAllStock():
    # 查询所有数据
    mydoc = mongo_collection.find()
    Allcode = []
    Allname = []
    for x in mydoc:
        # print(x['code'])
        # print(x['name'])
        Allcode.append(x['code'])
        Allname.append(x['name'])
    return Allcode, Allname


# 通过汗水代码时间查找收盘价格  有价格,对于无价格的如何处理
def shwoClosePrice(allStock, Fdate):
    print('print all stocks price...')
    EverStockPrice = []
    # 查找一个股票价格
    for numE in range(len(allStock)):
        # print(allStock[numE])
        print('查找结果: {:.2%}'.format(numE / 4942))  # for 循环为啥不输出呢
        # print(str(numE))
        mongo_price = mongo_db[allStock[numE]]

        # FeverStockPrice = mongo_price.find_one({"date": Fdate})  # 查找停牌等问题
        # if FeverStockPrice is None:
        #     EverStockPrice.append(999999)  # 用999999进行站位
        # else:
        #     EverStockPrice.append(FeverStockPrice['close'])

        whileNum = 0
        while True:
            whileNum = whileNum + 1
            # code block
            s = Fdate
            FeverStockPrice = mongo_price.find_one({"date": s})  # 查找停牌等问题
            s = INDEcreas(s, -1)
            # break out of the loop
            if FeverStockPrice:
                EverStockPrice.append(FeverStockPrice['close'])
                break
            if whileNum > 100:
                EverStockPrice.append(999999)
                break

    return EverStockPrice


def main():
    print("main...")
    allcode, allname = FindAllStock()  # 数据库查找所有股票代码、名称
    print(allcode)
    print(allname)
    print(len(allcode))  # 股票数量
    print(len(allname))

    AllStockClosePrice = shwoClosePrice(allcode, "2018-01-02")  # 所有股票股价
    print(AllStockClosePrice)
    print(len(AllStockClosePrice))

    # 'bj871970', 'bj871981', 'bj872925', 'bj873122', 'bj873169', 'bj873223'] 比如新的创业板股票交易日期都很新
    # 对股价由低到高排序
    # 1.创建一个字典
    Code_Price_dict = {allcode[k]: AllStockClosePrice[k] for k in range(len(AllStockClosePrice))}
    print(Code_Price_dict)
    # 2.使用zip方法将学生字典转换为(value, key)的元组格式
    Code_Price_tuplelist = list(zip(Code_Price_dict.values(),
                                    Code_Price_dict.keys()))

    # 3.使用sorted函数对元组列表student_list由分数高低进行排序
    print("+++++++++++++++++++++++++++++++++++++++++++++++++++++")
    Code_Price_sorted = sorted(Code_Price_tuplelist, reverse=True)
    print(Code_Price_sorted)
    print(len(Code_Price_sorted))


if __name__ == '__main__':
    main()

import pymongo
from datetime import datetime, timedelta

date = [(5.39, 'sh600375'), (5.38, 'sz002400'), (5.38, 'sz002379'), (5.38, 'sh601169'), (5.37, 'sz002337'),
        (5.37, 'sz000597'), (5.37, 'sh600156'), (5.37, 'bj832566'), (5.36, 'sz002351'), (5.35, 'sz300586'),
        (5.35, 'sz001696'), (5.35, 'sz000409'), (5.35, 'sh600654'), (5.35, 'sh600222'), (5.35, 'sh600064'),
        (5.34, 'sh600078'), (5.33, 'sz002157'), (5.33, 'sh600461'), (5.31, 'sz000591'), (5.31, 'sh600854'),
        (5.3, 'sh603969'), (5.3, 'sh600185'), (5.3, 'bj833819'), (5.29, 'sz000159'), (5.29, 'sh601226'),
        (5.29, 'sh600716'), (5.29, 'sh600662'), (5.29, 'sh600395'), (5.28, 'sz002420'), (5.28, 'sz002003'),
        (5.28, 'sz000692'), (5.27, 'sz002218'), (5.27, 'sz002014'), (5.27, 'sz000932'), (5.26, 'sz000036'),
        (5.26, 'sh600657'), (5.25, 'sh600881'), (5.25, 'sh600331'), (5.24, 'sh600268'), (5.23, 'sh601898'),
        (5.23, 'sh601333'), (5.23, 'sh600880'), (5.22, 'sz300239'), (5.21, 'sz002483'), (5.21, 'sh601908'),
        (5.21, 'sh601199'), (5.2, 'sz300253'), (5.2, 'sz000725'), (5.2, 'sz000090'), (5.2, 'sh600173'),
        (5.19, 'sz002503'), (5.18, 'sz300059'), (5.18, 'sz002162'), (5.18, 'sz000791'), (5.18, 'sz000751'),
        (5.17, 'sz300105'), (5.17, 'sz002702'), (5.17, 'sz002206'), (5.17, 'sz002132'), (5.17, 'sz000530'),
        (5.17, 'sh600997'), (5.17, 'sh600981'), (5.16, 'sh603033'), (5.16, 'sh601636'), (5.15, 'sz002528'),
        (5.15, 'sz002099'), (5.15, 'sh600361'), (5.13, 'sz002361'), (5.12, 'sz002666'), (5.12, 'sz000987'),
        (5.12, 'sh601158'), (5.11, 'sh600836'), (5.1, 'sz300158'), (5.1, 'sz000965'), (5.09, 'sz000680'),
        (5.09, 'sh600177'), (5.09, 'sh600121'), (5.08, 'sz300150'), (5.07, 'sz000700'), (5.07, 'sh601588'),
        (5.06, 'sh600225'), (5.05, 'sz002481'), (5.04, 'sz000301'), (5.03, 'sz002169'), (5.02, 'sz002062'),
        (5.02, 'sz000558'), (5.01, 'sz002689'), (5.01, 'sz002392'), (5.01, 'sz000545'), (5.01, 'sh601968'),
        (5.0, 'sh601919'), (5.0, 'sh600828'), (5.0, 'sh600683'), (5.0, 'sh600502'), (4.99, 'sz000906'),
        (4.98, 'sh601519'), (4.95, 'sz000598'), (4.95, 'sz000166'), (4.95, 'sh601998'), (4.95, 'sh600708'),
        (4.94, 'sz002177'), (4.94, 'sh600540'), (4.94, 'sh600208'), (4.93, 'sh601992'), (4.92, 'sz002490'),
        (4.92, 'sz002225'), (4.91, 'sz002478'), (4.91, 'sh603601'), (4.9, 'sz000862'), (4.9, 'sh600812'),
        (4.9, 'sh600320'), (4.89, 'sz300234'), (4.89, 'sh600642'), (4.88, 'sh600705'), (4.87, 'sz300684'),
        (4.86, 'sz300569'), (4.86, 'sz000650'), (4.86, 'sh600973'), (4.85, 'sz002084'), (4.85, 'sz000958'),
        (4.85, 'sz000030'), (4.83, 'sh600963'), (4.83, 'sh600537'), (4.82, 'sz002487'), (4.81, 'sz000737'),
        (4.81, 'sh601018'), (4.8, 'sh601398'), (4.79, 'sz002306'), (4.78, 'sz000543'), (4.78, 'sz000521'),
        (4.77, 'sz300214'), (4.77, 'sz002535'), (4.77, 'sh600423'), (4.76, 'sh600105'), (4.75, 'sh600336'),
        (4.74, 'sz002554'), (4.73, 'sz002325'), (4.73, 'sh600798'), (4.73, 'sh600126'), (4.72, 'sz002064'),
        (4.71, 'sz002181'), (4.71, 'sz000536'), (4.71, 'sh601208'), (4.7, 'sz000622'), (4.7, 'sh600277'),
        (4.68, 'sz002550'), (4.68, 'sh600866'), (4.67, 'sz300507'), (4.66, 'sz000027'), (4.66, 'sh601008'),
        (4.66, 'sh600810'), (4.66, 'sh600488'), (4.65, 'sh601328'), (4.65, 'sh600386'), (4.64, 'sh600824'),
        (4.63, 'sz002638'), (4.63, 'sz000682'), (4.62, 'sz000659'), (4.62, 'sh600811'), (4.62, 'sh600448'),
        (4.59, 'sz002578'), (4.59, 'sz002463'), (4.59, 'sz000572'), (4.58, 'sh600611'), (4.58, 'sh600468'),
        (4.58, 'sh600400'), (4.58, 'sh600350'), (4.58, 'sh600127'), (4.57, 'sh600675'), (4.57, 'sh600462'),
        (4.56, 'sz000927'), (4.56, 'sh600025'), (4.55, 'sz002546'), (4.54, 'sz002109'), (4.53, 'sz002320'),
        (4.52, 'sz300276'), (4.52, 'sz300107'), (4.52, 'sh600780'), (4.5, 'sh601996'), (4.49, 'sh601339'),
        (4.48, 'sz000937'), (4.47, 'sz002274'), (4.47, 'sz000720'), (4.47, 'sh600635'), (4.46, 'sz002305'),
        (4.46, 'sh600815'), (4.45, 'sh600569'), (4.44, 'sz300067'), (4.44, 'sz300066'), (4.44, 'sh600023'),
        (4.43, 'sz002102'), (4.43, 'sh600533'), (4.43, 'sh600433'), (4.42, 'sh600020'), (4.41, 'sh600546'),
        (4.39, 'sz000949'), (4.39, 'sz000778'), (4.39, 'sz000539'), (4.39, 'sh600057'), (4.39, 'sh600035'),
        (4.37, 'sz002021'), (4.36, 'sz300012'), (4.36, 'sz002226'), (4.36, 'sh600759'), (4.35, 'sh600256'),
        (4.34, 'sz002516'), (4.34, 'sh600792'), (4.34, 'sh600261'), (4.33, 'sz002687'), (4.33, 'sz000652'),
        (4.32, 'sz000850'), (4.32, 'sz000573'), (4.32, 'sz000005'), (4.32, 'sh601618'), (4.32, 'sh600231'),
        (4.32, 'sh600028'), (4.32, 'sh600008'), (4.31, 'sh600369'), (4.3, 'sh600512'), (4.29, 'sz000425'),
        (4.28, 'sz300062'), (4.28, 'sz002467'), (4.28, 'bj831961'), (4.27, 'sh600823'), (4.26, 'sh600269'),
        (4.25, 'sz002506'), (4.25, 'sz002133'), (4.24, 'sh600403'), (4.23, 'sz300079'), (4.23, 'sh600300'),
        (4.22, 'sz000750'), (4.22, 'sh601179'), (4.21, 'sz002565'), (4.21, 'sz000972'), (4.21, 'sh600586'),
        (4.2, 'sz000859'), (4.19, 'sz002266'), (4.19, 'sh600582'), (4.18, 'sz002256'), (4.18, 'sz000898'),
        (4.17, 'sz300026'), (4.17, 'sz002641'), (4.17, 'sh600310'), (4.16, 'sz000883'), (4.16, 'sz000836'),
        (4.16, 'sz000761'), (4.16, 'sz000428'), (4.16, 'sh600496'), (4.15, 'sz000422'), (4.15, 'sh601899'),
        (4.14, 'sz000825'), (4.14, 'sz000677'), (4.13, 'sz300390'), (4.13, 'sz002145'), (4.13, 'sz000863'),
        (4.13, 'sh600567'), (4.12, 'sz002204'), (4.11, 'sh603300'), (4.11, 'sh601789'), (4.1, 'sz002593'),
        (4.1, 'sh601608'), (4.1, 'sh601388'), (4.1, 'sh600108'), (4.08, 'sh600163'), (4.06, 'sh600873'),
        (4.05, 'sz002471'), (4.04, 'sz300259'), (4.04, 'sz000520'), (4.03, 'sz002239'), (4.03, 'sh600321'),
        (4.03, 'sh600190'), (4.02, 'bj831445'), (4.01, 'sz300451'), (4.0, 'sh600868'), (3.99, 'sz000563'),
        (3.98, 'sz300352'), (3.98, 'sh600603'), (3.97, 'sz002063'), (3.95, 'sz002495'), (3.95, 'sz000656'),
        (3.95, 'sh600691'), (3.95, 'sh600210'), (3.94, 'sh600246'), (3.93, 'sh600397'), (3.92, 'sh600252'),
        (3.9, 'sz000875'), (3.88, 'sh600982'), (3.87, 'sz000809'), (3.85, 'sz002228'), (3.85, 'sh600979'),
        (3.85, 'sh600777'), (3.84, 'sz300048'), (3.83, 'sz300370'), (3.82, 'sh601918'), (3.82, 'sh600255'),
        (3.81, 'sz300420'), (3.79, 'sh600744'), (3.79, 'sh600425'), (3.78, 'sz002124'), (3.78, 'sh601106'),
        (3.76, 'sz000713'), (3.76, 'sh600223'), (3.75, 'sz000631'), (3.75, 'sh600984'), (3.74, 'sz000753'),
        (3.73, 'sz002607'), (3.73, 'sh600017'), (3.72, 'sz000589'), (3.71, 'sh601991'), (3.7, 'sz000157'),
        (3.7, 'sh600481'), (3.69, 'sz300135'), (3.69, 'sh601616'), (3.69, 'bj832278'), (3.68, 'sz000982'),
        (3.68, 'sh600282'), (3.67, 'sz000966'), (3.67, 'sz000718'), (3.67, 'sh600052'), (3.66, 'sh600601'),
        (3.65, 'sz002328'), (3.65, 'sh601099'), (3.64, 'sh601107'), (3.62, 'sz300111'), (3.62, 'sh600665'),
        (3.61, 'sz002429'), (3.61, 'sz002060'), (3.6, 'sh600236'), (3.6, 'sh600039'), (3.59, 'sh601188'),
        (3.56, 'sz002613'), (3.56, 'sh600790'), (3.56, 'sh600159'), (3.56, 'sh600063'), (3.55, 'sh601010'),
        (3.53, 'sh600077'), (3.5, 'sh600577'), (3.5, 'sh600169'), (3.48, 'sz300189'), (3.48, 'sh600565'),
        (3.44, 'sz000709'), (3.44, 'sz000100'), (3.44, 'sh601518'), (3.42, 'sz000897'), (3.42, 'sh601872'),
        (3.41, 'sz002195'), (3.41, 'sh600839'), (3.41, 'bj830832'), (3.4, 'sz000616'), (3.4, 'sh601218'),
        (3.38, 'sh600368'), (3.36, 'sh600327'), (3.33, 'sz300303'), (3.33, 'sh601058'), (3.32, 'sz000903'),
        (3.3, 'sh600578'), (3.28, 'sz002414'), (3.28, 'sz002269'), (3.22, 'sh600725'), (3.21, 'sz000816'),
        (3.2, 'sh600743'), (3.2, 'sh600724'), (3.2, 'sh600408'), (3.2, 'sh600313'), (3.19, 'sh600726'),
        (3.17, 'sz000420'), (3.17, 'sh600221'), (3.14, 'sh600808'), (3.12, 'sh600396'), (3.12, 'sh600219'),
        (3.11, 'sz002498'), (3.11, 'sh601818'), (3.1, 'sh600027'), (3.09, 'sz000517'), (3.07, 'sh600467'),
        (3.07, 'sh600186'), (3.05, 'sh601866'), (3.05, 'sh600033'), (3.04, 'sz000882'), (3.04, 'sz000667'),
        (3.04, 'sh601016'), (3.02, 'sh600170'), (3.01, 'sh600853'), (3.0, 'sz000861'), (3.0, 'sz000552'),
        (3.0, 'sh601988'), (2.98, 'sh600220'), (2.97, 'sz000767'), (2.95, 'sz000683'), (2.93, 'sz300217'),
        (2.93, 'sh601929'), (2.93, 'sh600106'), (2.92, 'sh601288'), (2.92, 'sh600103'), (2.91, 'sh600307'),
        (2.88, 'sz002154'), (2.88, 'sh600795'), (2.87, 'sh600370'), (2.85, 'sz002263'), (2.83, 'sz002505'),
        (2.82, 'sh600166'), (2.78, 'sh601000'), (2.73, 'sz300587'), (2.73, 'sz002031'), (2.73, 'sz000630'),
        (2.72, 'sh600871'), (2.71, 'sh600653'), (2.64, 'sz002131'), (2.63, 'sh601880'), (2.53, 'sh601258'),
        (2.52, 'sz000727'), (2.51, 'sh600863'), (2.46, 'sh600010'), (2.39, 'sz002610'), (2.24, 'sh600527'),
        (2.18, 'sz002526'), (2.13, 'sz300185'), (2.06, 'sh600022'), (2.0, 'sh603077')]


# 打开数据库
mongo_client = pymongo.MongoClient('mongodb://localhost:27017/')
# 若没有Database 和Collection,则会自动创建
mongo_db = mongo_client['SinaData']
# # 若没有Database 和Collection,则会自动创建
mongo_collection = mongo_db['SinaAllStockName']


def INDEcreas(date, dayNUM):
    date = datetime.strptime(date, "%Y-%m-%d")
    modified_date = date + timedelta(days=dayNUM)
    datetime.strftime(modified_date, "%Y-%m-%d")
    dateINDE = modified_date.strftime("%Y-%m-%d")
    return dateINDE


# 通过汗水代码时间查找收盘价格  有价格,对于无价格的如何处理
def shwoClosePrice(allStock, Fdate):
    print('print all stocks price...')
    EverStockPrice = []
    # 查找一个股票价格
    for numE in range(len(allStock)):
        # print(allStock[numE])
        print('查找结果: {:.2%}'.format(numE / 4942))  # for 循环为啥不输出呢
        # print(str(numE))
        mongo_price = mongo_db[allStock[numE]]

        # FeverStockPrice = mongo_price.find_one({"date": Fdate})  # 查找停牌等问题
        # if FeverStockPrice is None:
        #     EverStockPrice.append(999999)  # 用999999进行站位
        # else:
        #     EverStockPrice.append(FeverStockPrice['close'])

        whileNum = 0
        while True:
            whileNum = whileNum + 1
            # code block
            s = Fdate
            FeverStockPrice = mongo_price.find_one({"date": s})  # 查找停牌等问题
            s = INDEcreas(s, -1)
            # break out of the loop
            if FeverStockPrice:
                EverStockPrice.append(FeverStockPrice['close'])
                break
            if whileNum > 100:
                EverStockPrice.append(999999)
                break

    return EverStockPrice


def FindStockName(Scode):
    stockName = mongo_collection.find_one({"code": Scode})  # 查找停牌等问题
    # print(stockName)
    return stockName


print(date)

# 循环10次
stockTen = 0
tenStockName = []
newPrice = []
oldPrice = []
tenStock = []
for stockTu in date:
    stockTen = stockTen + 1
    if stockTen == 10:
        break
    print(stockTu[1] + ':' + str(stockTu[0]), end="    ")
    stockName = FindStockName(stockTu[1])
    print(stockName['name'])
    tenStockName.append(stockName['name'])
    tenStock.append(stockTu[1])
    oldPrice.append(stockTu[0])

newPrice = shwoClosePrice(tenStock, '2019-01-04')
print('-----------------')
print(tenStockName)
print(oldPrice)
print(newPrice)

# 计算盈利比例
# print(type(oldPrice[0])) #<class 'float'>

s = sum(oldPrice)
m = sum(newPrice)
print("初始资金" + str(s))
print("现在资金" + str(m))
print('盈利: {:.2%}'.format(m / s))


![在这里插入图片描述](https://img-blog.csdnimg.cn/a85e03b222e9400fa015053283db9b0e.png)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值