Skip to content

Commit

Permalink
feat(strategy): include turtle trading strategy (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunebakan authored Mar 19, 2022
1 parent 5bb2fda commit 985ba6e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/strategies/emacross.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (e *CrossEMA) OnCandle(df *ninjabot.Dataframe, broker service.Broker) {
assetPosition, quotePosition, err := broker.Position(df.Pair)
if err != nil {
log.Error(err)
return
}

if quotePosition > 10 && df.Metadata["ema8"].Crossover(df.Metadata["ema21"]) {
Expand Down
1 change: 1 addition & 0 deletions examples/strategies/ocosell.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func (e *OCOSell) OnCandle(df *model.Dataframe, broker service.Broker) {
assetPosition, quotePosition, err := broker.Position(df.Pair)
if err != nil {
log.Error(err)
return
}

buyAmount := 4000.0
Expand Down
66 changes: 66 additions & 0 deletions examples/strategies/turtle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package strategies

import (
"github.com/rodrigo-brito/ninjabot"
"github.com/rodrigo-brito/ninjabot/service"

"github.com/markcheno/go-talib"
log "github.com/sirupsen/logrus"
)

// https://www.investopedia.com/articles/trading/08/turtle-trading.asp
type Turtle struct{}

func (e Turtle) Timeframe() string {
return "4h"
}

func (e Turtle) WarmupPeriod() int {
return 40
}

func (e Turtle) Indicators(df *ninjabot.Dataframe) {
df.Metadata["turtleHighest"] = talib.Max(df.Close, 40)
df.Metadata["turtleLowest"] = talib.Min(df.Close, 20)
}

func (e *Turtle) OnCandle(df *ninjabot.Dataframe, broker service.Broker) {
closePrice := df.Close.Last(0)
highest := df.Metadata["turtleHighest"].Last(0)
lowest := df.Metadata["turtleLowest"].Last(0)

assetPosition, quotePosition, err := broker.Position(df.Pair)
if err != nil {
log.Error(err)
return
}

// If position already open wait till it will be closed
if assetPosition == 0 && closePrice >= highest {
_, err := broker.CreateOrderMarketQuote(ninjabot.SideTypeBuy, df.Pair, quotePosition/2)
if err != nil {
log.WithFields(map[string]interface{}{
"pair": df.Pair,
"side": ninjabot.SideTypeBuy,
"close": closePrice,
"asset": assetPosition,
"quote": quotePosition,
}).Error(err)
}
return
}

if assetPosition > 0 && closePrice <= lowest {
_, err := broker.CreateOrderMarket(ninjabot.SideTypeSell, df.Pair, assetPosition)
if err != nil {
log.WithFields(map[string]interface{}{
"pair": df.Pair,
"side": ninjabot.SideTypeSell,
"close": closePrice,
"asset": assetPosition,
"quote": quotePosition,
"size": assetPosition,
}).Error(err)
}
}
}

0 comments on commit 985ba6e

Please sign in to comment.