python 量化之路 (下)

结合上一期

import time

class mid_class():
    
    def __init__(self, this_exchaneg):
        '''
        初始化数据
        :param this_exchaneg:  FMZ的交易所结构
        '''
        self.init_time = time.time()
        self.exchange = this_exchaneg
        self.name = self.exchange.GetName()  # 返回交易所名称
        self.jyd = self.exchange.GetCurrency()  # 返回操作货币对名称 例:BTC_USD
        
    def get_account(self):
        '''
        账户信息
        :return: 获取信息成功返回True 错误返回False
        '''
        try:
            self.account = self.exchange.GetAccount()  # 返回交易所信息
            self.Balance = self.account['Balance']  # 余额
            self.amount = self.account['Stocks']  # 币种
            self.FrozenBalance = self.account['FrozenBalance']  # 冻结钱
            self.ForzenStocks = self.account['FrozenStocks']  # 冻结币
            return self.account
        except:
            return False

    def get_ticker(self):
        '''
        获取市场信息
        :return:
        '''
        try:
            self.ticker = self.exchange.GetTicker()  # 获取市场信息
            return self.ticker
        except:
            return False

    def get_depth(self):
        '''
        获取深度
        :return:
        '''

        try:
            self.depth = self.exchange.GetDepth()  # 获取交易所订单薄
            self.ask = self.depth['Asks']  # 价格
            self.bid = self.depth['Bids']  # 数量
            return True
        except:
            return False

    def get_oglc_data(self, Period=PERIOD_M5):
        '''
        K线
        :param Period: K线周期 PERIOD_M1指1分钟,PERIOD_M5指5分钟,PERIOD_M15指15分钟,PERIOD_M30指30分钟,PERIOD_H1指1小时,PERIOD_D1指一天
        :return:
        '''
        self.oglc_data = self.exchange.GetRecords(Period)

    def create_order(self, order_tpye, price, account):
        '''
        创建订单
        :param order_tpye: 挂单类型 buy买,sell 卖单
        :param price:  价格
        :param account: 数量
        :return: 挂单id
        '''
        if order_tpye == 'buy':
            try:
                order_id = self.exchange.Buy(price, account)
            except:
                return False
        elif order_tpye == 'sell':
            try:
                order_id = self.exchange.Sell(price, account)
            except:
                return False

        return order_id

    def cancel_order(self, order_id):
        '''
        取消挂单
        order_id: 挂单id号
        :return:
        '''
        return self.exchange.CancelOrder(order_id)

    def refreash_data(self):
        '''
        刷新信息
        :return: 刷新信息: 成功refreash_data_finish
        '''

        if not self.get_account():
            return 'false_get_account'

        if not self.get_ticker():
            return 'false_get_ticker'

        if not self.get_depth():
            return 'false_get_depth'
        try:
            self.get_oglc_data()
        except:
            return 'false_get_K_line_info'

        return 'refreash_data_finish!'

def main():
    test_mid = mid_class(exchange)
    price = 430        # 设定一个基价
    wave = 1          # 波动范围
    amount = 0.01           #  数量
    Log('刷新信息', test_mid.refreash_data())

    while True:
        Sleep(1000)
    # 买卖处理
        Log('Price',price)
        robot_buy = test_mid.create_order('buy', price - wave, amount)
        robot_buy_id = exchange.GetOrder(robot_buy)

        robot_sell = test_mid.create_order('sell', price + wave, amount)
        robot_sell_id = exchange.GetOrder(robot_sell)
        
    # 成功之后返回 Id 和 false
        Log('买单', robot_buy_id)
        Log('卖单', robot_sell_id)
    
        if robot_buy_id['Status'] == 1:
            price = robot_buy_id['Price']
            Log('买单成交,撤销全部订单')
            test_mid.cancel_order(robot_sell_id['Id'])
        elif robot_sell_id['Status'] == 1:
            price = robot_sell_id['Price']
            Log('卖单成交,撤销全部订单')
            test_mid.cancel_order(robot_buy_id['Id'])
        else:
            Log('都没成交')
            test_mid.cancel_order(robot_sell_id['Id'])
            test_mid.cancel_order(robot_buy_id['Id'])
        Log('循环开始')
        Log('账户信息', test_mid.get_account())

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
基于fmz平台开发,仅供学习网格策略,请勿实盘使用

猜你喜欢

转载自blog.csdn.net/weixin_43554217/article/details/108407741