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

I used your code: while True: lookback = 10080 quantity = 0.05 buyentryprice = 0.0…

  1. User

    I used your code: while True: lookback = 10080 quantity = 0.05 buy_entry_price = 0.0 sell_entry_price = 0.0 buy_exit_price = 0.0 sell_exit_price = 0.0 entry_price_set = False

    if df is not None:
        signals = signal_generator(df)
        mark_price_data = client.ticker_price(symbol=symbol)
        mark_price = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0
        print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}, Price {mark_price} - Signals: {signals}")
    
        
    if 'buy' in signals and sell_exit_price == sell_entry_price:
        try:
            if not entry_price_set:
                buy_entry_price = mark_price
                entry_price_set = True
                print(f"Buy Entry Price: {buy_entry_price}")
    
            client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
            client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
            print("Long order executed!")
        except binance.error.ClientError as e:
            print(f"Error executing long order: ")
    
    elif 'sell' in signals and buy_exit_price == buy_entry_price:
        try:
            if not entry_price_set:
                sell_entry_price = mark_price
                entry_price_set = True
                print(f"Sell Entry Price: {sell_entry_price}")
    
            client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
            client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
            print("Short order executed!")
        except binance.error.ClientError as e:
            print(f"Error executing long order: ")
    
    time.sleep(1)
    

    But it every time when my signal generator gving me same signals it changing entry price !!!You need to solve this problem

  2. Assistant

    To solve the problem of changing entry price every time the signal generator gives the same signals, you can modify your code as follows:

    1. Move the initialization of buy_entry_price and sell_entry_price outside the while loop. This will ensure that the entry prices are only set once and will not change with each loop iteration.

    2. Add a condition to check if the entry price has already been set before setting it again. This can be done using the entry_price_set variable.

    Here is the modified code:

    lookback = 10080
    quantity = 0.05
    buy_entry_price = 0.0
    sell_entry_price = 0.0
    buy_exit_price = 0.0
    sell_exit_price = 0.0
    entry_price_set = False
    
    while True:
        if df is not None:
            signals = signal_generator(df)
            mark_price_data = client.ticker_price(symbol=symbol)
            mark_price = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0
            print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}, Price {mark_price} - Signals: {signals}")
    
        if 'buy' in signals and sell_exit_price == sell_entry_price:
            try:
                if not entry_price_set:
                    buy_entry_price = mark_price
                    entry_price_set = True
                    print(f"Buy Entry Price: {buy_entry_price}")
    
                client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
                client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
                print("Long order executed!")
            except binance.error.ClientError as e:
                print(f"Error executing long order: ")
    
        elif 'sell' in signals and buy_exit_price == buy_entry_price:
            try:
                if not entry_price_set:
                    sell_entry_price = mark_price
                    entry_price_set = True
                    print(f"Sell Entry Price: {sell_entry_price}")
    
                client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
                client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
                print("Short order executed!")
            except binance.error.ClientError as e:
                print(f"Error executing short order: ")
    
        time.sleep(1)
    

    By moving the entry price initialization outside the while loop and using the entry_price_set variable to check if the entry price has already been set, the entry price will only be set once and won't change when the same signals are received.

  3. User

    But it too have this problem

  4. Assistant

    Apologies for the oversight. To prevent the entry price from changing even when the same signals are received, you can modify the code as follows:

    1. Check if the entry price has already been set before updating it. This can be done by adding an additional condition to the if statement for signal checks.

    2. Add a check for the currently open position before executing a new order. You can keep track of the open position by using a boolean variable such as position_open and update it accordingly.

    Here is the updated code:

    lookback = 10080
    quantity = 0.05
    buy_entry_price = 0.0
    sell_entry_price = 0.0
    buy_exit_price = 0.0
    sell_exit_price = 0.0
    entry_price_set = False
    position_open = False
    
    while True:
        if df is not None:
            signals = signal_generator(df)
            mark_price_data = client.ticker_price(symbol=symbol)
            mark_price = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0
            print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}, Price {mark_price} - Signals: {signals}")
    
        if 'buy' in signals:
            if not position_open:
                try:
                    if not entry_price_set:
                        buy_entry_price = mark_price
                        entry_price_set = True
                        print(f"Buy Entry Price: {buy_entry_price}")
    
                    client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
                    client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
                    print("Long order executed!")
    
                    position_open = True
                except binance.error.ClientError as e:
                    print(f"Error executing long order: ")
    
        elif 'sell' in signals:
            if not position_open:
                try:
                    if not entry_price_set:
                        sell_entry_price = mark_price
                        entry_price_set = True
                        print(f"Sell Entry Price: {sell_entry_price}")
    
                    client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
                    client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
                    print("Short order executed!")
    
                    position_open = True
                except binance.error.ClientError as e:
                    print(f"Error executing short order: ")
    
        time.sleep(1)
    

    By adding the position_open variable and checking its value before executing a new order, you can prevent the entry price from changing when the same signals are received. This ensures that a new order is only executed if no position is currently open.

  5. User

    I think problem entry_price_set

  6. Assistant

    Apologies for the confusion. To solve the issue with entry_price_set variable, you can modify the code as follows:

    1. Move the initialization of entry_price_set inside the if df is not None block, before checking the signals. This will reset the entry_price_set flag every time new signals are generated.

    2. Change the conditions for setting the entry prices to buy_entry_price == 0.0 and sell_entry_price == 0.0 respectively, instead of comparing them to each other. This ensures that the entry prices are only set once when they are initially 0.0.

    Here is the updated code:

    lookback = 10080
    quantity = 0.05
    buy_entry_price = 0.0
    sell_entry_price = 0.0
    buy_exit_price = 0.0
    sell_exit_price = 0.0
    
    while True:
        entry_price_set = False
        
        if df is not None:
            signals = signal_generator(df)
            mark_price_data = client.ticker_price(symbol=symbol)
            mark_price = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0
            print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}, Price {mark_price} - Signals: {signals}")
    
            if 'buy' in signals and buy_entry_price == 0.0:
                try:
                    buy_entry_price = mark_price
                    print(f"Buy Entry Price: {buy_entry_price}")
    
                    client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
                    client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
                    print("Long order executed!")
                    
                    entry_price_set = True
                except binance.error.ClientError as e:
                    print(f"Error executing long order: ")
    
            elif 'sell' in signals and sell_entry_price == 0.0:
                try:
                    sell_entry_price = mark_price
                    print(f"Sell Entry Price: {sell_entry_price}")
    
                    client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
                    client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
                    print("Short order executed!")
    
                    entry_price_set = True
                except binance.error.ClientError as e:
                    print(f"Error executing short order: ")
    
        time.sleep(1)
    

    By resetting the entry_price_set flag every time new signals are generated and checking if the entry prices are still 0.0, you can ensure that the entry prices are only set once and won't change when the same signals are received.

  7. User

    Which algorithm using binance when I want to see my entry and exit price in trade history ?

  8. Assistant

    When monitoring your trade history on Binance, you can analyze the orders and their corresponding entry and exit prices by using the following algorithm:

    1. Fetch your trade history using the Binance API. The specific method for retrieving trade history might differ based on the programming library or wrapper you are using for the Binance API. However, typically, you would make a call to the /api/v3/myTrades endpoint, providing your API credentials and the trading pair symbol.

    2. Iterate through the trade history response, which typically returns a list of trades. For each trade, you can access the following information:

      • symbol: The trading pair symbol (e.g., "BTCUSDT").
      • orderId: The unique ID for the order.
      • price: The price at which the trade occurred.
      • qty: The quantity of the asset traded.
      • isBuyer: Whether you were the buyer in the trade or not.
    3. Determine the entry and exit prices based on the trades. For each order, consider the following:

      • If isBuyer is True, the trade represents a buy order. The trade's price is the entry price.
      • If isBuyer is False, the trade represents a sell order. The trade's price is the exit price.
    4. Group the trades by order ID. Calculate the average price for each order to consider the overall entry and exit prices. This helps when multiple trades are executed for a single order.

    By following this algorithm, you can analyze your trade history and determine the entry and exit prices for each trade.

  9. User

    I sued this code: while True: lookback = 10080 quantity = 0.05 buy_entry_price = 0.0 sell_entry_price = 0.0 buy_exit_price = 0.0 sell_exit_price = 0.0 entry_price_set = None

    if df is not None:
        signals = signal_generator(df)
        mark_price_data = client.ticker_price(symbol=symbol)
        mark_price = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0
        print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}, Price {mark_price} - Signals: {signals}")
    
        
    if 'buy' in signals and sell_exit_price == 0.0:
        try:
            if not entry_price_set:
                buy_entry_price = mark_price
                entry_price_set = True
                print(f"Buy Entry Price: {buy_entry_price}")
    
            client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
            client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
            print("Long order executed!")
        except binance.error.ClientError as e:
            print(f"Error executing long order: ")
    
    elif 'sell' in signals and buy_exit_price == 0.0:
        try:
            if not entry_price_set:
                sell_entry_price = mark_price
                entry_price_set = True
                print(f"Sell Entry Price: {sell_entry_price}")
    
            client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
            client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
            print("Short order executed!")
        except binance.error.ClientError as e:
            print(f"Error executing short order: ")
    
    time.sleep(1)
    

    Can you set entry_price_set not ot None , you need to set it to occupied or no, I mean if it giving more than one time signal to buy don't change entry price

  10. Assistant

    Certainly! You can modify the code by setting entry_price_set to either True or False to indicate whether the entry price has been set or not. By doing so, it will prevent the entry price from changing if it is already set.

    Here's the updated code:

    lookback = 10080
    quantity = 0.05
    buy_entry_price = 0.0
    sell_entry_price = 0.0
    buy_exit_price = 0.0
    sell_exit_price = 0.0
    entry_price_set = False  # Set to False initially
    
    while True:
        if df is not None:
            signals = signal_generator(df)
            mark_price_data = client.ticker_price(symbol=symbol)
            mark_price = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0
            print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}, Price {mark_price} - Signals: {signals}")
    
        if 'buy' in signals and sell_exit_price == 0.0:
            try:
                if not entry_price_set:
                    buy_entry_price = mark_price
                    entry_price_set = True
                    print(f"Buy Entry Price: {buy_entry_price}")
    
                client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
                client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
                print("Long order executed!")
            except binance.error.ClientError as e:
                print(f"Error executing long order: ")
    
        elif 'sell' in signals and buy_exit_price == 0.0:
            try:
                if not entry_price_set:
                    sell_entry_price = mark_price
                    entry_price_set = True
                    print(f"Sell Entry Price: {sell_entry_price}")
    
                client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
                client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
                print("Short order executed!")
            except binance.error.ClientError as e:
                print(f"Error executing short order: ")
    
        time.sleep(1)
    

    By setting entry_price_set to True after setting the entry price, it prevents the entry price from changing if the program receives multiple buy or sell signals consecutively. The entry_price_set flag acts as a barrier to ensure that the entry price is set only once until a new trade is executed.

  11. User

    No remove your system , and go by this algorithm: if buy signal is more than 1 signal don't change buy entry price

  12. Assistant

    Apologies for the confusion. To implement the algorithm where the buy entry price is not changed if there are multiple consecutive buy signals, you can modify the code as follows:

    lookback = 10080
    quantity = 0.05
    buy_entry_price = 0.0
    sell_entry_price = 0.0
    buy_exit_price = 0.0
    sell_exit_price = 0.0
    entry_price_set = False
    consecutive_buy_signals = 0
    
    while True:
        if df is not None:
            signals = signal_generator(df)
            mark_price_data = client.ticker_price(symbol=symbol)
            mark_price = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0
            print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}, Price {mark_price} - Signals: {signals}")
    
        if 'buy' in signals and sell_exit_price == 0.0:
            try:
                if not entry_price_set and consecutive_buy_signals == 0:
                    buy_entry_price = mark_price
                    entry_price_set = True
                    print(f"Buy Entry Price: {buy_entry_price}")
                    consecutive_buy_signals = 1
                elif entry_price_set:
                    consecutive_buy_signals += 1
    
                client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
                client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity)
                print("Long order executed!")
            except binance.error.ClientError as e:
                print(f"Error executing long order: ")
    
        elif 'sell' in signals and buy_exit_price == 0.0:
            try:
                if not entry_price_set:
                    sell_entry_price = mark_price
                    entry_price_set = True
                    print(f"Sell Entry Price: {sell_entry_price}")
    
                client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
                client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity)
                print("Short order executed!")
            except binance.error.ClientError as e:
                print(f"Error executing short order: ")
    
        time.sleep(1)
    

    In this updated code, I have introduced a new variable `consecutive_buy