-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathkiller.py
executable file
·144 lines (113 loc) · 4.49 KB
/
killer.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
########################################
# KILLER.py
# Grabs data from Redis datanase
# Buys and sells based on data
########################################
def buy(stock,ask_gap,r):
from utils import *
import redis
r = redis.Redis(host='localhost',port=6669)
r.set_response_callback('SMEMBERS', list)
# import the robinhood api library
from Robinhood import Robinhood
import os
import time
import datetime
#key setup
import ast
keys =[]
f = open('keys')
keys = ast.literal_eval(f.read())
f.close()
# login
## log into robinhood
my_trader = Robinhood();
my_trader.login(username=keys[0][1], password=keys[1][1])
# config robinhood to trade on that stock
stock_instrument = my_trader.instruments(stock)[0]
############################
# BUY LOOP
############################
# average and slope declaration
count = 1
buy_prices = []
buy_price_slope = 0
buy_average_prices = []
buy_slope_of_averages = 0
buy_sum = 0
## begin buy loop
buy = False
bought_at = 0.0
while buy == False:
if (last_bid(stock,r)-last_ask(stock,r)) > ask_gap:
# buy a single share of the stock at the ask price
#buy_order = my_trader.place_buy_order(stock_instrument, 1, ask_price)
# break the buy loop
buy = True
bought_at=last_ask(stock,r)
return buy,bought_at
def sell(stock,bought_at,risk_appetite,risk_appetite_slope,recovery_appetite,r):
from utils import *
import redis
r = redis.Redis(host='localhost',port=6669)
r.set_response_callback('SMEMBERS', list)
# import the robinhood api library
from Robinhood import Robinhood
import os
import time
import datetime
#key setup
import ast
keys =[]
f = open('keys')
keys = ast.literal_eval(f.read())
f.close()
# login
## log into robinhood
my_trader = Robinhood();
my_trader.login(username=keys[0][1], password=keys[1][1])
sold_at = 0.0
sell = False
while sell == False:
# SELL IF SLOPE OF AVERAGES IS DORPPING TO BELOW .02
# Need to determine optimal minimum count
##########################################################################
# Selling conditions
##########################################################################
# If current price is less than our bought at threshold, sell.
# If the current price is stagnant for n number of obs, sell.
# If the current price is descending and cannot recover peak gain, sell.
##########################################################################
# FIRST CONDITION: Price floor
# Is the price at a point where we are breaking even, at verge of a loss
if last_ask(stock,r) <= (bought_at + 0.03):
# sell single share at bid_price
#sell_order = my_trader.place_sell_order(stock_instrument, 1, last_bid(stock,r))
sell = True
sold_at=last_bid(stock,r)
# SECOND CONDITION: Stagnancy
# Is the stock varying around the same small window for a period of obs?
elif get_slope(sd_get_list(stock,r,risk_appetite)) <= (risk_appetite_slope/risk_appetite):
# sell single share at bid_price
#sell_order = my_trader.place_sell_order(stock_instrument, 1, last_bid(stock,r))
sell = True
sold_at=last_bid(stock,r)
# THIRD CONDITION: Gains and recovery
# Is the stock varying around the same small window for a period of obs?
elif (neg_count(sla_get_list(stock,r,recovery_appetite))+1) >= recovery_appetite:
# sell single share at bid_price
#sell_order = my_trader.place_sell_order(stock_instrument, 1, last_bid(stock,r))
sell = True
sold_at=last_bid(stock,r)
'''
# Conservative track
max_loss = (last_ask(stock,r) - bought_at)/last_ask(stock,r)/2
if last_ask(stock,r) <= last_ask(stock,r) - (last_ask(stock,r)*max_loss):
# Risk-attracted approach
if get_sla(get_all_col(stock,r,2)) > 0.0001 and len(get_all_row(stock,r)) > risk_appetite:
# sell single share at bid_price
#sell_order = my_trader.place_sell_order(stock_instrument, 1, bid_price)
sell = True
sold_at=bid_price
'''
return sell, sold_at