text
stringlengths
1
93.6k
f"Changing SL for deal {deal_id}/{deal['pair']} on bot \"{bot_name}\"\n"
f"Changed SL from {deal['stop_loss_percentage']}% to {new_stoploss}% - current profit = {deal['actual_profit_percentage']}%",
True,
)
else:
if error and "msg" in error:
logger.error(
"Error occurred updating bot with new take profit values: %s"
% error["msg"]
)
else:
logger.error("Error occurred updating bot with new take profit values")
def trailing_stoploss(thebot):
"""Check deals from bot and compare SL against the database."""
deals_count = 0
deals = thebot["active_deals"]
logger.info(f"Activation: {activation_percentage}%")
logger.info(f"Set stoploss % value @ activation: {initial_stoploss_percentage}")
if deals:
for deal in deals:
deals_count += 1
sl_price = float(
(0 if deal["stop_loss_price"] is None else deal["stop_loss_price"])
)
if sl_price != 0:
deal_id = deal["id"]
actual_profit_percentage = float(deal["actual_profit_percentage"])
stoploss = float(deal["stop_loss_percentage"])
existing_deal = check_deal(cursor, deal_id)
last_profit_percentage = round(
actual_profit_percentage
if existing_deal is None
or existing_deal["last_stop_loss_percentage"] != stoploss
else float(existing_deal["last_profit_percentage"]),
2,
) # If user changes SL in 3C then we have to start again
logger.debug(f"Pair: {deal['pair']}")
logger.debug(f"Deal id: {deal_id}")
logger.debug(f"Bot Strategy: {thebot['strategy']}")
logger.debug(f"Actual Profit: {actual_profit_percentage}%")
logger.debug(f"Current Stoploss: {stoploss}")
logger.debug(f"Last Used Profit: {last_profit_percentage}%")
logger.debug(
"Tracking Position: {}".format(
"No" if existing_deal is None else "Yes"
)
)
if (
existing_deal is None
or actual_profit_percentage >= last_profit_percentage
) and actual_profit_percentage >= activation_percentage:
# If the deal has never been tracked before and an inital stoploss value has been configured
new_stoploss = round(
initial_stoploss_percentage
if existing_deal is None
and initial_stoploss_percentage is not None
else stoploss
+ (last_profit_percentage - actual_profit_percentage),
2,
)
if new_stoploss < stoploss:
update_deal(thebot, deal, new_stoploss)
if existing_deal is not None:
db.execute(
f"UPDATE deals SET last_profit_percentage = {actual_profit_percentage}, "
f"last_stop_loss_percentage = {new_stoploss} "
f"WHERE dealid = {deal_id}"
)
else:
db.execute(
f"INSERT INTO deals (dealid, last_profit_percentage, last_stop_loss_percentage) "
f"VALUES ({deal_id}, {actual_profit_percentage}, {new_stoploss})"
)
logger.info(
f"New deal found {deal_id}/{deal['pair']} on bot \"{thebot['name']}\""
)
logger.info(
f"Finished processing {deals_count} deals for bot \"{thebot['name']}\""
)
db.commit()
def init_tsl_db():
"""Create or open database to store bot and deals data."""
try: