-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
91 lines (75 loc) · 2.61 KB
/
app.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
import threading
from flask import Flask, render_template, request, jsonify
from trading_bot import TradingBot, scrape_openinsider, trade_history, lock
import logging
# Initialize the Flask app
app = Flask(__name__)
# Initialize the trading bot
bot = TradingBot()
@app.route("/")
def index():
"""
Displays the main dashboard of the trading bot.
Shows current positions and the available budget.
"""
positions_with_prices = {}
with lock:
for ticker, data in bot.positions.items():
current_price = bot.get_current_price(ticker)
gain_loss = ((current_price - data["buy_price"]) / data["buy_price"]) * 100
positions_with_prices[ticker] = {
"quantity": data["quantity"],
"buy_price": data["buy_price"],
"current_price": current_price,
"gain_loss": round(gain_loss, 2),
}
with lock:
return render_template(
"index.html",
budget=bot.budget,
positions=positions_with_prices,
trade_history=trade_history,
)
def run_bot(insider_data, gain_threshold, drop_threshold):
"""
Run the bot in a separate thread.
"""
try:
bot.run_trading_cycle(
insider_data, gain_threshold=gain_threshold, drop_threshold=drop_threshold
)
except Exception as e:
logging.error(f"Error while running the bot: {e}")
@app.route("/start", methods=["POST"])
def start_trading():
"""
Start the trading bot with the provided OpenInsider URL and thresholds.
Runs the bot in a separate thread to prevent blocking the Flask server.
"""
custom_url = request.form["url"] # Get the custom OpenInsider URL from the form
gain_threshold = float(
request.form["gain_threshold"]
) # Gain threshold (percentage)
drop_threshold = float(
request.form["drop_threshold"]
) # Drop threshold (percentage)
# Scrape insider data from the provided URL
insider_data = scrape_openinsider(custom_url)
# Start the bot trading cycle in a background thread
thread = threading.Thread(
target=run_bot, args=(insider_data, gain_threshold, drop_threshold)
)
thread.start()
return jsonify(
{"status": "success", "message": "Trading bot started in background"}
)
@app.route("/stop", methods=["POST"])
def stop_trading():
"""
Stop the trading bot.
"""
bot.stop() # Set the flag to stop the bot
return jsonify({"status": "success", "message": "Trading bot stopped"})
if __name__ == "__main__":
# Run the Flask app
app.run(debug=True)