forked from hummingbot/hummingbot
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdirectional_strategy_rsi_spot.py
95 lines (80 loc) · 3.86 KB
/
directional_strategy_rsi_spot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from decimal import Decimal
from hummingbot.data_feed.candles_feed.candles_factory import CandlesConfig, CandlesFactory
from hummingbot.strategy.directional_strategy_base import DirectionalStrategyBase
class RSISpot(DirectionalStrategyBase):
"""
RSI (Relative Strength Index) strategy implementation based on the DirectionalStrategyBase.
This strategy uses the RSI indicator to generate trading signals and execute trades based on the RSI values.
It defines the specific parameters and configurations for the RSI strategy.
Parameters:
directional_strategy_name (str): The name of the strategy.
trading_pair (str): The trading pair to be traded.
exchange (str): The exchange to be used for trading.
order_amount_usd (Decimal): The amount of the order in USD.
leverage (int): The leverage to be used for trading.
Position Parameters:
stop_loss (float): The stop-loss percentage for the position.
take_profit (float): The take-profit percentage for the position.
time_limit (int): The time limit for the position in seconds.
trailing_stop_activation_delta (float): The activation delta for the trailing stop.
trailing_stop_trailing_delta (float): The trailing delta for the trailing stop.
Candlestick Configuration:
candles (List[CandlesBase]): The list of candlesticks used for generating signals.
Markets:
A dictionary specifying the markets and trading pairs for the strategy.
Methods:
get_signal(): Generates the trading signal based on the RSI indicator.
get_processed_df(): Retrieves the processed dataframe with RSI values.
market_data_extra_info(): Provides additional information about the market data.
Inherits from:
DirectionalStrategyBase: Base class for creating directional strategies using the PositionExecutor.
"""
directional_strategy_name: str = "RSI_spot"
# Define the trading pair and exchange that we want to use and the csv where we are going to store the entries
trading_pair: str = "ETH-USDT"
exchange: str = "binance"
order_amount_usd = Decimal("40")
leverage = 10
# Configure the parameters for the position
stop_loss: float = 0.0075
take_profit: float = 0.015
time_limit: int = 60 * 55
trailing_stop_activation_delta = 0.004
trailing_stop_trailing_delta = 0.001
candles = [CandlesFactory.get_candle(CandlesConfig(connector=exchange, trading_pair=trading_pair, interval="3m", max_records=1000))]
markets = {exchange: {trading_pair}}
def get_signal(self):
"""
Generates the trading signal based on the RSI indicator.
Returns:
int: The trading signal (-1 for sell, 0 for hold, 1 for buy).
"""
candles_df = self.get_processed_df()
rsi_value = candles_df.iat[-1, -1]
if rsi_value > 70:
return -1
elif rsi_value < 30:
return 1
else:
return 0
def get_processed_df(self):
"""
Retrieves the processed dataframe with RSI values.
Returns:
pd.DataFrame: The processed dataframe with RSI values.
"""
candles_df = self.candles[0].candles_df
candles_df.ta.rsi(length=7, append=True)
return candles_df
def market_data_extra_info(self):
"""
Provides additional information about the market data to the format status.
Returns:
List[str]: A list of formatted strings containing market data information.
"""
lines = []
columns_to_show = ["timestamp", "open", "low", "high", "close", "volume", "RSI_7"]
candles_df = self.get_processed_df()
lines.extend([f"Candles: {self.candles[0].name} | Interval: {self.candles[0].interval}\n"])
lines.extend(self.candles_formatted_list(candles_df, columns_to_show))
return lines