Class: BudaApi::AI::TradingAssistant

Inherits:
Object
  • Object
show all
Defined in:
lib/buda_api/ai/trading_assistant.rb

Overview

AI-powered trading assistant that provides market analysis and trading insights

Instance Method Summary collapse

Constructor Details

#initialize(client, llm_provider: :openai, model: nil) ⇒ TradingAssistant

Returns a new instance of TradingAssistant.



7
8
9
10
11
12
13
14
15
# File 'lib/buda_api/ai/trading_assistant.rb', line 7

def initialize(client, llm_provider: :openai, model: nil)
  @client = client
  @llm = RubyLLM.new(
    provider: llm_provider,
    model: model || default_model_for_provider(llm_provider)
  )
  
  BudaApi::Logger.info("Trading Assistant initialized with #{llm_provider}")
end

Instance Method Details

#analyze_market(market_id) ⇒ Hash

Analyze market conditions and provide trading insights

Examples:

assistant = BudaApi.trading_assistant(client)
analysis = assistant.analyze_market("BTC-CLP")
puts analysis[:sentiment] # => "bullish", "bearish", or "neutral"

Parameters:

  • market_id (String) —

    market identifier (e.g., "BTC-CLP")

Returns:

  • (Hash) —

    analysis results with sentiment, signals, and recommendations



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
# File 'lib/buda_api/ai/trading_assistant.rb', line 25

def analyze_market(market_id)
  BudaApi::Logger.info("Analyzing market #{market_id}")
  
  # Gather comprehensive market data
  ticker = @client.ticker(market_id)
  order_book = @client.order_book(market_id)
  trades = @client.trades(market_id, limit: 50)
  
  # Get historical data for context
  start_time = Time.now - 86400  # 24 hours
  candlesticks = @client.candlestick_report(market_id, start_at: start_time)
  
  prompt = build_market_analysis_prompt(market_id, ticker, order_book, trades, candlesticks)
  
  begin
    analysis = @llm.complete(prompt, max_tokens: 1000)
    parsed_analysis = parse_market_analysis(analysis)
    
    BudaApi::Logger.info("Market analysis completed for #{market_id}")
    parsed_analysis
    
  rescue => e
    BudaApi::Logger.error("Failed to analyze market: #{e.message}")
    {
      error: "Analysis failed: #{e.message}",
      market_id: market_id,
      timestamp: Time.now
    }
  end
end

#get_entry_exit_signals(market_id, action, amount) ⇒ Hash

Get AI-powered insights on optimal entry/exit points

Parameters:

  • market_id (String) —

    market identifier

  • action (String) —

    intended action ("buy" or "sell")

  • amount (Float) —

    intended trade amount

Returns:

  • (Hash) —

    entry/exit recommendations



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
# File 'lib/buda_api/ai/trading_assistant.rb', line 100

def get_entry_exit_signals(market_id, action, amount)
  BudaApi::Logger.info("Getting #{action} signals for #{amount} #{market_id}")
  
  # Get current market state
  ticker = @client.ticker(market_id)
  order_book = @client.order_book(market_id)
  
  # Get quotation for the intended trade
  quotation_type = action == "buy" ? "bid_given_size" : "ask_given_size"
  quotation = @client.quotation(market_id, quotation_type, amount)
  
  prompt = build_entry_exit_prompt(market_id, action, amount, ticker, order_book, quotation)
  
  begin
    signals = @llm.complete(prompt, max_tokens: 800)
    parsed_signals = parse_entry_exit_signals(signals)
    
    BudaApi::Logger.info("Entry/exit signals generated for #{market_id}")
    parsed_signals
    
  rescue => e
    BudaApi::Logger.error("Failed to generate signals: #{e.message}")
    {
      error: "Signal generation failed: #{e.message}",
      market_id: market_id,
      action: action,
      timestamp: Time.now
    }
  end
end

#suggest_trading_strategy(market_id, balance, risk_tolerance: 'medium', investment_horizon: 'medium') ⇒ Hash

Suggest trading strategy based on market conditions and user preferences

Parameters:

  • market_id (String) —

    market identifier

  • balance (Models::Balance) —

    current balance information

  • risk_tolerance (String) (defaults to: 'medium') —

    risk level ("low", "medium", "high")

  • investment_horizon (String) (defaults to: 'medium') —

    time horizon ("short", "medium", "long")

Returns:

  • (Hash) —

    strategy recommendations



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
# File 'lib/buda_api/ai/trading_assistant.rb', line 63

def suggest_trading_strategy(market_id, balance, risk_tolerance: 'medium', investment_horizon: 'medium')
  BudaApi::Logger.info("Generating trading strategy for #{market_id}")
  
  # Get market analysis first
  market_analysis = analyze_market(market_id)
  
  # Get portfolio context
  portfolio_data = gather_portfolio_data(balance)
  
  prompt = build_strategy_prompt(
    market_id, market_analysis, portfolio_data, 
    risk_tolerance, investment_horizon
  )
  
  begin
    strategy = @llm.complete(prompt, max_tokens: 1200)
    parsed_strategy = parse_strategy_recommendations(strategy)
    
    BudaApi::Logger.info("Trading strategy generated for #{market_id}")
    parsed_strategy
    
  rescue => e
    BudaApi::Logger.error("Failed to generate strategy: #{e.message}")
    {
      error: "Strategy generation failed: #{e.message}",
      market_id: market_id,
      timestamp: Time.now
    }
  end
end