freqtrade / freqtrade

Free, open source crypto trading bot
https://www.freqtrade.io
GNU General Public License v3.0
28.32k stars 6.07k forks source link

Leverage problem after pulling new docker image #9516

Closed aykutdemirel closed 9 months ago

aykutdemirel commented 10 months ago

Environment

Note: All issues other than enhancement requests will be closed without further comment if the above template is deleted or not filled out.

The problem:

After I pulled the new docker image my leverage got stuck as 1.

Steps to reproduce:

  1. Use one of the older stable docker image versions which was published before July.
  2. Docker system prune -a (clear all images)
  3. Change database name to clean start (sqlite database)
  4. Execute with docker compose up -d
  5. Set leverage as more than 1
  6. Make sure that the dry run is false

Observed Results:

Relevant code exceptions or logs

2023-12-03 22:56:20,017 - freqtrade.freqtradebot - INFO - Short signal found: about create a new trade for LTC/USDT:USDT with stake_amount: 29.19727087398 ...
2023-12-03 22:56:20,017 - ccxt.base.exchange - DEBUG - POST https://fapi.binance.com/fapi/v1/marginType, Request: {'X-MBX-APIKEY': 'e2lgMzWIYRL57CmoB4ZJhXNMpycSJ6FmfQOJvJvglYvlftHusFUJF6mMsrCwCkvM', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'python-requests/2.31.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'} timestamp=1701644180017&symbol=LTCUSDT&marginType=ISOLATED&recvWindow=10000&signature=my_signature
2023-12-03 22:56:20,247 - urllib3.connectionpool - DEBUG - https://fapi.binance.com:443 "POST /fapi/v1/marginType HTTP/1.1" 400 53
2023-12-03 22:56:20,247 - ccxt.base.exchange - DEBUG - POST https://fapi.binance.com/fapi/v1/marginType, Response: 400 {'Date': 'Sun, 03 Dec 2023 22:56:20 GMT', 'Content-Type': 'application/json', 'Content-Length': '53', 'Connection': 'keep-alive', 'Server': 'Tengine', 'x-mbx-used-weight-1m': '49', 'x-response-time': '5ms', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS'} {"code":-4046,"msg":"No need to change margin type."}
2023-12-03 22:56:20,247 - ccxt.base.exchange - DEBUG - POST https://fapi.binance.com/fapi/v1/leverage, Request: {'X-MBX-APIKEY': 'e2lgMzWIYRL57CmoB4ZJhXNMpycSJ6FmfQOJvJvglYvlftHusFUJF6mMsrCwCkvM', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'python-requests/2.31.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'} timestamp=1701644180247&symbol=LTCUSDT&leverage=1&recvWindow=10000&signature=my_signature
2023-12-03 22:56:20,477 - urllib3.connectionpool - DEBUG - https://fapi.binance.com:443 "POST /fapi/v1/leverage HTTP/1.1" 200 None
2023-12-03 22:56:20,477 - ccxt.base.exchange - DEBUG - POST https://fapi.binance.com/fapi/v1/leverage, Response: 200 {'Date': 'Sun, 03 Dec 2023 22:56:20 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Server': 'Tengine', 'Vary': 'Accept-Encoding', 'x-mbx-used-weight-1m': '50', 'x-response-time': '4ms', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', 'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Headers': '*,clienttype,Content-Type,lang,x-ui-request-trace,bnc-uuid', 'Content-Security-Policy': "default-src 'self'", 'cache-control': 'no-cache, no-store, must-revalidate', 'expires': '0', 'pragma': 'no-cache', 'strict-transport-security': 'max-age=31536000; includeSubdomains', 'x-content-type-options': 'nosniff', 'x-frame-options': 'SAMEORIGIN', 'x-xss-protection': '1; mode=block', 'Content-Encoding': 'gzip'} {"symbol":"LTCUSDT","leverage":1,"maxNotionalValue":"50000000"}
2023-12-03 22:56:20,478 - ccxt.base.exchange - DEBUG - POST https://fapi.binance.com/fapi/v1/order, Request: {'X-MBX-APIKEY': 'e2lgMzWIYRL57CmoB4ZJhXNMpycSJ6FmfQOJvJvglYvlftHusFUJF6mMsrCwCkvM', 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'python-requests/2.31.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'} timestamp=1701644180477&symbol=LTCUSDT&side=SELL&newClientOrderId=x-xcKtGhcubf76b86b526ccbe303d55e&newOrderRespType=RESULT&type=LIMIT&quantity=0.402&price=72.52&timeInForce=GTC&recvWindow=10000&signature=my_signature
2023-12-03 22:56:20,707 - urllib3.connectionpool - DEBUG - https://fapi.binance.com:443 "POST /fapi/v1/order HTTP/1.1" 200 None
2023-12-03 22:56:20,707 - ccxt.base.exchange - DEBUG - POST https://fapi.binance.com/fapi/v1/order, Response: 200 {'Date': 'Sun, 03 Dec 2023 22:56:20 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Server': 'Tengine', 'Vary': 'Accept-Encoding', 'x-mbx-used-weight-1m': '-1', 'x-mbx-order-count-10s': '1', 'x-mbx-order-count-1m': '1', 'x-response-time': '4ms', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', 'Access-Control-Allow-Credentials': 'true', 'Access-Control-Allow-Headers': '*,clienttype,Content-Type,lang,x-ui-request-trace,bnc-uuid', 'Content-Security-Policy': "default-src 'self'", 'cache-control': 'no-cache, no-store, must-revalidate', 'expires': '0', 'pragma': 'no-cache', 'strict-transport-security': 'max-age=31536000; includeSubdomains', 'x-content-type-options': 'nosniff', 'x-frame-options': 'SAMEORIGIN', 'x-xss-protection': '1; mode=block', 'Content-Encoding': 'gzip'} {"orderId":28133361393,"symbol":"LTCUSDT","status":"NEW","clientOrderId":"x-xcKtGhcubf76b86b526ccbe303d55e","price":"72.52","avgPrice":"0.00","origQty":"0.402","executedQty":"0.000","cumQty":"0.000","cumQuote":"0.00000","timeInForce":"GTC","type":"LIMIT","reduceOnly":false,"closePosition":false,"side":"SELL","positionSide":"BOTH","stopPrice":"0.00","workingType":"CONTRACT_PRICE","priceProtect":false,"origType":"LIMIT","priceMatch":"NONE","selfTradePreventionMode":"NONE","goodTillDate":0,"updateTime":1701644180594}
2023-12-03 22:56:20,708 - freqtrade.freqtradebot - INFO - Order 28133361393 was created for LTC/USDT:USDT and status is open.
2023-12-03 22:56:23,217 - freqtrade.persistence.trade_model - INFO - LIMIT_SELL has been fulfilled for Trade(id=65, pair=LTC/USDT:USDT, amount=0.40200000, is_short=True, leverage=1.0, open_rate=72.52000000, open_since=2023-12-03 22:56:20).
2023-12-03 22:56:23,217 - freqtrade.persistence.trade_model - DEBUG - LTC/USDT:USDT - Keeping current stoploss...
2023-12-03 22:56:23,218 - freqtrade.persistence.trade_model - DEBUG - LTC/USDT:USDT - Stoploss adjusted. current_price=72.52000000, open_rate=72.52000000, max_rate=72.52000000, initial_stop_loss=77.95000000, stop_loss=77.95000000. Trailing stoploss saved us: 0.00000000.

