공부하기싫어

목차

     

     

    'NoneType' object is not subscriptable

     

     

    import pyupbit
    import numpy as np
    import boto3
    import slack_sdk
    
    
    def get_ror(k):
        df = pyupbit.get_ohlcv("KRW-ETH", count=14)
        df['range'] = (df['high'] - df['low']) * k
        df['target'] = df['open'] + df['range'].shift(1)
    
        df['ror'] = np.where(df['high'] > df['target'],
                             df['close'] / df['target'],
                             1)
    
        ror = df['ror'].cumprod()[-2]
        return ror
    
    
    
    def update_dynamodb_table(bestk):
        print("start function : updating dynamoDB talbe")
        dynamodb = boto3.client('dynamodb')
        
        # define the table name and the key of the item to be updated
        table_name = 'Table-ForEthauto-PROD-ethauto'
        item_key = {'env': {'S': 'PROD'}}
        
        # define the attribute to be updated and its new value
        attribute_name = 'k-value'
        new_value = bestk
        
        # update the item with the new attribute value
        try:
            response = dynamodb.update_item(
                TableName=table_name,
                Key=item_key,
                UpdateExpression='SET #attr = :val',
                ExpressionAttributeNames={'#attr': attribute_name},
                ExpressionAttributeValues={':val': {'N': str(new_value)}}
            )
            print("success : updating dynamoDB talbe")
        except Exception as e:
            print("Exception : ", e)
    
        return response
    
    def get_parameter_fromSSM() :
        print("start function : get_parameter_fromSSM")
        ssm = boto3.client('ssm')
    
        parameters=['/ethauto/slack-token']
        ssm_para=list()
    
        for i in parameters:
            response = ssm.get_parameter(
                Name=i,
                WithDecryption=True
            )
            ssm_para.append(response['Parameter']['Value'])
        
        return ssm_para[0]
    
    
    def handler(event, context):
        slack_token=get_parameter_fromSSM()
        client=slack_sdk.WebClient(token=slack_token)
        
        try:
            dict={}
            for k in np.arange(0.05, 1.0, 0.05):
                ror = get_ror(k)
                print("%.2f %f" % (k, ror))
                if ror<1 :
                    k=k*(-1)
                dict[k]=ror
            bestk=max(dict, key=dict.get)
            bestk=round(bestk, 2)
            
            
            result=update_dynamodb_table(bestk)
    
            client.chat_postMessage(channel='#ethauto-step',
                                    text='Lambda-Bestk: k-value: '+str(bestk))
        except Exception as e:
            client.chat_postMessage(channel='#ethauto-step',
                                    text='Lambda-Bestk: Exception : '+str(e))
    
        return result

     

    aws lambda 에서 aws python lambda OS 로 위 코드를 실행할 때 에러 발생

     

    def get_ror(k):
        df = pyupbit.get_ohlcv("KRW-ETH", count=14)
        print(df)
        df['range'] = (df['high'] - df['low']) * k
        print(df)
        df['target'] = df['open'] + df['range'].shift(1)
        print(df)
    
        df['ror'] = np.where(df['high'] > df['target'],
                             df['close'] / df['target'],
                             1)
        print(df)
        ror = df['ror'].cumprod()[-2]
        return ror

    연산 결과 확인을 위해 dataframe 확인 시 아래 결과 확인

     

                              open       high        low      close        volume         value
    2023-09-11 09:00:00  2205000.0  2205000.0  2100000.0  2117000.0  13922.147352  2.997155e+10
    2023-09-12 09:00:00  2117000.0  2200000.0  2113000.0  2161000.0   9429.845497  2.036473e+10
    2023-09-13 09:00:00  2161000.0  2190000.0  2150000.0  2177000.0   7085.961545  1.536401e+10
    2023-09-14 09:00:00  2177000.0  2223000.0  2175000.0  2197000.0   8069.592262  1.775989e+10
    2023-09-15 09:00:00  2197000.0  2233000.0  2186000.0  2215000.0   7022.229910  1.549405e+10
    2023-09-16 09:00:00  2215000.0  2227000.0  2208000.0  2215000.0   4623.758620  1.024185e+10
    2023-09-17 09:00:00  2215000.0  2215000.0  2182000.0  2187000.0   3693.718011  8.113242e+09
    2023-09-18 09:00:00  2188000.0  2242000.0  2170000.0  2200000.0   9680.421878  2.138760e+10
    2023-09-19 09:00:00  2200000.0  2227000.0  2183000.0  2205000.0   7059.367434  1.555607e+10
    2023-09-20 09:00:00  2205000.0  2214000.0  2166000.0  2185000.0   9245.595337  2.028276e+10
    2023-09-21 09:00:00  2185000.0  2197000.0  2130000.0  2146000.0   9363.547861  2.031339e+10
    2023-09-22 09:00:00  2148000.0  2165000.0  2140000.0  2158000.0   4711.552650  1.015547e+10
    2023-09-23 09:00:00  2157000.0  2162000.0  2151000.0  2158000.0   2464.083515  5.313279e+09
    2023-09-24 09:00:00  2158000.0  2161000.0  2151000.0  2156000.0   2289.263986  4.935979e+09
                              open       high        low      close        volume         value    range
    2023-09-11 09:00:00  2205000.0  2205000.0  2100000.0  2117000.0  13922.147352  2.997155e+10  52500.0
    2023-09-12 09:00:00  2117000.0  2200000.0  2113000.0  2161000.0   9429.845497  2.036473e+10  43500.0
    2023-09-13 09:00:00  2161000.0  2190000.0  2150000.0  2177000.0   7085.961545  1.536401e+10  20000.0
    2023-09-14 09:00:00  2177000.0  2223000.0  2175000.0  2197000.0   8069.592262  1.775989e+10  24000.0
    2023-09-15 09:00:00  2197000.0  2233000.0  2186000.0  2215000.0   7022.229910  1.549405e+10  23500.0
    2023-09-16 09:00:00  2215000.0  2227000.0  2208000.0  2215000.0   4623.758620  1.024185e+10   9500.0
    2023-09-17 09:00:00  2215000.0  2215000.0  2182000.0  2187000.0   3693.718011  8.113242e+09  16500.0
    2023-09-18 09:00:00  2188000.0  2242000.0  2170000.0  2200000.0   9680.421878  2.138760e+10  36000.0
    2023-09-19 09:00:00  2200000.0  2227000.0  2183000.0  2205000.0   7059.367434  1.555607e+10  22000.0
    2023-09-20 09:00:00  2205000.0  2214000.0  2166000.0  2185000.0   9245.595337  2.028276e+10  24000.0
    2023-09-21 09:00:00  2185000.0  2197000.0  2130000.0  2146000.0   9363.547861  2.031339e+10  33500.0
    2023-09-22 09:00:00  2148000.0  2165000.0  2140000.0  2158000.0   4711.552650  1.015547e+10  12500.0
    2023-09-23 09:00:00  2157000.0  2162000.0  2151000.0  2158000.0   2464.083515  5.313279e+09   5500.0
    2023-09-24 09:00:00  2158000.0  2161000.0  2151000.0  2156000.0   2289.263986  4.935979e+09   5000.0
                              open       high        low      close        volume         value    range     target
    2023-09-11 09:00:00  2205000.0  2205000.0  2100000.0  2117000.0  13922.147352  2.997155e+10  52500.0        NaN
    2023-09-12 09:00:00  2117000.0  2200000.0  2113000.0  2161000.0   9429.845497  2.036473e+10  43500.0  2169500.0
    2023-09-13 09:00:00  2161000.0  2190000.0  2150000.0  2177000.0   7085.961545  1.536401e+10  20000.0  2204500.0
    2023-09-14 09:00:00  2177000.0  2223000.0  2175000.0  2197000.0   8069.592262  1.775989e+10  24000.0  2197000.0
    2023-09-15 09:00:00  2197000.0  2233000.0  2186000.0  2215000.0   7022.229910  1.549405e+10  23500.0  2221000.0
    2023-09-16 09:00:00  2215000.0  2227000.0  2208000.0  2215000.0   4623.758620  1.024185e+10   9500.0  2238500.0
    2023-09-17 09:00:00  2215000.0  2215000.0  2182000.0  2187000.0   3693.718011  8.113242e+09  16500.0  2224500.0
    2023-09-18 09:00:00  2188000.0  2242000.0  2170000.0  2200000.0   9680.421878  2.138760e+10  36000.0  2204500.0
    2023-09-19 09:00:00  2200000.0  2227000.0  2183000.0  2205000.0   7059.367434  1.555607e+10  22000.0  2236000.0
    2023-09-20 09:00:00  2205000.0  2214000.0  2166000.0  2185000.0   9245.595337  2.028276e+10  24000.0  2227000.0
    2023-09-21 09:00:00  2185000.0  2197000.0  2130000.0  2146000.0   9363.547861  2.031339e+10  33500.0  2209000.0
    2023-09-22 09:00:00  2148000.0  2165000.0  2140000.0  2158000.0   4711.552650  1.015547e+10  12500.0  2181500.0
    2023-09-23 09:00:00  2157000.0  2162000.0  2151000.0  2158000.0   2464.083515  5.313279e+09   5500.0  2169500.0
    2023-09-24 09:00:00  2158000.0  2161000.0  2151000.0  2156000.0   2289.263986  4.935979e+09   5000.0  2163500.0
                              open       high        low      close        volume         value    range     target       ror
    2023-09-11 09:00:00  2205000.0  2205000.0  2100000.0  2117000.0  13922.147352  2.997155e+10  52500.0        NaN  1.000000
    2023-09-12 09:00:00  2117000.0  2200000.0  2113000.0  2161000.0   9429.845497  2.036473e+10  43500.0  2169500.0  0.996082
    2023-09-13 09:00:00  2161000.0  2190000.0  2150000.0  2177000.0   7085.961545  1.536401e+10  20000.0  2204500.0  1.000000
    2023-09-14 09:00:00  2177000.0  2223000.0  2175000.0  2197000.0   8069.592262  1.775989e+10  24000.0  2197000.0  1.000000
    2023-09-15 09:00:00  2197000.0  2233000.0  2186000.0  2215000.0   7022.229910  1.549405e+10  23500.0  2221000.0  0.997299
    2023-09-16 09:00:00  2215000.0  2227000.0  2208000.0  2215000.0   4623.758620  1.024185e+10   9500.0  2238500.0  1.000000
    2023-09-17 09:00:00  2215000.0  2215000.0  2182000.0  2187000.0   3693.718011  8.113242e+09  16500.0  2224500.0  1.000000
    2023-09-18 09:00:00  2188000.0  2242000.0  2170000.0  2200000.0   9680.421878  2.138760e+10  36000.0  2204500.0  0.997959
    2023-09-19 09:00:00  2200000.0  2227000.0  2183000.0  2205000.0   7059.367434  1.555607e+10  22000.0  2236000.0  1.000000
    2023-09-20 09:00:00  2205000.0  2214000.0  2166000.0  2185000.0   9245.595337  2.028276e+10  24000.0  2227000.0  1.000000
    2023-09-21 09:00:00  2185000.0  2197000.0  2130000.0  2146000.0   9363.547861  2.031339e+10  33500.0  2209000.0  1.000000
    2023-09-22 09:00:00  2148000.0  2165000.0  2140000.0  2158000.0   4711.552650  1.015547e+10  12500.0  2181500.0  1.000000
    2023-09-23 09:00:00  2157000.0  2162000.0  2151000.0  2158000.0   2464.083515  5.313279e+09   5500.0  2169500.0  1.000000
    2023-09-24 09:00:00  2158000.0  2161000.0  2151000.0  2156000.0   2289.263986  4.935979e+09   5000.0  2163500.0  1.000000
    /home/cyaninn/Cryptocurrency-autotrade-pyupbit/test/test-bestk.py:16: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`
      ror = df['ror'].cumprod()[-2]
    0.9913633563495294

    target 컬럼에 있는 NaN 이 문제이지 않나 싶음

     

    아래 코드로 수정 후 도커 빌드 후 테스트

    ...
    
    def get_ror(k):
        try:
            df = pyupbit.get_ohlcv("KRW-ETH", count=14)
            #print(df)
        except Exception as e:
            print("Exception1 : ", e)
            
        try:
            df['range'] = (df['high'] - df['low']) * k
            #print(df)
        except Exception as e:
            print("Exception2 : ", e)
            
        try:
            df['target'] = df['open'] + df['range'].shift(1)
            #print(df)
        except Exception as e:
            print("Exception3 : ", e)
        try:
            df['ror'] = np.where(df['high'] > df['target'],
                                df['close'] / df['target'],
                                1)
            #print(df)
        except Exception as e:
            print("Exception4 : ", e)
            
        ror = df['ror'].cumprod().iloc[-2]
        return ror
        
       ...

     

    테스트 결과

    START RequestId: 25c3fbf1-8bb5-4300-a13d-78c2671d3cb7 Version: $LATEST
    start function : get_parameter_fromSSM
    0.05 1.016940
    0.10 1.002644
    0.15 0.988569
    0.20 0.976520
    0.25 1.000298
    0.30 0.999902
    0.35 0.997725
    0.40 1.001663
    0.45 0.996496
    0.50 0.991363
    0.55 0.986266
    0.60 0.981203
    0.65 0.976174
    Exception2 :  'NoneType' object is not subscriptable
    Exception3 :  'NoneType' object is not subscriptable
    Exception4 :  'NoneType' object is not subscriptable
    [ERROR] UnboundLocalError: local variable 'result' referenced before assignment
    Traceback (most recent call last):
      File "/var/task/app.py", line 118, in handler
        return result
    END RequestId: 25c3fbf1-8bb5-4300-a13d-78c2671d3cb7
    REPORT RequestId: 25c3fbf1-8bb5-4300-a13d-78c2671d3cb7	Duration: 7586.89 ms	Billed Duration: 7587 ms	Memory Size: 512 MB	Max Memory Used: 81 MB

    0.65 까진 잘 가다가 에러 발생

    0.65, 0.7 이 나올때 로컬에서 테스트 후 df 출력 확인

     

                              open       high        low      close        volume         value    range
    2023-09-11 09:00:00  2205000.0  2205000.0  2100000.0  2117000.0  13922.147352  2.997155e+10  68250.0
    2023-09-12 09:00:00  2117000.0  2200000.0  2113000.0  2161000.0   9429.845497  2.036473e+10  56550.0
    2023-09-13 09:00:00  2161000.0  2190000.0  2150000.0  2177000.0   7085.961545  1.536401e+10  26000.0
    2023-09-14 09:00:00  2177000.0  2223000.0  2175000.0  2197000.0   8069.592262  1.775989e+10  31200.0
    2023-09-15 09:00:00  2197000.0  2233000.0  2186000.0  2215000.0   7022.229910  1.549405e+10  30550.0
    2023-09-16 09:00:00  2215000.0  2227000.0  2208000.0  2215000.0   4623.758620  1.024185e+10  12350.0
    2023-09-17 09:00:00  2215000.0  2215000.0  2182000.0  2187000.0   3693.718011  8.113242e+09  21450.0
    2023-09-18 09:00:00  2188000.0  2242000.0  2170000.0  2200000.0   9680.421878  2.138760e+10  46800.0
    2023-09-19 09:00:00  2200000.0  2227000.0  2183000.0  2205000.0   7059.367434  1.555607e+10  28600.0
    2023-09-20 09:00:00  2205000.0  2214000.0  2166000.0  2185000.0   9245.595337  2.028276e+10  31200.0
    2023-09-21 09:00:00  2185000.0  2197000.0  2130000.0  2146000.0   9363.547861  2.031339e+10  43550.0
    2023-09-22 09:00:00  2148000.0  2165000.0  2140000.0  2158000.0   4711.552650  1.015547e+10  16250.0
    2023-09-23 09:00:00  2157000.0  2162000.0  2151000.0  2158000.0   2464.083515  5.313279e+09   7150.0
    2023-09-24 09:00:00  2158000.0  2161000.0  2150000.0  2150000.0   2510.385134  5.411795e+09   7150.0
    0.65 0.976174
                              open       high        low      close        volume         value    range
    2023-09-11 09:00:00  2205000.0  2205000.0  2100000.0  2117000.0  13922.147352  2.997155e+10  73500.0
    2023-09-12 09:00:00  2117000.0  2200000.0  2113000.0  2161000.0   9429.845497  2.036473e+10  60900.0
    2023-09-13 09:00:00  2161000.0  2190000.0  2150000.0  2177000.0   7085.961545  1.536401e+10  28000.0
    2023-09-14 09:00:00  2177000.0  2223000.0  2175000.0  2197000.0   8069.592262  1.775989e+10  33600.0
    2023-09-15 09:00:00  2197000.0  2233000.0  2186000.0  2215000.0   7022.229910  1.549405e+10  32900.0
    2023-09-16 09:00:00  2215000.0  2227000.0  2208000.0  2215000.0   4623.758620  1.024185e+10  13300.0
    2023-09-17 09:00:00  2215000.0  2215000.0  2182000.0  2187000.0   3693.718011  8.113242e+09  23100.0
    2023-09-18 09:00:00  2188000.0  2242000.0  2170000.0  2200000.0   9680.421878  2.138760e+10  50400.0
    2023-09-19 09:00:00  2200000.0  2227000.0  2183000.0  2205000.0   7059.367434  1.555607e+10  30800.0
    2023-09-20 09:00:00  2205000.0  2214000.0  2166000.0  2185000.0   9245.595337  2.028276e+10  33600.0
    2023-09-21 09:00:00  2185000.0  2197000.0  2130000.0  2146000.0   9363.547861  2.031339e+10  46900.0
    2023-09-22 09:00:00  2148000.0  2165000.0  2140000.0  2158000.0   4711.552650  1.015547e+10  17500.0
    2023-09-23 09:00:00  2157000.0  2162000.0  2151000.0  2158000.0   2464.083515  5.313279e+09   7700.0
    2023-09-24 09:00:00  2158000.0  2161000.0  2150000.0  2150000.0   2510.385134  5.411795e+09   7700.0
    0.70 0.971179

    Exception2 부터 에러가 발생했는데

    그것도 for문 중간에 에러가 난게 이상하다

    테스트 환경과 lambda 의 라이브러리 버전도 맞추고 실행해봐도 똑같다.

     

    로컬에서 문제가 없길래 람다 함수 이미지를 띄워서 테스트해봤다.

    이미지 - public.ecr.aws/lambda/python:3.9
    #requirements.txt
    pyupbit==0.2.33
    botocore==1.20.55
    Django==1.11.3
    slack_sdk
    plotly==5.11.0
    numpy==1.25.2

     

    테스트 코드1

    import pyupbit
    import numpy as np
    
    
    def get_ror(k):
        print('start get_ror - k :'+str(k))
        print('line 1')
        try:
            df = pyupbit.get_ohlcv("KRW-ETH", count=14)
            #print(df)
        except Exception as e:
            print("Exception1 : ", e)
        
        print('line 2')    
        try:
            df['range'] = (df['high'] - df['low']) * k
            #print(df)
            #print(k)
            #print(df['high'])
        except Exception as e:
            print("Exception2 : ", e)
            
        print('line 3')
        try:
            df['target'] = df['open'] + df['range'].shift(1)
            #print(df)
        except Exception as e:
            print("Exception3 : ", e)
        
        print('line 4')
        try:
            df['ror'] = np.where(df['high'] > df['target'],
                                df['close'] / df['target'],
                                1)
            #print(df)
        except Exception as e:
            print("Exception4 : ", e)
        
        #print(df['ror'].cumprod())
        print('line 5')
        ror = df['ror'].cumprod().iloc[-2]
        return ror
    
    
    
    
    dict={}
    '''
    try:
        for k in np.arange(0.05, 1.0, 0.05):
            ror = get_ror(k)
            print("%.2f %f" % (k, ror))
            if ror<1 :
                k=k*(-1)
            dict[k]=ror
    except:
        pass
    '''
    for k in np.arange(0.05, 1.0, 0.05):
        ror = get_ror(k)
        print("%.2f %f" % (k, ror))
        if ror<1 :
            k=k*(-1)
        dict[k]=ror
    bestk=max(dict, key=dict.get)
    bestk=round(bestk, 2)
    
    print(dict)
    
    bestk=max(dict, key=dict.get)
    bestk=round(bestk, 2)
    print(bestk)
    #print(get_ror(0.5))
    #print(get_ror(0.55))

    위 테스트 코드를 실행시켜 나온 결과에서

    에러가 뜨는 k 값만 실행시키는 테스트코드 2를 실행시켰다

     

    테스트코드1 실행 결과

    start get_ror - k :0.5
    line 1
    line 2
    line 3
    line 4
    line 5
    0.50 0.991363
    start get_ror - k :0.55
    line 1
    line 2
    Exception2 :  'NoneType' object is not subscriptable
    line 3
    Exception3 :  'NoneType' object is not subscriptable
    line 4
    Exception4 :  'NoneType' object is not subscriptable
    line 5
    Traceback (most recent call last):
      File "/var/task/lambda.py", line 60, in <module>
        ror = get_ror(k)
      File "/var/task/lambda.py", line 41, in get_ror
        ror = df['ror'].cumprod().iloc[-2]
    TypeError: 'NoneType' object is not subscriptable

    k 값이 0.55 일때 실패한다

     

    테스트코드2

    import numpy as np
    
    
    def get_ror(k):
        try:
            df = pyupbit.get_ohlcv("KRW-ETH", count=14)
            print(df)
        except Exception as e:
            print("Exception1 : ", e)
            
        try:
            df['range'] = (df['high'] - df['low']) * k
            print(df)
            #print(k)
            #print(df['high'])
        except Exception as e:
            print("Exception2 : ", e)
            
        try:
            df['target'] = df['open'] + df['range'].shift(1)
            print(df)
        except Exception as e:
            print("Exception3 : ", e)
        try:
            df['ror'] = np.where(df['high'] > df['target'],
                                df['close'] / df['target'],
                                1)
            print(df)
        except Exception as e:
            print("Exception4 : ", e)
        #print(df['ror'].cumprod())
        #print(df['ror'].cumprod().iloc[-1])
        #print(df['ror'].cumprod().iloc[-2])
        print(df)
        ror = df['ror'].cumprod().iloc[-2]
        return ror
    
    
    
    '''
    dict={}
    for k in np.arange(0.05, 1.0, 0.05):
        ror = get_ror(k)
        print("%.2f %f" % (k, ror))
        if ror<1 :
            k=k*(-1)
        dict[k]=ror
    bestk=max(dict, key=dict.get)
    bestk=round(bestk, 2)
    
    #print(dict)
    
    bestk=max(dict, key=dict.get)
    bestk=round(bestk, 2)
    print(bestk)'''
    #print(get_ror(0.5))
    print(get_ror(0.55))

     

    테스트코드2 실행 결과

    bash-4.2# python3 2-lambda.py 
                              open       high        low      close        volume         value
    2023-09-11 09:00:00  2205000.0  2205000.0  2100000.0  2117000.0  13922.147352  2.997155e+10
    2023-09-12 09:00:00  2117000.0  2200000.0  2113000.0  2161000.0   9429.845497  2.036473e+10
    2023-09-13 09:00:00  2161000.0  2190000.0  2150000.0  2177000.0   7085.961545  1.536401e+10
    2023-09-14 09:00:00  2177000.0  2223000.0  2175000.0  2197000.0   8069.592262  1.775989e+10
    2023-09-15 09:00:00  2197000.0  2233000.0  2186000.0  2215000.0   7022.229910  1.549405e+10
    2023-09-16 09:00:00  2215000.0  2227000.0  2208000.0  2215000.0   4623.758620  1.024185e+10
    2023-09-17 09:00:00  2215000.0  2215000.0  2182000.0  2187000.0   3693.718011  8.113242e+09
    2023-09-18 09:00:00  2188000.0  2242000.0  2170000.0  2200000.0   9680.421878  2.138760e+10
    2023-09-19 09:00:00  2200000.0  2227000.0  2183000.0  2205000.0   7059.367434  1.555607e+10
    2023-09-20 09:00:00  2205000.0  2214000.0  2166000.0  2185000.0   9245.595337  2.028276e+10
    2023-09-21 09:00:00  2185000.0  2197000.0  2130000.0  2146000.0   9363.547861  2.031339e+10
    2023-09-22 09:00:00  2148000.0  2165000.0  2140000.0  2158000.0   4711.552650  1.015547e+10
    2023-09-23 09:00:00  2157000.0  2162000.0  2151000.0  2158000.0   2464.083515  5.313279e+09
    2023-09-24 09:00:00  2158000.0  2164000.0  2139000.0  2149000.0   3368.613061  7.257310e+09
                              open       high        low      close        volume         value    range
    2023-09-11 09:00:00  2205000.0  2205000.0  2100000.0  2117000.0  13922.147352  2.997155e+10  57750.0
    2023-09-12 09:00:00  2117000.0  2200000.0  2113000.0  2161000.0   9429.845497  2.036473e+10  47850.0
    2023-09-13 09:00:00  2161000.0  2190000.0  2150000.0  2177000.0   7085.961545  1.536401e+10  22000.0
    2023-09-14 09:00:00  2177000.0  2223000.0  2175000.0  2197000.0   8069.592262  1.775989e+10  26400.0
    2023-09-15 09:00:00  2197000.0  2233000.0  2186000.0  2215000.0   7022.229910  1.549405e+10  25850.0
    2023-09-16 09:00:00  2215000.0  2227000.0  2208000.0  2215000.0   4623.758620  1.024185e+10  10450.0
    2023-09-17 09:00:00  2215000.0  2215000.0  2182000.0  2187000.0   3693.718011  8.113242e+09  18150.0
    2023-09-18 09:00:00  2188000.0  2242000.0  2170000.0  2200000.0   9680.421878  2.138760e+10  39600.0
    2023-09-19 09:00:00  2200000.0  2227000.0  2183000.0  2205000.0   7059.367434  1.555607e+10  24200.0
    2023-09-20 09:00:00  2205000.0  2214000.0  2166000.0  2185000.0   9245.595337  2.028276e+10  26400.0
    2023-09-21 09:00:00  2185000.0  2197000.0  2130000.0  2146000.0   9363.547861  2.031339e+10  36850.0
    2023-09-22 09:00:00  2148000.0  2165000.0  2140000.0  2158000.0   4711.552650  1.015547e+10  13750.0
    2023-09-23 09:00:00  2157000.0  2162000.0  2151000.0  2158000.0   2464.083515  5.313279e+09   6050.0
    2023-09-24 09:00:00  2158000.0  2164000.0  2139000.0  2149000.0   3368.613061  7.257310e+09  13750.0
                              open       high        low      close        volume         value    range     target
    2023-09-11 09:00:00  2205000.0  2205000.0  2100000.0  2117000.0  13922.147352  2.997155e+10  57750.0        NaN
    2023-09-12 09:00:00  2117000.0  2200000.0  2113000.0  2161000.0   9429.845497  2.036473e+10  47850.0  2174750.0
    2023-09-13 09:00:00  2161000.0  2190000.0  2150000.0  2177000.0   7085.961545  1.536401e+10  22000.0  2208850.0
    2023-09-14 09:00:00  2177000.0  2223000.0  2175000.0  2197000.0   8069.592262  1.775989e+10  26400.0  2199000.0
    2023-09-15 09:00:00  2197000.0  2233000.0  2186000.0  2215000.0   7022.229910  1.549405e+10  25850.0  2223400.0
    2023-09-16 09:00:00  2215000.0  2227000.0  2208000.0  2215000.0   4623.758620  1.024185e+10  10450.0  2240850.0
    2023-09-17 09:00:00  2215000.0  2215000.0  2182000.0  2187000.0   3693.718011  8.113242e+09  18150.0  2225450.0
    2023-09-18 09:00:00  2188000.0  2242000.0  2170000.0  2200000.0   9680.421878  2.138760e+10  39600.0  2206150.0
    2023-09-19 09:00:00  2200000.0  2227000.0  2183000.0  2205000.0   7059.367434  1.555607e+10  24200.0  2239600.0
    2023-09-20 09:00:00  2205000.0  2214000.0  2166000.0  2185000.0   9245.595337  2.028276e+10  26400.0  2229200.0
    2023-09-21 09:00:00  2185000.0  2197000.0  2130000.0  2146000.0   9363.547861  2.031339e+10  36850.0  2211400.0
    2023-09-22 09:00:00  2148000.0  2165000.0  2140000.0  2158000.0   4711.552650  1.015547e+10  13750.0  2184850.0
    2023-09-23 09:00:00  2157000.0  2162000.0  2151000.0  2158000.0   2464.083515  5.313279e+09   6050.0  2170750.0
    2023-09-24 09:00:00  2158000.0  2164000.0  2139000.0  2149000.0   3368.613061  7.257310e+09  13750.0  2164050.0
                              open       high        low      close        volume         value    range     target       ror
    2023-09-11 09:00:00  2205000.0  2205000.0  2100000.0  2117000.0  13922.147352  2.997155e+10  57750.0        NaN  1.000000
    2023-09-12 09:00:00  2117000.0  2200000.0  2113000.0  2161000.0   9429.845497  2.036473e+10  47850.0  2174750.0  0.993677
    2023-09-13 09:00:00  2161000.0  2190000.0  2150000.0  2177000.0   7085.961545  1.536401e+10  22000.0  2208850.0  1.000000
    2023-09-14 09:00:00  2177000.0  2223000.0  2175000.0  2197000.0   8069.592262  1.775989e+10  26400.0  2199000.0  0.999090
    2023-09-15 09:00:00  2197000.0  2233000.0  2186000.0  2215000.0   7022.229910  1.549405e+10  25850.0  2223400.0  0.996222
    2023-09-16 09:00:00  2215000.0  2227000.0  2208000.0  2215000.0   4623.758620  1.024185e+10  10450.0  2240850.0  1.000000
    2023-09-17 09:00:00  2215000.0  2215000.0  2182000.0  2187000.0   3693.718011  8.113242e+09  18150.0  2225450.0  1.000000
    2023-09-18 09:00:00  2188000.0  2242000.0  2170000.0  2200000.0   9680.421878  2.138760e+10  39600.0  2206150.0  0.997212
    2023-09-19 09:00:00  2200000.0  2227000.0  2183000.0  2205000.0   7059.367434  1.555607e+10  24200.0  2239600.0  1.000000
    2023-09-20 09:00:00  2205000.0  2214000.0  2166000.0  2185000.0   9245.595337  2.028276e+10  26400.0  2229200.0  1.000000
    2023-09-21 09:00:00  2185000.0  2197000.0  2130000.0  2146000.0   9363.547861  2.031339e+10  36850.0  2211400.0  1.000000
    2023-09-22 09:00:00  2148000.0  2165000.0  2140000.0  2158000.0   4711.552650  1.015547e+10  13750.0  2184850.0  1.000000
    2023-09-23 09:00:00  2157000.0  2162000.0  2151000.0  2158000.0   2464.083515  5.313279e+09   6050.0  2170750.0  1.000000
    2023-09-24 09:00:00  2158000.0  2164000.0  2139000.0  2149000.0   3368.613061  7.257310e+09  13750.0  2164050.0  1.000000
                              open       high        low      close        volume         value    range     target       ror
    2023-09-11 09:00:00  2205000.0  2205000.0  2100000.0  2117000.0  13922.147352  2.997155e+10  57750.0        NaN  1.000000
    2023-09-12 09:00:00  2117000.0  2200000.0  2113000.0  2161000.0   9429.845497  2.036473e+10  47850.0  2174750.0  0.993677
    2023-09-13 09:00:00  2161000.0  2190000.0  2150000.0  2177000.0   7085.961545  1.536401e+10  22000.0  2208850.0  1.000000
    2023-09-14 09:00:00  2177000.0  2223000.0  2175000.0  2197000.0   8069.592262  1.775989e+10  26400.0  2199000.0  0.999090
    2023-09-15 09:00:00  2197000.0  2233000.0  2186000.0  2215000.0   7022.229910  1.549405e+10  25850.0  2223400.0  0.996222
    2023-09-16 09:00:00  2215000.0  2227000.0  2208000.0  2215000.0   4623.758620  1.024185e+10  10450.0  2240850.0  1.000000
    2023-09-17 09:00:00  2215000.0  2215000.0  2182000.0  2187000.0   3693.718011  8.113242e+09  18150.0  2225450.0  1.000000
    2023-09-18 09:00:00  2188000.0  2242000.0  2170000.0  2200000.0   9680.421878  2.138760e+10  39600.0  2206150.0  0.997212
    2023-09-19 09:00:00  2200000.0  2227000.0  2183000.0  2205000.0   7059.367434  1.555607e+10  24200.0  2239600.0  1.000000
    2023-09-20 09:00:00  2205000.0  2214000.0  2166000.0  2185000.0   9245.595337  2.028276e+10  26400.0  2229200.0  1.000000
    2023-09-21 09:00:00  2185000.0  2197000.0  2130000.0  2146000.0   9363.547861  2.031339e+10  36850.0  2211400.0  1.000000
    2023-09-22 09:00:00  2148000.0  2165000.0  2140000.0  2158000.0   4711.552650  1.015547e+10  13750.0  2184850.0  1.000000
    2023-09-23 09:00:00  2157000.0  2162000.0  2151000.0  2158000.0   2464.083515  5.313279e+09   6050.0  2170750.0  1.000000
    2023-09-24 09:00:00  2158000.0  2164000.0  2139000.0  2149000.0   3368.613061  7.257310e+09  13750.0  2164050.0  1.000000
    0.986265922071536
    df['range'] = (df['high'] - df['low']) * k

    에서부터 에러가 나고 있는데 이유를 모르겠다

    없는 행을 미리 호출해서인가?

     

    df 가 생성되지 않아서 nonetype 이 떳다

    더 알아보기 싫어서 그냥 try except 로 pass 하려고 한다

     

     

     

    use `ser.iloc[pos]`

    @추가로 numpy 라이브러리가 업데이트 되었는지 아래와 같은 경고가 발생했다.

    test-bestk.py:16: 
    FutureWarning: Series.__getitem__ treating keys as positions is deprecated. 
    In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). 
    To access a value by position, use `ser.iloc[pos]`
    ror = df['ror'].cumprod()[-2]

     

    챗지피티

    아래와 같이 ser.iloc[pos]를 사용하여 코드를 수정할 수 있습니다:
    
    python
    ror = df['ror'].cumprod().iloc[-2]
    이렇게 하면 'ror' 열의 데이터에 대한 누적 곱을 계산한 후, 
    그 결과 중에서 뒤에서 두 번째 값을 iloc을 사용하여 위치(position)로 접근하게 됩니다. 
    이렇게 하면 미래 버전의 Numpy나 Pandas에서도 호환성 문제가 발생하지 않을 것입니다.

     

     

    참고

    https://seong6496.tistory.com/216

     

    [Pandas] 특정한 컬럼의 행 올리기,내리기(shift)

    데이터 작업을 하다보면 다른 행과 계산할 경우 shift를 이용해 해결할 수 있습니다. for문으로 인덱스를 일일히 돌리지 않아도 되고 필요에 따라 컬럼을 추가해서 눈으로 확인해도 되지만 다른

    seong6496.tistory.com

     

    https://www.snugarchive.com/blog/python-pandas-series/

     

    Pandas Series 사용법

    Series 다루는 법 살펴보기

    www.snugarchive.com

     

    https://www.codeit.kr/community/questions/UXVlc3Rpb246NWUzNDUyMjU4MGU1MTMzNzNkOTYxYmJl

     

    TypeError: 'int' object is not subscriptable 오류 메시지

     

    www.codeit.kr

     

    https://hogni.tistory.com/28

     

    파이썬을 엑셀처럼 사용하기 (5): 사칙연산하기 pandas arithmetic

    파이썬을 엑셀처럼 사용하기 시리즈 (1) 엑셀이 있는데 왜 파이썬을 배워야 하지? (2) 데이터 열기 pandas read_excel() (3) 데이터 정렬하기 pandas sort_values() (4) 데이터 필터링하기, 비교 연산자 pandas filt

    hogni.tistory.com