124 lines
3.1 KiB
Go
124 lines
3.1 KiB
Go
package strategy
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/pheinrich/aitrade/pkg/model"
|
|
)
|
|
|
|
func TestDefensiveStrategy(t *testing.T) {
|
|
strategy := NewDefensiveStrategy(true, 2.0)
|
|
|
|
if strategy.Name() != "defensive" {
|
|
t.Errorf("Expected name 'defensive', got '%s'", strategy.Name())
|
|
}
|
|
|
|
params := strategy.GetRiskParams()
|
|
if params.MaxParallelTrades != 2 {
|
|
t.Errorf("Expected MaxParallelTrades=2, got %d", params.MaxParallelTrades)
|
|
}
|
|
if params.MaxTradesPerHour != 3 {
|
|
t.Errorf("Expected MaxTradesPerHour=3, got %d", params.MaxTradesPerHour)
|
|
}
|
|
}
|
|
|
|
func TestNormalStrategy(t *testing.T) {
|
|
strategy := NewNormalStrategy(true, 3.0)
|
|
|
|
if strategy.Name() != "normal" {
|
|
t.Errorf("Expected name 'normal', got '%s'", strategy.Name())
|
|
}
|
|
|
|
params := strategy.GetRiskParams()
|
|
if params.MaxParallelTrades != 5 {
|
|
t.Errorf("Expected MaxParallelTrades=5, got %d", params.MaxParallelTrades)
|
|
}
|
|
if params.MaxTradesPerHour != 6 {
|
|
t.Errorf("Expected MaxTradesPerHour=6, got %d", params.MaxTradesPerHour)
|
|
}
|
|
}
|
|
|
|
func TestAggressiveStrategy(t *testing.T) {
|
|
strategy := NewAggressiveStrategy(true, 5.0)
|
|
|
|
if strategy.Name() != "aggressive" {
|
|
t.Errorf("Expected name 'aggressive', got '%s'", strategy.Name())
|
|
}
|
|
|
|
params := strategy.GetRiskParams()
|
|
if params.MaxParallelTrades != 10 {
|
|
t.Errorf("Expected MaxParallelTrades=10, got %d", params.MaxParallelTrades)
|
|
}
|
|
if params.MaxTradesPerHour != 12 {
|
|
t.Errorf("Expected MaxTradesPerHour=12, got %d", params.MaxTradesPerHour)
|
|
}
|
|
}
|
|
|
|
func TestStrategyFactory(t *testing.T) {
|
|
defensive := NewStrategy("defensive", true, 2.0)
|
|
if defensive.Name() != "defensive" {
|
|
t.Error("Factory should create defensive strategy")
|
|
}
|
|
|
|
normal := NewStrategy("normal", true, 3.0)
|
|
if normal.Name() != "normal" {
|
|
t.Error("Factory should create normal strategy")
|
|
}
|
|
|
|
aggressive := NewStrategy("aggressive", true, 5.0)
|
|
if aggressive.Name() != "aggressive" {
|
|
t.Error("Factory should create aggressive strategy")
|
|
}
|
|
|
|
// Default to normal
|
|
defaultStrat := NewStrategy("unknown", true, 3.0)
|
|
if defaultStrat.Name() != "normal" {
|
|
t.Error("Factory should default to normal strategy")
|
|
}
|
|
}
|
|
|
|
func TestAnalyzeWithPositiveSentiment(t *testing.T) {
|
|
strategy := NewNormalStrategy(true, 3.0)
|
|
|
|
market := &MarketData{
|
|
Symbol: "AAPL",
|
|
LastPrice: 150.0,
|
|
}
|
|
|
|
sentiment := 0.8
|
|
news := []*model.NewsArticle{
|
|
{
|
|
Title: "AAPL shows strong growth",
|
|
Symbols: "AAPL",
|
|
SentimentScore: &sentiment,
|
|
SentimentLabel: "positive",
|
|
},
|
|
{
|
|
Title: "Apple AAPL beats expectations",
|
|
Symbols: "AAPL",
|
|
SentimentScore: &sentiment,
|
|
SentimentLabel: "positive",
|
|
},
|
|
}
|
|
|
|
signal, err := strategy.Analyze(context.Background(), market, news)
|
|
if err != nil {
|
|
t.Fatalf("Analyze failed: %v", err)
|
|
}
|
|
|
|
if signal == nil {
|
|
t.Error("Expected trade signal, got nil")
|
|
} else {
|
|
if signal.Symbol != "AAPL" {
|
|
t.Errorf("Expected symbol AAPL, got %s", signal.Symbol)
|
|
}
|
|
if signal.Action != model.ActionBuy {
|
|
t.Errorf("Expected BUY action, got %s", signal.Action)
|
|
}
|
|
if signal.Confidence <= 0 || signal.Confidence > 1 {
|
|
t.Errorf("Confidence should be between 0 and 1, got %.2f", signal.Confidence)
|
|
}
|
|
}
|
|
}
|