CONFIG

{
    "max_open_trades": 10,
    "stake_currency": "USDT",
    "stake_amount": "unlimited",
    "tradable_balance_ratio": 0.99,
    "fiat_display_currency": "USD",
    "dry_run": false,
    "dry_run_wallet": 10000,
    "cancel_open_orders_on_exit": false,
    "trading_mode": "futures",
    "margin_mode": "isolated",
    "unfilledtimeout": {
        "entry": 10,
        "exit": 10,
        "exit_timeout_count": 0,
        "unit": "minutes"
    },
    "entry_pricing": {
        "price_side": "same",
        "use_order_book": true,
        "order_book_top": 1,
        "price_last_balance": 0.0,
        "check_depth_of_market": {
            "enabled": false,
            "bids_to_ask_delta": 1
        }
    },
    "exit_pricing":{
        "price_side": "same",
        "use_order_book": true,
        "order_book_top": 1
    },
    "exchange": {
        "name": "binance",
        "sandbox": false,
        "key": "my_key",
        "secret": "my_secret",
        "ccxt_config": {
            "enableRateLimit": true,
            "options": { "defaultType": "future", "leverage": 7 }
        },
        "ccxt_async_config": {
            "enableRateLimit": true,
            "rateLimit": 200,
            "options": { "defaultType": "future", "leverage": 7 }
        },
        "pair_whitelist": [
            "SOL/USDT:USDT",
            "LTC/USDT:USDT",
            "NEAR/USDT:USDT",
            "AVAX/USDT:USDT",
            "ENJ/USDT:USDT",
            "MATIC/USDT:USDT",
            "DOT/USDT:USDT",
            "LINK/USDT:USDT",
            "ATOM/USDT:USDT",
            "DYDX/USDT:USDT"
        ],
        "pair_blacklist": [
        ]
    },
    "pairlists": [
        {
            "method": "StaticPairList",
            "number_assets": 20,
            "sort_key": "quoteVolume",
            "min_value": 0,
            "refresh_period": 1800
        }
    ],
    "telegram": {
        "enabled": true,
        "token": "my_telegram_token",
        "chat_id": "my_telegram_token_chat_id"
    },
    "api_server": {
        "enabled": false,
        "listen_ip_address": "0.0.0.0",
        "listen_port": 8080,
        "verbosity": "error",
        "enable_openapi": false,
        "jwt_secret_key": "my_jwt_token",
        "ws_token": "my_ws_token",
        "CORS_origins": ["my_server_ip_address"],
        "username": "username",
        "password": "pass"
    },
    "bot_name": "MyBot",
    "initial_state": "running",
    "force_entry_enable": false,
    "internals": {
        "process_throttle_secs": 5
    }
}

DOCKER-COMPOSE

version: '3'
services:
  myBot:
    image: freqtradeorg/freqtrade:stable
    restart: unless-stopped
    container_name: myBot
    volumes:
      - "./user_data:/freqtrade/user_data"
    ports:
      - "0.0.0.0:9092:9090"
    command: >
      trade
      --vv
      --logfile /freqtrade/user_data/logs/freqtrade.log
      --db-url sqlite:////freqtrade/user_data/tradesv3-db.sqlite
      --config /freqtrade/user_data/config.json
      --strategy MyStrategy

CODE

from freqtrade.strategy import IStrategy
from typing import Dict, List, Optional
from functools import reduce
from pandas import DataFrame

import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
import numpy # noqa
import datetime

class MyStrategy(IStrategy):

    INTERFACE_VERSION: int = 3
    minimal_roi = {
        "0":  100
    }

    stoploss = -0.075
    trailing_stop = True
    trailing_stop_positive = 0.02
    trailing_stop_positive_offset = 0.075
    trailing_only_offset_is_reached = True
    max_open_trades = 12
    # Optimal timeframe for the strategy
    timeframe = '1h'

    process_only_new_candles = True

    use_exit_signal = True
    exit_profit_only = True
    ignore_roi_if_entry_signal = False
    can_short = True

    position_adjustment_enable = True

    # Optional order type mapping
    order_types = {
        'entry': 'limit',
        'exit': 'limit',
        'stoploss': 'market',
        'stoploss_on_exchange': False
    }

    output_csv = []
    CONST_LOSS_IN_HOURS = 24
    CONST_WAIT_IN_HOURS = 36
    enterance_date = 0
    loss_date = 0
    last_candle = None

    def populate_indicators(self, dataframe, metadata) -> DataFrame:
        // something
        return dataframe

    def populate_entry_trend(self, dataframe, metadata) -> DataFrame:
        // something
        return dataframe

    def populate_exit_trend(self, dataframe, metadata) -> DataFrame:
        // something
        return dataframe

    def leverage(self, pair, current_time, current_rate,
                 proposed_leverage, max_leverage, entry_tag, side) -> float:
        return 3

    def adjust_trade_position(self, trade, current_time,
                              current_rate, current_profit,
                              min_stake, max_stake,
                              current_entry_rate, current_exit_rate,
                              current_entry_profit, current_exit_profit,
                              **kwargs) -> Optional[float]:
        // something
        return None

    def confirm_trade_exit(self, pair, trade, order_type, amount,
                           rate, time_in_force, exit_reason,
                           current_time, **kwargs) -> bool:
       // something
        return True

    def confirm_trade_entry(self, pair, order_type, amount, rate,
                            time_in_force, current_time, entry_tag,
                            side, **kwargs) -> bool:
        return True
