-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUpdated_bot.py
86 lines (69 loc) · 2.99 KB
/
Updated_bot.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
#UPDATED 2024 EDITION (ENJOY THE CODE)
import yfinance as yf
import pandas as pd
import numpy as np
import logging
import alpaca_trade_api as tradeapi
from datetime import datetime
#If you want to trade on paper trading follow this additional step
API_KEY = 'your_api_key_here'
SECRET_KEY = 'your_secret_key_here'
BASE_URL = 'https://paper-api.alpaca.markets'
# Create an instance of the Alpaca API
api = tradeapi.REST(API_KEY, SECRET_KEY, BASE_URL, api_version='v2')
# Download historical data
data = yf.download("AAPL", start="2022-01-01", end="2022-12-31")
def calculate_indicators(data):
# Exponential Moving Averages
data['EMA_12'] = data['Close'].ewm(span=12, adjust=False).mean()
data['EMA_26'] = data['Close'].ewm(span=26, adjust=False).mean()
# Relative Strength Index
delta = data['Close'].diff()
gain = (delta.where(delta > 0, 0)).rolling(window=14).mean()
loss = (-delta.where(delta < 0, 0)).rolling(window=14).mean()
rs = gain / loss
data['RSI'] = 100 - (100 / (1 + rs))
return data
data = calculate_indicators(data)
def generate_signals(data):
# Buy and Sell signals based on EMA and RSI
data['Signal'] = 0
buy_signal = (data['EMA_12'] > data['EMA_26']) & (data['RSI'] < 30)
sell_signal = (data['EMA_12'] < data['EMA_26']) & (data['RSI'] > 70)
data.loc[buy_signal, 'Signal'] = 1
data.loc[sell_signal, 'Signal'] = -1
data['Position'] = data['Signal'].replace(to_replace=0, method='ffill')
return data
data = generate_signals(data)
def backtest(data, initial_balance=10000):
balance = initial_balance
position = 0
stop_loss = 0.95
take_profit = 1.10
entry_price = 0
for i, row in data.iterrows():
if row['Signal'] == 1 and balance != 0:
position = balance / row['Close']
balance = 0
entry_price = row['Close']
logging.info(f"BUY at {row['Close']} on {row.name.date()}")
elif row['Signal'] == -1 and position != 0:
balance = position * row['Close']
position = 0
logging.info(f"SELL at {row['Close']} on {row.name.date()}")
entry_price = 0
if position != 0:
if row['Close'] <= entry_price * stop_loss:
balance = position * row['Close']
position = 0
logging.info(f"STOP LOSS at {row['Close']} on {row.name.date()}")
elif row['Close'] >= entry_price * take_profit:
balance = position * row['Close']
position = 0
logging.info(f"TAKE PROFIT at {row['Close']} on {row.name.date()}")
if position != 0:
balance = position * data.iloc[-1]['Close'] # Close position at the last available price
return balance
logging.basicConfig(filename='trading_bot.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
final_balance = backtest(data)
print(f"Final Balance: ${final_balance:.2f}")