internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: freqtrade/freqtrade

Issue:
wrong runmode value during dryrun
Hi,

using in dryrun

`print('current mode : {}'.format(self.dp.runmode.value))`

got this (i have cut other logs)

```
2020-02-26 09:48:38,425 - freqtrade.configuration.configuration - INFO - Dry run is enabled
current mode : live
```

i expected 

`current mode : dryrun`

Have a nice day.


Discussion:
where (which part) did you place this in? 

i've just tried in the strategy (both in `populate_buy()` and `populate_indicators()` - and i get dry_run assigned correctly every time.

also how (where) is dry_run set? command line or configuration? (not that it should matter)

Ok i have tested when in config file you have `"dry_run": false` and in the command line the `--dry-run` flag you should be able to reproduce this issue (no impact if it is in populate_buy() or populate_indicators()).



Resolving patch:
diff --git a/freqtrade/configuration/configuration.py b/freqtrade/configuration/configuration.py
--- a/freqtrade/configuration/configuration.py
+++ b/freqtrade/configuration/configuration.py
@@ -167,10 +167,6 @@ def _process_common_options(self, config: Dict[str, Any]) -> None:
         if 'sd_notify' in self.args and self.args["sd_notify"]:
             config['internals'].update({'sd_notify': True})
 
-        self._args_to_config(config, argname='dry_run',
-                             logstring='Parameter --dry-run detected, '
-                             'overriding dry_run to: {} ...')
-
     def _process_datadir_options(self, config: Dict[str, Any]) -> None:
         """
         Extract information for sys.argv and load directory configurations
@@ -376,6 +372,10 @@ def _process_plot_options(self, config: Dict[str, Any]) -> None:
 
     def _process_runmode(self, config: Dict[str, Any]) -> None:
 
+        self._args_to_config(config, argname='dry_run',
+                             logstring='Parameter --dry-run detected, '
+                             'overriding dry_run to: {} ...')
+
         if not self.runmode:
             # Handle real mode, infer dry/live from config
             self.runmode = RunMode.DRY_RUN if config.get('dry_run', True) else RunMode.LIVE


Test patch:
diff --git a/tests/test_configuration.py b/tests/test_configuration.py
--- a/tests/test_configuration.py
+++ b/tests/test_configuration.py
@@ -319,6 +319,7 @@ def test_load_dry_run(default_conf, mocker, config_value, expected, arglist) ->
     validated_conf = configuration.load_config()
 
     assert validated_conf['dry_run'] is expected
+    assert validated_conf['runmode'] == (RunMode.DRY_RUN if expected else RunMode.LIVE)
 
 
 def test_load_custom_strategy(default_conf, mocker) -> None:
Repository: freqtrade/freqtrade

Issue:
missing db_url for show_trades
Currently 
```
$ freqtrade show-trades
```
gives
```
...
2020-05-05 20:18:27,042 - freqtrade - ERROR - Fatal exception!
Traceback (most recent call last):
  File "/home/user/freqtrade-wrk/github-hroff-1902/freqtrade/freqtrade/main.py", line 36, in main
    return_code = args['func'](args)
  File "/home/user/freqtrade-wrk/github-hroff-1902/freqtrade/freqtrade/commands/list_commands.py", line 209, in start_show_trades
    init(config['db_url'], clean_open_orders=False)
KeyError: 'db_url'
```

Instead, a human-readable error message should be processed like for missing `--exchange` for `list-markets`:

```
freqtrade - ERROR - This command requires a configured exchange. You should either use `--exchange <exchange_name>` or specify a configuration file via `--config`.
```

-- similar should be printed for show_trades.



Discussion:

Resolving patch:
diff --git a/freqtrade/commands/list_commands.py b/freqtrade/commands/list_commands.py
--- a/freqtrade/commands/list_commands.py
+++ b/freqtrade/commands/list_commands.py
@@ -206,6 +206,11 @@ def start_show_trades(args: Dict[str, Any]) -> None:
     from freqtrade.persistence import init, Trade
     import json
     config = setup_utils_configuration(args, RunMode.UTIL_NO_EXCHANGE)
+
+    if 'db_url' not in config:
+        raise OperationalException("--db-url is required for this command.")
+
+    logger.info(f'Using DB: "{config["db_url"]}"')
     init(config['db_url'], clean_open_orders=False)
     tfilter = []
 


Test patch:
diff --git a/tests/commands/test_commands.py b/tests/commands/test_commands.py
--- a/tests/commands/test_commands.py
+++ b/tests/commands/test_commands.py
@@ -1077,3 +1077,11 @@ def test_show_trades(mocker, fee, capsys, caplog):
     assert '"trade_id": 1' in captured.out
     assert '"trade_id": 2' in captured.out
     assert '"trade_id": 3' not in captured.out
+    args = [
+        "show-trades",
+    ]
+    pargs = get_args(args)
+    pargs['config'] = None
+
+    with pytest.raises(OperationalException, match=r"--db-url is required for this command."):
+        start_show_trades(pargs)
Repository: freqtrade/freqtrade

Issue:
configure fee to 0 using --fee argument take default exchange fees instead of 0
Hi,

During early stage of developing a strategy i put fees to 0 to see winrate when i close the trade after 1 bar, 2 bars, ... .
No matter i 'force' the fees to 0 it seems the fallback to default exchange fees if the value choose is under the exchange fee.

Have a nice day.


Discussion:
--fee is only applicable to backtesting / hyperopt.

Also - it's not really meant to set to 0 - but to the "real" value the exchange is offering for you (assuming it's different than the default). 
I assume since 0.0 is "falsy", it's assuming it ain't set. try to set it to 0.00001 or something very low - and you'll see that this will fix it.

should be fixed if confirmed, imho

Resolving patch:
diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py
--- a/freqtrade/optimize/backtesting.py
+++ b/freqtrade/optimize/backtesting.py
@@ -101,7 +101,7 @@ def __init__(self, config: Dict[str, Any]) -> None:
         if len(self.pairlists.whitelist) == 0:
             raise OperationalException("No pair in whitelist.")
 
-        if config.get('fee'):
+        if config.get('fee', None) is not None:
             self.fee = config['fee']
         else:
             self.fee = self.exchange.get_fee(symbol=self.pairlists.whitelist[0])


Test patch:
diff --git a/tests/optimize/test_backtesting.py b/tests/optimize/test_backtesting.py
--- a/tests/optimize/test_backtesting.py
+++ b/tests/optimize/test_backtesting.py
@@ -308,6 +308,11 @@ def test_data_with_fee(default_conf, mocker, testdatadir) -> None:
     assert backtesting.fee == 0.1234
     assert fee_mock.call_count == 0
 
+    default_conf['fee'] = 0.0
+    backtesting = Backtesting(default_conf)
+    assert backtesting.fee == 0.0
+    assert fee_mock.call_count == 0
+
 
 def test_data_to_dataframe_bt(default_conf, mocker, testdatadir) -> None:
     patch_exchange(mocker)
Repository: freqtrade/freqtrade

Issue:
Hyperopt results design is broken

## Describe your environment

  * Operating system: Ubuntu
  * Python Version: 3.6.9 (`python -V`)
  * CCXT version: ccxt==1.34.59 (`pip freeze | grep ccxt`)
  * Freqtrade Version: freqtrade develop-d2111c08 (`freqtrade -V` or `docker-compose run --rm freqtrade -V` for Freqtrade running in docker)
  
## Describe the problem:

When I run hyperopt calculations results are not so beautiful like in the previous versions

### Steps to reproduce:

  1. Run freqtrade hyperopt -c ....
  2. Wait for results table
  
### Observed Results:

  * What happened?
Table is interrupted by " - freqtrade.misc - INFO - dumping json to "/home/sk/freqtrade/user_data/hyperopt_results/.last_result.json" messages

![fq-1](https://user-images.githubusercontent.com/2704050/94985538-78c09200-0579-11eb-84f5-8e4e4c4bd8f5.JPG)


  * What did you expect to happen?

I want results table to be unbreakable 




Discussion:

Resolving patch:
diff --git a/freqtrade/misc.py b/freqtrade/misc.py
--- a/freqtrade/misc.py
+++ b/freqtrade/misc.py
@@ -42,7 +42,7 @@ def datesarray_to_datetimearray(dates: np.ndarray) -> np.ndarray:
     return dates.dt.to_pydatetime()
 
 
-def file_dump_json(filename: Path, data: Any, is_zip: bool = False) -> None:
+def file_dump_json(filename: Path, data: Any, is_zip: bool = False, log: bool = True) -> None:
     """
     Dump JSON data into a file
     :param filename: file to create
