Class: Stockfighter::GM

Inherits:
Object
  • Object
show all
Defined in:
lib/stockfighter/gm.rb

Constant Summary collapse

GM_URL =
"https://www.stockfighter.io/gm"

Instance Attribute Summary collapse

Instance Method Summary collapse

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_idObject

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

Returns:

  • (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 add_message_callback(&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

#configObject



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_instanceObject



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" => ,
    "explanation_link" => explanation_link,
    "executive_summary" => executive_summary
  }
  perform_request("post",  "#{GM_URL}/instances/#{@instance_id}/judge", body: JSON.dump(evidence))
end

#restartObject



71
72
73
# File 'lib/stockfighter/gm.rb', line 71

def restart
  perform_request("post", "#{GM_URL}/instances/#{@instance_id}/restart")
end

#resumeObject



79
80
81
# File 'lib/stockfighter/gm.rb', line 79

def resume
  perform_request("post", "#{GM_URL}/instances/#{@instance_id}/resume")
end

#stopObject



75
76
77
# File 'lib/stockfighter/gm.rb', line 75

def stop
  perform_request("post", "#{GM_URL}/instances/#{@instance_id}/stop")
end