-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrader.rb
More file actions
executable file
·151 lines (136 loc) · 3.99 KB
/
trader.rb
File metadata and controls
executable file
·151 lines (136 loc) · 3.99 KB
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
require "selenium-webdriver"
require "./credentials"
require "./market_values"
class Trader
def initialize
@driver = Selenium::WebDriver.for :firefox
@market_values = MarketValues.new(@driver, 5, false)
@market_url = "https://www.predictit.org/Market/4389/Who-will-win-the-2018-Republican-primary-for-Oregon's-4th-District"
end
def run
login
navigate_to_market
update_prices
while true
# begin
navigate_to_market
update_prices
action = @market_values.suggest_action
p action
perform_action(action)
sleep(4)
# rescue
# end
end
end
private
def perform_action(action)
buy(action) if action[:type] == :buy
cancel(action) if action[:type] == :cancel
sell(action) if action[:type] == :sell
end
def buy(action)
col = action[:shares] == :yes ? 3 : 5
element = @driver.find_elements(css: "tbody tr:nth-of-type(#{action[:idx] + 1}) td:nth-of-type(#{col}) span a")[0]
element.click
sleep_while_spinner
element = @driver.find_element(id: 'Quantity')
3.times { element.send_keys "\ue003" }
element.send_keys action[:quantity]
element = @driver.find_element(id: 'PricePerShare')
3.times { element.send_keys "\ue017" }
element.send_keys action[:price]
button_id = action[:shares] == :no ? "submitSell" : "submitBuy"
element = @driver.find_element(id: button_id)
element.click
sleep_while_spinner
element = @driver.find_elements(css: 'button.btn-success')[0]
element.click
end
def sell(action)
col = action[:shares] == :yes ? 4 : 6
element = @driver.find_elements(css: "tbody tr:nth-of-type(#{action[:idx] + 1}) td:nth-of-type(#{col}) span a")[0]
element.click
sleep_while_spinner
complete_purchase(action)
end
def complete_purchase(action)
element = @driver.find_element(id: 'Quantity')
3.times { element.send_keys "\ue003" }
element.send_keys action[:quantity]
element = @driver.find_element(id: 'PricePerShare')
3.times { element.send_keys "\ue017" }
element.send_keys action[:price]
button_id = action[:shares] == :no || action[:type] == :sell ? "submitSell" : "submitBuy"
element = @driver.find_element(id: button_id)
element.click
sleep_while_spinner
element = @driver.find_elements(css: 'button.btn-success')[0]
element.click
end
def cancel(action)
col = action[:offer] == :buy ? 8 : 9
element = @driver.find_elements(css: "tbody tr:nth-of-type(#{action[:idx] + 1}) td:nth-of-type(#{col}) a")[0]
element.click
sleep_while_spinner
sleep(0.5)
element = @driver.find_elements(css: "#ownershipmodal a.cancelOrderBook")[0]
element.click
# sleep_while_spinner
sleep(1)
@driver.switch_to.alert.accept
end
def login
@driver.navigate.to "https://www.predictit.org/"
element = @driver.find_elements(css: 'a[href="#SignInModal"]')[0]
element.click
element = @driver.find_element(id: 'Email')
element.send_keys Credentials.username
element = @driver.find_element(id: 'Password')
element.send_keys Credentials.password
element.submit
sleep_while_spinner
end
def sleep_while_spinner
sleep(1)
while true
begin
element = @driver.find_element(id: 'spinnnerGo')
break if element.attribute("outerHTML").include?("display: none")
sleep(0.5)
rescue
break
end
end
end
def click_alert_if_present
begin
element.send_keys:return
@driver.switch_to.alert.accept
rescue
end
end
def navigate_to_market
sleep_while_spinner
click_alert_if_present
@driver.navigate.to @market_url
expand
end
def update_prices
@market_values.update_prices
while @market_values.cur_prices == {}
sleep(1)
@market_values.update_prices
end
end
def expand
sleep(1)
begin
element = @driver.find_element(id: "showMoreLinkContent")
element.click if element
rescue
end
end
end
trader = Trader.new
trader.run