I used your code: def setstoploss(symbol, entryprice, markprice): lookback = 1 df =…
- User
I used your 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': lookback = 1 df = get_klines(symbol, interval, lookback) # Get last position entry price trades = client.get_account_trades(symbol=symbol) entry_price = float(trades[-1]['price']) # Set stop loss price stop_loss_price = set_stop_loss(symbol, entry_price) client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=0.1, stopPrice=stop_loss_price) print("Long order executed!") if signals == 'sell': lookback = 1 df = get_klines(symbol, interval, lookback) # Get last position entry price trades = client.get_account_trades(symbol=symbol) entry_price = float(trades[-1]['price']) # Set stop loss price stop_loss_price = set_stop_loss(symbol, entry_price) client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=0.1, stopPrice=stop_loss_price) print("Short order executed!") time.sleep(1)But I getting ERROR: The signal time is: 2023-08-03 18:55:00 - Signals: sell No data found for the given timeframe and symbol Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 216, in stop_loss_price = set_stop_loss(symbol, entry_price) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: set_stop_loss() missing 1 required positional argument: 'mark_price'
- Assistant
It seems that you are missing the 'mark_price' argument when calling the 'set_stop_loss()' function. To fix this, make sure to pass the 'mark_price' argument when calling 'set_stop_loss()' in both the 'buy' and 'sell' conditions.
For example:
stop_loss_price = set_stop_loss(symbol, entry_price, mark_price)Make sure to replace 'mark_price' with the appropriate variable or value in each condition.
- User
But
mark_priceis defined - Assistant
Apologies for the oversight. It seems that 'mark_price' is defined in your code, it was just not included in the function call.
To fix the issue, modify your code where you call the 'set_stop_loss()' function to include the 'mark_price' argument.
For example:
stop_loss_price = set_stop_loss(symbol, entry_price, mark_price)Ensure that you replace 'mark_price' with the appropriate variable or value in both the 'buy' and 'sell' conditions.
- User
But wait it still defined
- Assistant
Apologies for the confusion. Upon reviewing your code again, it appears that you have a missing argument when calling the
set_stop_loss()function.The function definition for
set_stop_loss()isdef set_stop_loss(symbol, entry_price, mark_price):, and it expects three arguments:symbol,entry_price, andmark_price.However, when you call the
set_stop_loss()function, you are only passing two arguments:symbolandentry_price.To fix this issue, you need to ensure that you pass the
mark_priceargument when calling theset_stop_loss()function.For example:
stop_loss_price = set_stop_loss(symbol, entry_price, mark_price)Make sure that
mark_priceis defined and appropriately assigned a value before calling theset_stop_loss()function. - User
Waht I need to change ?
- Assistant
To fix the issue, you need to modify the code where you call the
set_stop_loss()function to pass all the required arguments:symbol,entry_price, andmark_price.In your code, you are calling the
set_stop_loss()function in two places:stop_loss_price = set_stop_loss(symbol, entry_price)You need to update these lines to include the
mark_priceargument:stop_loss_price = set_stop_loss(symbol, entry_price, mark_price)Make sure you have a defined value for the
mark_pricevariable before calling theset_stop_loss()function in each case (buyandsell). - User
I used it ERROR: The signal time is: 2023-08-03 19:23:01 - Signals: buy Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 201, in stop_loss_price = set_stop_loss(symbol, entry_price, mark_price) ^^^^^^^^^^ NameError: name 'mark_price' is not defined. Did you mean: 'entry_price'?
- Assistant
Apologies for the confusion. It seems that the variable
mark_priceis not defined in your code, which is causing theNameError.To fix this, you need to ensure that you have a defined value for
mark_pricebefore calling theset_stop_loss()function.If
mark_pricerepresents a variable that is available in your code, please make sure that it is assigned a value before calling theset_stop_loss()function.If you don't have a specific value for
mark_priceand you intended to use it as a placeholder, you can update your code to pass a placeholder value to theset_stop_loss()function.For example:
stop_loss_price = set_stop_loss(symbol, entry_price, 0)Replace
0with the appropriate value or placeholder depending on your requirements.Make sure to update the line for both the
buyandsellconditions. - User
Used your code, but I getting
- Assistant
Apologies for the confusion. Upon further review, it seems like you have not defined the
mark_pricevariable in your code, which is causing theNameError.To resolve this issue, you need to ensure that you have a
mark_pricevalue available before calling the function. - User
Code how to place stop loss order: client.new_order(symbol=symbol, side=‘SELL’ or BUY, type=‘STOP_MARKET’, quantity=0.1, stopPrice=stop_loss_price)
- Assistant
To place a stop loss order using the Binance API, you can use the
client.new_order()function with thetypeparameter set to'STOP_MARKET'.Here's an example of how you can modify your code to place a stop loss order:
# For a sell order client.new_order(symbol=symbol, side='SELL', type='STOP_MARKET', quantity=0.1, stopPrice=stop_loss_price) # For a buy order client.new_order(symbol=symbol, side='BUY', type='STOP_MARKET', quantity=0.1, stopPrice=stop_loss_price)Make sure to replace
'SELL'or'BUY'with the appropriate side depending on whether it is a sell or buy stop loss order.The
'type'parameter should be set to'STOP_MARKET'to place a stop loss order.The
'quantity'parameter specifies the quantity of the asset to trade, and you can adjust it as needed.Lastly, the
'stopPrice'parameter should be set tostop_loss_price, which is the price at which the stop loss order will be triggered.Ensure that you have the necessary permissions and available funds in your Binance account to place the stop loss order. Also, make any other adjustments needed according to your specific requirements.
- User
Set it in my code
- Assistant
Certainly! Based on your existing code, here's an example of how you can integrate the stop loss order placement using the
client.new_order()function:while True: lookback = 10080 df = get_klines(symbol, interval, lookback) if df is not None: signals = signal_generator(df) mark_price = float(df.iloc[-1]['close']) print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Signals: {signals}") if signals == 'buy': lookback = 1 df = get_klines(symbol, interval, lookback) # Get last position entry price trades = client.get_account_trades(symbol=symbol) entry_price = float(trades[-1]['price']) # Set stop loss price stop_loss_price = set_stop_loss(symbol, entry_price, mark_price) # Place stop loss order client.new_order(symbol=symbol, side='BUY', type='STOP_MARKET', quantity=0.1, stopPrice=stop_loss_price) print("Long order executed!") if signals == 'sell': lookback = 1 df = get_klines(symbol, interval, lookback) # Get last position entry price trades = client.get_account_trades(symbol=symbol) entry_price = float(trades[-1]['price']) # Set stop loss price stop_loss_price = set_stop_loss(symbol, entry_price, mark_price) # Place stop loss order client.new_order(symbol=symbol, side='SELL', type='STOP_MARKET', quantity=0.1, stopPrice=stop_loss_price) print("Short order executed!") time.sleep(1)