I used your code: lookback = 10080 quantity = 0.05 activesignal = None buyentryprice =…
- User
I used your 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!") if buy_entry_price is not None: buy_exit_price = mark_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}") else: print("Buy Entry price is not defined.") except binance.error.ClientError as e: print(f"Error executing long order: {e}") 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: {e}") 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}") else: print("Sell Entry price or Buy Entry price is not defined.") time.sleep(1)But it printing me only short position profit and loss ,
- Assistant
Based on the code you provided, it seems that the code is only calculating and printing the profit and loss for the short positions (
sellsignal), but not for the long positions (buysignal).To fix this issue, you can modify the code to calculate and print the profit and loss for both long and short positions. Here's an updated version of the code that includes the calculation and printing of profit and loss for both positions:
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!") if buy_entry_price is not None: buy_exit_price = mark_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}") else: print("Buy Entry price is not defined.") except binance.error.ClientError as e: print(f"Error executing long order: {e}") 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: {e}") 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}") difference_buy = calculate_percentage_difference(buy_entry_price, sell_exit_price) profit_buy = difference_buy * 50 total_profit_buy = profit_buy + (0.04 * profit_buy) print(f"Buy Profit: {total_profit_buy}") else: print("Sell Entry price or Buy Entry price is not defined.") time.sleep(1)With this updated code, it will calculate and print the profit and loss for both long and short positions based on the entry and exit prices.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.