示例#1
0
    def __init__(self, ):
        self.pusher = PushSlack()

        self.machine = UpbitMachine()
        self.currency_type = self.machine.currency_type
        self.compare_disparity = None
        self.now_price = None
        self.order_balance = 20000  #float(self.machine.get_free_balance()) / 4# 총보유현금 4등분
        self.free_balance = self.machine.get_free_balance()
        self.term = 5
        self.Log_Text = None

        logger.info(self.currency_type)
示例#2
0
    def __init__(self, ):
        self.pusher = PushSlack()
        self.db_handler = MongoDBHandler(db_name="trader",
                                         collection_name="trade_status")
        self.machine = UpbitMachine()
        self.currency_type = self.machine.currency_type
        self.compare_disparity = None
        self.now_price = None
        self.order_balance = 20000  #float(self.machine.get_free_balance()) / 4# 총보유현금 4등분
        self.free_balance = self.machine.get_free_balance()
        self.term = 5
        self.Log_Text = None

        logger.info(self.currency_type)
示例#3
0
    def __init__(self):

        super(Run, self).__init__()
        uic.loadUi('Auto-trading.ui', self)
        self.machine = UpbitMachine()
        self.setWindowTitle("Auto-trading")
        self.trader = None
        self.Start_Button.clicked.connect(self.on_Start_clicked)
        if (self.Combo_Box_minute.currentText() == " "):
            self.Start_Button.setEnabled(False)
        self.Stop_Button.clicked.connect(self.on_Stop_clicked)
        self.Setting.clicked.connect(self.Set_minute)
        self.Predict_value_cal.clicked.connect(self.get_Item)
        self.Assets.setText(str(int(self.machine.get_All_balance())))
        self.free_balance = self.machine.get_free_balance()
        self.first_balance = self.machine.get_free_balance()
        self.parent_conn, self.child_conn = Pipe()
        self.term = 10
        self.Hope_coin_list = None
        self.pusher = PushSlack()
示例#4
0
def get_currency_info():
    upbit = UpbitMachine()
    currency_type = [
        "KRW-BTC", "KRW-ETH", "KRW-EOS", "KRW-XRP", "KRW-TRX", "KRW-NEO"
    ]
    for item in currency_type:
        result = upbit.get_filled_orders(currency_type=item)
        mongodb = mongodb_handler.MongoDBHandler("local", "coiner",
                                                 "price_info")

        d = datetime.fromtimestamp(result["timestamp"] / 1000)
        result["year"] = d.year
        result["month"] = d.month
        result["day"] = d.day
        result["hour"] = d.hour
        result["minute"] = d.minute
        result["second"] = d.second
        result["amount"] = float(item["trade_volume"])
        result["timestamp"] = item["timestamp"] / 1000
        result["disparity"] = upbit.get_disparity(currency_type=item)
        result.pop("sequential_id")
        ids = mongodb.insert_items(result)
        print(ids)
示例#5
0
 def setUp(self):
     self.upbit_machine = UpbitMachine()
示例#6
0
def tensorflow_algorithm(currency_type=None,term=10,count=500,view=None):
    tf.set_random_seed(1000)
    tf.reset_default_graph()
    input_data_cnt = 5 # 입력데이터의 컬럼갯수 , 변수
    output_data_cnt = 1# 출력데이터의 컬럼갯수

    seq_length = 30 #1개의 시퀀스길이
    rnn_cell_hidden_dim = 20 #각 셀의 (hidden)출력 크기
    forget_bias = 1.0 # 막각편향(기본값 1.0)
    num_stacked_layers = 1 #stacked LSTM layers 갯수
    keep_prob = 1.0 #drop out 할 때 저장할 비율
    epoch_num = 1000 # 데이터를 몇번 반복해서 학습할 것인가 입력
    leaning_rate = 0.01 # 학습률

    def data_standardization(data):
        # 표준화 진행
        # return : (값-평균)/정규화값

        data_np= np.asarray(data)
        #배열로 변환

        return (data_np-data_np.mean())/data_np.std()

    def nomalization(data):
        # 정규화를 진행하여 너무 크거나 작은값을 방지
        # return (값-최소값)/(최대값-최소값)

        data_np = np.asarray(data)
        # 배열로

        return (data_np - data_np.min())/(data_np.max() - data_np.min() + 1e-10)
        #0으로 나누는것방지


    def reverse_nomalization(org_data, data):
        # 정규화 값을 원래대로 돌림
        # return (정규화된 값 * (원래값의 최대값 - 원래값의 최소값)+1e-10))+원래값의 최소값

        org_data_np = np.asarray(org_data)
        data_np = np.asarray(data)
        return (data_np *(org_data_np.max() - org_data_np.min() +1e-10)) + org_data_np.min()

    def lstm_cell():
        cell=tf.contrib.rnn.LSTMCell(num_units = 20, forget_bias = 1.0, state_is_tuple= True, activation = tf.nn.tanh)

        return cell

    # def get_prediction_value(self,prediction_value=prediction_value):
    #     return prediction_value

    upbit = UpbitMachine() #machine 변수선언

    result = upbit.get_candles_data(currency_type=currency_type,term=term,count =count) # 5분간격으로 과거데이터 총 200개 호출
    #print(result)
    data=[] #입력데이터 저장
    data.append(["open","high","low","price","volume"]) #저장 데이터 순서

    for item in result :
        list=[]
        list.append(item["opening_price"])#시장가 추가
        list.append(item["high_price"])#최고가 추가
        list.append(item["low_price"])# 최저가 추가
        list.append(item["trade_price"])# 최종거래 금액
        list.append(item["candle_acc_trade_volume"])# 거래된 양
        data.append(list)

    data=np.array(data[1:]).astype(np.float64)# data를 astype 으로 float64로 변환
    data = np.flip(data, 0)# 최신순에서 과건순으로 변경
    price=data[:, :-1] #price 라는 변수로 거래량만 제외한 변수 추가
    norm_price=nomalization(price) #price를 정규화
    #pprint.pprint(price.dtype)
   # pprint.pprint(price)
    #pprint.pprint(norm_price)

    volume=data[:,-1:]#거래량만 추가
    norm_volume=nomalization(volume)# 거래량을 정규화
   # pprint.pprint(norm_volume)

    x=np.concatenate((norm_price, norm_volume), axis=1)# 정규화된 가격과 거래량을 합침
   # print("x.shape:", x.shape)
    #print("x[0]: " , x[0])

    y = x[:,[-2]] # 최종거래가 저장

    dataX = []#입력데이터 변수지정
    dataY = []#출력데이터 변수지정
    for i in range(0, len(y)-seq_length):
        _x = x[i : i + seq_length]#i번째부터 i+seq_length-1까지 _x에 저장
        _y = y[i + seq_length]#i+seq_length를 출력 한마디로 시퀀스의 바로 다음값으로서 예측한 값이 맞냐 아니냐를 따지는 값
        # if i is 0:
        #     print(_x, "->", _y)#첫번째 행만 테스트 하기 위해 출력
        dataX.append(_x)
        dataY.append(_y)
    train_size = int(len(dataY) * 0.7)#데이터의 70%만 트레이닝
    test_size = int(len(dataY) - train_size)#데이터의 70%를 제외한 부분을 테스트 케이스로 출력
    trainX = np.array(dataX[0:train_size])#train 배열 설정
    trainY = np.array(dataY[0:train_size])#train 결과값 설정

    testX = np.array(dataX[train_size:len(dataX)])#test case 배열 설정
    testY = np.array(dataY[train_size:len(dataY)])#test case 결과값 설정
    X = tf.placeholder(tf.float32, [ None, seq_length, input_data_cnt])#placeholder설정
    Y = tf.placeholder(tf.float32, [None, 1])#placeholder설정

    targets = tf.placeholder(tf.float32, [None, 1])#검증용 지표를 계산 placeholder 지정
    predictions = tf.placeholder(tf.float32, [None, 1])

    for a in range(num_stacked_layers):
        stackedRnns=[lstm_cell()]
    #num_stacked_layers층으로 쌓인 Rnns 생성
    multi_cells = tf.contrib.rnn.MultiRNNCell(stackedRnns,state_is_tuple =True) if num_stacked_layers > 1 else lstm_cell()
    #Rnn 생성
    #Rnn층이 0이면 lstm_cell로실행
    # 왜 c++에서처럼 if 문을 주면 오류가 나지?

    prediction_value, _states= tf.nn.dynamic_rnn(multi_cells, X , dtype = tf.float32)
    # dynamic_rnn은 결과값과 상태를 나타내는 값을 return
    # 반복적인 Neural Network생성

    prediction_value = tf.contrib.layers.fully_connected(prediction_value[:,-1], output_data_cnt,activation_fn=tf.sigmoid)
    #가중치를 더한값을 precition_value로 지정

    loss = tf.reduce_sum(tf.square(prediction_value - Y))
    # 손실값 예상값- 실제값의 제곱을 더한값으로 지정
    optimizer = tf.train.AdamOptimizer(leaning_rate)
    # leaning_rate를 지정하여 최적화를 진행 Adam Algorithm을 이용
    train=optimizer.minimize(loss)#
    #손실값의 gradient를 계산한하여 train에 투입
    rmse = tf.sqrt(tf.reduce_mean(tf.squared_difference(targets, predictions)))
    #합성곱 평균을내어 train 값에 또 다시 제곱근을 한 값을 집어 넣어 return

    train_error_summary = []#train_error라는 배열을 지정하여 오류값 저장
    test_error_summary = []#test_error라는 값을 지정하여 오류값 저장
    test_predict = ''# 테스트 예측값 저장용

    sess = tf.Session()
    sess.run(tf.global_variables_initializer())


    start_time = datetime.datetime.now()# 시작시간을 저장

    for epoch in range(epoch_num):
        #epoch를 이용하여 몇번에 걸쳐 train할것인가를 저장후 epoch값을 저장
        _,_loss=sess.run([train,loss], feed_dict={X:trainX, Y:trainY})
        #x,y의 두개의 예측값을 저장함
        if ((epoch+1)%50 == 0) or (epoch_num-1):
            #값을 다 저장할순 없고 중간중간에 값을 저장하는 if문 생성
            train_predict = sess.run(prediction_value, feed_dict = {X : trainX})
            #예상값을 지정
            train_error = sess.run(rmse,feed_dict = {targets: trainY, predictions: train_predict})
            #만약 그 예측값이 빗나 갔다면 그에 대한 error값을 배열에 저장
            train_error_summary.append(train_error)
            #만약 예측값이 빗나갔으면 빗나간것을 저장

            test_predict = sess.run(prediction_value, feed_dict = {X: testX})
            # test case에서의 예측 값을 저장
            test_error = sess.run(rmse, feed_dict={targets: testY, predictions: test_predict})
            #test case에서의 오류값을 저장
            test_error_summary.append(test_error)
            # print("epoch : {}, train_error(A) :{} , test_error(B):{}, B-A :{}".format(epoch+1, train_error, test_error, test_error-train_error))
    test_predict = reverse_nomalization(price, test_predict)
    if view is None :
        pass
    else:
        end_time=datetime.datetime.now()# 종료 시간 지정
        elapsed_time = end_time - start_time #종료된 시간과 시작시간을 비교하여 elapsed_time을 구함
        print("elapsed_time : ", elapsed_time)
        print("elapsed_time per epoch:", elapsed_time/epoch_num)
        plt.figure("epoch")#도표 1로지정
        plt.plot(train_error_summary, 'g',label="train_error")# train_error값을 green으로 지정
        plt.plot(test_error_summary,'b',label="test_error")#test_error_summary값을 파란색으로 지정
        plt.xlabel("Epoch(x1000)")#x 축의 값에는Epoch x1000이라는 값을 지정
        plt.ylabel("Error Percentage") #오류가 날 확률을 지정

        plt.figure("price") #도표 2로 지정
        testY=reverse_nomalization(price, testY)
        plt.plot(testY, 'r', label="Actual_value") # 실제 값들을 red 로 지정
        plt.plot(test_predict, "b",label="Predict_value") # 예측값들을 파란색으로 지정
        plt.legend(loc=2)
        plt.xlabel("Time") #X축에는 시간을
        plt.ylabel("coin price")# Y축에는 코인값을
        plt.show() #도표를 출력


    np.array([x[len(x) - seq_length:]])

    #역 정규화를 통해 값을 출력 저장
    #과연?
    print("실제값 길이 " + str(len(testY)))
    print("coin name : "+ currency_type)
    print("next_time coin price ", test_predict[0])
    result=test_predict[0]
    result=str(result[0])
    return result
示例#7
0
文件: Run.py 项目: too489/Project
class Run(
        QMainWindow, ):
    def __init__(self):

        super(Run, self).__init__()
        uic.loadUi('Auto-trading.ui', self)
        self.machine = UpbitMachine()
        self.setWindowTitle("Auto-trading")
        self.trader = None
        self.Start_Button.clicked.connect(self.on_Start_clicked)
        if (self.Combo_Box_minute.currentText() == " "):
            self.Start_Button.setEnabled(False)
        self.Stop_Button.clicked.connect(self.on_Stop_clicked)
        self.Refresh.clicked.connect(self.load_info)
        self.Setting.clicked.connect(self.Set_minute)
        self.Predict_value_cal.clicked.connect(self.get_Item)
        self.Assets.setText(str(int(self.machine.get_All_balance())))
        self.q = Queue()
        self.ql = Queue()
        self.ass = self.machine.get_All_balance()
        self.first_balance = self.machine.get_All_balance()
        self.term = 10
        self.Hope_coin_list = None
        self.pusher = PushSlack()
        self.setinel = 1

    @pyqtSlot()
    def on_Start_clicked(self):
        self.Assets.setText(str(int(self.machine.get_All_balance())))
        self.Profit.setText(
            str(100 - (float(self.ass) / float(self.first_balance)) * 100) +
            "%")
        self.Start_Button.setEnabled(False)
        self.trader = multiprocessing.Process(target=StepTrade().run,
                                              args=(
                                                  self.term,
                                                  self.q,
                                                  self.ql,
                                              ))
        self.trader.start()
        self.Log_Text.appendPlainText("자동매매 프로그램이 실행되었습니다")

    @pyqtSlot()
    def on_Stop_clicked(self):
        self.trader.terminate()
        self.trader.join()
        self.Start_Button.setEnabled(True)

    @pyqtSlot()
    def Set_minute(self):
        self.Start_Button.setEnabled(True)
        item = self.Combo_Box_minute.currentText()
        item = item.split(" ")
        self.term = item[0]

    @pyqtSlot()
    def get_Item(self):
        self.Log_Text.appendPlainText("잠시만 기다려 주세요.")
        self.Log_Text.appendPlainText("예측까지 5초정도 걸립니다")
        item = self.Combo_Box.currentText()
        item = item.split("-")
        item = "KRW-" + item[0]
        self.Predict_Value_Label.setText(
            str(
                float(
                    tensorflow_algorithm_keras(currency_type=item,
                                               view=1,
                                               term=self.term))))
        self.Now_Value_Label.setText(
            str(int(self.machine.get_now_price(currency_type=item))))

    # @pyqtSlot()
    # def check(self,):
    #     while(1):
    #         self.free_balance = self.machine.get_free_balance()
    #         self.Assets.setText(str(int(self.machine.get_All_balance())))
    #         self.Profit.setText(str(100-(float(self.free_balance)/float(self.first_balance))*100)+"%")
    #         self.Hope_coin_list = StepTrade().get_hopeful_coin()  # 종목선정
    #         self.Coin_Collections.appendPlainText(str(datetime.now()))
    #         for info in self.Hope_coin_list:
    #             self.Coin_Collections.appendPlainText(info)
    #         time.sleep(60*self.term)
    @pyqtSlot()
    def load_info(self):
        self.ass = self.machine.get_All_balance()
        self.Assets.setText(str(int(self.ass)))
        self.Profit.setText(
            str(100 - round(
                (float(self.ass) / float(self.first_balance)), 2) * 100) + "%")
        self.Coin_Collections.appendPlainText(str(datetime.now()))

        if self.q.empty():
            pass
        else:
            for data in self.q.get():
                self.Log_Text.appendPlainText(data)
        if self.ql.empty():
            self.Hope_coin_list = StepTrade().get_hopeful_coin(
                term=self.term, ql=self.ql)  # 종목선정
            for info in self.Hope_coin_list:
                self.Coin_Collections.appendPlainText(info)
        else:
            for data1 in self.ql.get():
                self.Coin_Collections.appendPlainText(data1)
示例#8
0
    def Search_Coin(
            term=10,
            count=500):  #평균 5분봉으로 500개정도를 뽑아서 테스트로 사용 하지만 실제사용에선 30분 주기로 할것임
        upbit = UpbitMachine()  # machine 변수선언
        test_result = {
            'index': [],
            'coin': [],
            'adf_statistic': [],
            'adf_1': [],
            'adf_5': [],
            'adf_10': [],
            'hurst': [],
            'halflife': []
        }
        index = 0
        for coin in upbit.currency_type:  #타입에 있는 모든 코인 조회
            result = upbit.get_candles_data(
                currency_type=coin, term=term,
                count=count)  # 5분간격으로 과거데이터 총 200개 호출
            # print(count)
            # print(coin)
            # print(term)

            data = []  # 입력데이터 저장

            for item in result:
                data.append(item["trade_price"])  # 최종거래 금액
            test_result['index'] = index
            test_result['coin'].append(coin)
            test_result['hurst'].append(calHurstExponent(data))
            test_result['halflife'].append(calcHalfLife(data))
            test_stat, adf_1, adf_5, adf_10 = calcADF(data)
            test_result['adf_statistic'].append(test_stat)
            test_result['adf_1'].append(adf_1)
            test_result['adf_5'].append(adf_5)
            test_result['adf_10'].append(adf_10)
            index += 1
        df_result = pd.DataFrame(test_result)

        df_result['rank_adf'] = 0
        df_result['rank_hurst'] = 0
        df_result['rank_halflife'] = 0
        halflife_percentile = np.percentile(df_result['halflife'],
                                            np.arange(0, 100, 10))
        for row_index in range(df_result.shape[0]):
            df_result.loc[row_index, 'rank_adf'] = assessADF(
                df_result.loc[row_index, 'adf_statistic'],
                df_result.loc[row_index, 'adf_1'],
                df_result.loc[row_index, 'adf_5'], df_result.loc[row_index,
                                                                 'adf_10'])
            df_result.loc[row_index,
                          'rank_hurst'] = assessHurst(df_result.loc[row_index,
                                                                    'hurst'])
            df_result.loc[row_index, 'rank_halflife'] = assessHalflife(
                halflife_percentile, df_result.loc[row_index, 'halflife'])
            df_result['rank'] = df_result['rank_adf'] + df_result[
                'rank_hurst'] + df_result['rank_halflife']
#        print(df_result['coin'])
        ratio = 0.8
        percentile_column = np.percentile(df_result['rank'],
                                          np.arange(0, 100, 10))
        ratio_index = np.trunc(ratio * len(percentile_column))
        result = {}
        result_list = []
        for row_index in range(df_result.shape[0]):
            percentile_index = getPercentileIndex(
                percentile_column, df_result.loc[row_index, 'rank'])
            if percentile_index >= ratio_index:
                result[df_result.loc[row_index,
                                     'index']] = df_result.loc[row_index,
                                                               'coin']
                result_list.append(df_result.loc[row_index, 'coin'])
        #최종리스트를 데베에 저장
        #print("회귀분석 내",result_list)
        return result_list
示例#9
0
class UpbitMachineTestCase(unittest.TestCase):
    def setUp(self):
        self.upbit_machine = UpbitMachine()

    def tearDown(self):
        pass

    def test_set_token(self) -> None:
        # print(inspect.stack()[0][3])
        access_token = self.upbit_machine.set_token(grant_type="password")
        assert access_token
        # print("Access_token:",access_token)

        params = {'Authorization': access_token}
        result = requests.get('https://api.upbit.com/v1/accounts',
                              headers=params)
        # print("it's count  balanece", result.json())

    def test_get_token(self) -> None:
        self.upbit_machine.set_token(grant_type="password")
        access_token = self.upbit_machine.get_token()
        assert access_token
        # print("get_Access_token:",access_token)

    def test_get_ticker(self):
        pass
        # print(inspect.stack()[0][3])
        # ticker=self.upbit_machine.get_ticker("krw-btc")
        # assert ticker
        # pprint.pprint(ticker)

    def test_get_filled_orders(self):
        pass
        # order_book=self.upbit_machine.get_filled_orders(currency_type="krw-btc")
        # pprint.pprint(order_book)
        # pprint.pprint('출력되나?')
        # test=self.upbit_machine.get_disparity(currency_type="KRW-BTC")
        # pprint.pprint(test)

    def test_get_wallet_status(self):
        pass
        # print(inspect.stack()[0][3])
        # wallet_status=self.upbit_machine.get_wallet_status()
        # assert wallet_status
        # pprint.pprint(wallet_status)

    # def test_buy_order(self):
    #     print(inspect.stack()[0][3])
    #     buy_order = self.upbit_machine.buy_order(currency_type="KRW-ETH", price="15000", qty="1", order_type="limit")
    #     assert buy_order
    #     print(buy_order)
    #
    # def test_sell_order(self):
    #     print(inspect.stack()[0][3])
    #     sell_order=self.upbit_machine.sell_order(currency_type="KRW-BTC",price="4000",qty="1",order_type="limit")
    #     assert sell_order
    #     print(sell_order)

    # def test_get_nonce(self):
    # print(inspect.stack()[0][3])
    # nonce=self.upbit_machine.get_nonce()
    # assert nonce
    # print(nonce)
    # def test_cancel_order(self):
    #     print(inspect.stack()[0][3])
    #     cancel_order = self.upbit_machine.cancel_order(currency_type="KRW-BTC", order_id="5064611")
    #     assert cancel_order
    #     print(cancel_order)

    def test_get_myorder_status(self):
        pass
        # print('여기야 여기 ')
        # result=self.upbit_machine.get_ticker(currency_type="KRW-BTC")
        # print(result['trade_price'])
        # my_order= self.upbit_machine.get_my_order_wait_list()
        # assert my_order
        #  for var in my_order:
        #      print(var["uuid"])
        #  print(my_order)
        #      print('마무리는 여기')
        my_order = self.upbit_machine.get_coin_valume("KRW-RFR")
        #my_order=self.upbit_machine.get_has_coin_info_list()

        print(my_order)
示例#10
0
class Run(
        QMainWindow, ):
    def __init__(self):

        super(Run, self).__init__()
        uic.loadUi('Auto-trading.ui', self)
        self.machine = UpbitMachine()
        self.setWindowTitle("Auto-trading")
        self.trader = None
        self.Start_Button.clicked.connect(self.on_Start_clicked)
        if (self.Combo_Box_minute.currentText() == " "):
            self.Start_Button.setEnabled(False)
        self.Stop_Button.clicked.connect(self.on_Stop_clicked)
        self.Setting.clicked.connect(self.Set_minute)
        self.Predict_value_cal.clicked.connect(self.get_Item)
        self.Assets.setText(str(int(self.machine.get_All_balance())))
        self.free_balance = self.machine.get_free_balance()
        self.first_balance = self.machine.get_free_balance()
        self.parent_conn, self.child_conn = Pipe()
        self.term = 10
        self.Hope_coin_list = None
        self.pusher = PushSlack()

    @pyqtSlot()
    def on_Start_clicked(self):

        self.Start_Button.setEnabled(False)
        self.trader = threading.Thread(target=self.roof,
                                       args=(self.Coin_Collections, ))
        self.trader.start()
        self.Log_Text.appendPlainText("시작")

    @pyqtSlot()
    def on_Stop_clicked(self):

        self.trader.join(timeout=5)
        self.Start_Button.setEnabled(True)

    @pyqtSlot()
    def Set_minute(self):
        self.Start_Button.setEnabled(True)
        item = self.Combo_Box_minute.currentText()
        item = item.split(" ")
        self.term = item[0]

    @pyqtSlot()
    def get_Item(self):
        item = self.Combo_Box.currentText()
        item = item.split("-")
        item = "KRW-" + item[0]
        self.Log_Text.appendPlainText("잠시만 기다려 주세요.")
        self.Log_Text.appendPlainText("예측까지 5초정도 걸립니다")
        self.Predict_Value_Label.setText(
            str(
                int(
                    tensorflow_algorithm_keras(currency_type=item,
                                               view=1,
                                               term=self.term))))
        self.Now_Value_Label.setText(
            str(int(self.machine.get_now_price(currency_type=item))))

    @pyqtSlot()
    def roof(self, Coin_Collections):
        self.pusher.send_message("#general", "가상화폐 자동매매 프로그램이 실행되었습니다")
        while (1):
            self.free_balance = self.machine.get_free_balance()
            self.Assets.setText(str(int(self.machine.get_All_balance())))
            print(self.free_balance, self.first_balance)
            self.Profit.setText(
                str(100 -
                    (float(self.free_balance) / float(self.first_balance)) *
                    100) + "%")
            self.Hope_coin_list = StepTrade().get_hopeful_coin()  # 종목선정
            # Coin_Collections.clear()
            self.Coin_Collections.appendPlainText(str(datetime.now()))
            for info in self.Hope_coin_list:
                Coin_Collections.appendPlainText(info)
            StepTrade().run(term=self.term,
                            Log_Text=self.Log_Text,
                            Hope_coin_list=self.Hope_coin_list,
                            free_balance=self.free_balance)
            time.sleep(60 * self.term)
def tensorflow_algorithm_keras(currency_type=None,
                               term=10,
                               count=500,
                               view=None):
    look_back = 1
    tf.set_random_seed(1000)
    tf.reset_default_graph()

    def data_standardization(data):
        # 표준화 진행
        # return : (값-평균)/정규화값

        data_np = np.asarray(data)
        #배열로 변환

        return (data_np - data_np.mean()) / data_np.std()

    def nomalization(data):
        # 정규화를 진행하여 너무 크거나 작은값을 방지
        # return (값-최소값)/(최대값-최소값)

        data_np = np.asarray(data)
        # 배열로

        return (data_np - data_np.min()) / (data_np.max() - data_np.min() +
                                            1e-10)
        #0으로 나누는것방지

    def reverse_nomalization(org_data, data):
        # 정규화 값을 원래대로 돌림
        # return (정규화된 값 * (원래값의 최대값 - 원래값의 최소값)+1e-10))+원래값의 최소값

        org_data_np = np.asarray(org_data)
        data_np = np.asarray(data)
        return (data_np * (org_data_np.max() - org_data_np.min() + 1e-10)
                ) + org_data_np.min()

    def create_dataset(dataset, look_back=1):
        dataX, dataY = [], []
        for i in range(len(dataset) - look_back - 1):
            a = dataset[i:(i + look_back)]
            dataX.append(a)
            dataY.append(dataset[i + look_back])
        return np.array(dataX), np.array(dataY)

    # def get_prediction_value(self,prediction_value=prediction_value):
    #     return prediction_value

    upbit = UpbitMachine()  #machine 변수선언

    result = upbit.get_candles_data(currency_type=currency_type,
                                    term=term,
                                    count=count)  # 5분간격으로 과거데이터 총 200개 호출
    #print(result)
    data = []  #입력데이터 저장
    for item in result:  #최근순서
        data.append(item["trade_price"])  # 최종거래 금액
    data = np.array(data).astype(np.float64)
    nptf = nomalization(data)
    # data = np.flip(data, 0)# 최신순에서 과건순으로 변경
    # price=data[:,-2:-1] #price 라는 변수로 거래량만 제외한 변수 추가
    #
    # norm_price=nomalization(price) #price를 정규화 가격만
    #
    # #사이즈 나눔
    train_size = int(len(nptf) * 0.7)
    test_size = len(nptf) - train_size
    train, test = nptf[0:train_size], nptf[train_size:len(nptf)]
    #
    trainX, trainY = create_dataset(train)
    testX, testY = create_dataset(test)
    #
    trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
    testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))
    #
    model = Sequential()
    model.add(LSTM(4, input_shape=(1, look_back)))  #tanh 를사용
    model.add(Dense(1))
    model.compile(loss='mean_squared_error', optimizer='adam')
    model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

    testPredict = model.predict(testX)
    testPredict = reverse_nomalization(org_data=data, data=testPredict)
    testY = reverse_nomalization(org_data=data, data=testY)
    #testScore=math.sqrt(mean_squared_error(testY, testPredict))
    #print('Train Score: %.2f RMSE' % testScore)

    lastX = nptf[-1]
    lastX = np.reshape(lastX, (1, 1, 1))
    lastY = model.predict(lastX)
    lastY = reverse_nomalization(org_data=data, data=lastY)
    # print(testY)
    # print(testPredict)
    if view is None:
        pass
    else:
        plt.xlabel("Time")  #X축에는 시간을
        plt.ylabel("coin price")  # Y축에는 코인값을
        plt.plot(testY, 'r', label="Actual_value")  # 실제 값들을 red 로 지정
        plt.plot(testPredict, "b", label="Predict_value")  # 예측값들을 파란색으로 지정
        plt.legend(loc=2)
        plt.show()
        print("실제값 길이 " + str(len(testY)))
        print("coin name : " + currency_type)
        print("next_time coin price ", lastY)
    return lastY[0][0]
示例#12
0
class StepTrade(Strategy):
    def __init__(self, ):
        self.pusher = PushSlack()
        self.db_handler = MongoDBHandler(db_name="trader",
                                         collection_name="trade_status")
        self.machine = UpbitMachine()
        self.currency_type = self.machine.currency_type
        self.compare_disparity = None
        self.now_price = None
        self.order_balance = 20000  #float(self.machine.get_free_balance()) / 4# 총보유현금 4등분
        self.free_balance = self.machine.get_free_balance()
        self.term = 5
        self.Log_Text = None

        logger.info(self.currency_type)

    def run(self,
            term=10,
            Log_Text=None,
            Hope_coin_list=None,
            free_balance=None):
        if Hope_coin_list is None and Log_Text is None:
            raise Exception("I need Hope_coin_list and Conn")
        self.Log_Text = Log_Text
        self.term = term
        self.free_balance = free_balance
        self.check_my_order()
        buy_processes = [
            multiprocessing.Process(target=self.buy_trading_logic,
                                    args=(arg, )) for arg in Hope_coin_list
        ]
        sell_processes = [
            multiprocessing.Process(target=self.sell_trading_logic,
                                    args=(arg, ))
            for arg in self.machine.get_coin_list()
        ]
        for p1 in buy_processes[:4]:
            p1.start()
        for p in buy_processes[:4]:
            p.join()
        time.sleep(60)
        for p2 in buy_processes[4:]:
            p2.start()
        for p2 in buy_processes[4:]:
            p2.join()
        for p3 in sell_processes:
            p3.start()
        for p33 in sell_processes:
            p3.join()
        time.sleep(1)
        time.sleep(int(self.term) * 60)

    def check_my_order(self):
        assets = int(
            self.my_all_assets(machine=self.machine,
                               db_handler=self.db_handler))
        self.check_ordered_state()
        self.pusher.send_message(
            "#general",
            str(datetime.now()) + " 현재 자산 :" + str(assets) + "원 입니다")
        self.Log_Text.appendPlainText(
            str(datetime.now()) + " 현재 자산 :" + str(assets) + "원 입니다")
        self.pusher.send_message(
            "#general", "그 중 거래 가능 현금 :" + str(self.free_balance) + "원 입니다")
        self.Log_Text.appendPlainText("그 중 거래 가능 현금 :" +
                                      str(self.free_balance) + "원 입니다")
        #self.check_completed()
        self.check_keep_ordered()

    def check_ordered_state(self):
        DB_order_list = self.db_handler.find_items(
            {"state": "wait"},
            db_name="trader",
            collection_name="trade_status")
        # print(DB_order_list)
        for list in DB_order_list:
            # print(list["uuid"])
            item = self.machine.get_my_order_status(
                uuid=list["uuid"]
            )  # 구매 리스트를 불러옴 단 딱히 market을 지정하지 않음 전부다 조회하기 위해서 !!!나중에 페이지를 넓히던 방법 강구해야됨
            logger.info(item)  #로그파일저장분
            # print(item)
            if (item.get('error')):
                pass
            else:
                # print("여기출력!!!!!!!!!!")
                # print(item['uuid'])
                if item["state"] == "done":  #만약 디비에 wait되어 있던 게 완료되었으면 디비로 저장
                    order_result_dict = item
                    real_amount = float(
                        order_result_dict["executed_volume"])  #거래량
                    real_value = float(
                        order_result_dict["avg_price"])  #평균 구매가격
                    created_at = float(order_result_dict["created_at"] /
                                       1000)  #구매시간
                    fee = float(order_result_dict["paid_fee"])  # 수수료비율
                    self.update_trade_status(db_handler=self.db_handler,
                                             item_id={"uuid": item["uuid"]},
                                             value={
                                                 "status": "done",
                                                 "real_amount": real_amount,
                                                 "created_at": created_at,
                                                 "real_value": real_value,
                                                 "real_fee": fee,
                                                 "side": item["side"]
                                             })
                    self.pusher.send_message("#general", "completed:" +
                                             item["side"] + str(item))  #이상없
                    self.Log_Text.appendPlainText("completed:" + item["side"] +
                                                  str(item))  #이상없

                elif item["state"] == "wait":
                    if item["side"] == "bid":  #아직 wait라면
                        if float(item["price"]
                                 ) * 1.05 <= self.machine.get_now_price(
                                     currency_type=item["market"]):  #체크값
                            logger.info("CancelOrder")
                            logger.info(item)
                            try:
                                self.order_cancel_trasaction(
                                    machine=self.machine,
                                    db_handler=self.db_handler,
                                    item=item)
                            except:
                                error = traceback.format_exc()
                                logger.info(error)
                                self.update_trade_status(
                                    db_handler=self.db_handler,
                                    item_id={"uuid": item["uuid"]},
                                    value={"transaction": "failed"})
                                print("구매주문 취소")
                                self.pusher.send_message(
                                    "#general",
                                    "해당 코인의 가격이 더 올라 취소되었습니다. 추격매수 말고 기다려주세요" +
                                    str(item))
                                self.Log_Text.appendPlainText(
                                    "해당 코인의 가격이 더 올라 취소되었습니다. 추격매수 말고 기다려주세요" +
                                    str(item))

                    elif item["side"] == "ask":
                        if (item.get('price')):
                            if float(item["price"]
                                     ) < self.machine.get_now_price(
                                         currency_type=item['market']) * 1.05:
                                self.order_cancel_trasaction(
                                    machine=self.machine,
                                    db_handler=self.db_handler,
                                    item=item)
                                #self.update_trade_status(db_handler=self.db_handler, item_id={"uuid": item["uuid"]}, value={"status": "wait"})
                                print("판매주문 취소")
                                self.pusher.send_message(
                                    "#general",
                                    "해당 코인의 가격이 너무 내려 매도 주문이 취소되었습니다, 손절을 추천드립니다"
                                    + str(item))
                                self.Log_Text.appendPlainText(
                                    "해당 코인의 가격이 너무 내려 매도 주문이 취소되었습니다, 손절을 추천드립니다"
                                    + str(item))
                else:
                    self.update_trade_status(
                        db_handler=self.db_handler,
                        item_id={"uuid": item["uuid"]},
                        value={"status": "CANCEL_ORDERED"})

    def check_completed(self):  #굳이 있을 필요가 있나
        DB_order_list = self.db_handler.find_items(
            {"state": "done"},
            db_name="trader",
            collection_name="trade_status")
        logger.info("ORDER_COMPELETED")
        for item in DB_order_list:
            # print(item)
            logger.info(item)
            try:
                self.order_transaction(machine=self.machine,
                                       db_handler=self.db_handler,
                                       item=item,
                                       side=item["side"])
                self.pusher.send_message(
                    "#general", item["side"] + "ordered : " + str(item))
                self.Log_Text.appendPlainText(item["side"] + "ordered : " +
                                              str(item))
            except:
                error = traceback.format_exc()
                logger.info(error)
                if (item.get('uuid')):
                    self.update_trade_status(db_handler=self.db_handler,
                                             item_id={"uuid": item["uuid"]},
                                             value={"transactions": "failed"})

    def get_hopeful_coin(self):  #평균회귀 평가모델로 뽑아온 coin목록을 db에넣어
        result = MeanReversionModel()
        self.db_handler.set_db_collection("trader", "wish_list")

        for coin in result:
            self.db_handler.insert_item({
                "market": coin,
                "time": datetime.now()
            })
        self.currency_type = result
        return self.currency_type

    def check_keep_ordered(self):  #여기가 손익라인 #5프로 정도로 한다
        has_coin_list = self.machine.get_has_coin_info_list()
        for item in has_coin_list:
            now_price = self.machine.get_now_price(
                currency_type=item["market"])
            #print(item['market'])
            if (item['market'] == "KRW-ETH"):
                pass
            else:
                if float(item["price"]) * 1.05 < now_price and float(
                        item["price"]) * 1.05 > 1000:  #수익라인
                    self.order_transaction(machine=self.machine,
                                           db_handler=self.db_handler,
                                           currency_type=item["market"],
                                           side='ask',
                                           qty=item["balance"],
                                           price=now_price)
                    self.pusher.send_message(
                        "#general", "보유 중인 " + str(item["market"]) +
                        "코인을 수익 매도 하였습니다. info : " + str(item))
                    logger.info("sell order from keeped" + str(item["uuid"]))
                    self.Log_Text.appendPlainText("보유 중인 " +
                                                  str(item["market"]) +
                                                  "코인을 수익 매도 하였습니다. info : " +
                                                  str(item))

                elif float(item["price"]) * 0.95 > now_price and float(
                        item["price"]) * 1.05 > 1000:  #손절라인
                    self.order_transaction(machine=self.machine,
                                           db_handler=self.db_handler,
                                           currency_type=item["market"],
                                           side='ask',
                                           qty=item["balance"],
                                           price=now_price)
                    self.pusher.send_message(
                        "#general", "보유 중인 " + str(item["market"]) +
                        "코인을 손절 매도 하였습니다. info : " + str(item))
                    self.Log_Text.appendPlainText("보유 중인 " +
                                                  str(item["market"]) +
                                                  "코인을 손절 매도 하였습니다. info : " +
                                                  str(item))
                    logger.info(str(item["market"]) + "을 매도 하였습니다 ")

    def set_thread_price(self, currency_type=None):

        coin_type = currency_type
        #for coin_type in currency_type:
        price_info = {}
        price_info['market'] = coin_type
        price_info['pridict_price'] = tensorflow_algorithm_keras(
            term=self.term, count=100, currency_type=coin_type)
        bollingerband = self.machine.get_bollingerband(term=self.term,
                                                       count=20,
                                                       currency_type=coin_type)
        price_info['bolin_high'] = bollingerband['high']
        price_info['bolin_mid'] = bollingerband['mid']
        price_info['bolin_low'] = bollingerband['low']
        price_info['disparity'] = self.machine.get_disparity(
            term=self.term, count=20, currency_type=coin_type)
        price_info['now_price'] = self.machine.get_now_price(
            currency_type=coin_type)
        return (price_info)

        # time.sleep(300)

    def buy_trading_logic(self, currency_type=None):
        if currency_type is None:
            raise Exception("I have to need info")
        print("구매로직 진입")
        print(currency_type)
        term = self.term
        info = self.set_thread_price(currency_type=currency_type)
        wallet = self.free_balance
        if (float(wallet) > self.order_balance):
            count = 0

            if (float(info["pridict_price"]) > float(info['now_price'])):
                count += 1
            if (float(info["disparity"]) < -1):
                disparity = float(info["disparity"])
                count += 1
            if (float(info["bolin_low"]) > float(info['now_price'])):
                count += 1
            if (count >= 2):
                print("구매 검토 중")
                time.sleep(int(term) * 60)
                while (1):
                    if (float(
                            self.machine.get_disparity(
                                currency_type=info["market"], term=self.term))
                            > disparity):
                        wallet = self.free_balance
                        now_price = self.machine.get_now_price(
                            currency_type=info["market"])
                        if float(wallet) >= self.order_balance:
                            volume = (self.order_balance) / float(now_price)
                            self.order_transaction(
                                machine=self.machine,
                                db_handler=self.db_handler,
                                qty=volume * 0.98,
                                currency_type=info["market"],
                                side="bid",
                                price=now_price)
                            time.sleep(int(term) * 60)
                            self.check_ordered_state()
                            self.pusher.send_message(
                                "#general", "현재" + str(info["market"]) +
                                "매수 하였습니다 info : " + str(info))
                            self.Log_Text.appendPlainText("현재" +
                                                          str(info["market"]) +
                                                          "매수 하였습니다 info : " +
                                                          str(info))

                            logger.info("buy transaction")
                        else:
                            print("돈없어서 pass")
                            self.Log_Text.appendPlainText(
                                "보유 중인 금액이 부족하여 구매에 실패하였습니다")
                        break
                    disparity = float(
                        self.machine.get_disparity(
                            currency_type=info["market"], term=self.term))
                    print("매수 시점 기다리는 중")
                    time.sleep(int(term) * 60)
                    self.check_ordered_state()

    def sell_trading_logic(self, currency_type=None):
        if currency_type is None:
            raise Exception("I have to need info")
        print("판매 로직 진입")
        print(currency_type)
        term = self.term
        #info 는 가지고 있는 코인 리스트
        has_coin_info = self.machine.get_has_coin_info()
        print("판매 위해 가지고 있는 코인들 조회")
        evalueation_coin = self.set_thread_price(currency_type=currency_type)
        #print(evalueation_coin)
        count = 0
        if (float(evalueation_coin["pridict_price"]) < float(
                evalueation_coin['now_price'])):
            count += 1
        if (float(evalueation_coin["disparity"]) > 1):
            disparity = float(evalueation_coin["disparity"])
            count += 1
        if (float(evalueation_coin["bolin_high"]) < float(
                evalueation_coin['now_price'])):
            count += 1
        if (count >= 2):
            print("판매 검토중")
            time.sleep(int(term) * 60)
            now_price = self.machine.get_now_price(
                currency_type=evalueation_coin["market"])
            while (1):
                if (float(
                        self.machine.get_disparity(
                            currency_type=evalueation_coin["market"],
                            term=self.term)) < disparity):
                    volume = self.machine.get_coin_valume(
                        evalueation_coin["market"])
                    self.order_transaction(
                        machine=self.machine,
                        db_handler=self.db_handler,
                        qty=float(volume) * 0.98,
                        currency_type=evalueation_coin["market"],
                        side="ask",
                        price=now_price)
                    time.sleep(int(term) * 60)
                    self.check_ordered_state()
                    logger.info("sell transaction")
                    self.pusher.send_message(
                        "#general", "현재" + str(evalueation_coin["market"]) +
                        "매도 하였습니다 info : " + str(evalueation_coin))
                    self.Log_Text.appendPlainText(
                        "현재" + str(evalueation_coin["market"]) +
                        "매도 하였습니다 info : " + str(evalueation_coin))
                    print("판매 완료")
                    break
                disparity = float(
                    self.machine.get_disparity(
                        currency_type=evalueation_coin["market"],
                        term=self.term))
                print("매도 시점 기다리는 중")
                time.sleep(int(term) * 60)
                self.check_ordered_state()

    def stop(self):
        return 0