-
Notifications
You must be signed in to change notification settings - Fork 0
/
babyQuant.py
177 lines (150 loc) · 6.52 KB
/
babyQuant.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import pandas as pd
import time
from textblob import TextBlob
import ccxt
# Configuration
API_KEY = 'your_api_key'
API_SECRET = 'your_api_secret'
EXCHANGE_ID = 'binance' # Example: Binance exchange
SYMBOL = 'BTC/USDT' # Example trading pair
TIMEFRAME = '1m' # Candle timeframe
TRADE_AMOUNT = 0.001 # Base trade amount (consider dynamic sizing)
TAKE_PROFIT_PERCENT = 1.5 # Take profit percentage
STOP_LOSS_PERCENT = 1.0 # Stop loss percentage
MOMENTUM_PERIOD = 10 # Period for trend-following logic
VOLATILITY_PERIOD = 20 # Period for volatility calculation
VOLATILITY_THRESHOLD = 0.015 # Threshold for high volatility
PAIR_SYMBOLS = ['ETH/USDT', 'BTC/USDT', 'LTC/USDT', 'BNB/USDT'] # Pairs for trading
PAIR_SPREAD_THRESHOLD = 30 # Spread threshold for pairs trading
if CCXT_AVAILABLE:
# Initialize the exchange
exchange = getattr(ccxt, EXCHANGE_ID)({
'apiKey': API_KEY,
'secret': API_SECRET,
'enableRateLimit': True,
})
# Sample DataFrame for positions
data = {
'symbol': ['ZILUSD', 'DYDXUSD', 'LTCUSD', 'SOLUSD', 'BNBUSD', 'XRPUSD', 'ETHUSD'],
'open_side': [None, None, None, None, None, None, 'Buy'],
'index_pos': [0, 1, 2, 3, 4, 5, 11],
'open_size': [0, 0, 0, 0, 0, 0, 2],
'open_bool': [False, False, False, False, False, False, True],
'long': [None, None, None, None, None, None, True]
}
positions_df = pd.DataFrame(data)
# Helper functions
def fetch_latest_candle(symbol):
"""Fetch the latest candle for the given symbol and timeframe."""
if not CCXT_AVAILABLE:
print("CCXT not available. Cannot fetch candle data.")
return None
candles = exchange.fetch_ohlcv(symbol, timeframe=TIMEFRAME, limit=2)
return candles[-1] # Return the most recent candle
def calculate_volatility(prices, period=VOLATILITY_PERIOD):
"""Calculate volatility using standard deviation."""
if len(prices) < period:
return None
return pd.Series(prices).pct_change().rolling(period).std().iloc[-1]
def calculate_rsi(prices, period=14):
"""Calculate the Relative Strength Index (RSI)."""
if len(prices) < period:
return None
gains = [prices[i] - prices[i - 1] for i in range(1, len(prices)) if prices[i] > prices[i - 1]]
losses = [prices[i - 1] - prices[i] for i in range(1, len(prices)) if prices[i] < prices[i - 1]]
avg_gain = sum(gains[-period:]) / period
avg_loss = sum(losses[-period:]) / period
if avg_loss == 0:
return 100
rs = avg_gain / avg_loss
rsi = 100 - (100 / (1 + rs))
return rsi
def perform_sentiment_analysis(text):
"""Perform sentiment analysis on a given text."""
analysis = TextBlob(text)
sentiment = analysis.sentiment.polarity
return sentiment
def calculate_momentum(prices, period=MOMENTUM_PERIOD):
"""Calculate momentum for trend-following logic."""
if len(prices) < period:
return None
return prices[-1] - prices[-period] # Momentum as price difference
def place_order(side, amount, symbol):
"""Place a market order."""
if not CCXT_AVAILABLE:
print("CCXT not available. Cannot place order.")
return None
try:
order = exchange.create_order(
symbol=symbol,
type='market',
side=side,
amount=amount
)
return order
except Exception as e:
print(f"Error placing {side} order: {e}")
return None
def pairs_trading(symbols):
"""Execute pairs trading strategy based on price spreads."""
prices = [fetch_latest_candle(symbol)[4] for symbol in symbols]
if len(prices) == 2:
spread = prices[0] - prices[1]
print(f"Pair Spread: {spread}")
if spread > PAIR_SPREAD_THRESHOLD: # Arbitrary threshold
print("Spread too wide: Short first asset, Long second asset")
place_order('sell', TRADE_AMOUNT, symbols[0])
place_order('buy', TRADE_AMOUNT, symbols[1])
elif spread < -PAIR_SPREAD_THRESHOLD:
print("Spread too negative: Long first asset, Short second asset")
place_order('buy', TRADE_AMOUNT, symbols[0])
place_order('sell', TRADE_AMOUNT, symbols[1])
def find_open_positions(df):
"""Find positions where open_bool is True and return the symbols."""
open_positions = df[df['open_bool'] == True]
symbols = open_positions['symbol'].tolist()
print(f"Open positions found: {symbols}")
return symbols
def trading_logic():
"""Main trading logic with pairs trading, volatility arbitrage, and momentum."""
prices = []
open_positions = find_open_positions(positions_df)
news_headline = "Bitcoin rally continues as institutional interest surges."
sentiment = perform_sentiment_analysis(news_headline)
print(f"Sentiment Analysis on news headline: {sentiment}")
while True:
try:
# Fetch the latest candle
candle = fetch_latest_candle(SYMBOL)
if candle is None:
print("No candle data available. Skipping iteration.")
time.sleep(5)
continue
close_price = candle[4] # Closing price
prices.append(close_price)
# Ensure we have enough data for calculations
if len(prices) > MOMENTUM_PERIOD:
rsi = calculate_rsi(prices)
momentum = calculate_momentum(prices)
volatility = calculate_volatility(prices)
print(f"RSI: {rsi}, Momentum: {momentum}, Volatility: {volatility}")
# Volatility arbitrage
if volatility is not None and volatility > VOLATILITY_THRESHOLD:
print(f"High volatility detected ({volatility}): Placing trades.")
place_order('buy', TRADE_AMOUNT, SYMBOL)
# Trend-following conditions (momentum-based)
if momentum > 0 and sentiment > 0:
print(f"Momentum {momentum}: Buying signal with positive sentiment ({sentiment}).")
place_order('buy', TRADE_AMOUNT, SYMBOL)
elif momentum < 0 and sentiment < 0:
print(f"Momentum {momentum}: Selling signal with negative sentiment ({sentiment}).")
place_order('sell', TRADE_AMOUNT, SYMBOL)
# Pairs trading logic
pairs_trading(PAIR_SYMBOLS)
time.sleep(exchange.rateLimit / 1000 if CCXT_AVAILABLE else 5)
except Exception as e:
print(f"Error in trading logic: {e}")
time.sleep(5)
if __name__ == '__main__':
print("Starting trading bot...")
trading_logic()