Esempio n. 1
0
 def __init__(self, instrument_id, time_frame, fast_length, slow_length, long_stop, short_stop, start_asset):
     try:
         print("{} {} 双均线多空策略已启动!".format(get_localtime(), instrument_id))   # 程序启动时打印提示信息
         config.loads('config.json')  # 载入配置文件
         self.instrument_id = instrument_id  # 合约ID
         self.time_frame = time_frame  # k线周期
         self.exchange = OKEXFUTURES(config.access_key, config.secret_key, config.passphrase, self.instrument_id)  # 初始化交易所
         self.position = POSITION(self.exchange, self.instrument_id, self.time_frame)  # 初始化potion
         self.market = MARKET(self.exchange, self.instrument_id, self.time_frame)  # 初始化market
         self.indicators = INDICATORS(self.exchange, self.instrument_id, self.time_frame)    # 初始化indicators
         # 在第一次运行程序时,将初始资金数据保存至数据库中
         self.database = "回测"    # 回测时必须为"回测"
         self.datasheet = self.instrument_id.split("-")[0].lower() + "_" + time_frame
         if config.first_run == "true":
             storage.mysql_save_strategy_run_info(self.database, self.datasheet, get_localtime(),
                                             "none", 0, 0, 0, 0, "none", 0, 0, 0, start_asset)
         # 读取数据库中保存的总资金数据
         self.total_asset = storage.read_mysql_datas(0, self.database, self.datasheet, "总资金", ">")[-1][-1]
         self.total_profit = storage.read_mysql_datas(0, self.database, self.datasheet, "总资金", ">")[-1][-2]  # 策略总盈亏
         self.counter = 0  # 计数器
         self.fast_length = fast_length  # 短周期均线长度
         self.slow_length = slow_length  # 长周期均线长度
         self.long_stop = long_stop   # 多单止损幅度
         self.short_stop = short_stop    # 空单止损幅度
         self.contract_value = self.market.contract_value()  # 合约面值,每次获取需发起网络请求,故于此处声明变量,优化性能
     except:
         logger.warning()
Esempio n. 2
0
 def __init__(self, instrument_id, time_frame, start_asset):     # 策略初始化时需传入合约id、k线周期、初始资金参数
     print("{} {} 海龟交易策略已启动!".format(get_localtime(), instrument_id))    # 程序启动时打印提示信息
     config.loads("config.json")     # 载入配置文件
     self.instrument_id = instrument_id  # 合约id
     self.time_frame = time_frame    # k线周期
     self.exchange = OKEXFUTURES(config.access_key, config.secret_key, config.passphrase, self.instrument_id, leverage=20)   # 初始化交易所
     self.market = MARKET(self.exchange, self.instrument_id, self.time_frame)    # 初始化market
     self.position = POSITION(self.exchange, self.instrument_id, self.time_frame)    # 初始化position
     self.indicators = INDICATORS(self.exchange, self.instrument_id, self.time_frame)    # 初始化indicators
     self.database = "回测"  # 如从purequant服务器的数据库上获取历史k线数据进行回测,必须为"回测"
     self.datasheet = self.instrument_id.split("-")[0].lower() + "_" + time_frame    # 数据表
     if config.first_run == "true":  # 程序第一次启动时保存数据,实盘时如策略中止再重启时,可以将配置文件中的first_run改成"false",程序再次启动会直接读取数据库中保存的数据
         storage.mysql_save_strategy_run_info(self.database, self.datasheet, get_localtime(),
                                              "none", 0, 0, 0, 0, "none", 0, 0, 0, start_asset)
     # 读取数据库中保存的总资金、总盈亏数据
     self.total_asset = storage.read_mysql_datas(0, self.database, self.datasheet, "总资金", ">")[-1][-1]
     self.total_profit = storage.read_mysql_datas(0, self.database, self.datasheet, "总资金", ">")[-1][-2]  # 策略总盈亏
     # 一些策略参数
     self.contract_value = self.market.contract_value()  # 合约面值
     self.ATRLength = 20    # 平均波动周期
     self.boLength = 20  # 短周期 BreakOut Length
     self.fsLength = 55  # 长周期 FailSafe Length
     self.teLength = 10   # 离市周期 Trailing Exit Length
     self.LastProfitableTradeFilter = 1   # 使用入市过滤条件
     self.PreBreakoutFailure = False  # 前一次是否突破失败
     self.CurrentEntries = 0  # 当前持仓的开仓次数
     self.counter = 0    # 计数器,用以控制单根bar最大交易次数
