Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

I used this code: while True: df = getklines(symbol, interval, lookback) Get current…

  1. User

    I used this code: while True: df = get_klines(symbol, interval, lookback) # Get current position try: account = client.account except binance.error.ClientError as e: print(f"Error getting account information: {e}") account = None # Find position for the desired symbol if account is not None: positions = account['positions'] for p in positions: if p['symbol'] == symbol: position = p['positionAmt']

    if df is not None:
        signals = signal_generator(df)
        print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Signals: {signals}")
    
        if signals == 'buy':
                client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=0.1)
                print("Long order executed!")
                # Set Stop Loss
                stop_loss = df['Open'][-1] * 0.9
                if position == '+10%':
                    stop_loss *= 1.03
                elif position == '>+10%':
                    stop_loss = df['Open'][-1]
    
                try:
                    # Place Stop Loss order
                    client.new_order(symbol=symbol, side='SELL', type='STOP_MARKET', stopPrice=stop_loss, quantity=0.1)
                    print(f"Stop Loss order placed at {stop_loss}")
                except binance.error.ClientError as e:
                    print(f"Error placing Stop Loss order: {e}")
    
        if signals == 'sell':
                client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=0.1)
                print("Short order executed!")
                stop_loss = df['Open'][-1] * 0.9
                if position == '+10%':
                    stop_loss *= 1.03
                elif position == '>+10%':
                    stop_loss = df['Open'][-1]
    
                try:
                    # Place Stop Loss order
                    client.new_order(symbol=symbol, side='BUY', type='STOP_MARKET', stopPrice=stop_loss, quantity=0.1)
                    print(f"Stop Loss order placed at {stop_loss}")
                except binance.error.ClientError as e:
                    print(f"Error placing Stop Loss order: {e}")
    
    time.sleep(1)
    

    But I getting ERROR: The signal time is: 2023-08-02 18:18:01 - Signals: sell Short order executed! Traceback (most recent call last): File "C:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 211, in if position == '+10%': ^^^^^^^^ NameError: name 'position' is not defined

  2. Assistant

    The error occurred because the variable 'position' is not defined in the scope where you are trying to compare it with the value '+10%'. It seems that the block of code where you find the position for the desired symbol is indented incorrectly. It should be at the same level as the 'try' block.

    Here's the corrected code:

    while True:
        df = get_klines(symbol, interval, lookback)
        # Get current position
        try:
            account = client.account
        except binance.error.ClientError as e:
            print(f"Error getting account information: {e}")
            account = None
    
        # Find position for the desired symbol
        if account is not None:
            positions = account['positions']
            for p in positions:
                if p['symbol'] == symbol:
                    position = p['positionAmt']
    
        if df is not None:
            signals = signal_generator(df)
            print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Signals: {signals}")
    
            if signals == 'buy':
                client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=0.1)
                print("Long order executed!")
                # Set Stop Loss
                stop_loss = df['Open'][-1] * 0.9
                if position == '+10%':
                    stop_loss *= 1.03
                elif position == '>+10%':
                    stop_loss = df['Open'][-1]
    
                try:
                    # Place Stop Loss order
                    client.new_order(symbol=symbol, side='SELL', type='STOP_MARKET', stopPrice=stop_loss, quantity=0.1)
                    print(f"Stop Loss order placed at {stop_loss}")
                except binance.error.ClientError as e:
                    print(f"Error placing Stop Loss order: {e}")
    
            if signals == 'sell':
                client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=0.1)
                print("Short order executed!")
                stop_loss = df['Open'][-1] * 0.9
                if position == '+10%':
                    stop_loss *= 1.03
                elif position == '>+10%':
                    stop_loss = df['Open'][-1]
    
                try:
                    # Place Stop Loss order
                    client.new_order(symbol=symbol, side='BUY', type='STOP_MARKET', stopPrice=stop_loss, quantity=0.1)
                    print(f"Stop Loss order placed at {stop_loss}")
                except binance.error.ClientError as e:
                    print(f"Error placing Stop Loss order: {e}")
    
        time.sleep(1)
    

    Make sure to adjust the indentation as necessary for the rest of your code as well.

  3. User

    ERROR: File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 33, in print(positions = account['positions']) ~~~~~~~^^^^^^^^^^^^^ TypeError: 'method' object is not subscriptable

  4. Assistant

    The error message suggests that the 'positions' attribute of the 'account' object is a method, not a dictionary or subscriptable object. This means that you are trying to access the 'positions' attribute incorrectly.

    To fix this issue, you need to check the documentation or API reference of the client library you are using to determine the correct way to access the positions information.

    Here's an example of how you can modify your code to print the positions information correctly:

    while True:
        df = get_klines(symbol, interval, lookback)
        # Get current position
        try:
            account = client.account()
        except binance.error.ClientError as e:
            print(f"Error getting account information: {e}")
            account = None
    
        # Find position for the desired symbol
        if account is not None:
            positions = account.get('positions', [])  # Default to empty list if 'positions' is not available
            for p in positions:
                if p['symbol'] == symbol:
                    position = p['positionAmt']
    
        if df is not None:
            signals = signal_generator(df)
            print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Signals: {signals}")
    
            if signals == 'buy':
                # Rest of the code for 'buy' signal
    
            if signals == 'sell':
                # Rest of the code for 'sell' signal
    
        time.sleep(1)
    

    In this modified code, I added the () to the client.account line to call the method and retrieve the account information correctly. Additionally, I used the get() method to safely retrieve the 'positions' attribute from the 'account' dictionary, defaulting to an empty list if it is not available.

  5. User

    It doesn't have an position , you need to change it !

  6. Assistant

    Apologies for the confusion. If the account object does not have a 'positions' attribute, you can modify the code as follows:

    while True:
        df = get_klines(symbol, interval, lookback)
        # Get current position
        try:
            account = client.account()
        except binance.error.ClientError as e:
            print(f"Error getting account information: {e}")
            account = None
    
        # Find position for the desired symbol
        position = None
        if account is not None:
            for asset in account['balances']:
                if asset['symbol'] == symbol:
                    position = asset['free']
                    break
    
        if df is not None:
            signals = signal_generator(df)
            print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Signals: {signals}")
    
            if signals == 'buy':
                # Rest of the code for 'buy' signal
    
            if signals == 'sell':
                # Rest of the code for 'sell' signal
    
        time.sleep(1)
    

    In this updated code, I used the 'balances' attribute of the 'account' dictionary to check the available balance for the desired symbol ('symbol'). The 'free' field of the relevant asset is assigned to the 'position' variable. If the asset is not found in the 'balances' list, the 'position' remains as None.

    Please note that this assumes that the available balance in 'free' represents your position for the symbol. If the structure of the returned account data differs, please consult the documentation or API reference of the client library you are using to retrieve the position information correctly.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.