/
strategy.go
85 lines (78 loc) · 2.89 KB
/
strategy.go
1
2
3
4
5
6
7
8
9
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package service
import (
"github.com/jeremyhahn/tradebot/common"
"github.com/jeremyhahn/tradebot/dao"
"github.com/jeremyhahn/tradebot/mapper"
)
type StrategyService interface {
GetStrategy(name string) (common.Plugin, error)
GetChartStrategy(chart common.Chart, name string, candles []common.Candlestick) (common.TradingStrategy, error)
GetChartStrategies(chart common.Chart, params *common.TradingStrategyParams, candles []common.Candlestick) ([]common.TradingStrategy, error)
}
type DefaultStrategyService struct {
ctx common.Context
chartStrategyDAO dao.ChartStrategyDAO
pluginService PluginService
indicatorService IndicatorService
chartMapper mapper.ChartMapper
pluginMapper mapper.PluginMapper
StrategyService
}
func NewStrategyService(ctx common.Context, chartStrategyDAO dao.ChartStrategyDAO, pluginService PluginService,
indicatorService IndicatorService, chartMapper mapper.ChartMapper) StrategyService {
return &DefaultStrategyService{
ctx: ctx,
chartStrategyDAO: chartStrategyDAO,
pluginService: pluginService,
indicatorService: indicatorService,
chartMapper: chartMapper}
}
func (service *DefaultStrategyService) GetStrategy(name string) (common.Plugin, error) {
entity, err := service.pluginService.GetPlugin(name, common.STRATEGY_PLUGIN_TYPE)
if err != nil {
return nil, err
}
return service.pluginService.GetMapper().MapPluginEntityToDto(entity), nil
}
func (service *DefaultStrategyService) GetChartStrategy(chart common.Chart, name string, candles []common.Candlestick) (common.TradingStrategy, error) {
financialIndicators, err := service.indicatorService.GetChartIndicators(chart, candles)
if err != nil {
return nil, err
}
constructor, err := service.pluginService.CreateStrategy(name)
if err != nil {
return nil, err
}
trades := chart.GetTrades()
tradeLen := len(trades)
lastTrade := trades[tradeLen-1]
params := common.TradingStrategyParams{
CurrencyPair: &common.CurrencyPair{
Base: chart.GetBase(),
Quote: chart.GetQuote(),
LocalCurrency: service.ctx.GetUser().GetLocalCurrency()},
LastTrade: lastTrade,
Indicators: financialIndicators}
return constructor(¶ms)
}
func (service *DefaultStrategyService) GetChartStrategies(chart common.Chart, params *common.TradingStrategyParams,
candles []common.Candlestick) ([]common.TradingStrategy, error) {
var strategies []common.TradingStrategy
daoChart := service.chartMapper.MapChartDtoToEntity(chart)
strategyEntities, err := service.chartStrategyDAO.Find(daoChart)
if err != nil {
return nil, err
}
for _, strategyEntity := range strategyEntities {
constructor, err := service.pluginService.CreateStrategy(strategyEntity.GetName())
if err != nil {
return nil, err
}
TradingStrategy, err := constructor(params)
if err != nil {
return nil, err
}
strategies = append(strategies, TradingStrategy)
}
return strategies, nil
}