Esempio n. 3
0
 def __init__(self, instrument_id, time_frame, fast_length, slow_length, long_stop, short_stop, start_asset):
     config.loads('config.json')  # 载入配置文件
     self.instrument_id = instrument_id  # 合约ID
     self.time_frame = time_frame  # k线周期
     self.exchange = OKEXFUTURES(config.access_key, config.secret_key, config.passphrase, self.instrument_id)  # 初始化交易所
     self.position = POSITION(self.exchange, self.instrument_id, self.time_frame)  # 初始化potion
     self.market = MARKET(self.exchange, self.instrument_id, self.time_frame)  # 初始化market
     self.indicators = INDICATORS(self.exchange, self.instrument_id, self.time_frame)    # 初始化indicators
     # 在第一次运行程序时,将初始资金数据保存至数据库中
     self.database = "回测"    # 无论实盘或回测,此处database名称可以任意命名
     self.datasheet = self.instrument_id.split("-")[0].lower() + "_" + time_frame
     if config.first_run:
         storage.mysql_save_strategy_run_info(self.database, self.datasheet, "策略参数为" + str(fast_length) + "&" + str(slow_length),
                                         "none", 0, 0, 0, 0, "none", 0, 0, 0, start_asset)
     # 读取数据库中保存的总资金数据
     self.total_asset = storage.read_mysql_datas(0, self.database, self.datasheet, "总资金", ">")[-1][-1]
     self.counter = 0  # 计数器
     self.fast_length = fast_length  # 短周期均线长度
     self.slow_length = slow_length  # 长周期均线长度
     self.long_stop = long_stop   # 多单止损幅度
     self.short_stop = short_stop    # 空单止损幅度
     self.total_profit = 0
     self.contract_value = self.market.contract_value()  # 合约面值,每次获取需发起网络请求,故于此处声明变量,优化性能
     # 声明持仓方向、数量与价格变量,每次开平仓后手动重新赋值
     self.hold_direction = "none"
     self.hold_amount = 0
     self.hold_price = 0
     print("{} {} 双均线多空策略已启动!".format(get_localtime(), instrument_id))  # 程序启动时打印提示信息
Esempio n. 4
0
    def __init__(self, platform, symbol, time_frame):

        self.__platform = platform
        self.__symbol = symbol
        self.__time_frame = time_frame
        self.__market = MARKET(self.__platform, self.__symbol,
                               self.__time_frame)

        # pull some data
        self.__indicators = INDICATORS(self.__platform, self.__symbol,
                                       self.__time_frame)
        self.__kline = platform.get_kline(self.__time_frame)
        self.__kline.reverse()

        # format it in pandas
        try:  # dataframe有7列的情况
            self.__df = pd.DataFrame(self.__kline,
                                     columns=[
                                         'time', 'open', 'high', 'low',
                                         'close', 'volume', 'currency_volume'
                                     ])
            self.__df = self.__df.astype({
                'time': 'datetime64[ns]',
                'open': 'float64',
                'close': 'float64',
                'high': 'float64',
                'low': 'float64',
                'volume': 'float64',
                'currency_volume': 'float64'
            })
        except:  # dataframe只有6列的情况,如okex的现货k线数据
            self.__df = pd.DataFrame(
                self.__kline,
                columns=['time', 'open', 'high', 'low', 'close', 'volume'])
            self.__df = self.__df.astype({
                'time': 'datetime64[ns]',
                'open': 'float64',
                'close': 'float64',
                'high': 'float64',
                'low': 'float64',
                'volume': 'float64'
            })

        # create three plot 创建三层图纸,第一层画k线,第二层画成交量,第三层画一些适宜于副图显示的指标
        fplt.foreground = '#FFFFFF'  # 前景色
        fplt.background = '#333333'  # 背景色
        fplt.odd_plot_background = '#333333'  # 第二层图纸的背景色
        fplt.cross_hair_color = "#FFFFFF"  # 准星的颜色
        self.__ax, self.__ax2, self.__ax3 = fplt.create_plot(symbol, rows=3)

        # plot candle sticks
        candles = self.__df[['time', 'open', 'close', 'high', 'low']]
        fplt.candlestick_ochl(candles, ax=self.__ax)

        # overlay volume on the plot
        volumes = self.__df[['time', 'open', 'close', 'volume']]
        fplt.volume_ocv(volumes, ax=self.__ax2)
        fplt.add_legend("VOLUME", self.__ax2)  # 增加"VOLUME"图例
Esempio n. 5
0
 def __init__(self, databank, database, data_sheet, exchange, instrument_id, time_frame):
     print("{} {} 持仓同步功能已启动!".format(get_localtime(), instrument_id))
     self.__databank = databank
     self.__database = database
     self.__datasheet = data_sheet
     self.__exchange = exchange
     self.__instrument_id = instrument_id
     self.__time_frame = time_frame
     self.__position = POSITION(self.__exchange, self.__instrument_id, self.__time_frame)
     self.__market = MARKET(self.__exchange, self.__instrument_id, self.__time_frame)
     self.__overprice_range = config.overprice_range
 def __init__(self, instrument_id, time_frame, bollinger_lengths,
              filter_length, start_asset):
     try:
         # 策略启动时控制台输出提示信息
         print("{} {} 布林强盗突破策略已启动!".format(get_localtime(),
                                           instrument_id))  # 程序启动时打印提示信息
         config.loads("config.json")  # 载入配置文件
         # 初始化
         self.instrument_id = instrument_id  # 合约ID
         self.time_frame = time_frame  # k线周期
         self.exchange = OKEXFUTURES(config.access_key, config.secret_key,
                                     config.passphrase,
                                     self.instrument_id)  # 交易所
         self.market = MARKET(self.exchange, self.instrument_id,
                              self.time_frame)  # 行情
         self.position = POSITION(self.exchange, self.instrument_id,
                                  self.time_frame)  # 持仓
         self.indicators = INDICATORS(self.exchange, self.instrument_id,
                                      self.time_frame)  # 指标
         # 在第一次运行程序时,将初始资金、总盈亏等数据保存至数据库中
         self.database = "回测"  # 数据库,回测时必须为"回测"
         self.datasheet = self.instrument_id.split(
             "-")[0].lower() + "_" + time_frame  # 数据表
         if config.first_run == "true":
             storage.mysql_save_strategy_run_info(self.database,
                                                  self.datasheet,
                                                  get_localtime(), "none",
                                                  0, 0, 0, 0, "none", 0, 0,
                                                  0, start_asset)
         # 读取数据库中保存的总资金数据
         self.total_asset = storage.read_mysql_datas(
             0, self.database, self.datasheet, "总资金", ">")[-1][-1]
         self.total_profit = storage.read_mysql_datas(
             0, self.database, self.datasheet, "总资金", ">")[-1][-2]  # 策略总盈亏
         # 策略参数
         self.contract_value = self.market.contract_value()  # 合约面值
         self.counter = 0  # 计数器
         self.bollinger_lengths = bollinger_lengths  # 布林通道参数
         self.filter_length = filter_length  # 过滤器参数
         self.out_day = 50  # 自适应出场ma的初始值为50,开仓后赋值为布林通道参数的值
     except:
         logger.warning()
Esempio n. 7
0
 def __init__(self, instrument_id, time_frame, fast_length, slow_length,
              long_stop, short_stop, start_asset, precision):
     try:
         print("{} {} 双均线多空策略已启动!".format(get_localtime(),
                                          instrument_id))  # 程序启动时打印提示信息
         config.loads('config.json')  # 载入配置文件
         self.instrument_id = instrument_id  # 合约ID
         self.time_frame = time_frame  # k线周期
         self.precision = precision  # 精度,即币对的最小交易数量
         self.exchange = OKEXSPOT(config.access_key, config.secret_key,
                                  config.passphrase,
                                  self.instrument_id)  # 初始化交易所
         self.position = POSITION(self.exchange, self.instrument_id,
                                  self.time_frame)  # 初始化potion
         self.market = MARKET(self.exchange, self.instrument_id,
                              self.time_frame)  # 初始化market
         self.indicators = INDICATORS(self.exchange, self.instrument_id,
                                      self.time_frame)  # 初始化indicators
         # 在第一次运行程序时,将初始资金数据保存至数据库中
         self.database = "回测"  # 回测时必须为"回测"
         self.datasheet = self.instrument_id.split(
             "-")[0].lower() + "_" + time_frame
         if config.first_run == "true":
             storage.mysql_save_strategy_run_info(self.database,
                                                  self.datasheet,
                                                  get_localtime(), "none",
                                                  0, 0, 0, 0, "none", 0, 0,
                                                  0, start_asset)
         # 读取数据库中保存的总资金数据
         self.total_asset = storage.read_mysql_datas(
             0, self.database, self.datasheet, "总资金", ">")[-1][-1]
         self.total_profit = storage.read_mysql_datas(
             0, self.database, self.datasheet, "总资金", ">")[-1][-2]  # 策略总盈亏
         self.counter = 0  # 计数器
         self.fast_length = fast_length  # 短周期均线长度
         self.slow_length = slow_length  # 长周期均线长度
         self.long_stop = long_stop  # 多单止损幅度
         self.short_stop = short_stop  # 空单止损幅度
         self.hold_price = 0  # 注意:okex的现货没有获取持仓均价的接口,故需实盘时需要手动记录入场价格。此种写法对于不同的交易所是通用的。
         # 此种写法,若策略重启,持仓价格会回归0
     except:
         logger.warning()
from purequant.trade import HUOBISPOT
from purequant.market import MARKET
from purequant.indicators import INDICATORS
from purequant.position import POSITION

# 账户和策略参数等信息
accessy_key = 'your access_key'
secret_key = 'your secret_key'
instrument_id = 'ETC-USDT'
time_frame = '1d'

# 初始化交易所、行情模块与指标等模块
exchange = HUOBISPOT(accessy_key, secret_key, instrument_id)
market = MARKET(exchange, instrument_id, time_frame)
indicators = INDICATORS(exchange, instrument_id, time_frame)
position = POSITION(exchange, instrument_id, time_frame)

# 下单交易,买入和卖出
# info = exchange.buy(7.35, 0.02)           # 以7.35的价格买入0.02个ETC
# info = exchange.sell(7.35, 0.01)            # 卖出0.02个ETC

# 获取行情信息
# info = exchange.get_kline(time_frame)      # 获取k线数据
# info = market.last()                      # 获取ETC-USDT的最新成交价
# info = market.open(-1)                      # 获取ETC-USDT的当日开盘价
# info = market.high(-1)                      # 获取ETC-USDT的当日最高价
# info = market.low(-1)                      # 获取ETC-USDT的当日最低价
# info = market.close(-1)                      # 获取ETC-USDT的当日收盘价

# 持仓信息
# info = position.amount()                  # 获取ETC-USDT交易对的ETC可用余额
Esempio n. 9
0
 def __init__(self, platform, instrument_id, time_frame):
     self.__platform = platform
     self.__instrument_id = instrument_id
     self.__time_frame = time_frame
     self.__market = MARKET(self.__platform, self.__instrument_id, self.__time_frame)