-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backtesting): include OCO/Limit order support (#6)
- Loading branch information
1 parent
9b7c13e
commit 6b4b9b8
Showing
27 changed files
with
1,004 additions
and
329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package strategies | ||
|
||
import ( | ||
"github.com/rodrigo-brito/ninjabot/pkg/exchange" | ||
"github.com/rodrigo-brito/ninjabot/pkg/model" | ||
|
||
"github.com/markcheno/go-talib" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type OCOSell struct{} | ||
|
||
func (e OCOSell) Init(settings model.Settings) {} | ||
|
||
func (e OCOSell) Timeframe() string { | ||
return "1d" | ||
} | ||
|
||
func (e OCOSell) WarmupPeriod() int { | ||
return 9 | ||
} | ||
|
||
func (e OCOSell) Indicators(df *model.Dataframe) { | ||
df.Metadata["stoch"], df.Metadata["stoch_signal"] = talib.Stoch( | ||
df.High, | ||
df.Low, | ||
df.Close, | ||
8, | ||
3, | ||
talib.SMA, | ||
3, | ||
talib.SMA, | ||
) | ||
} | ||
|
||
func (e *OCOSell) OnCandle(df *model.Dataframe, broker exchange.Broker) { | ||
closePrice := df.Close.Last(0) | ||
log.Info("New Candle = ", df.Pair, df.LastUpdate, closePrice) | ||
|
||
assetPosition, quotePosition, err := broker.Position(df.Pair) | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
|
||
buyAmount := 4000.0 | ||
if quotePosition > buyAmount && df.Metadata["stoch"].Crossover(df.Metadata["stoch_signal"]) { | ||
size := buyAmount / closePrice | ||
_, err := broker.OrderMarket(model.SideTypeBuy, df.Pair, size) | ||
if err != nil { | ||
log.WithFields(map[string]interface{}{ | ||
"pair": df.Pair, | ||
"side": model.SideTypeBuy, | ||
"close": closePrice, | ||
"asset": assetPosition, | ||
"quote": quotePosition, | ||
"size": size, | ||
}).Error(err) | ||
} | ||
|
||
_, err = broker.OrderOCO(model.SideTypeSell, df.Pair, size, closePrice*1.05, closePrice*0.95, closePrice*0.95) | ||
if err != nil { | ||
log.WithFields(map[string]interface{}{ | ||
"pair": df.Pair, | ||
"side": model.SideTypeBuy, | ||
"close": closePrice, | ||
"asset": assetPosition, | ||
"quote": quotePosition, | ||
"size": size, | ||
}).Error(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.