|
@@ -8,9 +8,12 @@ import (
|
|
)
|
|
)
|
|
|
|
|
|
const (
|
|
const (
|
|
- ATR_MULTIPLIER = 1.5
|
|
|
|
- ATR_PERIOD = 14
|
|
|
|
- ATR_HHV = 8
|
|
|
|
|
|
+ ATR_MULTIPLIER = 1.5
|
|
|
|
+ ATR_PERIOD = 14
|
|
|
|
+ ATR_HHV = 10
|
|
|
|
+ ATR_STOPLOSS_THRESHOLD = 1
|
|
|
|
+
|
|
|
|
+ EXTRA_POSITION_THRESHOLD = 1.002
|
|
)
|
|
)
|
|
|
|
|
|
var Strategy = alpacaQQQ{}
|
|
var Strategy = alpacaQQQ{}
|
|
@@ -64,11 +67,12 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
|
|
|
|
|
|
for side, symbol := range s.PositionSymbols() {
|
|
for side, symbol := range s.PositionSymbols() {
|
|
var (
|
|
var (
|
|
- position sentio.Position
|
|
|
|
- quote *sentio.Quote
|
|
|
|
- bars []sentio.Bar
|
|
|
|
- sl float64
|
|
|
|
- ok bool
|
|
|
|
|
|
+ position sentio.Position
|
|
|
|
+ quote *sentio.Quote
|
|
|
|
+ bars []sentio.Bar
|
|
|
|
+ stoploss float64
|
|
|
|
+ uptrending bool
|
|
|
|
+ ok bool
|
|
)
|
|
)
|
|
|
|
|
|
// no need to trade BASE quote
|
|
// no need to trade BASE quote
|
|
@@ -92,8 +96,10 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
|
|
}
|
|
}
|
|
|
|
|
|
trailing := indicator.AtrTrailingStopLoss(h, l, c, ATR_PERIOD, ATR_MULTIPLIER, ATR_HHV)
|
|
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)
|
|
|
|
|
|
+ stoploss = trailing[len(trailing)-1]
|
|
|
|
+ uptrending = trailing[len(trailing)-1] > trailing[len(trailing)-2]
|
|
|
|
+
|
|
|
|
+ fmt.Printf("ATR Trailing StopLoss: [%s: %f; uptrading: %v]\n", symbol, stoploss, uptrending)
|
|
}
|
|
}
|
|
|
|
|
|
if portfolio != nil {
|
|
if portfolio != nil {
|
|
@@ -113,7 +119,7 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
|
|
}
|
|
}
|
|
|
|
|
|
// Close position if BidPrice less than StopLoss
|
|
// Close position if BidPrice less than StopLoss
|
|
- if ok && quote.BidPrice < sl {
|
|
|
|
|
|
+ if ok && !uptrending && quote.BidPrice/stoploss < ATR_STOPLOSS_THRESHOLD {
|
|
return []sentio.StrategyOrder{{
|
|
return []sentio.StrategyOrder{{
|
|
Symbol: symbol,
|
|
Symbol: symbol,
|
|
Action: sentio.OrderSell,
|
|
Action: sentio.OrderSell,
|
|
@@ -144,7 +150,7 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
|
|
}
|
|
}
|
|
|
|
|
|
// Prevent BUYs for delayed probas
|
|
// Prevent BUYs for delayed probas
|
|
- if ts.Add(time.Minute * time.Duration(int64(s.Interval()/2))).Before(now) {
|
|
|
|
|
|
+ if ts.Add(time.Minute * time.Duration(int64(s.Interval()/2))).Before(now.Round(time.Minute)) {
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
|
|
|
|
@@ -153,20 +159,15 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
|
|
|
|
- // Prevent BUYs if BID price less than StopLoss
|
|
|
|
- if quote.BidPrice < sl {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
if sentio.LONG == side && proba > 1 {
|
|
if sentio.LONG == side && proba > 1 {
|
|
size := float64(0)
|
|
size := float64(0)
|
|
|
|
|
|
- if ok && position.GetAvgPrice()/position.GetCurrentPrice() > 1.002 {
|
|
|
|
|
|
+ if ok && position.GetAvgPrice()/position.GetCurrentPrice() > EXTRA_POSITION_THRESHOLD {
|
|
// extra position with cheaper price
|
|
// extra position with cheaper price
|
|
- size = .4
|
|
|
|
|
|
+ size = .5
|
|
} else if !ok && proba > 1.0008 {
|
|
} else if !ok && proba > 1.0008 {
|
|
// new trade position
|
|
// new trade position
|
|
- size = .6
|
|
|
|
|
|
+ size = .7
|
|
}
|
|
}
|
|
|
|
|
|
if size > 0 {
|
|
if size > 0 {
|
|
@@ -183,12 +184,12 @@ func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]
|
|
if sentio.SHORT == side && proba < 1 {
|
|
if sentio.SHORT == side && proba < 1 {
|
|
size := float64(0)
|
|
size := float64(0)
|
|
|
|
|
|
- if ok && position.GetAvgPrice()/position.GetCurrentPrice() > 1.002 {
|
|
|
|
|
|
+ if ok && position.GetAvgPrice()/position.GetCurrentPrice() > EXTRA_POSITION_THRESHOLD {
|
|
// extra position with cheaper price
|
|
// extra position with cheaper price
|
|
- size = .4
|
|
|
|
|
|
+ size = .5
|
|
} else if !ok && proba < .9990 {
|
|
} else if !ok && proba < .9990 {
|
|
// new trade position
|
|
// new trade position
|
|
- size = .6
|
|
|
|
|
|
+ size = .7
|
|
}
|
|
}
|
|
|
|
|
|
if size > 0 {
|
|
if size > 0 {
|