-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
69 lines (61 loc) · 2 KB
/
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
#import all libraries
from traderlib import *
from logger import *
import sys
import general_variables
import alpaca_trade_api as tradeapi
from alpaca_trade_api.rest import REST
#check our trading account and make general API calls
def check_account(api):
try:
account = api.get_account()
if account.status != 'ACTIVE':
logging.error("Could log into account")
sys.exit()
except Exception as e:
logging.error('Count not get info')
logging.info(str(e))
sys.exit()
def check_asset_okay(asset,api):
#IN: TICKER
#OUT: TRUE if it exists and is tradable / FALSE otherwise
try:
asset = api.get_asset(asset)
if asset.tradable:
return True
else:
return False
except Exception as e:
logging.error("Error checking asset")
sys.exit()
#close current orders
def clean_open_orders(api):
open_orders = api.list_orders(status='open',limit=100,nested=True)
logging.info('List of open orders')
logging.info(str(open_orders))
for order in open_orders:
logging.info('Order %s closed' % str(order.id))
try:
api.cancel_all_orders()
logging.info("closing orders complete")
except Exception as e:
logging.info("ERROR IN CANCELLING ORDERS")
sys.exit()
#call trading botf
#IN: string
#OUT boolean
def main():
api = tradeapi.REST(general_variables.Alpaca_api_key_id,general_variables.Alpaca_secret_key,general_variables.Alpaca_endpoint,api_version='v2')
# type: ignore # type: ignore
#initialise logger
initialise_logger()
check_account(api)
clean_open_orders(api)
ticker = input("Write the ticker you want to trade with:")
check_asset_okay(ticker,api)
trader = Trader(ticker,api)
complete_trading = trader.run()
if not complete_trading:
logging.info("Trading unsuccesful")
if __name__ == '__main__':
main()