@@ -53,12 +53,14 @@ def file_dump_json(filename: Path, data: Any, is_zip: bool = False) -> None:
     if is_zip:
         if filename.suffix != '.gz':
             filename = filename.with_suffix('.gz')
-        logger.info(f'dumping json to "{filename}"')
+        if log:
+            logger.info(f'dumping json to "{filename}"')
 
         with gzip.open(filename, 'w') as fp:
             rapidjson.dump(data, fp, default=str, number_mode=rapidjson.NM_NATIVE)
     else:
-        logger.info(f'dumping json to "{filename}"')
+        if log:
+            logger.info(f'dumping json to "{filename}"')
         with open(filename, 'w') as fp:
             rapidjson.dump(data, fp, default=str, number_mode=rapidjson.NM_NATIVE)
 
diff --git a/freqtrade/optimize/hyperopt.py b/freqtrade/optimize/hyperopt.py
--- a/freqtrade/optimize/hyperopt.py
+++ b/freqtrade/optimize/hyperopt.py
@@ -163,7 +163,8 @@ def _save_results(self) -> None:
                          f"saved to '{self.results_file}'.")
             # Store hyperopt filename
             latest_filename = Path.joinpath(self.results_file.parent, LAST_BT_RESULT_FN)
-            file_dump_json(latest_filename, {'latest_hyperopt': str(self.results_file.name)})
+            file_dump_json(latest_filename, {'latest_hyperopt': str(self.results_file.name)},
+                           log=False)
 
     @staticmethod
     def _read_results(results_file: Path) -> List:


Test patch:
Repository: freqtrade/freqtrade

Issue:
0 trades --dmmp [Backtesting-Hyperopt]
## Describe your environment

  * Operating system: Centos 7
  * Python Version: 3.8
  * CCXT version: 1.36.85 
  * Freqtrade Version: 2020.10
  

## Describe the problem:

When --dmmp option is enabled in backtesting or hyperopt, there are no trades.  But when you don't run with that option, it works.

-> In the previous version it worked correctly.


Discussion:

Resolving patch:
diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py
--- a/freqtrade/optimize/backtesting.py
+++ b/freqtrade/optimize/backtesting.py
@@ -340,7 +340,7 @@ def backtest(self, processed: Dict, stake_amount: float,
                 # max_open_trades must be respected
                 # don't open on the last row
                 if ((position_stacking or len(open_trades[pair]) == 0)
-                        and max_open_trades > 0 and open_trade_count_start < max_open_trades
+                        and (max_open_trades <= 0 or open_trade_count_start < max_open_trades)
                         and tmp != end_date
                         and row[BUY_IDX] == 1 and row[SELL_IDX] != 1):
                     # Enter trade


Test patch:
Repository: freqtrade/freqtrade

Issue:
StoplossGuard should include stoploss_on_exchange as well
The document is here: https://www.freqtrade.io/en/latest/configuration/#stoploss-guard

But it should include stoploss_on_exchange as well.


Discussion:

Resolving patch:
diff --git a/freqtrade/plugins/protections/stoploss_guard.py b/freqtrade/plugins/protections/stoploss_guard.py
--- a/freqtrade/plugins/protections/stoploss_guard.py
+++ b/freqtrade/plugins/protections/stoploss_guard.py
@@ -53,8 +53,9 @@ def _stoploss_guard(self, date_now: datetime, pair: str = None) -> ProtectionRet
         # trades = Trade.get_trades(filters).all()
 
         trades1 = Trade.get_trades_proxy(pair=pair, is_open=False, close_date=look_back_until)
-        trades = [trade for trade in trades1 if str(trade.sell_reason) == SellType.STOP_LOSS.value
-                  or (str(trade.sell_reason) == SellType.TRAILING_STOP_LOSS.value
+        trades = [trade for trade in trades1 if (str(trade.sell_reason) in (
+                    SellType.TRAILING_STOP_LOSS.value, SellType.STOP_LOSS.value,
+                    SellType.STOPLOSS_ON_EXCHANGE.value)
                       and trade.close_profit < 0)]
 
         if len(trades) > self._trade_limit:


Test patch:
Repository: freqtrade/freqtrade

Issue:
Add ping support to rest_client.py
## Describe your environment

  * Operating system: Linux
  * Python Version: 3.8.6
  * CCXT version:1.40.99
  * Freqtrade Version: 2021.1


## Describe the enhancement

Add `ping` support to the `rest_client.py` script.

I'm using the `rest_client.py` script to monitor my freqtrade bot using a custom shell+python script, and it looks like I cannot use the `ping` command to verify that freqtrade is running fine.

According to the [documentation](https://www.freqtrade.io/en/latest/rest-api/#available-endpoints), I should be able to use the `ping` command on the HTTP endpoint, but sadly it's not working with the `rest_client.py` script.

Current result :
```
root@3fda54e2f926:/freqtrade# python3 scripts/rest_client.py --config config_binance.json ping
2021-02-01 01:34:03,473 - ft_rest_client - ERROR - Command ping not defined
```

Expected result (when freqtrade is running) :
```
root@3fda54e2f926:/freqtrade# python3 scripts/rest_client.py --config config_binance.json ping
{'status': 'pong'}
# or
{'pong': 'ok'}
# or
{'response': 'pong'}
```

Expected result (when freqtrade is NOT running) :
```
root@3fda54e2f926:/freqtrade# python3 scripts/rest_client.py --config config_binance.json ping
{'status': 'not_running'}
# or
{'pong': 'ko'}
# or
{'response': 'not_running'}
```
Thanks :)






Discussion:
I think this was left out intentionally, as ping is kindof a pointless command for the rest-client - which is supposed to control the bot and issue authenticated commands, while "ping" is not.
It'll only give you a "pong" back, no information about the state of the bot (nor shold it do that - as that's not the purpose of this command).

the show_config endpoint will give you the state (running / stopped) - so if you need this information, please use this endpoint.

Feel free to contribute this if you really need it - it should be no more than a few lines.

I've also labeled it good first issue, which means i'll not be working on this and it'll be left to new contributors - so please either try to contribute this yourself or be patient as it can take a while until it's implemented.

Resolving patch:
diff --git a/scripts/rest_client.py b/scripts/rest_client.py
--- a/scripts/rest_client.py
+++ b/scripts/rest_client.py
@@ -174,6 +174,16 @@ def show_config(self):
         """
         return self._get("show_config")
 
+    def ping(self):
+        """simple ping"""
+        configstatus = self.show_config()
+        if not configstatus:
+            return {"status": "not_running"}
+        elif configstatus['state'] == "running":
+            return {"status": "pong"}
+        else:
+            return {"status": "not_running"}
+
     def logs(self, limit=None):
         """Show latest logs.
 


Test patch:
Repository: freqtrade/freqtrade

Issue:
current_profit in custom_sell and custom_stoploss does not align with backtest sell value
Since we discussed this in Discord I am skipping the usual bits of information and just opening this for tracking purposes.

Currently the "current_rate" and "current_profit" parameters fed into the custom_sell and custom_stoploss methods uses the current candle "high", but if a sell is triggered based on this profit value, it gets recorded in the backtest at the value of the next candles "open" price.

This causes a discrepancy between what the strategy thinks it is doing and what backtest / hyperopt are reporting.

I do not necessarily know a better/best way to do this but I we both seem to agree that the sell rate which is recorded should match the current_rate value at the time the sale is triggered.



Discussion:
I think not get from "open", but get data from previus candle close.
Sample from backtesting. Real profit calculating from next candle. (in sell_reason i save profit from custom_sell and value is 0.01502759) 
 {
                    "pair": "LUNA/USDT",
                    "stake_amount": 150,
                    "amount": 8.73362445,
                    "open_date": "2021-05-02 11:10:00+00:00",
                    "close_date": "2021-05-02 11:50:00+00:00",
                    "open_rate": 17.175,
                    "close_rate": 17.278,
                    "fee_open": 0.001,
                    "fee_close": 0.001,
                    "trade_duration": 40,
                    "profit_ratio": 0.0039871,
                    "profit_abs": 0.59866376,
                    "sell_reason": "profit_010.01502759",
                    "initial_stop_loss_abs": 0.17175000000000015,
                    "initial_stop_loss_ratio": -0.99,
                    "stop_loss_abs": 0.17175000000000015,
                    "stop_loss_ratio": -0.99,
                    "min_rate": 17.175,
                    "max_rate": 17.468,
                    "is_open": false,
                    "open_timestamp": 1619953800000.0,
                    "close_timestamp": 1619956200000.0
                }

Resolving patch:
diff --git a/freqtrade/strategy/interface.py b/freqtrade/strategy/interface.py
--- a/freqtrade/strategy/interface.py
+++ b/freqtrade/strategy/interface.py
@@ -573,6 +573,10 @@ def should_sell(self, trade: Trade, rate: float, date: datetime, buy: bool,
 
         sell_signal = SellType.NONE
         custom_reason = ''
+        # use provided rate in backtesting, not high/low.
+        current_rate = rate
+        current_profit = trade.calc_profit_ratio(current_rate)
+
         if (ask_strategy.get('sell_profit_only', False)
                 and current_profit <= ask_strategy.get('sell_profit_offset', 0)):
             # sell_profit_only and profit doesn't reach the offset - ignore sell signal


Test patch:
Repository: freqtrade/freqtrade

Issue:
Running asyncio.get_event_loop() in some thread other than the main thread – however, asyncio only generates an event loop for the main thread.
OS: Ubuntu 21.04
Branch: develop
Command: freqtrade trade --config config.json --strategy SampleStrategy
....
Error Message: 
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/fredtrade/develop/freqtrade/freqtrade/rpc/api_server/uvicorn_threaded.py", line 36, in run
    loop = asyncio.get_event_loop()
  File "/usr/lib/python3.9/asyncio/events.py", line 642, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-1'.

Solution: 
File - ./freqtrade/rpc/api_server/uvicorn_threaded.py
Line - 36

Change:
```
        loop = asyncio.get_event_loop()
```
To:
```
        try:
            loop = asyncio.get_event_loop()
        except RuntimeError as ex:
            if "There is no current event loop in thread" in str(ex):
                asyncio.set_event_loop(asyncio.new_event_loop())
                loop = asyncio.get_event_loop()                
```        


Discussion:

Resolving patch:
diff --git a/freqtrade/rpc/api_server/uvicorn_threaded.py b/freqtrade/rpc/api_server/uvicorn_threaded.py
--- a/freqtrade/rpc/api_server/uvicorn_threaded.py
+++ b/freqtrade/rpc/api_server/uvicorn_threaded.py
@@ -32,8 +32,11 @@ def run(self, sockets=None):
             asyncio_setup()
         else:
             asyncio.set_event_loop(uvloop.new_event_loop())
-
-        loop = asyncio.get_event_loop()
+        try:
+            loop = asyncio.get_event_loop()
+        except RuntimeError:
+            # When running in a thread, we'll not have an eventloop yet.
+            loop = asyncio.new_event_loop()
         loop.run_until_complete(self.serve(sockets=sockets))
 
     @contextlib.contextmanager


Test patch:
Repository: freqtrade/freqtrade

Issue:
error Mysql and RPC
Hello;

 * Operating system: ____Raspian
  * Python Version: _____ 3.7.3
  * CCXT version: _____ 1.55.28
  * Freqtrade Version: ____ 2021.8
  External MYSQL
  freqtrade trade -c config.json -s Strategy001 --db-url mysql+pymysql://user:password@192.168.1.100:3306/fbot


At first I thought the problem was freqUI, but I tried the frogtrade9000 script and the same thing happens.

When I use freqtrade with mysql database, the rpc does not work correctly, it does not update data to freqUI, the bot seems to work correctly, but not the transfer of information through the RPC.

I have not caught any errors in mysql either.

The failure occurs in both LIVE and DRY-RUN.

If I exit the bot and initialize it again, the data is updated with FreqUI. But I do not think that the error is due to FreqUI, because it does not work for me in other applications that retrieve the data by RPC




Discussion:
is thee "anything" in the logs (anything with warning or higher) that might point to an issue?
otherwise this will be very difficult to investigate.

Also, what about telegram, does telegram report correctly?
Hi;
Looking in telegram, dont work too. All RPC is out when use mysql., bot work fine, don´t report errors, mysql work fine too and save data, but don´t update trades and in few time dont update candles too in RPC systems (FreqUI, Frogtrade9000, Telegram)
i think i'm onto it ... but a fix might need "a bit" more time :laughing: 


Resolving patch:
diff --git a/freqtrade/rpc/api_server/deps.py b/freqtrade/rpc/api_server/deps.py
--- a/freqtrade/rpc/api_server/deps.py
+++ b/freqtrade/rpc/api_server/deps.py
@@ -1,5 +1,6 @@
 from typing import Any, Dict, Optional
 
+from freqtrade.persistence import Trade
 from freqtrade.rpc.rpc import RPC, RPCException
 
 from .webserver import ApiServer
@@ -14,6 +15,7 @@ def get_rpc_optional() -> Optional[RPC]:
 def get_rpc() -> Optional[RPC]:
     _rpc = get_rpc_optional()
     if _rpc:
+        Trade.query.session.rollback()
         return _rpc
     else:
         raise RPCException('Bot is not in the correct state')
diff --git a/freqtrade/rpc/telegram.py b/freqtrade/rpc/telegram.py
--- a/freqtrade/rpc/telegram.py
+++ b/freqtrade/rpc/telegram.py
@@ -25,6 +25,7 @@
 from freqtrade.enums import RPCMessageType
 from freqtrade.exceptions import OperationalException
 from freqtrade.misc import chunks, plural, round_coin_value
+from freqtrade.persistence import Trade
 from freqtrade.rpc import RPC, RPCException, RPCHandler
 
 
@@ -59,7 +60,8 @@ def wrapper(self, *args, **kwargs):
                 update.message.chat_id
             )
             return wrapper
-
+        # Rollback session to avoid getting data stored in a transaction.
+        Trade.query.session.rollback()
         logger.debug(
             'Executing handler: %s for chat_id: %s',
             command_handler.__name__,


Test patch: