I used your code: while True: lookback = 10080 quantity = 0.05 buyentryprice = 0.0…
- 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
- 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:
-
Move the initialization of
buy_entry_priceandsell_entry_priceoutside thewhileloop. This will ensure that the entry prices are only set once and will not change with each loop iteration. -
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_setvariable.
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
whileloop and using theentry_price_setvariable 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. -
- User
But it too have this problem
- 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:
-
Check if the entry price has already been set before updating it. This can be done by adding an additional condition to the
ifstatement for signal checks. -
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_openand 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_openvariable 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. -
- User
I think problem
entry_price_set - Assistant
Apologies for the confusion. To solve the issue with
entry_price_setvariable, you can modify the code as follows:-
Move the initialization of
entry_price_setinside theif df is not Noneblock, before checking the signals. This will reset theentry_price_setflag every time new signals are generated. -
Change the conditions for setting the entry prices to
buy_entry_price == 0.0andsell_entry_price == 0.0respectively, 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_setflag 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. -
- User
Which algorithm using binance when I want to see my entry and exit price in trade history ?
- 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:
-
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/myTradesendpoint, providing your API credentials and the trading pair symbol. -
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.
-
Determine the entry and exit prices based on the trades. For each order, consider the following:
- If
isBuyerisTrue, the trade represents a buy order. The trade's price is the entry price. - If
isBuyerisFalse, the trade represents a sell order. The trade's price is the exit price.
- If
-
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.
-
- 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_setnot 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 - Assistant
Certainly! You can modify the code by setting
entry_price_setto eitherTrueorFalseto 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_settoTrueafter setting the entry price, it prevents the entry price from changing if the program receives multiple buy or sell signals consecutively. Theentry_price_setflag acts as a barrier to ensure that the entry price is set only once until a new trade is executed. - User
No remove your system , and go by this algorithm: if buy signal is more than 1 signal don't change buy entry price
- 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