123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package main
- import (
- "errors"
- "git.beejay.kim/Gshopper/sentio"
- )
- var Strategy = alpacaQQQ{}
- type alpacaQQQ struct{}
- func (s alpacaQQQ) Name() string {
- return "Alpaca: QQQ"
- }
- func (s alpacaQQQ) Model() string {
- return "qqq001"
- }
- func (s alpacaQQQ) MarketId() string {
- return "alpaca"
- }
- func (s alpacaQQQ) PositionSymbols() map[sentio.Side]string {
- return map[sentio.Side]string{
- sentio.LONG: "QQQ",
- }
- }
- func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.StrategyOrder, error) {
- if !market.IsMarketOpened() {
- return nil, sentio.ErrMarketClosed
- }
- var (
- symbol string
- portfolio sentio.Portfolio
- ok bool
- err error
- )
- if portfolio, err = market.Portfolio(); err != nil {
- return nil, err
- }
- if symbol, ok = s.PositionSymbols()[sentio.LONG]; !ok {
- return nil, errors.New("incorrect strategy")
- }
- var (
- position sentio.Position
- act sentio.OrderAction
- t = market.Time().Now()
- )
- if portfolio != nil {
- position, ok = portfolio.Get(symbol)
- ok = ok && position.GetSize() != 0
- } else {
- ok = false
- }
- // Close positions before market closed
- if ok && t.Hour() >= 19 && t.Minute() >= 30 {
- act = sentio.OrderSell
- if position.GetSize() < 0 {
- act = sentio.OrderBuy
- }
- return []sentio.StrategyOrder{
- {
- Symbol: position.GetSymbol(),
- Action: act,
- Ratio: 1,
- },
- }, nil
- }
- if proba < 0 {
- return nil, nil
- }
- // Close LONG position
- if ok && position.GetSize() > 0 && proba < 1 {
- return []sentio.StrategyOrder{
- {
- Symbol: position.GetSymbol(),
- Action: sentio.OrderSell,
- Ratio: 1,
- },
- }, nil
- }
- // Close SHORT position
- if ok && position.GetSize() < 0 && proba > 1 {
- return []sentio.StrategyOrder{
- {
- Symbol: position.GetSymbol(),
- Action: sentio.OrderBuy,
- Ratio: 1,
- },
- }, nil
- }
- if t.Hour() == 19 && t.Minute() >= 30 {
- return nil, nil
- }
- if proba > 1.002 {
- return []sentio.StrategyOrder{
- {
- Symbol: symbol,
- Action: sentio.OrderBuy,
- Ratio: func() float64 {
- if ok {
- return .3
- } else {
- return .6
- }
- }(),
- },
- }, nil
- }
- if proba < .998 {
- return []sentio.StrategyOrder{
- {
- Symbol: symbol,
- Action: sentio.OrderSell,
- Ratio: func() float64 {
- if ok {
- return .3
- } else {
- return .6
- }
- }(),
- },
- }, nil
- }
- return nil, nil
- }
|