-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
55 lines (48 loc) · 1.98 KB
/
main.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
# Orchestrate operations here
import time
from datetime import datetime, timezone
from loguru import logger
from trading_strategies import Strategy
from trading_strategies.moving_averages import MovingAverageStrategy
from utils import at_stop_loss, log_in
# Parameters
TICKER = "AAPL"
STOP_LOSS_PERCENTAGE = 0.15
def main(strat: Strategy) -> None:
log_in()
position = None
while True:
# Get our data. We need things like historical prices, current price, etc.
# Anything that our strategy needs to make decisions. OR our strategy could do that on it's own
# But I'm trying to not have the strategy know about which ticker it's trading or it's current position. That's handled by this loop... I think
if datetime.now(timezone.utc) >= "20:30":
logger.info("Selling")
# sell_option_limit_order
break
if position:
if at_stop_loss(position, current_price, STOP_LOSS_PERCENTAGE):
logger.info(f"Selling because of stop loss at {STOP_LOSS_PERCENTAGE}")
# sell_option_limit_order
break
if strat.should_sell():
logger.info("Selling because of strategy signal")
# sell_option_limit_order
break
else:
logger.info("Waiting for a signal to sell")
continue
else:
if strat.should_buy():
logger.info("Buying because of strategy signal")
# buy_option_limit_order
position = {"entry_price": current_price}
continue
else:
logger.info("Waiting for a signal to buy")
time.sleep(10)
if __name__ == "__main__":
# We could build a CLI tool for this so we can pass in the strategy and ticker from the command line
rsi_period = 14
rsi_threshold_low = 30
rsi_threshold_high = 70
main(MovingAverageStrategy(rsi_threshold_low, rsi_threshold_high, rsi_period))