I used this code: lookback = 10080 quantity = 0.05 activesignal = None buyentryprice =…
- User
I used this code: lookback = 10080 quantity = 0.05 active_signal = None buy_entry_price = None sell_entry_price = None
def calculate_percentage_difference(entry_price, exit_price): percentage_difference = ((exit_price - entry_price) / entry_price) * 100 return percentage_difference
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 active_signal != 'buy': try: active_signal = 'buy' buy_entry_price = mark_price # Record the Buy entry price print(f"Buy Entry Price: {buy_entry_price}") # Execute Buy orders here 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: ") if buy_entry_price is not None and sell_entry_price is not None: buy_exit_price = sell_entry_price difference_buy = calculate_percentage_difference(buy_entry_price, buy_exit_price) profit_buy = difference_buy * 50 total_profit_buy = profit_buy - (0.04 * profit_buy) print(f"Buy Profit: {total_profit_buy} %, buy entry: {buy_entry_price}, sell entry {sell_entry_price}") else: print("Buy Entry price or Sell Entry price is not defined.") elif 'sell' in signals and active_signal != 'sell': try: active_signal = 'sell' sell_entry_price = mark_price # Record the sell entry price print(f"Sell Entry Price: {sell_entry_price}") # Execute sell orders here 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: ") if sell_entry_price is not None and buy_entry_price is not None: sell_exit_price = buy_entry_price difference_sell = calculate_percentage_difference(sell_entry_price, sell_exit_price) profit_sell = difference_sell * 50 total_profit_sell = profit_sell - (0.04 * profit_sell) print(f"Sell Profit: {total_profit_sell} %, sell entry price {sell_entry_price} buy entry price {buy_entry_price}") else: print("Sell Entry price or Buy Entry price is not defined.") time.sleep(1)But it returning me wrong info, just look at Sell or buy profit and entry prices: The signal time is: 2023-09-18 19:30:20, Price 220.46 - Signals: ['sell'] Sell Entry Price: 220.46 Error executing short order: Sell Profit: -3.0481720039919757 %, sell entry price 220.46 buy entry price 220.32 The signal time is: 2023-09-18 19:30:22, Price 220.42 - Signals: [''] The signal time is: 2023-09-18 19:30:24, Price 220.46 - Signals: [''] The signal time is: 2023-09-18 19:30:26, Price 220.47 - Signals: ['buy'] Buy Entry Price: 220.47 Error executing long order: Buy Profit: -0.2177166961489379 %, buy entry: 220.47, sell entry 220.46
- Assistant
There seem to be a couple of issues with your code:
- The
signalsvariable is only updated inside theif df is not Nonecondition. If the condition is not met, thesignalsvariable will not be updated and will remain asNone. This is why you see an empty list for the signals in the second and third print statements. To fix this, you can initialize thesignalsvariable outside theif df is not Nonecondition, like this:
signals = [] 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}")- The
active_signalvariable is never reset back toNoneafter executing a buy or sell order. This causes the conditionsactive_signal != 'buy'andactive_signal != 'sell'to always evaluate toFalseafter the first trade. To fix this, you can reset theactive_signalvariable toNoneafter executing a buy or sell order, like this:
if 'buy' in signals and active_signal != 'buy': # code try: # code except binance.error.ClientError as e: # code # code active_signal = None elif 'sell' in signals and active_signal != 'sell': # code try: # code except binance.error.ClientError as e: # code # code active_signal = NoneBy making these changes, the
signalsvariable will always be initialized, and theactive_signalvariable will be properly reset after executing a trade. - The