xmatthias commented 10 months ago

this is the 3rd issue that you open about this topic, and the 3rd time you

This is the same exact information i requested in your other issue, https://github.com/freqtrade/freqtrade/issues/9495#issuecomment-1837882826, and https://github.com/freqtrade/freqtrade/issues/9495#issuecomment-1844517041

To quote from the above issues:

Please provide at least 20 lines (more is better, including startup logs would be best) up to the creation of the trade - so we can determine if other problems might cause this.

source

We asked for more logs - which were not provided. We still don't know the version you've been using when you encountered the problem (issue template!)

source

so let me repeat this request again for clarity:

please note: as this is the 3rd issue about the same topic where the information we're requesting is missing, I feel obliged to issue an official warning.

I expect your next comment to include this information. If it does not, i'll close this issue as a clear sign that you do not intend to receive help and are simply trying to waste our time.

Opening another issue about the same topic with the same information missing again will force me to block you from interacting with this organization, as that's just trolly behavior, without actually wanting to provide us with the information necessary to actually assist you.

xmatthias commented 9 months ago

unfortunately, from your description it's unclear if it's working in the older images (before july) - or in the newer images (after july).

To clarify this - can you please use the following to provide us with the precise versions (for both the version you think it's working,and the version it's not):

docker compose run --rm freqtrade -V (taken from the issue template).

As a side note: please submit this as a new comment below - that will give us a "ping" (notification) - whereas an edit to the original comment will not.

aykutdemirel commented 9 months ago

Hi, I really would like to provide logs and try out "docker compose run --rm freqtrade -V" part. Unfortunately, we changed our server just a few days ago and on the new server we are using the freqtradeorg/freqtrade:2023.7 version.

I can provide the logs in 2 weeks. You can keep this issue open, or please close it, I can re-create it when I tryout with the new version again. @xmatthias

xmatthias commented 9 months ago

I've just tested a binance futures position with the latest develop (2023.12-x) - and leverage is working just fine. I'll not do the reverse test with 2023.7, as that version is not something i'll even attempt to support (to properly invest our limited resources (speak, our free time) we do support the current stable, and unreleased develop versions only).

image

(using tiny sizes as i'm not actually going to stick to that position - i've just opened it to test for this issue.

image

your logs you indicate don't really show much other than "open a position at 1x". As mentioned before, using a wrong strategy (duplicate class names) can be a quite common cause for this.


What i can advise though: do not use outdated versions. Exchange API's update all the time, so does ccxt. Using a version that's 6 months old on something that's managing my money is not something i'd personally do.

I'll close this for now, as it's not something i'm able to even remotely recreate, however the issue won't be locked, and i'll get a notification if you respond below - so if you still encounter this with a supported version (2023.11 or 2023.12-dev) please let me know so we can have a look what's wrong in your environment (for now, i doubt it's a freqtrade issue - as it's working perfectly fine for me).

stash86 commented 9 months ago
  1. Make sure you move the right strategy as well
  2. Make sure no duplicate strategies with the same class name