• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

freqtrade / freqtrade / 6181253459

08 Sep 2023 06:04AM UTC coverage: 94.614% (+0.06%) from 94.556%
6181253459

push

github-actions

web-flow
Merge pull request #9159 from stash86/fix-adjust

remove old codes when we only can do partial entries

2 of 2 new or added lines in 1 file covered. (100.0%)

19114 of 20202 relevant lines covered (94.61%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.3
/freqtrade/rpc/api_server/api_background_tasks.py
1
import logging
1✔
2
from copy import deepcopy
1✔
3

4
from fastapi import APIRouter, BackgroundTasks, Depends
1✔
5
from fastapi.exceptions import HTTPException
1✔
6

7
from freqtrade.constants import Config
1✔
8
from freqtrade.enums import CandleType
1✔
9
from freqtrade.exceptions import OperationalException
1✔
10
from freqtrade.rpc.api_server.api_schemas import (BackgroundTaskStatus, BgJobStarted,
1✔
11
                                                  ExchangeModePayloadMixin, PairListsPayload,
12
                                                  PairListsResponse, WhitelistEvaluateResponse)
13
from freqtrade.rpc.api_server.deps import get_config, get_exchange
1✔
14
from freqtrade.rpc.api_server.webserver_bgwork import ApiBG
1✔
15

16

17
logger = logging.getLogger(__name__)
1✔
18

19
# Private API, protected by authentication and webserver_mode dependency
20
router = APIRouter()
1✔
21

22

23
@router.get('/background/{jobid}', response_model=BackgroundTaskStatus, tags=['webserver'])
1✔
24
def background_job(jobid: str):
1✔
25
    if not (job := ApiBG.jobs.get(jobid)):
1✔
26
        raise HTTPException(status_code=404, detail='Job not found.')
1✔
27

28
    return {
1✔
29
        'job_id': jobid,
30
        'job_category': job['category'],
31
        'status': job['status'],
32
        'running': job['is_running'],
33
        'progress': job.get('progress'),
34
        # 'job_error': job['error'],
35
    }
36

37

38
@router.get('/pairlists/available',
1✔
39
            response_model=PairListsResponse, tags=['pairlists', 'webserver'])
40
def list_pairlists(config=Depends(get_config)):
1✔
41
    from freqtrade.resolvers import PairListResolver
1✔
42
    pairlists = PairListResolver.search_all_objects(
1✔
43
        config, False)
44
    pairlists = sorted(pairlists, key=lambda x: x['name'])
1✔
45

46
    return {'pairlists': [{
1✔
47
        "name": x['name'],
48
        "is_pairlist_generator": x['class'].is_pairlist_generator,
49
        "params": x['class'].available_parameters(),
50
        "description": x['class'].description(),
51
         } for x in pairlists
52
    ]}
53

54

55
def __run_pairlist(job_id: str, config_loc: Config):
1✔
56
    try:
1✔
57

58
        ApiBG.jobs[job_id]['is_running'] = True
1✔
59
        from freqtrade.plugins.pairlistmanager import PairListManager
1✔
60

61
        exchange = get_exchange(config_loc)
1✔
62
        pairlists = PairListManager(exchange, config_loc)
1✔
63
        pairlists.refresh_pairlist()
1✔
64
        ApiBG.jobs[job_id]['result'] = {
1✔
65
                'method': pairlists.name_list,
66
                'length': len(pairlists.whitelist),
67
                'whitelist': pairlists.whitelist
68
            }
69
        ApiBG.jobs[job_id]['status'] = 'success'
1✔
70
    except (OperationalException, Exception) as e:
×
71
        logger.exception(e)
×
72
        ApiBG.jobs[job_id]['error'] = str(e)
×
73
        ApiBG.jobs[job_id]['status'] = 'failed'
×
74
    finally:
75
        ApiBG.jobs[job_id]['is_running'] = False
1✔
76
        ApiBG.pairlist_running = False
1✔
77

78

79
@router.post('/pairlists/evaluate', response_model=BgJobStarted, tags=['pairlists', 'webserver'])
1✔
80
def pairlists_evaluate(payload: PairListsPayload, background_tasks: BackgroundTasks,
1✔
81
                       config=Depends(get_config)):
82
    if ApiBG.pairlist_running:
1✔
83
        raise HTTPException(status_code=400, detail='Pairlist evaluation is already running.')
1✔
84

85
    config_loc = deepcopy(config)
1✔
86
    config_loc['stake_currency'] = payload.stake_currency
1✔
87
    config_loc['pairlists'] = payload.pairlists
1✔
88
    handleExchangePayload(payload, config_loc)
1✔
89
    # TODO: overwrite blacklist? make it optional and fall back to the one in config?
90
    # Outcome depends on the UI approach.
91
    config_loc['exchange']['pair_blacklist'] = payload.blacklist
1✔
92
    # Random job id
93
    job_id = ApiBG.get_job_id()
1✔
94

95
    ApiBG.jobs[job_id] = {
1✔
96
        'category': 'pairlist',
97
        'status': 'pending',
98
        'progress': None,
99
        'is_running': False,
100
        'result': {},
101
        'error': None,
102
    }
103
    background_tasks.add_task(__run_pairlist, job_id, config_loc)
1✔
104
    ApiBG.pairlist_running = True
1✔
105

106
    return {
1✔
107
        'status': 'Pairlist evaluation started in background.',
108
        'job_id': job_id,
109
    }
110

111

112
def handleExchangePayload(payload: ExchangeModePayloadMixin, config_loc: Config):
1✔
113
    """
114
    Handle exchange and trading mode payload.
115
    Updates the configuration with the payload values.
116
    """
117
    if payload.exchange:
1✔
118
        config_loc['exchange']['name'] = payload.exchange
1✔
119
    if payload.trading_mode:
1✔
120
        config_loc['trading_mode'] = payload.trading_mode
1✔
121
        config_loc['candle_type_def'] = CandleType.get_default(
1✔
122
            config_loc.get('trading_mode', 'spot') or 'spot')
123
    if payload.margin_mode:
1✔
124
        config_loc['margin_mode'] = payload.margin_mode
1✔
125

126

127
@router.get('/pairlists/evaluate/{jobid}', response_model=WhitelistEvaluateResponse,
1✔
128
            tags=['pairlists', 'webserver'])
129
def pairlists_evaluate_get(jobid: str):
1✔
130
    if not (job := ApiBG.jobs.get(jobid)):
1✔
131
        raise HTTPException(status_code=404, detail='Job not found.')
1✔
132

133
    if job['is_running']:
1✔
134
        raise HTTPException(status_code=400, detail='Job not finished yet.')
×
135

136
    if error := job['error']:
1✔
137
        return {
×
138
            'status': 'failed',
139
            'error': error,
140
        }
141

142
    return {
1✔
143
        'status': 'success',
144
        'result': job['result'],
145
    }
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc