Class: SQA::Strategy::KBS
- Inherits:
-
Object
- Object
- SQA::Strategy::KBS
- Defined in:
- lib/sqa/strategy/kbs_strategy.rb
Instance Attribute Summary collapse
-
#default_rules_loaded ⇒ Object
readonly
Returns the value of attribute default_rules_loaded.
-
#kb ⇒ Object
readonly
Returns the value of attribute kb.
Class Method Summary collapse
-
.trade(vector) ⇒ Object
Main strategy interface - compatible with SQA::Strategy framework.
Instance Method Summary collapse
-
#add_rule(name, &block) ⇒ Object
Add a custom trading rule.
-
#assert_fact(type, attributes = {}) ⇒ Object
Assert a fact into working memory.
-
#execute(vector) ⇒ Object
Execute strategy with given market data.
-
#initialize(load_defaults: true) ⇒ KBS
constructor
A new instance of KBS.
-
#print_facts ⇒ Object
Print current working memory (for debugging).
-
#print_rules ⇒ Object
Print all rules (for debugging).
-
#query_facts(type, pattern = {}) ⇒ Object
Query facts from working memory.
Constructor Details
#initialize(load_defaults: true) ⇒ KBS
Returns a new instance of KBS.
53 54 55 56 57 58 59 |
# File 'lib/sqa/strategy/kbs_strategy.rb', line 53 def initialize(load_defaults: true) @kb = ::KBS::DSL::KnowledgeBase.new @default_rules_loaded = false @last_signal = :hold load_default_rules if load_defaults end |
Instance Attribute Details
#default_rules_loaded ⇒ Object (readonly)
Returns the value of attribute default_rules_loaded.
51 52 53 |
# File 'lib/sqa/strategy/kbs_strategy.rb', line 51 def default_rules_loaded @default_rules_loaded end |
#kb ⇒ Object (readonly)
Returns the value of attribute kb.
51 52 53 |
# File 'lib/sqa/strategy/kbs_strategy.rb', line 51 def kb @kb end |
Class Method Details
.trade(vector) ⇒ Object
Main strategy interface - compatible with SQA::Strategy framework
62 63 64 65 |
# File 'lib/sqa/strategy/kbs_strategy.rb', line 62 def self.trade(vector) strategy = new strategy.execute(vector) end |
Instance Method Details
#add_rule(name, &block) ⇒ Object
Add a custom trading rule
Example:
add_rule :buy_dip do
on :rsi, { value: ->(v) { v < 30 } }
on :macd, { signal: :bullish }
perform { kb.assert(:signal, { action: :buy, confidence: :high }) }
end
Note: Use ‘kb.assert` in perform blocks, not just `assert`
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sqa/strategy/kbs_strategy.rb', line 92 def add_rule(name, &block) # Capture kb reference for use in perform blocks kb = @kb # Define the rule with kb available in closure @kb.instance_eval do rule(name, &block) end self end |
#assert_fact(type, attributes = {}) ⇒ Object
Assert a fact into working memory
105 106 107 |
# File 'lib/sqa/strategy/kbs_strategy.rb', line 105 def assert_fact(type, attributes = {}) @kb.assert(type, attributes) end |
#execute(vector) ⇒ Object
Execute strategy with given market data
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/sqa/strategy/kbs_strategy.rb', line 68 def execute(vector) # Reset working memory @kb.reset # Assert facts from vector assert_market_facts(vector) # Run the inference engine @kb.run # Query for trading signal determine_signal end |
#print_facts ⇒ Object
Print current working memory (for debugging)
115 116 117 |
# File 'lib/sqa/strategy/kbs_strategy.rb', line 115 def print_facts @kb.print_facts end |
#print_rules ⇒ Object
Print all rules (for debugging)
120 121 122 |
# File 'lib/sqa/strategy/kbs_strategy.rb', line 120 def print_rules @kb.print_rules end |
#query_facts(type, pattern = {}) ⇒ Object
Query facts from working memory
110 111 112 |
# File 'lib/sqa/strategy/kbs_strategy.rb', line 110 def query_facts(type, pattern = {}) @kb.query(type, pattern) end |