-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
69 lines (50 loc) · 1.99 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Import custom classes from various .py files
from ibkr_custom_class import Interactive_Brokers_Custom
from get_yf_data import Yahoo_Data
from get_trades import get_trade_list
import pandas as pd
import datetime
import time
import os
# Dataframe Settings
pd.set_option('display.max_columns', None)
# Current path
path_ = os.path.abspath(os.getcwd())
def run_bot(date="2022-05-12", market_open_time="07:45:05", ip='127.0.0.1', socket=7497, clientId=1):
# Current path
path_ = os.path.abspath(os.getcwd())
path = path_ + f"\\Trades_to_take\\{date} Reversion Trades.csv"
# Get Market Open timing in SGT with 5 seconds of buffer
market_open = market_open_time
# Initialize custom class as ib for easier usage.
ib = Interactive_Brokers_Custom('ReversionSys', 3)
# Get intro message to output
ib.intro()
# Connect to interactive brokers TWS Server
ib.connect(ip=ip, socket=socket, clientId=clientId)
# Run bot loop
while True:
# Get current date and time in formatted string
dt = datetime.datetime.now().strftime('%H:%M:%S')
date = datetime.datetime.today().strftime('%Y-%m-%d')
# Print time constantly and sleep 1 second for buffer
print(f"Time now is : {dt}, Waiting for market open.")
time.sleep(1)
# If time == market open, run bot.
if dt == market_open:
print("Market has opened... will begin trading sequence now....")
# Sell any positions that have exit criteria met.
ib.sell_positions()
# Sleep 2.5 seconds for buffer
ib.ibkr.sleep(2.5)
# Buy any positions that have entry criteria met.
ib.buy_positions(f'{path}')
time.sleep(1)
exit(0)
print(
f"Finished executing code @ {dt}. Shutting down program now.")
if __name__ == "__main__":
# Get Trade List
# get_trade_list()
# Uncomment and key in trade list date to run the bot.
run_bot(date="2022-05-12")