def get_sell_condition(symbol_value): period_value = misc.getConfigKeyValueByKeyName('config.ini', 'config', 'period_value') price_buy = float(misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'price_buy')) price_max = float(misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'price_max')) k_line = api.get_k_line(symbol_value, period_value, 11) k_line_0_close = k_line[0]['close'] price_low = price_max * (1 - down_percent) price_min = price_buy * (1 - down_percent_max) print('price_buy = %s' % price_buy) #print('price_max = %s' % price_max) #print('price_low = %s' % price_low) #print('price_min = %s' % price_min) print('close = %s' % k_line_0_close) print('percent = %.4f%%' % ((k_line_0_close / price_buy - 1) * 100)) condition1 = k_line_0_close < k_line[0]['open']#阴线 print('阴线 = %s' % condition1) if k_line_0_close > price_max: misc.setConfigKeyValue('config.ini', symbol_value, 'price_max', k_line_0_close) price_max = k_line_0_close if price_min > k_line_0_close: return True if price_low > k_line_0_close: return True else: return False
def tactics2(operationType): try: print(misc.getTimeStr()) ma5Value, closeValue, fatherMa5Slope = getMa5AndCloseAndFatherMa5Slope( ) operation, orderId = isBuyOrSellByMa5ValueAndCloseValue( operationType, ma5Value, closeValue, fatherMa5Slope) misc.setConfigKeyValue('config.ini', symbolValue, 'type', operation) if orderId: time.sleep(30) if operation == 'sell': tempOrder = getOrderInfo(orderId) fieldCashAmount = float(tempOrder['field-cash-amount']) fieldFees = float(tempOrder['field-fees']) usdt = getFloatStr(str(fieldCashAmount - fieldFees)) misc.setConfigKeyValue('config.ini', symbolValue, 'usdt', usdt) orderInfo = getMatchResults()[0] print(orderInfo) content = '<html>' content += '<p>symbol(交易对)=%s</p>' % orderInfo['symbol'] content += '<p>filled-amount(订单数量)=%s</p>' % orderInfo[ 'filled-amount'] content += '<p>filled-fees(已成交手续费)=%s</p>' % orderInfo[ 'filled-fees'] content += '<p>price(成交价格)=%s</p>' % orderInfo['price'] content += '<p>type(订单类型(buy-market:市价买, sell-market:市价卖))=%s</p>' % orderInfo[ 'type'] content += '<p>%s</p>' % str(orderInfo) content += '</html>' misc.sendEmail(mailHost, mailUser, mailPass, receivers, '交易报告', content) except Exception as e: print(e)
def tactics1(operationType): print(misc.getTimeStr()) periodStr = '30min' kLine = getKLine(periodStr, '15') smallMa = getMa(kLine, 3) bigMa = getMa(kLine, 14) checkCross(operationType, kLine[0], smallMa, bigMa) operation, orderId = checkSignal() if orderId: misc.setConfigKeyValue('config.ini', symbolValue, 'type', operation) global buySignal buySignal = 0 global sellSignal sellSignal = 0 time.sleep(10) if operation == 'sell': tempOrder = getOrderInfo(orderId) fieldCashAmount = float(tempOrder['field-cash-amount']) fieldFees = float(tempOrder['field-fees']) usdt = getFloatStr(str(fieldCashAmount - fieldFees)) misc.setConfigKeyValue('config.ini', symbolValue, 'usdt', usdt) orderInfo = getMatchResults()[0] print(orderInfo) content = '<html>' content += '<p>symbol(交易对)=%s</p>' % orderInfo['symbol'] content += '<p>type(订单类型)=%s</p>' % orderInfo['type'] content += '<p>price(成交价格)=%s</p>' % orderInfo['price'] content += '<p>filled-amount(订单数量)=%s</p>' % orderInfo['filled-amount'] content += '<p>filled-fees(已成交手续费)=%s</p>' % orderInfo['filled-fees'] content += '<p>%s</p>' % str(orderInfo) content += '</html>' misc.sendEmail(mailHost, mailUser, mailPass, receivers, '交易报告', content)
def tactics1(operationType): #try: print(misc.getTimeStr()) #获取均线斜率 period='60min' ma4LastSlope = getLastMASlope(period,4)[0] ma11LastSlope = getLastMASlope(period,11)[0] ma2LastSlope = getLastMASlope(period,2)[0] ma3LastSlope = getLastMASlope(period,3)[0] ma5LastSlope = getLastMASlope(period,5)[0] lastClose = getLastClose(period) price=getLastBuyOrderPrice() print('ma2=',ma2LastSlope,'\tma3=',ma3LastSlope,'\tma4=',ma4LastSlope,'\tlastClose=',lastClose,'\tprice=',price) checkOperation(operationType,ma4LastSlope,ma11LastSlope,ma2LastSlope,ma3LastSlope,ma5LastSlope,lastClose,price) operation,orderId=checkSignal() if orderId: misc.setConfigKeyValue('config.ini',symbolValue,'type',operation) global buySignal buySignal=0 global sellSignal sellSignal=0 time.sleep(10) if operation == 'sell': tempOrder=getOrderInfo(orderId) fieldCashAmount=float(tempOrder['field-cash-amount']) fieldFees=float(tempOrder['field-fees']) usdt=getFloatStr(str(fieldCashAmount-fieldFees)) misc.setConfigKeyValue('config.ini',symbolValue,'usdt',usdt) orderInfo=getMatchResults()[0] print(orderInfo) content='<html>' content+='<p>symbol(交易对)=%s</p>' % orderInfo['symbol'] content+='<p>filled-amount(订单数量)=%s</p>' % orderInfo['filled-amount'] content+='<p>filled-fees(已成交手续费)=%s</p>' % orderInfo['filled-fees'] content+='<p>price(成交价格)=%s</p>' % orderInfo['price'] content+='<p>type(订单类型(buy-market:市价买, sell-market:市价卖))=%s</p>' % orderInfo['type'] content+='<p>%s</p>' % str(orderInfo) content+='</html>' misc.sendEmail(mailHost, mailUser, mailPass, receivers, '交易报告', content)
def get_sell_condition(symbol_value): period_value = misc.getConfigKeyValueByKeyName('config.ini', 'config', 'period_value') k_line = api.get_k_line(symbol_value, period_value, 11) k_line_0_close = k_line[0]['close'] condition1 = k_line_0_close < k_line[0]['open'] #阴线 k_line_1_close = k_line[1]['close'] k_line_1_time = k_line[1]['id'] k_line_time = int( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'k_line_time')) x_value = (k_line_1_time - k_line_time) / 60 / 60 k_value = float( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'k_value')) b_value = float( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'b_value')) y_value = k_value * x_value + b_value if (k_line_1_close > y_value * up_point and x_value > 0): y_value_repair = y_value + (k_line_1_close - y_value) / 10 k_value_repair = (y_value_repair - b_value) / x_value print('原始k k_value=%s' % k_value) print('斜率k修正 k_value_repair=%s' % k_value_repair) misc.setConfigKeyValue('config.ini', symbol_value, 'k_value', str(k_value_repair)) #return False and condition1 k_value = float( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'k_value')) x_value = x_value + 1 y_value = k_value * x_value + b_value print('k_line_1时间 x=%s' % x_value) print('直线斜率 k=%s' % k_value) print('常数项 b=%s' % b_value) print('理想市价 y=%s' % y_value) print('实际市价 close=%s' % k_line_0_close) if (k_line_0_close < y_value * down_point): return True and condition1 else: return False and condition1
def main(symbol_value): print(misc.getTimeStr()) print(symbol_value) operationType = misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'type') buy_signal = int(misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'buy_signal')) sell_signal = int(misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'sell_signal')) buy_signal_max = int(misc.getConfigKeyValueByKeyName('config.ini', 'config', 'buy_signal_max')) sell_signal_max = int(misc.getConfigKeyValueByKeyName('config.ini', 'config', 'sell_signal_max')) condition = operationType == 'sell' order_id = None if condition:#买入判断 condition1, condition2, condition3 = get_buy_condition(symbol_value) if condition1 and condition2 and condition3: print('买入信号+1') buy_signal = buy_signal + 1 print('buy_signal = %s' % buy_signal) misc.setConfigKeyValue('config.ini', symbol_value, 'buy_signal', buy_signal) else: misc.setConfigKeyValue('config.ini', symbol_value, 'buy_signal', 0) else:#卖出判断 condition = get_sell_condition(symbol_value) if condition: print('卖出信号+1') sell_signal = sell_signal + 1 print('sell_signal = %s' % sell_signal) misc.setConfigKeyValue('config.ini', symbol_value, 'sell_signal', sell_signal) else: misc.setConfigKeyValue('config.ini', symbol_value, 'sell_signal', 0) if buy_signal >= buy_signal_max: #买入操作 amount = misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'money_value') #amount = api.get_balance(account_id, money_name) order_id = api.do_place(account_id, amount, symbol_value, 'buy-market') misc.setConfigKeyValue('config.ini', symbol_value, 'buy_signal', 0) if sell_signal >= sell_signal_max: #卖出操作 coin_name = misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'coin_name') amount = api.get_balance(account_id, coin_name) if coin_name == 'knc' or coin_name == 'qsp': amount = misc.get_float_str(amount,0) order_id = api.do_place(account_id, amount, symbol_value, 'sell-market') misc.setConfigKeyValue('config.ini', symbol_value, 'sell_signal', 0) if order_id: time.sleep(2) order_detail = api.get_order_detail(order_id)[0] if order_detail['type'] == 'buy-market': misc.setConfigKeyValue('config.ini', symbol_value, 'type', 'buy') misc.setConfigKeyValue('config.ini', symbol_value, 'price_buy', order_detail['price']) misc.setConfigKeyValue('config.ini', symbol_value, 'price_max', order_detail['price']) coin_value = misc.get_float_str(str(float(order_detail['filled-amount']) - float(order_detail['filled-fees']))) misc.setConfigKeyValue('config.ini', symbol_value, 'coin_value', coin_value) if order_detail['type'] == 'sell-market': order_info = api.get_order_info(order_id) misc.setConfigKeyValue('config.ini', symbol_value, 'type', 'sell') misc.setConfigKeyValue('config.ini', symbol_value, 'price_sell', order_detail['price']) money_value = misc.get_float_str(str(float(order_info['field-cash-amount']) - float(order_info['field-fees'])),6) misc.setConfigKeyValue('config.ini', symbol_value, 'money_value', money_value) '''
#content+='<p>filled-fees(已成交手续费)=%s</p>' % order_detail['filled-fees'] content+='<p>type(订单类型)=%s</p>' % order_detail['type'] content+='<p>%s</p>' % str(order_detail) content+='</html>' misc.sendEmail(mail_host, mail_user, mail_pass, receivers, '%s_%s_transaction_report' % (symbol_value, order_detail['type']), content) ''' #交易对 account_id = api.get_account_id() num = 0 while True: symbol_value_list = misc.getConfigKeyValueByKeyName('config.ini', 'config', 'symbol_value_list').split(',') if num == 0: num = num + 1 for symbol_value in symbol_value_list: misc.setConfigKeyValue('config.ini', symbol_value, 'buy_signal', '0') misc.setConfigKeyValue('config.ini', symbol_value, 'sell_signal', '0') down_percent = float(misc.getConfigKeyValueByKeyName('config.ini', 'config', 'down_percent')) down_percent_max = float(misc.getConfigKeyValueByKeyName('config.ini', 'config', 'down_percent_max')) second = int(misc.getConfigKeyValueByKeyName('config.ini', 'config', 'second')) for symbol_value in symbol_value_list: try: main(symbol_value) print() except Exception as e: print(e) #misc.sendEmail(mail_host, mail_user, mail_pass, receivers, 'error_report_%s' % misc.getTimeStr(), e) except ApiError as e: print(e) #misc.sendEmail(mail_host, mail_user, mail_pass, receivers, 'error_report_%s' % misc.getTimeStr(), e) except ApiNetworkError as e:
mailHost = misc.getConfigKeyValueByKeyName('config.ini', 'mail', 'mailHost') mailUser = misc.getConfigKeyValueByKeyName('config.ini', 'mail', 'mailUser') receivers = misc.getConfigKeyValueByKeyName('config.ini', 'mail', 'receivers').split(',') okexSpot = OkexSpot(apikey, secretkey) symbol = 'btc_usdt' highValue = misc.getConfigKeyValueByKeyName('config.ini', 'ticker', 'high') while True: ticker = okexSpot.ticker(symbol) high = ticker['ticker']['high'] print(ticker) if high > highValue: content = '<html>' content += '<p>返回数据时服务器时间:%s</p>' % misc.getTimeStrWithUnixTimestamp( int(ticker['date'])) content += '<p>买一价:%s</p>' % ticker['ticker']['buy'] content += '<p style="font-weight: bold; color: red;">最高价:%s</p>' % ticker[ 'ticker']['high'] content += '<p>最新成交价:%s</p>' % ticker['ticker']['last'] content += '<p>最低价:%s</p>' % ticker['ticker']['low'] content += '<p>卖一价:%s</p>' % ticker['ticker']['sell'] content += '<p>成交量(最近的24小时):%s</p>' % ticker['ticker']['vol'] content += '</html>' highValue = high misc.setConfigKeyValue('config.ini', 'ticker', 'high', high) misc.sendEmail(mailHost, mailUser, mailPass, receivers, 'BTC_USDT最新最高价', content) time.sleep(15)
def main(): print(misc.getTimeStr()) condition1, condition2, condition3, condition4, condition5, condition6, condition7, condition8 = get_condition( ) order_id = None global buy_signal global sell_signal if condition1 and condition2 and ( (condition3 and condition4) or (not condition4 and condition5 and condition7)) and condition6: print('买入信号+1') buy_signal = buy_signal + 1 else: buy_signal = 0 if not condition1 and not condition2 and not condition6: #(condition4 or (not condition4 and not condition5)): print('卖出信号+1') sell_signal = sell_signal + 1 else: sell_signal = 0 if buy_signal >= buy_signal_max: #买入操作 #amount = misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'usdt') amount = api.get_balance(account_id, money_name) order_id = api.do_place(account_id, amount, symbol_value, 'buy-market') buy_signal = 0 if sell_signal >= sell_signal_max: #卖出操作 amount = api.get_balance(account_id, coin_name) order_id = api.do_place(account_id, amount, symbol_value, 'sell-market') sell_signal = 0 if order_id: time.sleep(10) order_detail = api.get_order_detail(order_id)[0] if order_detail['type'] == 'buy-market': misc.setConfigKeyValue('config.ini', symbol_value, 'type', 'buy') misc.setConfigKeyValue('config.ini', symbol_value, 'price_buy', order_detail['price']) if order_detail['type'] == 'sell-market': misc.setConfigKeyValue('config.ini', symbol_value, 'type', 'sell') misc.setConfigKeyValue('config.ini', symbol_value, 'price_sell', order_detail['price']) content = '<html>' if order_detail['type'] == 'sell-market': price_buy = float( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'price_buy')) price_sell = float( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'price_sell')) content += '<p>盈亏=%.4f%%</p>' % ( (price_sell / price_buy - 1 - 0.005) * 100) content += '<p>created-at(交易时间)=%s</p>' % misc.getTimeStrWithUnixTimestamp( int(order_detail['created-at'] / 1000)) content += '<p>symbol(交易对)=%s</p>' % order_detail['symbol'] content += '<p>price(成交价格)=%s</p>' % order_detail['price'] #content+='<p>filled-amount(订单数量)=%s</p>' % order_detail['filled-amount'] #content+='<p>filled-fees(已成交手续费)=%s</p>' % order_detail['filled-fees'] content += '<p>type(订单类型(buy-market:市价买, sell-market:市价卖))=%s</p>' % order_detail[ 'type'] content += '<p>%s</p>' % str(order_detail) content += '<p>1-最后一次操作为卖出 = %s</p>' % condition1 content += '<p>2-市价高于均线 = %s</p>' % condition2 content += '<p>3-斜率之和大于0 = %s</p>' % condition3 content += '<p>4-波动幅度足够大 = %s</p>' % condition4 content += '<p>5-后半段斜率之和大于前半段三斜率之和 = %s</p>' % condition5 content += '<p>7-后半段斜率之和大于0 = %s</p>' % condition7 content += '<p>6-是否阳线 = %s</p>' % condition6 content += '<p>condition1 and condition2 and ((condition3 and condition4) or (not condition4 and condition5 and condition7)) and condition6</p>' content += '<p>not condition1 and not condition2 and not condition6</p>' content += '</html>' misc.sendEmail(mail_host, mail_user, mail_pass, receivers, '%s_%s_交易报告' % (symbol_value, order_detail['type']), content)
def main(symbol_value): print(misc.getTimeStr()) print(symbol_value) operationType = misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'type') buy_signal = int( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'buy_signal')) sell_signal = int( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'sell_signal')) buy_signal_max = int( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'buy_signal_max')) sell_signal_max = int( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'sell_signal_max')) condition = operationType == 'sell' order_id = None if condition: #买入判断 k_line_1_id, ma_line, slope_list, condition1, condition2, condition3 = get_buy_condition( symbol_value) if condition1 and condition2 and condition3: print('买入信号+1') buy_signal = buy_signal + 1 misc.setConfigKeyValue('config.ini', symbol_value, 'buy_signal', buy_signal) else: buy_signal = 0 misc.setConfigKeyValue('config.ini', symbol_value, 'buy_signal', buy_signal) else: #卖出判断 condition = get_sell_condition(symbol_value) if condition: print('卖出信号+1') sell_signal = sell_signal + 1 misc.setConfigKeyValue('config.ini', symbol_value, 'sell_signal', sell_signal) else: sell_signal = 0 misc.setConfigKeyValue('config.ini', symbol_value, 'sell_signal', sell_signal) if buy_signal >= buy_signal_max: #买入操作 amount = misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'money_value') #amount = api.get_balance(account_id, money_name) order_id = api.do_place(account_id, amount, symbol_value, 'buy-market') buy_signal = 0 misc.setConfigKeyValue('config.ini', symbol_value, 'buy_signal', buy_signal) if sell_signal >= sell_signal_max: #卖出操作 coin_name = misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'coin_name') amount = api.get_balance(account_id, coin_name) order_id = api.do_place(account_id, amount, symbol_value, 'sell-market') sell_signal = 0 misc.setConfigKeyValue('config.ini', symbol_value, 'sell_signal', sell_signal) if order_id: time.sleep(2) order_detail = api.get_order_detail(order_id)[0] if order_detail['type'] == 'buy-market': misc.setConfigKeyValue('config.ini', symbol_value, 'type', 'buy') misc.setConfigKeyValue('config.ini', symbol_value, 'price_buy', order_detail['price']) misc.setConfigKeyValue( 'config.ini', symbol_value, 'b_value', float(order_detail['price']) - ma_line[0] + ma_line[1]) misc.setConfigKeyValue('config.ini', symbol_value, 'k_value', ma_line[0] - ma_line[1]) misc.setConfigKeyValue('config.ini', symbol_value, 'k_line_time', k_line_1_id) coin_value = misc.get_float_str( str( float(order_detail['filled-amount']) - float(order_detail['filled-fees']))) misc.setConfigKeyValue('config.ini', symbol_value, 'coin_value', coin_value) if order_detail['type'] == 'sell-market': misc.setConfigKeyValue('config.ini', symbol_value, 'type', 'sell') misc.setConfigKeyValue('config.ini', symbol_value, 'price_sell', order_detail['price']) money_value = misc.get_float_str( str( float(order_detail['filled-amount']) - float(order_detail['filled-fees']))) misc.setConfigKeyValue('config.ini', symbol_value, 'money_value', money_value) content = '<html>' if order_detail['type'] == 'sell-market': price_buy = float( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'price_buy')) price_sell = float( misc.getConfigKeyValueByKeyName('config.ini', symbol_value, 'price_sell')) content += '<p>盈亏=%.4f%%</p>' % ( (price_sell / price_buy - 1 - 0.005) * 100) content += '<p>created-at(交易时间)=%s</p>' % misc.getTimeStrWithUnixTimestamp( int(order_detail['created-at'] / 1000)) content += '<p>symbol(交易对)=%s</p>' % order_detail['symbol'] content += '<p>price(成交价格)=%s</p>' % order_detail['price'] #content+='<p>filled-amount(订单数量)=%s</p>' % order_detail['filled-amount'] #content+='<p>filled-fees(已成交手续费)=%s</p>' % order_detail['filled-fees'] content += '<p>type(订单类型)=%s</p>' % order_detail['type'] content += '<p>%s</p>' % str(order_detail) content += '</html>' misc.sendEmail( mail_host, mail_user, mail_pass, receivers, '%s_%s_transaction_report' % (symbol_value, order_detail['type']), content)
kLineEarly = kLine[-2] timestampLast = kLineLast[0] / 1000 high = kLineLast[2] low = kLineLast[3] priceLast = kLineLast[4] priceEarly = kLineEarly[4] change = (float(priceLast) / float(priceEarly)) - 1 subject = 'BTC_USDT' content = '<html>' content += '<p>%s</p>' % misc.getTimeStr() if high > highValue: highValue = high misc.setConfigKeyValue('config.ini', 'global', 'high', high) content += '<p style="font-weight: bold; color: red;">最高价:%s</p>' % high subject += '“最高价”' isSend = True if low < lowValue: lowValue = low misc.setConfigKeyValue('config.ini', 'global', 'low', low) content += '<p style="font-weight: bold; color: green;">最低价:%s</p>' % low subject += '“最低价”' isSend = True if abs(change) > thresholdValue: if not change == changeValue: changeValue = change if change > 0: value = '+' + str(round(change * 100, 4)) + '%' else: