컴퓨터/Python

upbit 이동평균 수정중20210527

풍경소리^^ 2021. 5. 27. 18:18
import time
import pyupbit
ticker="KRW-ETC"
state = False
high_price = []
while True:
    # df10 = pyupbit.get_ohlcv(ticker=ticker, interval="minute10", count=1)
    df = pyupbit.get_ohlcv(ticker=ticker, interval="minute1", count =200)
    # print(df['close'])
    ma = df['close'].rolling(20).mean()
    # print(ma5)
    last_ma_3 = ma[-3]
    last_ma_2 = ma[-2]
    last_ma_1 = ma[-1]
    price = pyupbit.get_current_price(ticker)
    print(last_ma_3,last_ma_2,last_ma_1, "현재가 :" ,price)
    buy_price = []
    # high_price = []
    # print(df['high'][-3],df['high'][-2],df['high'][-1])
    # print(df['high'][-1])
    # 고가갱신
    def new_high():
        if high_price == []:
            print("고가 : null상태")
            if df['high'][-3] < df['high'][-2] and df['high'][-2] < df['high'][-1]:
                high_price.append(df['high'][-1])
                print("고가 :",high_price)
            else:
                if df['high'][-3] < df['high'][-2]:
                    high_price.append(df['high'][-2])
                    print("고가 :",high_price)
                else:
                    high_price.append(df['high'][-3])
                    print("고가 :",high_price)
        else:
            print("고가 : data 있음 갱신요망")
            if df['high'][-3] < df['high'][-2] and df['high'][-2] < df['high'][-1]:
                high_price.append(df['high'][-1])
                print("고가 :", high_price)
            else:
                if df['high'][-3] < df['high'][-2]:
                    high_price.append(df['high'][-2])
                    print("고가 :", high_price)
                else:
                    high_price.append(df['high'][-3])
                    print("고가 :", high_price)
    # if state == False and last_ma5_3 < last_ma5_2 and last_ma5_2 < last_ma5_1 and last_ma5_1 < price:
    if state == False and  last_ma_3 < last_ma_2 and last_ma_2 < last_ma_1 and last_ma_1 < price:
        print("-" * 80)
        print(last_ma_3, last_ma_2, last_ma_1, price)
        print("buy : ", price)
        buy_price.append(price)
        new_high()
        print(high_price[-1])
        state = True
    # elif state == True and price <= last_ma5_1:
    elif state == True:
        if price < last_ma_1 or price < high_price[-1]:
        # if  price < last_ma_1:
            # if last_ma_1 > price or buy_price > price:
            # if (last_ma_2 - last_ma_3) > (last_ma_1 - last_ma_2):
            print("-" * 80)
            print(last_ma_3, last_ma_2, last_ma_1)
            # print(last_ma_3-last_ma_2, last_ma_2-last_ma_1, "고가")
            print(last_ma_3-last_ma_2, last_ma_2-last_ma_1, "고가 : ", high_price[-1]) # *******************
            print("sell : ", price)
            state = False
            print("=" * 80)
        # print(high_price)
        # elif last_ma_3 < last_ma_2 and last_ma_2 < last_ma_1:
        #     if (last_ma_2 - last_ma_3) > (last_ma_1 - last_ma_2):
        #         print("-" * 80)
        #         print(last_ma_3, last_ma_2, last_ma_1)
        #         print(last_ma_3-last_ma_2, last_ma_2-last_ma_1)
        #         print("sell : ", price)
        #         state = False
        #         print("=" * 80)
        # else:
        #     print("-" * 80)
        #     print(last_ma_3, last_ma_2, last_ma_1)
        #     print(last_ma_3 - last_ma_2, last_ma_2 - last_ma_1)
        #     print("sell : ", price)
        #     state = False
        #     print("=" * 80)
    time.sleep(60) # 3분 동안 고가보다 아래에 있으면 매도

91381.0 91384.5 91397.0 현재가 : 91750.0
--------------------------------------------------------------------------------
91381.0 91384.5 91397.0 91750.0
buy :  91750.0
고가 : data 있음 갱신요망
고가 : [91760.0, 91750.0]
91750.0