美文网首页
第一个量化策略代码

第一个量化策略代码

作者: 日落_3d9f | 来源:发表于2023-02-01 18:25 被阅读0次

    第一个策略,当天股票下跌1%后尾盘买入,第二天如果有涨幅卖出,如果没有涨幅继续买入。代码如下:

    # -*- coding:utf-8 -*-
    
    import importlib
    import sys, json, re, os, time
    import MySQLdb,string,time,datetime
    importlib.reload(sys)
    
    def nextOP(id,investmentAmount,salesAmount,volume):
        conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="stock",charset="utf8")
        cursor = conn.cursor()
        cursor.execute("SET NAMES utf8");
        id = id + 1
        common_sql =  "select close,percent,open,high,low from `market` where id ="+str(id)
        cursor.execute(common_sql)
        data = cursor.fetchone()
        if(data[1]>0):
            salesAmount += data[0] * volume
            return investmentAmount,salesAmount,0
        elif data[1] < -1:
            return investmentAmount,salesAmount,volume
        else:
            investmentAmount += data[0] * 20000 - 5
            volume = volume + 20000
            return nextOP(id,investmentAmount,salesAmount,volume)
    
    
    if __name__=='__main__':
        conn = MySQLdb.connect(host="localhost",user="root",passwd="",db="stock",charset="utf8")
        cursor = conn.cursor()
        cursor.execute("SET NAMES utf8");
    
        common_sql =  "select id,close,add_time from `market` where percent<-1 order by id asc limit 10"
        investmentAmount = 0.0
        salesAmount = 0.0
        volume = 0.0
        n = cursor.execute(common_sql)
        if conn.affected_rows() > 0:
            common_values = cursor.fetchall()
            for common_value in common_values:
                investmentAmount += common_value[1] * 20000 - 5
                volume = volume + 20000
                investmentAmount,salesAmount,volume = nextOP(common_value[0],investmentAmount,salesAmount,volume)
                
        print("多次买入总金额(扣税后):" + str(investmentAmount))
        print("多次卖出后金额:"+ str(salesAmount))
        print("盈利:"+ str(salesAmount-investmentAmount))
    

    执行结果:

    多次买入总金额(扣税后):182010.0
    多次卖出后金额:186000.0
    盈利:3990.0

    相关文章

      网友评论

          本文标题:第一个量化策略代码

          本文链接:https://www.haomeiwen.com/subject/gjwihdtx.html