Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

I used your code: while True: lookback = 10080 df = getklines(symbol, interval,…

  1. User

    I used your code: while True: lookback = 10080 df = get_klines(symbol, interval, lookback)

    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':
            lookback = 1
            df = get_klines(symbol, interval, lookback)
            
            # Get last position entry price
            trades = client.get_account_trades(symbol=symbol)
            entry_price = float(trades[-1]['price'])
            
            # Set stop loss price at +0.2% of short entry price
            stop_loss_price = entry_price * (1 + 0.002)
            client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=0.1, stopPrice=stop_loss_price)
            
            print("Long order executed!")
    
        if signals == 'sell':
            lookback = 1
            df = get_klines(symbol, interval, lookback)
            
            # Get last position entry price
            trades = client.get_account_trades(symbol=symbol)
            entry_price = float(trades[-1]['price'])
            
            # Set stop loss price at +0.2% of short entry price
            stop_loss_price = entry_price * (1 + 0.002)
            client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=0.1, stopPrice=stop_loss_price)
            
            print("Short order executed!")
    
    time.sleep(1)
    

    Can you tell me please if the mark price is higher than 0.2% of the entry price where your code will place stop loss ?

  2. Assistant

    Yes, according to the code you provided, the stop loss price will be set at 0.2% above the entry price. This means that if the mark price (current price) is higher than 0.2% of the entry price, the code will place the stop loss order at that price.

  3. User

    Set in your code that if price is higher than 0.2% set stop loss to past candle open price , example: Entry price 200 and mark price is 200.7 you need to set stop loss on past candle open price

  4. Assistant

    Sure, I've modified the code to set the stop loss price based on the open price of the previous candle if the mark price is higher than 0.2% above the entry price. Please see the updated code below:

    import datetime as dt
    import time
    
    def set_stop_loss(symbol, entry_price):
        lookback = 1
        df = get_klines(symbol, interval, lookback)
        
        # Get the open price of the previous candle
        prev_open = float(df.iloc[-2]['open'])
        
        # Set stop loss price at the open price of the previous candle
        stop_loss_price = prev_open
        
        if entry_price * (1 + 0.002) < stop_loss_price:
            stop_loss_price = entry_price * (1 + 0.002)
        
        return stop_loss_price
    
    while True:
        lookback = 10080
        df = get_klines(symbol, interval, lookback)
    
        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':
                lookback = 1
                df = get_klines(symbol, interval, lookback)
                
                # Get last position entry price
                trades = client.get_account_trades(symbol=symbol)
                entry_price = float(trades[-1]['price'])
                
                # Set stop loss price
                stop_loss_price = set_stop_loss(symbol, entry_price)
                
                client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=0.1, stopPrice=stop_loss_price)
                
                print("Long order executed!")
    
            if signals == 'sell':
                lookback = 1
                df = get_klines(symbol, interval, lookback)
                
                # Get last position entry price
                trades = client.get_account_trades(symbol=symbol)
                entry_price = float(trades[-1]['price'])
                
                # Set stop loss price
                stop_loss_price = set_stop_loss(symbol, entry_price)
                
                client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=0.1, stopPrice=stop_loss_price)
                
                print("Short order executed!")
        
        time.sleep(1)
    

    Now, if the mark price is higher than 0.2% above the entry price, the code will set the stop loss price to the open price of the previous candle.

  5. User

    So if we have an short position and entry price is 200 and mark price is 199.1 where your code will place stop loss ?

  6. Assistant

    In the updated code, if you have a short position with an entry price of 200 and the mark price is 199.1, the code will set the stop loss price to the open price of the previous candle.

    Since the mark price is lower than the entry price * (1 - 0.002), the code will not adjust the stop loss based on the mark price in this scenario. Instead, it will use the open price of the previous candle as the stop loss price.

    Please note that this implementation assumes the open price of the previous candle is a suitable level for setting the stop loss. You can modify this logic as per your specific requirements.

  7. User

    But stop_loss in undefined

  8. Assistant

    Apologies for the oversight. To address the issue and ensure the stop loss is defined even if the mark price is lower than the entry price, you can update the set_stop_loss function as follows:

    def set_stop_loss(symbol, entry_price, mark_price):
        lookback = 1
        df = get_klines(symbol, interval, lookback)
        
        # Get the open price of the previous candle
        prev_open = float(df.iloc[-2]['open'])
        
        # Set stop loss price at the open price of the previous candle
        stop_loss_price = prev_open
        
        if mark_price > entry_price * (1 - 0.002):
            stop_loss_price = entry_price * (1 - 0.002)
        
        return stop_loss_price
    

    By passing the mark price as an argument to the set_stop_loss function, it will consider both the mark price and the entry price when determining the stop loss price. If the mark price is greater than the entry price * (1 - 0.002), it will use the entry price * (1 - 0.002) as the stop loss price. Otherwise, it will default to the open price of the previous candle.

    So, with a short position and an entry price of 200 and a mark price of 199.1, the updated code will set the stop loss price at 199.2, which is 0.2% below the entry price.

  9. User

    But I need it in my While True code

  10. Assistant

    Apologies for the confusion. Here's the modified code that incorporates the updated set_stop_loss function within your while True loop:

    import datetime as dt
    import time
    
    def set_stop_loss(symbol, entry_price, mark_price):
        lookback = 1
        df = get_klines(symbol, interval, lookback)
        
        # Get the open price of the previous candle
        prev_open = float(df.iloc[-2]['open'])
        
        # Set stop loss price
        stop_loss_price = prev_open
        
        if mark_price > entry_price * (1 - 0.002):
            stop_loss_price = entry_price * (1 - 0.002)
        
        return stop_loss_price
    
    while True:
        lookback = 10080
        df = get_klines(symbol, interval, lookback)
    
        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':
                lookback = 1
                df = get_klines(symbol, interval, lookback)
                
                # Get last position entry price
                trades = client.get_account_trades(symbol=symbol)
                entry_price = float(trades[-1]['price'])
                
                # Get the mark price
                mark_price = get_mark_price(symbol)
                
                # Set stop loss price
                stop_loss_price = set_stop_loss(symbol, entry_price, mark_price)
                
                client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=0.1, stopPrice=stop_loss_price)
                
                print("Long order executed!")
    
            if signals == 'sell':
                lookback = 1
                df = get_klines(symbol, interval, lookback)
                
                # Get last position entry price
                trades = client.get_account_trades(symbol=symbol)
                entry_price = float(trades[-1]['price'])
                
                # Get the mark price
                mark_price = get_mark_price(symbol)
                
                # Set stop loss price
                stop_loss_price = set_stop_loss(symbol, entry_price, mark_price)
                
                client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=0.1, stopPrice=stop_loss_price)
                
                print("Short order executed!")
        
        time.sleep(1)
    

    In this version, the set_stop_loss function is called within the while True loop, where both the entry price and the mark price are passed as arguments. The function then determines the stop loss price based on these values, using the open price of the previous candle if the mark price is lower than the entry price * (1 - 0.002).