音乐播放器
LightHunter
 
Powered by Gridea | Theme: Fog
载入天数...
载入时分秒...

量化交易——FMZ平台交易框架

  热度: loading...

现货交易基本框架概览

def main():
    btc_sell_flag = False #btc买入标志位(测试用)
    btc_buy_flag = False #btc卖出标志位(测试用)

    #交易策略所需参数 本次案例为MACD相关定义参数
    dea_sub_dif_new = 0
    dea_sub_dif_current = 0
    dif_sub_dea_new = 0
    dif_sub_dea_current = 0
    dif_buy_current = 0
    macd_buy_flag = False
    macd_sell_flag = False

    # 交易标志位设定
    BUY_FLAG = False
    SELL_FLAG = False
    ALREADY_BUY_FLAG = False
    ALREADY_SELL_FLAG = True

    while True:
        account = exchange.GetAccount()
        Balance = account.Balance #USTD剩余
        Stocks = account.Stocks #BTC剩余
        bar = exchange.GetRecords(60) 	# 获取K线数组
        #可否满足买入综合判定
        macd_buy_flag,dif_buy_current,dea_sub_dif_current = macd_buy_flag_function(bar,dea_sub_dif_current)
        macd_sell_flag,dif_sub_dea_current = macd_sell_flag_function(bar,dif_sub_dea_current,dif_buy_current)

        if macd_buy_flag == True :
            BUY_FLAG = True
            SELL_FLAG = False
        #可否满足卖出综合判定
        if macd_sell_flag == True :
            BUY_FLAG = False
            SELL_FLAG = True
        
        if BUY_FLAG and ALREADY_BUY_FLAG == False:
            exchange.Buy(-1, (Balance-1))#USTD剩余全仓买入
            ALREADY_BUY_FLAG = True
            ALREADY_SELL_FLAG = False
            BUY_FLAG = False
            macd_buy_flag = False

        if SELL_FLAG and ALREADY_SELL_FLAG == False:
            Log("round(Stocks,6):",round(Stocks,6))
            exchange.Sell(-1, (round(Stocks,6)-0.000001))
            ALREADY_SELL_FLAG = True
            ALREADY_BUY_FLAG = False
            SELL_FLAG = False
            macd_sell_flag = False
            
        #标志位重置    
        BUY_FLAG = False
        SELL_FLAG = False
        macd_buy_flag = False
        macd_sell_flag = False
#买入数额和卖出数额有问题
        Sleep(1000)

框架解释说明

  1. 进入主函数后首先定义本次策略所需变量,并进行初始化,也就是如下这一部分,该处的变量随策略的改变而发生改变
    #交易策略所需参数 本次案例为MACD相关定义参数
    dea_sub_dif_new = 0
    dea_sub_dif_current = 0
    dif_sub_dea_new = 0
    dif_sub_dea_current = 0
    dif_buy_current = 0
    macd_buy_flag = False
    macd_sell_flag = False

    # 交易标志位设定
    BUY_FLAG = False
    SELL_FLAG = False
    ALREADY_BUY_FLAG = False
    ALREADY_SELL_FLAG = True
  1. 而后进入while循环,首先在每一次循环的初始时刻调出当前仓位,余额,K线图
    while True:
        account = exchange.GetAccount()
        Balance = account.Balance #USTD剩余
        Stocks = account.Stocks #BTC剩余
        bar = exchange.GetRecords(60) 	# 获取K线数组
  1. 而后根据不同策略判断当前是否符合该策略的买/卖点,并返回该策略买卖标志位(macd_buy_flag),以及下一次循环可能用到的变量(dif_buy_current,dea_sub_dif_current)
        #可否满足买入综合判定
        macd_buy_flag,dif_buy_current,dea_sub_dif_current = macd_buy_flag_function(bar,dea_sub_dif_current)
        macd_sell_flag,dif_sub_dea_current = macd_sell_flag_function(bar,dif_sub_dea_current,dif_buy_current)
  1. 根据各类策略进行融合,综合判定是否可以进行买卖操作,并对整个系统的买卖标志位进行赋值:
        if macd_buy_flag == True :
            BUY_FLAG = True
            SELL_FLAG = False

        if macd_sell_flag == True :
            BUY_FLAG = False
            SELL_FLAG = True
  1. 根据整个系统的买卖标志位进行决定是否进行买入卖出操作,由于交易系统有小数后位数限制,所以使用round()函数进行精度控制。需要注意的是exchange.Buy()函数接受的为USDT单位,exchange.Sell()函数接受的为BTC单位
        if BUY_FLAG and ALREADY_BUY_FLAG == False:
            exchange.Buy(-1, (Balance-1))#USTD剩余全仓买入
            ALREADY_BUY_FLAG = True
            ALREADY_SELL_FLAG = False
            BUY_FLAG = False
            macd_buy_flag = False

        if SELL_FLAG and ALREADY_SELL_FLAG == False:
            Log("round(Stocks,6):",round(Stocks,6))
            exchange.Sell(-1, (round(Stocks,6)-0.000001))
            ALREADY_SELL_FLAG = True
            ALREADY_BUY_FLAG = False
            SELL_FLAG = False
            macd_sell_flag = False
  1. 最后在循环结束前对相关标志位进行复位
        #标志位重置    
        BUY_FLAG = False
        SELL_FLAG = False
        macd_buy_flag = False
        macd_sell_flag = False

请到客户端“主题--自定义配置--valine”中填入ID和KEY