Class: Stockfighter::GM
- Inherits:
-
Object
- Object
- Stockfighter::GM
- Defined in:
- lib/stockfighter/gm.rb
Constant Summary collapse
- GM_URL =
"https://www.stockfighter.io/gm"
Instance Attribute Summary collapse
-
#instance_id ⇒ Object
Returns the value of attribute instance_id.
Instance Method Summary collapse
- #active? ⇒ Boolean
- #add_message_callback(&block) ⇒ Object
- #add_state_change_callback(&block) ⇒ Object
- #add_trading_day_callback(&block) ⇒ Object
- #config ⇒ Object
- #get_instance ⇒ Object
-
#initialize(key:, level:, polling: false) ⇒ GM
constructor
A new instance of GM.
- #judge(account:, explanation_link:, executive_summary:) ⇒ Object
- #restart ⇒ Object
- #resume ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(key:, level:, polling: false) ⇒ GM
Returns a new instance of GM.
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 |
# File 'lib/stockfighter/gm.rb', line 10 def initialize(key:, level:, polling:false) @api_key = key @level = level @message_callbacks = [] @state_change_callbacks = [] @trading_day_callbacks = [] new_level_response = perform_request("post", "#{GM_URL}/levels/#{level}") previous_state = new_level_response['state'] previous_trading_day = 0 if polling # websocket API functionality instead of polling would be great here scheduler = Rufus::Scheduler.new(:overlap => false) scheduler.every '10s' do response = get_instance() current_state = response['state'] if previous_state != current_state @state_change_callbacks.each { |callback| callback.call(previous_state, current_state) } previous_state = current_state end if response.key?('details') details = response['details'] current_trading_day = details['tradingDay'] end_of_the_world_day = details['endOfTheWorldDay'] if previous_trading_day != current_trading_day @trading_day_callbacks.each { |callback| callback.call(previous_trading_day, current_trading_day, end_of_the_world_day) } previous_trading_day = current_trading_day end end end end end |
Instance Attribute Details
#instance_id ⇒ Object
Returns the value of attribute instance_id.
8 9 10 |
# File 'lib/stockfighter/gm.rb', line 8 def instance_id @instance_id end |
Instance Method Details
#active? ⇒ Boolean
92 93 94 95 |
# File 'lib/stockfighter/gm.rb', line 92 def active? response = get_instance() response["done"] end |
#add_message_callback(&block) ⇒ Object
51 52 53 |
# File 'lib/stockfighter/gm.rb', line 51 def (&block) @message_callbacks << block end |
#add_state_change_callback(&block) ⇒ Object
55 56 57 |
# File 'lib/stockfighter/gm.rb', line 55 def add_state_change_callback(&block) @state_change_callbacks << block end |
#add_trading_day_callback(&block) ⇒ Object
59 60 61 |
# File 'lib/stockfighter/gm.rb', line 59 def add_trading_day_callback(&block) @trading_day_callbacks << block end |
#config ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/stockfighter/gm.rb', line 63 def config if @config[:account] && @config[:venue] && @config[:symbol] @config else nil end end |
#get_instance ⇒ Object
97 98 99 |
# File 'lib/stockfighter/gm.rb', line 97 def get_instance perform_request("get", "#{GM_URL}/instances/#{@instance_id}") end |
#judge(account:, explanation_link:, executive_summary:) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/stockfighter/gm.rb', line 83 def judge(account:, explanation_link:, executive_summary:) evidence = { "account" => account, "explanation_link" => explanation_link, "executive_summary" => executive_summary } perform_request("post", "#{GM_URL}/instances/#{@instance_id}/judge", body: JSON.dump(evidence)) end |
#restart ⇒ Object
71 72 73 |
# File 'lib/stockfighter/gm.rb', line 71 def restart perform_request("post", "#{GM_URL}/instances/#{@instance_id}/restart") end |
#resume ⇒ Object
79 80 81 |
# File 'lib/stockfighter/gm.rb', line 79 def resume perform_request("post", "#{GM_URL}/instances/#{@instance_id}/resume") end |
#stop ⇒ Object
75 76 77 |
# File 'lib/stockfighter/gm.rb', line 75 def stop perform_request("post", "#{GM_URL}/instances/#{@instance_id}/stop") end |