-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfriar_tuck_run.py
169 lines (117 loc) · 5.35 KB
/
friar_tuck_run.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
MIT License
Copyright (c) 2017 Code Society
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import time
import argparse
import sys
from friartuck.api import FriarTuckLive, Order
import logging
from os import path
import configparser
parser = argparse.ArgumentParser(description='FriarTuck Quant Robinhood Broker Application')
parser.add_argument('--algo_script', action="store", dest="algo_script", help="Algorithm script file")
parser.add_argument('--config_file', action="store", dest="config_file",
help="Configuration file which should include credentials")
parser.add_argument('--data_frequency', action="store", dest="data_frequency",
help="[1m, 5m, 15m, 1h, 1d] The frequency of bar data... default is 1h")
# parser.add_argument('--tzone', action="store", dest="tzone", help="Time_zone")
log = logging.getLogger("friar_tuck")
PATH = path.abspath(path.dirname(__file__))
ROOT = path.dirname(PATH)
friar_tuck = None
def get_config(config_filename):
friar_config = configparser.ConfigParser(
interpolation=configparser.ExtendedInterpolation(),
allow_no_value=True,
delimiters='=',
inline_comment_prefixes='#'
)
local_filename = config_filename.replace('.cfg', '_local.cfg')
if path.isfile(local_filename):
config_filename = local_filename
with open(config_filename, 'r') as file:
friar_config.read_file(file)
return friar_config
def get_datetime():
return friar_tuck.get_datetime()
def lookup_security(symbol):
return friar_tuck.fetch_and_build_security(symbol)
def get_order(order):
# Checking to see if the order is the Order object, if yes, use the id
if isinstance(order, Order):
return friar_tuck.get_order(order.id)
return friar_tuck.get_order(order)
def get_last_filled_buy_order(security):
return friar_tuck.get_last_filled_buy_order(security=security)
def get_last_filled_sell_order(security):
return friar_tuck.get_last_filled_sell_order(security=security)
def get_last_filled_orders_by_side(security):
return friar_tuck.get_last_filled_orders_by_side(security=security)
def get_open_orders(security=None):
return friar_tuck.get_open_orders(security)
def cancel_order(order):
# Checking to see if the order is the Order object, if yes, use the id
if isinstance(order, Order):
return friar_tuck.cancel_order(order.id)
return friar_tuck.cancel_order(order)
def order_shares(security, shares, order_type=None, time_in_force='gfd'):
return friar_tuck.order_shares(security, shares, order_type, time_in_force)
def order_value(security, amount, order_type=None, time_in_force='gfd'):
return friar_tuck.order_value(security, amount, order_type, time_in_force)
def start_engine(input_args):
args = parser.parse_args(input_args)
global trading_algo, config
trading_algo = __import__(args.algo_script)
config_file = args.config_file
data_frequency = args.data_frequency
if not data_frequency:
data_frequency = "1h"
CONFIG_FILENAME = path.join(PATH, config_file)
config = get_config(CONFIG_FILENAME)
# os.environ['TZ'] = 'US/Eastern'
# time.tzset()
if (not config.get('LOGIN', 'username')) or (not config.get('LOGIN', 'password')):
exit('no login credentials given')
"""Start Shell Setup"""
global friar_tuck
friar_tuck = FriarTuckLive(config=config,
data_frequency=data_frequency)
trading_algo.friar_tuck = friar_tuck
trading_algo.lookup_security = lookup_security
trading_algo.get_order = get_order
trading_algo.get_last_filled_buy_order = get_last_filled_buy_order
trading_algo.get_last_filled_sell_order = get_last_filled_sell_order
trading_algo.get_last_filled_orders_by_side = get_last_filled_orders_by_side
trading_algo.get_open_orders = get_open_orders
trading_algo.cancel_order = cancel_order
trading_algo.order_shares = order_shares
trading_algo.order_value = order_value
trading_algo.get_datetime = get_datetime
friar_tuck.set_active_algo(trading_algo)
friar_tuck.run_engine()
"""End Shell Setup"""
if __name__ == "__main__":
# if len(sys.argv) <= 2:
# exit("Too less arguments calling script")
start_engine(sys.argv[1:])
while 1:
# log.info("Alive and well: %s" % datetime.datetime.now())
time.sleep(60)
friar_tuck.stop_engine()
time.sleep(1)