I have this code: def setstoploss(symbol, entryprice, markprice): lookback = 1 df =…
- User
I have this code: def set_stop_loss(symbol, entry_price, mark_price): lookback = 1 df = get_klines(symbol, interval, lookback)
# Get the open price of the previous candle prev_open = float(df.iloc[-2]['Open']) # Set stop loss price stop_loss_price = prev_open if mark_price > entry_price * (1 - 0.002): stop_loss_price = entry_price * (1 - 0.002) return stop_loss_pricewhile True: lookback = 10080 df = get_klines(symbol, interval, lookback)
if df is not None: signals = signal_generator(df) print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Signals: {signals}") if signals == 'buy': try: client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=0.1) print("Long order executed!") except binance.error.ClientError as e: print(f"Error executing long order: {e}") time.sleep(1) if signals == 'sell': try: client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=0.1) print("Short order executed!") except binance.error.ClientError as e: print(f"Error executing short order: {e}") time.sleep(1) time.sleep(1)Can you set my algorithm in my code , algorithm: Get price by this code = mark_price = client.ticker_price(symbol=symbol), if the signal == buy or sell set stop loss - 10% how to set stop loss - 10% :(get mark price , mark price - 0.2% result = stop loss , example: mark price = 200 , 200-0.2% result = 199.96. 199.96 will be stop loss price )
- Assistant
To modify your code to include the algorithm you described, you can make the following changes:
-
Replace the line
mark_price = client.ticker_price(symbol=symbol)withmark_price = float(client.ticker_price(symbol=symbol)). This will ensure that the retrieved ticker price is converted to a float. -
Inside the
if signals == 'buy':block, after executing the buy order, add the following code:
stop_loss = mark_price - (mark_price * 0.002) stop_loss_price = mark_price - (mark_price * 0.002 * 0.1) print(f"Stop loss set at: {stop_loss_price}")This calculates the stop loss price based on the mark price, where the stop loss is set at 10% below the mark price.
- Inside the
if signals == 'sell':block, after executing the sell order, add the following code:
stop_loss = mark_price + (mark_price * 0.002) stop_loss_price = mark_price + (mark_price * 0.002 * 0.1) print(f"Stop loss set at: {stop_loss_price}")This calculates the stop loss price based on the mark price, where the stop loss is set at 10% above the mark price.
-
- User
Code how to place stop loss: client.new_order(symbol=symbol, side='SELL', type='STOP_MARKET', quantity=0.1)
- Assistant
To place a stop loss using the
client.new_orderfunction, you can modify the code as follows:Inside the
if signals == 'buy':block, after executing the buy order, add the following code:stop_loss = mark_price - (mark_price * 0.002) stop_loss_price = mark_price - (mark_price * 0.002 * 0.1) print(f"Stop loss set at: {stop_loss_price}") try: client.new_order( symbol=symbol, side='SELL', type='STOP_MARKET', quantity=0.1, stopPrice=stop_loss_price ) print("Stop loss order placed!") except binance.error.ClientError as e: print(f"Error placing stop loss order: {e}") time.sleep(1)Inside the
if signals == 'sell':block, after executing the sell order, add the following code:stop_loss = mark_price + (mark_price * 0.002) stop_loss_price = mark_price + (mark_price * 0.002 * 0.1) print(f"Stop loss set at: {stop_loss_price}") try: client.new_order( symbol=symbol, side='BUY', type='STOP_MARKET', quantity=0.1, stopPrice=stop_loss_price ) print("Stop loss order placed!") except binance.error.ClientError as e: print(f"Error placing stop loss order: {e}") time.sleep(1) - User
So give me full code with While true please
- Assistant
- User
Set mark_price to : mark_price = client.ticker_price(symbol=symbol)
- Assistant
- User
So if the price is 100 and signal == sell where will your code place stop loss ?
- Assistant
In the given code, the stop loss will be placed 10% above the mark price in the if condition "
if signals == 'sell':". So, if the mark price is 100 and the signal is 'sell', the stop loss will be calculated as follows:mark_price = 100 stop_loss_price = mark_price + (mark_price * 0.002 * 0.1)Calculating the stop loss price will give you:
stop_loss_price = 100 + (100 * 0.002 * 0.1) stop_loss_price = 100.02Therefore, the code will place the stop loss at 100.02 if the price is 100 and the signal is 'sell'.
- User
And if price is 100 and signal == buy where will be stop loss /
- Assistant
In the given code, the stop loss will be placed 10% below the mark price in the if condition "
if signals == 'buy':". So, if the mark price is 100 and the signal is 'buy', the stop loss will be calculated as follows:mark_price = 100 stop_loss_price = mark_price - (mark_price * 0.002 * 0.1)Calculating the stop loss price will give you:
stop_loss_price = 100 - (100 * 0.002 * 0.1) stop_loss_price = 99.98Therefore, the code will place the stop loss at 99.98 if the price is 100 and the signal is 'buy'.