2022-05-10 09:11:24 +00:00
package pivotshort
2022-05-09 21:11:22 +00:00
import (
"context"
"fmt"
2022-06-09 18:39:14 +00:00
"os"
2022-06-10 16:26:44 +00:00
"sort"
2022-06-09 16:49:32 +00:00
"sync"
2022-06-09 03:30:24 +00:00
2022-06-04 17:09:31 +00:00
"github.com/sirupsen/logrus"
2022-05-09 21:11:22 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
2022-06-30 10:29:02 +00:00
"github.com/c9s/bbgo/pkg/dynamic"
2022-05-09 21:11:22 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
2022-05-13 10:05:25 +00:00
"github.com/c9s/bbgo/pkg/indicator"
2022-05-09 21:11:22 +00:00
"github.com/c9s/bbgo/pkg/types"
)
2022-05-10 09:11:24 +00:00
const ID = "pivotshort"
2022-05-09 21:11:22 +00:00
2022-06-26 08:13:58 +00:00
var one = fixedpoint . One
var zero = fixedpoint . Zero
2022-05-09 21:11:22 +00:00
var log = logrus . WithField ( "strategy" , ID )
func init ( ) {
bbgo . RegisterStrategy ( ID , & Strategy { } )
}
type IntervalWindowSetting struct {
types . IntervalWindow
}
2022-06-09 09:36:22 +00:00
// BreakLow -- when price breaks the previous pivot low, we set a trade entry
type BreakLow struct {
2022-06-25 10:13:50 +00:00
// Ratio is a number less than 1.0, price * ratio will be the price triggers the short order.
Ratio fixedpoint . Value ` json:"ratio" `
// MarketOrder is the option to enable market order short.
MarketOrder bool ` json:"marketOrder" `
// BounceRatio is a ratio used for placing the limit order sell price
// limit sell price = breakLowPrice * (1 + BounceRatio)
BounceRatio fixedpoint . Value ` json:"bounceRatio" `
2022-06-09 10:16:32 +00:00
Quantity fixedpoint . Value ` json:"quantity" `
StopEMARange fixedpoint . Value ` json:"stopEMARange" `
StopEMA * types . IntervalWindow ` json:"stopEMA" `
2022-06-09 09:36:22 +00:00
}
2022-06-29 09:58:43 +00:00
type ResistanceShort struct {
2022-06-30 10:29:02 +00:00
Enabled bool ` json:"enabled" `
Symbol string ` json:"-" `
Market types . Market ` json:"-" `
2022-06-11 08:41:56 +00:00
2022-06-10 16:26:44 +00:00
types . IntervalWindow
MinDistance fixedpoint . Value ` json:"minDistance" `
NumLayers int ` json:"numLayers" `
LayerSpread fixedpoint . Value ` json:"layerSpread" `
Quantity fixedpoint . Value ` json:"quantity" `
Ratio fixedpoint . Value ` json:"ratio" `
2022-06-30 10:29:02 +00:00
session * bbgo . ExchangeSession
orderExecutor * bbgo . GeneralOrderExecutor
resistancePivot * indicator . Pivot
resistancePrices [ ] float64
nextResistancePrice fixedpoint . Value
2022-06-30 16:57:19 +00:00
activeOrders * bbgo . ActiveOrderBook
2022-06-30 10:29:02 +00:00
}
func ( s * ResistanceShort ) Bind ( session * bbgo . ExchangeSession , orderExecutor * bbgo . GeneralOrderExecutor ) {
s . session = session
s . orderExecutor = orderExecutor
2022-06-30 16:57:19 +00:00
s . activeOrders = bbgo . NewActiveOrderBook ( s . Symbol )
s . activeOrders . BindStream ( session . UserDataStream )
2022-06-30 10:29:02 +00:00
2022-06-30 16:57:19 +00:00
store , _ := session . MarketDataStore ( s . Symbol )
2022-06-30 10:29:02 +00:00
s . resistancePivot = & indicator . Pivot { IntervalWindow : s . IntervalWindow }
s . resistancePivot . Bind ( store )
// preload history kline data to the resistance pivot indicator
// we use the last kline to find the higher lows
lastKLine := preloadPivot ( s . resistancePivot , store )
// use the last kline from the history before we get the next closed kline
2022-06-30 16:57:19 +00:00
if lastKLine != nil {
s . findNextResistancePriceAndPlaceOrders ( lastKLine . Close )
}
2022-06-30 10:29:02 +00:00
2022-06-30 17:06:10 +00:00
session . MarketDataStream . OnKLineClosed ( types . KLineWith ( s . Symbol , s . Interval , func ( kline types . KLine ) {
2022-06-30 16:57:19 +00:00
position := s . orderExecutor . Position ( )
if position . IsOpened ( kline . Close ) {
return
}
2022-06-30 10:29:02 +00:00
s . findNextResistancePriceAndPlaceOrders ( kline . Close )
2022-06-30 17:06:10 +00:00
} ) )
2022-06-10 16:26:44 +00:00
}
2022-06-30 10:29:02 +00:00
func ( s * ResistanceShort ) findNextResistancePriceAndPlaceOrders ( closePrice fixedpoint . Value ) {
minDistance := s . MinDistance . Float64 ( )
lows := s . resistancePivot . Lows
resistancePrices := findPossibleResistancePrices ( closePrice . Float64 ( ) , minDistance , lows )
log . Infof ( "last price: %f, possible resistance prices: %+v" , closePrice . Float64 ( ) , resistancePrices )
ctx := context . Background ( )
if len ( resistancePrices ) > 0 {
nextResistancePrice := fixedpoint . NewFromFloat ( resistancePrices [ 0 ] )
if nextResistancePrice . Compare ( s . nextResistancePrice ) != 0 {
2022-06-30 16:57:19 +00:00
bbgo . Notify ( "Found next resistance price: %f" , nextResistancePrice . Float64 ( ) )
2022-06-30 10:29:02 +00:00
s . nextResistancePrice = nextResistancePrice
s . placeResistanceOrders ( ctx , nextResistancePrice )
}
}
}
func ( s * ResistanceShort ) placeResistanceOrders ( ctx context . Context , resistancePrice fixedpoint . Value ) {
futuresMode := s . session . Futures || s . session . IsolatedFutures
_ = futuresMode
totalQuantity := s . Quantity
numLayers := s . NumLayers
if numLayers == 0 {
numLayers = 1
}
numLayersF := fixedpoint . NewFromInt ( int64 ( numLayers ) )
layerSpread := s . LayerSpread
quantity := totalQuantity . Div ( numLayersF )
2022-06-30 16:57:19 +00:00
if err := s . orderExecutor . CancelOrders ( ctx , s . activeOrders . Orders ( ) ... ) ; err != nil {
log . WithError ( err ) . Errorf ( "can not cancel resistance orders: %+v" , s . activeOrders . Orders ( ) )
2022-06-30 10:29:02 +00:00
}
log . Infof ( "placing resistance orders: resistance price = %f, layer quantity = %f, num of layers = %d" , resistancePrice . Float64 ( ) , quantity . Float64 ( ) , numLayers )
var orderForms [ ] types . SubmitOrder
for i := 0 ; i < numLayers ; i ++ {
balances := s . session . GetAccount ( ) . Balances ( )
quoteBalance := balances [ s . Market . QuoteCurrency ]
baseBalance := balances [ s . Market . BaseCurrency ]
_ = quoteBalance
_ = baseBalance
// price = (resistance_price * (1.0 + ratio)) * ((1.0 + layerSpread) * i)
price := resistancePrice . Mul ( fixedpoint . One . Add ( s . Ratio ) )
spread := layerSpread . Mul ( fixedpoint . NewFromInt ( int64 ( i ) ) )
price = price . Add ( spread )
log . Infof ( "price = %f" , price . Float64 ( ) )
log . Infof ( "placing bounce short order #%d: price = %f, quantity = %f" , i , price . Float64 ( ) , quantity . Float64 ( ) )
orderForms = append ( orderForms , types . SubmitOrder {
Symbol : s . Symbol ,
Side : types . SideTypeSell ,
Type : types . OrderTypeLimitMaker ,
Price : price ,
Quantity : quantity ,
Tag : "resistanceShort" ,
} )
2022-06-09 03:30:24 +00:00
2022-06-30 10:29:02 +00:00
// TODO: fix futures mode later
/ *
if futuresMode {
if quantity . Mul ( price ) . Compare ( quoteBalance . Available ) <= 0 {
}
}
* /
}
createdOrders , err := s . orderExecutor . SubmitOrders ( ctx , orderForms ... )
if err != nil {
log . WithError ( err ) . Errorf ( "can not place resistance order" )
}
2022-06-30 16:57:19 +00:00
s . activeOrders . Add ( createdOrders ... )
2022-06-03 08:38:06 +00:00
}
2022-05-09 21:11:22 +00:00
type Strategy struct {
2022-06-05 23:29:25 +00:00
Environment * bbgo . Environment
Symbol string ` json:"symbol" `
Market types . Market
2022-06-10 07:34:57 +00:00
// pivot interval and window
types . IntervalWindow
2022-05-12 11:27:57 +00:00
// persistence fields
2022-06-18 08:31:53 +00:00
Position * types . Position ` persistence:"position" `
ProfitStats * types . ProfitStats ` persistence:"profit_stats" `
TradeStats * types . TradeStats ` persistence:"trade_stats" `
2022-05-09 21:11:22 +00:00
2022-06-30 10:29:02 +00:00
// BreakLow is one of the entry method
2022-06-09 09:36:22 +00:00
BreakLow BreakLow ` json:"breakLow" `
2022-06-10 16:26:44 +00:00
2022-06-30 10:29:02 +00:00
// ResistanceShort is one of the entry method
2022-06-29 09:58:43 +00:00
ResistanceShort * ResistanceShort ` json:"resistanceShort" `
2022-06-10 16:26:44 +00:00
2022-06-30 07:13:42 +00:00
ExitMethods bbgo . ExitMethodSet ` json:"exits" `
2022-05-09 21:11:22 +00:00
2022-06-18 03:45:24 +00:00
session * bbgo . ExchangeSession
2022-06-18 08:31:53 +00:00
orderExecutor * bbgo . GeneralOrderExecutor
2022-05-09 21:11:22 +00:00
2022-06-30 16:57:19 +00:00
lastLow fixedpoint . Value
pivot * indicator . Pivot
resistancePivot * indicator . Pivot
stopEWMA * indicator . EWMA
pivotLowPrices [ ] fixedpoint . Value
2022-05-12 11:27:57 +00:00
// StrategyController
bbgo . StrategyController
2022-05-09 21:11:22 +00:00
}
func ( s * Strategy ) ID ( ) string {
return ID
}
func ( s * Strategy ) Subscribe ( session * bbgo . ExchangeSession ) {
2022-05-19 01:48:36 +00:00
session . Subscribe ( types . KLineChannel , s . Symbol , types . SubscribeOptions { Interval : s . Interval } )
2022-06-05 23:29:25 +00:00
session . Subscribe ( types . KLineChannel , s . Symbol , types . SubscribeOptions { Interval : types . Interval1m } )
2022-06-10 16:26:44 +00:00
2022-06-29 09:58:43 +00:00
if s . ResistanceShort != nil && s . ResistanceShort . Enabled {
2022-06-30 10:29:02 +00:00
dynamic . InheritStructValues ( s . ResistanceShort , s )
2022-06-29 09:58:43 +00:00
session . Subscribe ( types . KLineChannel , s . Symbol , types . SubscribeOptions { Interval : s . ResistanceShort . Interval } )
2022-06-10 16:26:44 +00:00
}
2022-06-26 11:06:16 +00:00
if ! bbgo . IsBackTesting {
session . Subscribe ( types . MarketTradeChannel , s . Symbol , types . SubscribeOptions { } )
}
2022-06-29 09:58:43 +00:00
2022-06-30 07:13:42 +00:00
s . ExitMethods . SetAndSubscribe ( session , s )
2022-06-05 23:29:25 +00:00
}
2022-06-22 05:45:48 +00:00
func ( s * Strategy ) InstanceID ( ) string {
return fmt . Sprintf ( "%s:%s" , ID , s . Symbol )
}
2022-06-20 03:39:18 +00:00
func ( s * Strategy ) CurrentPosition ( ) * types . Position {
return s . Position
}
2022-05-09 21:11:22 +00:00
func ( s * Strategy ) ClosePosition ( ctx context . Context , percentage fixedpoint . Value ) error {
2022-06-22 05:45:48 +00:00
return s . orderExecutor . ClosePosition ( ctx , percentage )
2022-05-12 11:27:57 +00:00
}
2022-05-09 21:11:22 +00:00
func ( s * Strategy ) Run ( ctx context . Context , orderExecutor bbgo . OrderExecutor , session * bbgo . ExchangeSession ) error {
2022-06-18 03:45:24 +00:00
var instanceID = s . InstanceID ( )
2022-05-09 21:11:22 +00:00
if s . Position == nil {
s . Position = types . NewPositionFromMarket ( s . Market )
}
2022-06-04 17:48:56 +00:00
if s . ProfitStats == nil {
s . ProfitStats = types . NewProfitStats ( s . Market )
}
2022-06-09 16:49:32 +00:00
if s . TradeStats == nil {
2022-06-18 08:31:53 +00:00
s . TradeStats = & types . TradeStats { }
2022-06-09 16:49:32 +00:00
}
2022-06-26 11:06:16 +00:00
s . lastLow = fixedpoint . Zero
2022-06-20 03:39:18 +00:00
// StrategyController
s . Status = types . StrategyStatusRunning
s . OnSuspend ( func ( ) {
// Cancel active orders
_ = s . orderExecutor . GracefulCancel ( ctx )
} )
s . OnEmergencyStop ( func ( ) {
// Cancel active orders
_ = s . orderExecutor . GracefulCancel ( ctx )
// Close 100% position
_ = s . ClosePosition ( ctx , fixedpoint . One )
} )
2022-06-18 07:27:11 +00:00
// initial required information
s . session = session
2022-06-18 08:31:53 +00:00
s . orderExecutor = bbgo . NewGeneralOrderExecutor ( session , s . Symbol , ID , instanceID , s . Position )
s . orderExecutor . BindEnvironment ( s . Environment )
2022-06-19 04:29:36 +00:00
s . orderExecutor . BindProfitStats ( s . ProfitStats )
2022-06-18 08:32:53 +00:00
s . orderExecutor . BindTradeStats ( s . TradeStats )
2022-06-21 07:57:26 +00:00
s . orderExecutor . TradeCollector ( ) . OnPositionUpdate ( func ( position * types . Position ) {
bbgo . Sync ( s )
} )
2022-06-19 05:01:22 +00:00
s . orderExecutor . Bind ( )
2022-05-09 21:11:22 +00:00
2022-06-09 04:34:12 +00:00
store , _ := session . MarketDataStore ( s . Symbol )
2022-06-27 11:48:14 +00:00
standardIndicator , _ := session . StandardIndicatorSet ( s . Symbol )
2022-06-10 16:26:44 +00:00
2022-06-10 07:34:57 +00:00
s . pivot = & indicator . Pivot { IntervalWindow : s . IntervalWindow }
2022-06-09 04:34:12 +00:00
s . pivot . Bind ( store )
2022-06-30 10:29:02 +00:00
preloadPivot ( s . pivot , store )
2022-06-27 11:48:14 +00:00
// update pivot low data
2022-06-30 17:06:10 +00:00
session . MarketDataStream . OnKLineClosed ( types . KLineWith ( s . Symbol , s . Interval , func ( kline types . KLine ) {
2022-06-27 11:48:14 +00:00
lastLow := fixedpoint . NewFromFloat ( s . pivot . LastLow ( ) )
if lastLow . IsZero ( ) {
return
}
if lastLow . Compare ( s . lastLow ) != 0 {
log . Infof ( "new pivot low detected: %f %s" , s . pivot . LastLow ( ) , kline . EndTime . Time ( ) )
}
s . lastLow = lastLow
s . pivotLowPrices = append ( s . pivotLowPrices , s . lastLow )
2022-06-30 17:06:10 +00:00
} ) )
2022-05-09 21:11:22 +00:00
2022-06-09 10:16:32 +00:00
if s . BreakLow . StopEMA != nil {
2022-06-10 07:18:12 +00:00
s . stopEWMA = standardIndicator . EWMA ( * s . BreakLow . StopEMA )
2022-06-09 10:16:32 +00:00
}
2022-06-26 11:06:16 +00:00
for _ , method := range s . ExitMethods {
method . Bind ( session , s . orderExecutor )
}
2022-06-29 09:58:43 +00:00
if s . ResistanceShort != nil && s . ResistanceShort . Enabled {
2022-06-30 10:29:02 +00:00
s . ResistanceShort . Bind ( session , s . orderExecutor )
2022-06-26 11:45:37 +00:00
}
2022-05-09 21:11:22 +00:00
2022-06-05 23:29:25 +00:00
// Always check whether you can open a short position or not
2022-06-30 17:06:10 +00:00
session . MarketDataStream . OnKLineClosed ( types . KLineWith ( s . Symbol , s . Interval , func ( kline types . KLine ) {
2022-06-20 03:39:18 +00:00
if s . Status != types . StrategyStatusRunning {
return
}
2022-06-30 17:06:10 +00:00
if s . Position . IsOpened ( kline . Close ) {
2022-06-26 08:31:48 +00:00
return
2022-06-05 04:55:36 +00:00
}
2022-06-09 09:36:22 +00:00
if len ( s . pivotLowPrices ) == 0 {
2022-06-26 08:13:58 +00:00
log . Infof ( "currently there is no pivot low prices, skip placing orders..." )
2022-06-09 09:36:22 +00:00
return
}
previousLow := s . pivotLowPrices [ len ( s . pivotLowPrices ) - 1 ]
// truncate the pivot low prices
if len ( s . pivotLowPrices ) > 10 {
s . pivotLowPrices = s . pivotLowPrices [ len ( s . pivotLowPrices ) - 10 : ]
}
2022-06-27 11:48:14 +00:00
ratio := fixedpoint . One . Add ( s . BreakLow . Ratio )
breakPrice := previousLow . Mul ( ratio )
2022-06-28 10:50:30 +00:00
openPrice := kline . Open
2022-06-27 11:48:14 +00:00
closePrice := kline . Close
// if previous low is not break, skip
if closePrice . Compare ( breakPrice ) >= 0 {
return
}
2022-06-28 10:50:30 +00:00
// we need the price cross the break line
// or we do nothing
if ! ( openPrice . Compare ( breakPrice ) > 0 && closePrice . Compare ( breakPrice ) < 0 ) {
return
}
2022-06-27 11:48:14 +00:00
log . Infof ( "%s breakLow signal detected, closed price %f < breakPrice %f" , kline . Symbol , closePrice . Float64 ( ) , breakPrice . Float64 ( ) )
2022-06-27 10:17:57 +00:00
// stop EMA protection
2022-06-29 08:59:50 +00:00
if s . stopEWMA != nil {
2022-06-10 07:18:12 +00:00
ema := fixedpoint . NewFromFloat ( s . stopEWMA . Last ( ) )
2022-06-09 10:16:32 +00:00
if ema . IsZero ( ) {
return
}
emaStopShortPrice := ema . Mul ( fixedpoint . One . Sub ( s . BreakLow . StopEMARange ) )
2022-06-27 11:48:14 +00:00
if closePrice . Compare ( emaStopShortPrice ) < 0 {
log . Infof ( "stopEMA protection: close price %f < EMA(%v) = %f" , closePrice . Float64 ( ) , s . BreakLow . StopEMA , ema . Float64 ( ) )
2022-06-09 10:16:32 +00:00
return
}
}
2022-06-18 04:30:42 +00:00
_ = s . orderExecutor . GracefulCancel ( ctx )
2022-06-09 09:36:22 +00:00
2022-06-10 03:36:04 +00:00
quantity := s . useQuantityOrBaseBalance ( s . BreakLow . Quantity )
if s . BreakLow . MarketOrder {
2022-06-19 04:29:36 +00:00
bbgo . Notify ( "%s price %f breaks the previous low %f with ratio %f, submitting market sell to open a short position" , s . Symbol , kline . Close . Float64 ( ) , previousLow . Float64 ( ) , s . BreakLow . Ratio . Float64 ( ) )
2022-06-28 10:50:30 +00:00
s . placeMarketSell ( ctx , quantity , "breakLowMarket" )
2022-06-10 03:36:04 +00:00
} else {
2022-06-28 14:17:55 +00:00
sellPrice := previousLow . Mul ( fixedpoint . One . Add ( s . BreakLow . BounceRatio ) )
2022-06-27 10:17:57 +00:00
bbgo . Notify ( "%s price %f breaks the previous low %f with ratio %f, submitting limit sell @ %f" , s . Symbol , kline . Close . Float64 ( ) , previousLow . Float64 ( ) , s . BreakLow . Ratio . Float64 ( ) , sellPrice . Float64 ( ) )
2022-06-28 10:50:30 +00:00
s . placeLimitSell ( ctx , sellPrice , quantity , "breakLowLimit" )
2022-06-10 03:36:04 +00:00
}
2022-06-30 17:06:10 +00:00
} ) )
2022-06-05 04:48:54 +00:00
2022-06-27 10:17:57 +00:00
if ! bbgo . IsBackTesting {
// use market trade to submit short order
session . MarketDataStream . OnMarketTrade ( func ( trade types . Trade ) {
} )
}
2022-06-30 05:48:04 +00:00
bbgo . OnShutdown ( func ( ctx context . Context , wg * sync . WaitGroup ) {
2022-06-09 18:39:14 +00:00
_ , _ = fmt . Fprintln ( os . Stderr , s . TradeStats . String ( ) )
2022-06-09 16:49:32 +00:00
wg . Done ( )
} )
2022-05-09 21:11:22 +00:00
return nil
}
2022-06-09 03:30:24 +00:00
func ( s * Strategy ) findHigherPivotLow ( price fixedpoint . Value ) ( fixedpoint . Value , bool ) {
for l := len ( s . pivotLowPrices ) - 1 ; l > 0 ; l -- {
if s . pivotLowPrices [ l ] . Compare ( price ) > 0 {
return s . pivotLowPrices [ l ] , true
}
}
return price , false
}
2022-06-18 07:27:11 +00:00
func ( s * Strategy ) placeOrder ( ctx context . Context , price fixedpoint . Value , quantity fixedpoint . Value ) {
2022-06-19 07:57:59 +00:00
_ , _ = s . orderExecutor . SubmitOrders ( ctx , types . SubmitOrder {
2022-06-09 03:30:24 +00:00
Symbol : s . Symbol ,
Side : types . SideTypeSell ,
2022-06-25 10:13:50 +00:00
Type : types . OrderTypeLimitMaker ,
2022-06-10 16:26:44 +00:00
Price : price ,
Quantity : quantity ,
2022-06-18 07:27:11 +00:00
} )
2022-06-10 16:26:44 +00:00
}
2022-06-09 03:30:24 +00:00
2022-06-29 09:59:11 +00:00
func ( s * Strategy ) useQuantityOrBaseBalance ( quantity fixedpoint . Value ) fixedpoint . Value {
balance , hasBalance := s . session . Account . Balance ( s . Market . BaseCurrency )
if hasBalance {
if quantity . IsZero ( ) {
bbgo . Notify ( "sell quantity is not set, submitting sell with all base balance: %s" , balance . Available . String ( ) )
quantity = balance . Available
} else {
quantity = fixedpoint . Min ( quantity , balance . Available )
}
}
if quantity . IsZero ( ) {
log . Errorf ( "quantity is zero, can not submit sell order, please check settings" )
}
return quantity
}
func ( s * Strategy ) placeLimitSell ( ctx context . Context , price , quantity fixedpoint . Value , tag string ) {
_ , _ = s . orderExecutor . SubmitOrders ( ctx , types . SubmitOrder {
Symbol : s . Symbol ,
Price : price ,
Side : types . SideTypeSell ,
Type : types . OrderTypeLimit ,
Quantity : quantity ,
MarginSideEffect : types . SideEffectTypeMarginBuy ,
Tag : tag ,
} )
}
func ( s * Strategy ) placeMarketSell ( ctx context . Context , quantity fixedpoint . Value , tag string ) {
_ , _ = s . orderExecutor . SubmitOrders ( ctx , types . SubmitOrder {
Symbol : s . Symbol ,
Side : types . SideTypeSell ,
Type : types . OrderTypeMarket ,
Quantity : quantity ,
MarginSideEffect : types . SideEffectTypeMarginBuy ,
Tag : tag ,
} )
}
2022-06-29 09:59:33 +00:00
func findPossibleResistancePrices ( closePrice float64 , minDistance float64 , lows [ ] float64 ) [ ] float64 {
// sort float64 in increasing order
2022-06-30 10:29:02 +00:00
// lower to higher prices
2022-06-29 09:59:33 +00:00
sort . Float64s ( lows )
var resistancePrices [ ] float64
for _ , low := range lows {
if low < closePrice {
continue
}
last := closePrice
if len ( resistancePrices ) > 0 {
last = resistancePrices [ len ( resistancePrices ) - 1 ]
}
if ( low / last ) < ( 1.0 + minDistance ) {
continue
}
resistancePrices = append ( resistancePrices , low )
}
return resistancePrices
}
2022-06-29 10:00:50 +00:00
func preloadPivot ( pivot * indicator . Pivot , store * bbgo . MarketDataStore ) * types . KLine {
klines , ok := store . KLinesOfInterval ( pivot . Interval )
if ! ok {
return nil
}
last := ( * klines ) [ len ( * klines ) - 1 ]
log . Debugf ( "updating pivot indicator: %d klines" , len ( * klines ) )
for i := pivot . Window ; i < len ( * klines ) ; i ++ {
pivot . Update ( ( * klines ) [ 0 : i + 1 ] )
}
log . Debugf ( "found %v previous lows: %v" , pivot . IntervalWindow , pivot . Lows )
log . Debugf ( "found %v previous highs: %v" , pivot . IntervalWindow , pivot . Highs )
return & last
}