美文网首页
火币API量化交易Python3

火币API量化交易Python3

作者: 有信仰的人 | 来源:发表于2018-12-12 23:21 被阅读0次

      最近又开始搞自动化交易了,下面是一个简单地量化交易机制,贴出来供大家参考;


    程序流程图.jpg

      使用的是Python3的代码,仅作为一个demo。

        #!/usr/bin/env python3
        #-*- coding:utf-8 -*-
    
        from HuobiServices import *
        import json
        import demjson
        import time
        import re
        import os
        import hues
        import numpy
    
        '''
        date:1211
        comment:程序由递归实现循环
                参数的定义后期将使用json/conf文件保存,数据的保存后期将使用数据库IO
                提供接口供其他Python程序调用(通过面向对象来实现)
                后期需解决递归的中断问题
                需解决目前已知的一些多维数据的处理
        '''
    
        '''
        常量参数的定义
        '''
        #币种
        symbol = 'ethusdt'
        #不同条件下买入卖出的量的设置
        amount_one = 1
        amount_two = 3
        amount_three = 4
        def one():
            kline = get_kline(symbol, '1min', size=1)
            old_kline = get_kline(symbol, '30min', size=1)
            old_price = old_kline['data'][0]['close']
            now_price = (kline['data'][0]['open']+kline['data'][0]['close'])/2
            if now_price <= old_price - 1 :
                  hues.log('买入操作')
                  trade_info = send_order(amount, 'web', symbol, 'buy-limit', price=0)
                  time.sleep(2)
                  trade_price_info = order_matchresults(trade_info['data'])
                  trade_price_one  = trade_price_info['data'][0]['price']
                  huse.info('成功买入' + ',买入价格为 ' + str(round(trade_price_one,2)) + ',买入量为 ' + str(amount_one) )
                two(trade_price_one)
            else :
                hues.log('当前价格为 ' + str(round(now_price,2)) + ' ,' + '基准价格为 ' + str(round(old_price,2)))
                time.sleep(1)
                one()
        def two(trade_price_one):
            kline = get_kline(symbol, '1min', size=1)
            now_price = (kline['data'][0]['open']+kline['data'][0]['close'])/2
            if now_price <= trade_price_one - 1 :
                hues.log('买入操作')
                trade_info = send_order(amount, 'web', symbol, 'buy-limit', price=0)
                time.sleep(2)
                trade_price_info = order_matchresults(trade_info['data'])
                trade_price_one  = trade_price_info['data'][0]['price']
                huse.info('成功买入' + ',买入价格为 ' + str(round(trade_price_one,2)) + ',买入量为 ' + str(amount_one) )
                two(trade_price_one)
            elif :
                hues.log('卖出操作')
                trade_info = send_order(amount, 'web', symbol, 'sell-limit', price=0)
                time.sleep(2)
                trade_price_info = order_matchresults(trade_info['data'])
                trade_price_two  = trade_price_info['data'][0]['price']
                huse.info('成功卖出' + ',买入价格为 ' + str(round(trade_price_two,2)) + ',买入量为 ' + str(amount_one) )
                three(trade_price_two)
            else :
                hues.log('当前价格为 ' + str(round(now_price,2)) + ' ,' + '基准价格为 ' + str(round(old_price,2)))
                time.sleep(1)
                two()
        def three(trade_price_two):
            kline = get_kline(symbol, '1min', size=1)
            now_price = (kline['data'][0]['open']+kline['data'][0]['close'])/2
            if now_price <= trade_price_two - 1 :
                hues.log('买入操作')
                trade_info = send_order(amount, 'web', symbol, 'sell-limit', price=0)
                time.sleep(2)
                trade_price_info = order_matchresults(trade_info['data'])
                huse.info('盈利卖出' + ',卖出价格为 ' + str(round(trade_price_two,2)) + ',卖出量为 ' + str(amount_one) )
                one()
            elif :
                hues.log('卖出操作')
                trade_info = send_order(amount, 'web', symbol, 'sell-limit', price=0)
                time.sleep(2)
                trade_price_info = order_matchresults(trade_info['data'])
                trade_price_two  = trade_price_info['data'][0]['price']
                huse.info('止损卖出' + ',卖出价格为 ' + str(round(trade_price_two,2)) + ',卖出量为 ' + str(amount_one) )
                one()
            else :
                hues.log('当前价格为 ' + str(round(now_price,2)) + ' ,' + '基准价格为 ' + str(round(old_price,2)))
                time.sleep(1)
                three()
        def main():
            one()
        main()
    

      代码总体采用了递归的思想去实现,发现了一个问题,也就是递归有最大次数(递归的数据栈溢出),超出这个次数程序就被中断,计划将在之后使用多线/进程来控制递归。问题作为一个初期的demo,后面将按照面向对象的思想构建API

    相关文章

      网友评论

          本文标题:火币API量化交易Python3

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