From 83b14523ca1a60b221229827ff66947616ef1d8f Mon Sep 17 00:00:00 2001 From: Janne Sinivirta Date: Thu, 7 Sep 2017 17:31:55 +0300 Subject: [PATCH 1/5] add stoploss to example config and json schema validation --- config.json.example | 1 + utils.py | 1 + 2 files changed, 2 insertions(+) diff --git a/config.json.example b/config.json.example index 29eb75075..a9fc3be79 100644 --- a/config.json.example +++ b/config.json.example @@ -8,6 +8,7 @@ "720": 0.01, "0": 0.02 }, + "stoploss": -0.10, "poloniex": { "enabled": false, "key": "key", diff --git a/utils.py b/utils.py index e7bb006fd..0bde3fce5 100644 --- a/utils.py +++ b/utils.py @@ -24,6 +24,7 @@ _conf_schema = { }, 'minProperties': 1 }, + 'stoploss': {'type': 'number', 'maximum': 0, 'exclusiveMaximum': True}, 'poloniex': {'$ref': '#/definitions/exchange'}, 'bittrex': {'$ref': '#/definitions/exchange'}, 'telegram': { From bd161ea0a9167b89be814cc46e0206fc4687b4e9 Mon Sep 17 00:00:00 2001 From: Janne Sinivirta Date: Thu, 7 Sep 2017 17:33:04 +0300 Subject: [PATCH 2/5] extract selling to a method --- main.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index a97419c94..881b51f83 100755 --- a/main.py +++ b/main.py @@ -138,6 +138,22 @@ def close_trade_if_fulfilled(trade: Trade) -> bool: return True return False +def execute_sell(trade: Trade, current_rate: float) -> None: + # Get available balance + currency = trade.pair.split('_')[1] + balance = api_wrapper.get_balance(currency) + + profit = trade.exec_sell_order(current_rate, balance) + message = '*{}:* Selling [{}]({}) at rate `{:f} (profit: {}%)`'.format( + trade.exchange.name, + trade.pair.replace('_', '/'), + api_wrapper.get_pair_detail_url(trade.pair), + trade.close_rate, + round(profit, 2) + ) + logger.info(message) + TelegramHandler.send_msg(message) + def handle_trade(trade: Trade) -> None: """ @@ -153,26 +169,12 @@ def handle_trade(trade: Trade) -> None: current_rate = api_wrapper.get_ticker(trade.pair)['bid'] current_profit = 100 * ((current_rate - trade.open_rate) / trade.open_rate) - # Get available balance - currency = trade.pair.split('_')[1] - balance = api_wrapper.get_balance(currency) - for duration, threshold in sorted(CONFIG['minimal_roi'].items()): duration, threshold = float(duration), float(threshold) # Check if time matches and current rate is above threshold time_diff = (datetime.utcnow() - trade.open_date).total_seconds() / 60 if time_diff > duration and current_rate > (1 + threshold) * trade.open_rate: - # Execute sell - profit = trade.exec_sell_order(current_rate, balance) - message = '*{}:* Selling [{}]({}) at rate `{:f} (profit: {}%)`'.format( - trade.exchange.name, - trade.pair.replace('_', '/'), - api_wrapper.get_pair_detail_url(trade.pair), - trade.close_rate, - round(profit, 2) - ) - logger.info(message) - TelegramHandler.send_msg(message) + execute_sell(trade, current_rate) return else: logger.debug('Threshold not reached. (cur_profit: %1.2f%%)', current_profit) From 75f891194e0600ed3d8d29a66e8d3401330f8870 Mon Sep 17 00:00:00 2001 From: Janne Sinivirta Date: Thu, 7 Sep 2017 17:34:34 +0300 Subject: [PATCH 3/5] sell immediately if we go below stoploss --- main.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/main.py b/main.py index 881b51f83..ee1cbd113 100755 --- a/main.py +++ b/main.py @@ -169,6 +169,11 @@ def handle_trade(trade: Trade) -> None: current_rate = api_wrapper.get_ticker(trade.pair)['bid'] current_profit = 100 * ((current_rate - trade.open_rate) / trade.open_rate) + if ('stoploss' in CONFIG) & (current_profit < float(CONFIG['stoploss'])*100): + logger.debug('Stop loss hit.') + execute_sell(trade, current_rate) + return + for duration, threshold in sorted(CONFIG['minimal_roi'].items()): duration, threshold = float(duration), float(threshold) # Check if time matches and current rate is above threshold From a1d616f8c21876e5a6cef3da645fc4f366b1e822 Mon Sep 17 00:00:00 2001 From: Janne Sinivirta Date: Thu, 7 Sep 2017 17:38:28 +0300 Subject: [PATCH 4/5] update README to document stoploss --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index e8997a017..bfd640060 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,9 @@ See the example below: }, ``` +`stoploss` is loss in percentage that should trigger a sale. +For example value `-0.10` will cause immediate sell if the +profit dips below -10% for a given trade. This parameter is optional. The other values should be self-explanatory, if not feel free to raise a github issue. From 69bfc2d777235688aea39902d65507d9d2d1a182 Mon Sep 17 00:00:00 2001 From: Janne Sinivirta Date: Fri, 8 Sep 2017 08:03:24 +0300 Subject: [PATCH 5/5] remove unnecessary parentheses --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index ee1cbd113..239666d6f 100755 --- a/main.py +++ b/main.py @@ -169,7 +169,7 @@ def handle_trade(trade: Trade) -> None: current_rate = api_wrapper.get_ticker(trade.pair)['bid'] current_profit = 100 * ((current_rate - trade.open_rate) / trade.open_rate) - if ('stoploss' in CONFIG) & (current_profit < float(CONFIG['stoploss'])*100): + if 'stoploss' in CONFIG & current_profit < float(CONFIG['stoploss'])*100: logger.debug('Stop loss hit.') execute_sell(trade, current_rate) return