|
@@ -10,7 +10,7 @@ import (
|
|
|
const (
|
|
|
ATR_MULTIPLIER = 1.5
|
|
|
ATR_PERIOD = 14
|
|
|
- ATR_HHV = 10
|
|
|
+ ATR_HHV = 8
|
|
|
)
|
|
|
|
|
|
var Strategy = alpacaQQQ{}
|
|
@@ -65,6 +65,9 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
|
|
|
for side, symbol := range s.PositionSymbols() {
|
|
|
var (
|
|
|
position sentio.Position
|
|
|
+ quote sentio.Quote
|
|
|
+ bars []sentio.Bar
|
|
|
+ sl float64
|
|
|
ok bool
|
|
|
)
|
|
|
|
|
@@ -73,6 +76,26 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
+ if quote, err = market.Quote(symbol); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if bars, err = market.HistoricalBars(symbol, time.Minute, nil); err == nil && len(bars) > 0 {
|
|
|
+ h := make([]float64, len(bars))
|
|
|
+ l := make([]float64, len(bars))
|
|
|
+ c := make([]float64, len(bars))
|
|
|
+
|
|
|
+ for i := range bars {
|
|
|
+ h[i] = bars[i].High
|
|
|
+ l[i] = bars[i].Low
|
|
|
+ c[i] = bars[i].Close
|
|
|
+ }
|
|
|
+
|
|
|
+ trailing := indicator.AtrTrailingStopLoss(h, l, c, ATR_PERIOD, ATR_MULTIPLIER, ATR_HHV)
|
|
|
+ sl = trailing[len(trailing)-1]
|
|
|
+ fmt.Printf("ATR Trailing StopLoss: [%f]\n", sl)
|
|
|
+ }
|
|
|
+
|
|
|
if portfolio != nil {
|
|
|
position, ok = portfolio.Get(symbol)
|
|
|
ok = ok && position != nil && position.GetSize() != 0
|
|
@@ -80,36 +103,6 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
|
|
|
ok = false
|
|
|
}
|
|
|
|
|
|
- if ok {
|
|
|
- var (
|
|
|
- sl []float64
|
|
|
- bars []sentio.Bar
|
|
|
- )
|
|
|
-
|
|
|
- if bars, err = market.HistoricalBars(symbol, time.Minute, nil); err == nil && len(bars) > 0 {
|
|
|
- h := make([]float64, len(bars))
|
|
|
- l := make([]float64, len(bars))
|
|
|
- c := make([]float64, len(bars))
|
|
|
-
|
|
|
- for i := range bars {
|
|
|
- h[i] = bars[i].High
|
|
|
- l[i] = bars[i].Low
|
|
|
- c[i] = bars[i].Close
|
|
|
- }
|
|
|
-
|
|
|
- sl = indicator.AtrTrailingStopLoss(h, l, c, ATR_PERIOD, ATR_MULTIPLIER, ATR_HHV)
|
|
|
- fmt.Printf("ATR Trailing StopLoss: [%f]\n", sl[len(sl)-1])
|
|
|
-
|
|
|
- if position.GetCurrentPrice() < sl[len(sl)-1] {
|
|
|
- return []sentio.StrategyOrder{{
|
|
|
- Symbol: symbol,
|
|
|
- Action: sentio.OrderSell,
|
|
|
- Ratio: 1,
|
|
|
- }}, nil
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
// Close positions before market closed
|
|
|
if ok && now.Hour() == 15 && now.Minute() > 55 {
|
|
|
return []sentio.StrategyOrder{{
|
|
@@ -119,6 +112,15 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
|
|
|
}}, nil
|
|
|
}
|
|
|
|
|
|
+ // Close position if BidPrice less than StopLoss
|
|
|
+ if ok && quote.BidPrice < sl {
|
|
|
+ return []sentio.StrategyOrder{{
|
|
|
+ Symbol: symbol,
|
|
|
+ Action: sentio.OrderSell,
|
|
|
+ Ratio: 1,
|
|
|
+ }}, nil
|
|
|
+ }
|
|
|
+
|
|
|
if proba < 0 {
|
|
|
continue
|
|
|
}
|
|
@@ -151,6 +153,11 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
+ // Prevent BUYs if BID price less than StopLoss
|
|
|
+ if quote.BidPrice < sl {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
if sentio.LONG == side && proba > 1 {
|
|
|
size := float64(0)
|
|
|
|