I have this algorithm for count buy price difference between buy price and sell price…
- User
I have this algorithm for count buy price difference between buy price and sell price :def calculate_percentage_difference_buy(entry_price, exit_price): percentage_difference = ((exit_price - entry_price) / entry_price) * 100 return percentage_difference
But I need code for sell entry and buy exit counting besd on my algorithm:
entry_price = sell_entry_price exit_price = buy_entry_price
entry_price - exit_price = result
entry divide to 100 = price_result # price result = 1% after that divide entry to price_result = percent_difference
after that percent_difference multiply to 50
- Assistant
Here is the code that you can use for sell entry and buy exit counting based on your algorithm:
def calculate_percentage_difference_sell(sell_entry_price, buy_exit_price): percentage_difference = ((buy_exit_price - sell_entry_price) / sell_entry_price) * 100 price_result = 1 / 100 # price result = 1% percent_difference = percentage_difference / price_result result = percent_difference * 50 return result sell_entry_price = 100 buy_exit_price = 120 price_difference = calculate_percentage_difference_sell(sell_entry_price, buy_exit_price) print(price_difference)In the above code, we have created a new function
calculate_percentage_difference_sellthat takes insell_entry_priceandbuy_exit_priceas parameters. Within this function, we calculate the percentage difference using your algorithm. Then, we divide the entry price by 100 to getprice_resultas 1% and divide the percentage difference byprice_resultto getpercent_difference. Finally, we multiply by 50 to get the final result. - User
- Assistant
- User
So, in if buy signal, will it count difference and form this difference minus 4% of all profit ?
- Assistant
Yes, in the
if 'buy' in signals and active_signal != 'buy'condition, the code calculates the difference between thesell_entry_priceandsell_exit_priceusing thecalculate_percentage_difference_sellfunction. It then multiplies this difference by 50 to get theprofit_sell. Finally, it subtracts 4% of theprofit_sellfrom thetotal_profit_sellvariable.if sell_entry_price is not None and buy_entry_price is not None: sell_exit_price = mark_price difference_sell = calculate_percentage_difference_sell(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}, sell exit price {sell_exit_price}") else: print("Sell Entry price or Buy Entry price is not defined.")Here, the line
total_profit_sell = profit_sell - (0.04 * profit_sell)subtracts 4% (0.04) of theprofit_sellfrom thetotal_profit_sell. - User
Wait , how does working your code? Code: def calculate_percentage_difference_sell(sell_entry_price, sell_exit_price): percentage_difference = ((sell_exit_price - sell_entry_price) / sell_entry_price) * 100 price_result = 1 / 100 # price result = 1% percent_difference = percentage_difference / price_result result = percent_difference * 50
return resultwhile 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 sell_entry_price is not None and buy_entry_price is not None: sell_exit_price = mark_price difference_sell = calculate_percentage_difference_sell(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}, sell exit price {sell_exit_price}") else: print("Sell Entry price or Buy Entry price is not defined.") - Assistant
The code you provided calculates the percentage difference between
sell_entry_priceandsell_exit_priceusing thecalculate_percentage_difference_sellfunction. It then calculates theresultby multiplying thepercent_differenceby 50.In the main
while Trueloop, the code first checks ifdfis not None and generates thesignalsbased ondf. It then gets the market price (mark_price) and prints the current signal time, price, and signals.Next, if the 'buy' signal is present and
active_signalis not 'buy', it executes the following steps:- It sets
active_signalto 'buy' and records thebuy_entry_priceas the currentmark_price. - It executes the buy orders.
- If both
sell_entry_priceandbuy_entry_priceare not None, it sets thesell_exit_priceto the current . It calculates the (percentage difference) using , then multiplies it by 50 to get the .
- It sets