컴퓨터/Python

upbit 이동평균 수정중 20210528

풍경소리^^ 2021. 5. 28. 06:28
import time
import pyupbit
ticker="KRW-ETC"
state = False
high_price = []
buy_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(5).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:
    if state == False 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)
            print("구입가 :",buy_price[-1],"판매가 :",price,"손익 :",price-buy_price[-1],
                  "수수료:",(buy_price[-1]*0.05/100)+(price*0.05/100),
                  "수수료 차감후 손익 :", price-(buy_price[-1])-((buy_price[-1] * 0.05 / 100) + (price * 0.05 / 100)),
                  "수수료 차감후 손익률(%) :", (price-(price-(buy_price[-1])-((buy_price[-1] * 0.05 / 100) + (price * 0.05 / 100)))/buy_price[-1]*100))
            del buy_price[-1]
            # print(buy_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) # 3분 동안 고가보다 아래에 있으면 매도

'컴퓨터 > Python' 카테고리의 다른 글

mysql fetchall 이해하기  (0) 2021.06.23
mysql 다른ip 접속  (0) 2021.06.23
upbit 이동평균 수정중20210527  (0) 2021.05.27
upbit 이동평균  (0) 2021.05.26
upbit sim 20210526  (0) 2021.05.26