python 量化策略之路(上)

量化策略

个人理解: 就是用程序去做交易员的事情,各大交易所股票,或者区块链里面的比特币。
再通俗易懂点的就是,程序去炒股,炒币

python代码段 基于FMZ平台开发代码

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买,shell 卖单
        :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 = 450  # 设定一个基价
    wave = 50  # 波动范围
    amount = 1  # 数量
    while True:
        Sleep(1000)
        Log('刷新信息', test_mid.refreash_data())
        Log('市场信息', test_mid.get_ticker())

        # 买卖处理
        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)
        Log('账户信息', test_mid.get_account())
        # 成功之后返回 Id 和 false
        Log('买单', robot_buy_id)
        Log('卖单', robot_sell_id)
        if robot_buy_id['Status'] == 1:
            Log('买单成交,撤销全部订单')
            test_mid.cancel_order(robot_sell_id['Id'])
        else:
            test_mid.cancel_order(robot_buy_id['Id'])
            Log('卖单成交,撤销全部订单')

在这里插入图片描述

代码逻辑通顺,只是学习交易,不可实际运用。接口请看FMZ 量化平台api接口文档

猜你喜欢

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