-
Notifications
You must be signed in to change notification settings - Fork 4
/
hourly_run.py
121 lines (102 loc) · 3.24 KB
/
hourly_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
import alpaca_trade_api as tradeapi
import os
import sys
import numpy as np
import pandas as pd
import pyarrow
import argparse
from joblib import load
from datetime import date
from dotenv import load_dotenv
load_dotenv()
#authentication and connection details
api_key = os.getenv('ALPACA_API_KEY')
api_secret = os.getenv('ALPACA_SECRET_KEY')
base_url = 'https://paper-api.alpaca.markets'
#instantiate REST API
api = tradeapi.REST(api_key, api_secret, base_url, api_version='v2')
#obtain account information
account = api.get_account()
sys.path.append('.')
import algo_lib
def update_df(ticker):
df = algo_lib.historical_hourly(ticker)
df['Daily_return'] = df.Close.dropna().pct_change()
return df
def prediction(ticker, crossover_signal):
model = load(f'{ticker}_hourly.joblib')
return model.predict(crossover_signal)
def get_bar(ticker):
return api.get_barset(ticker.upper(), 'day', limit=1)[ticker.upper()][0].c
def make_trade(signal, ticker, amount):
# first get all positions
positions = [
position._raw for position in api.list_positions()
if position._raw['symbol'] == ticker
]
position = positions[0] if len(positions) > 0 else None
# decision
if signal == 1:
side = 'buy' if position == None else None
qty = (
round(amount/api.get_last_quote('TSLA')._raw['askprice'])
if position == None else None
)
elif signal == 0:
side = 'sell' if position != None else None
qty = position['qty'] if position != None else None
else:
side = None
# trade!!!
if side != None:
print(f'''
+++++++++++++++++++++++++++++++++++++++++++++++++
Decision: {side}ing {qty} shares of {ticker}
+++++++++++++++++++++++++++++++++++++++++++++++++
''')
new_order = api.submit_order(
symbol = ticker,
qty = qty,
side = side,
type = 'market',
time_in_force = 'gtc'
)
return new_order._raw['client_order_id']
def main(args):
# Init
ticker = args[0]
amount = int(args[1])
print(f'''
+++++++++++++++++++++++++++++++++++++++++++++++++
Initialize {ticker} Trading Decision for ${amount}
+++++++++++++++++++++++++++++++++++++++++++++++++
''')
# Load and update historical dataframe
df = update_df(ticker)
# Generate signal
crossover_signal = algo_lib.crossover_signal(
df, ['crossover_signal']
).tail(1)[['crossover_signal']]
# Make prediction
signal = prediction(ticker, crossover_signal)[0]
print(f'''
+++++++++++++++++++++++++++++++++++++++++++++++++
Signal = {signal}
+++++++++++++++++++++++++++++++++++++++++++++++++
''')
# Final execution
order_id = make_trade(signal, ticker, amount)
if order_id != None:
print(f'''
+++++++++++++++++++++++++++++++++++++++++++++++++
Order accepted - Client Order ID: {order_id}
+++++++++++++++++++++++++++++++++++++++++++++++++
''')
else:
print(f'''
+++++++++++++++++++++++++++++++++++++++++++++++++
Staying Put... No action on {ticker}
+++++++++++++++++++++++++++++++++++++++++++++++++
''')
if __name__ == '__main__':
main(sys.argv[1:])