Skip to content

Commit

Permalink
Merge pull request #633 from zenixls2/fix/ewo_entry
Browse files Browse the repository at this point in the history
Fix/ewo entry, backtest
  • Loading branch information
zenixls2 authored May 30, 2022
2 parents 867b148 + e3a8ef4 commit 8652b4e
Show file tree
Hide file tree
Showing 10 changed files with 661 additions and 299 deletions.
6 changes: 3 additions & 3 deletions apps/backtest-report/components/TradingViewChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ const ordersToMarkets = (interval: string, orders: Array<Order> | void): Array<M
// var markers = [{ time: data[data.length - 48].time, position: 'aboveBar', color: '#f68410', shape: 'circle', text: 'D' }];
for (let i = 0; i < orders.length; i++) {
let order = orders[i];
let t = order.update_time.getTime() / 1000.0;
let t = (order.update_time || order.time).getTime() / 1000.0;
let lastMarker = markers.length > 0 ? markers[markers.length - 1] : null;
if (lastMarker) {
let remainder = lastMarker.time % intervalSecs;
Expand Down Expand Up @@ -264,7 +264,7 @@ const positionBaseHistoryToLineData = (interval: string, hs: Array<PositionHisto
}

// ignore duplicated entry
if (hs[i].time.getTime() === hs[i - 1].time.getTime()) {
if (i > 0 && hs[i].time.getTime() === hs[i - 1].time.getTime()) {
continue
}

Expand Down Expand Up @@ -296,7 +296,7 @@ const positionAverageCostHistoryToLineData = (interval: string, hs: Array<Positi
}

// ignore duplicated entry
if (hs[i].time.getTime() === hs[i - 1].time.getTime()) {
if (i > 0 && hs[i].time.getTime() === hs[i - 1].time.getTime()) {
continue
}

Expand Down
35 changes: 22 additions & 13 deletions config/ewo_dgtrd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@ exchangeStrategies:
- on: binance
ewo_dgtrd:
symbol: MATICUSDT
interval: 2h
# kline interval for indicators
interval: 15m
# use ema as MA
useEma: false
# use sma as MA, used when ema is false
# if both sma and ema are false, use EVMA
useSma: false
sigWin: 8
stoploss: 10%
# ewo signal line window size
sigWin: 5
# SL percentage from entry price
stoploss: 2%
# use HeikinAshi klines instead of normal OHLC
useHeikinAshi: true
# disable SL when short
disableShortStop: false
#stops:
#- trailingStop:
# callbackRate: 5.1%
# closePosition: 20%
# minProfit: 1%
# interval: 1m
# virtual: true
# disable SL when long
disableLongStop: false
# CCI Stochastic Indicator high filter
ccistochFilterHigh: 80
# CCI Stochastic Indicator low filter
ccistochFilterLow: 20
# print record exit point in log messages
record: false

sync:
userDataStream:
Expand All @@ -36,7 +45,7 @@ sync:

backtest:
startTime: "2022-05-01"
endTime: "2022-05-11"
endTime: "2022-05-27"
symbols:
- MATICUSDT
sessions: [binance]
Expand All @@ -45,5 +54,5 @@ backtest:
#makerFeeRate: 0
#takerFeeRate: 15
balances:
MATIC: 5000.0
USDT: 10000
MATIC: 000.0
USDT: 15000.0
4 changes: 2 additions & 2 deletions pkg/backtest/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ func (e *Exchange) QueryMarkets(ctx context.Context) (types.MarketMap, error) {
return e.markets, nil
}

func (e Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) {
func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []types.Deposit, err error) {
return nil, nil
}

func (e Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error) {
func (e *Exchange) QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []types.Withdraw, err error) {
return nil, nil
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/backtest/matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,15 @@ func (m *SimplePriceMatching) SellToPrice(price fixedpoint.Value) (closedOrders
func (m *SimplePriceMatching) processKLine(kline types.KLine) {
m.CurrentTime = kline.EndTime.Time()
m.LastKLine = kline
if m.LastPrice.IsZero() {
m.LastPrice = kline.Open
} else {
if m.LastPrice.Compare(kline.Open) > 0 {
m.SellToPrice(kline.Open)
} else {
m.BuyToPrice(kline.Open)
}
}

switch kline.Direction() {
case types.DirectionDown:
Expand Down
8 changes: 5 additions & 3 deletions pkg/service/backtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func (s *BacktestService) SyncKLineByInterval(ctx context.Context, exchange type
if err := s.BatchInsert(klines); err != nil {
return err
}

count += len(klines)
}
log.Debugf("inserted klines %s %s data: %d", symbol, interval.String(), count)
Expand Down Expand Up @@ -321,8 +320,11 @@ func (s *BacktestService) BatchInsert(kline []types.KLine) error {
sql := fmt.Sprintf("INSERT INTO `%s` (`exchange`, `start_time`, `end_time`, `symbol`, `interval`, `open`, `high`, `low`, `close`, `closed`, `volume`, `quote_volume`, `taker_buy_base_volume`, `taker_buy_quote_volume`)"+
" values (:exchange, :start_time, :end_time, :symbol, :interval, :open, :high, :low, :close, :closed, :volume, :quote_volume, :taker_buy_base_volume, :taker_buy_quote_volume); ", tableName)

_, err := s.DB.NamedExec(sql, kline)
return err
tx := s.DB.MustBegin()
if _, err := tx.NamedExec(sql, kline); err != nil {
return err
}
return tx.Commit()
}

func (s *BacktestService) _deleteDuplicatedKLine(k types.KLine) error {
Expand Down
86 changes: 86 additions & 0 deletions pkg/strategy/ewoDgtrd/heikinashi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package ewoDgtrd

import (
"fmt"
"math"

"github.com/c9s/bbgo/pkg/types"
)

type Queue struct {
arr []float64
size int
}

func NewQueue(size int) *Queue {
return &Queue{
arr: make([]float64, 0, size),
size: size,
}
}

func (inc *Queue) Last() float64 {
if len(inc.arr) == 0 {
return 0
}
return inc.arr[len(inc.arr)-1]
}

func (inc *Queue) Index(i int) float64 {
if len(inc.arr)-i-1 < 0 {
return 0
}
return inc.arr[len(inc.arr)-i-1]
}

func (inc *Queue) Length() int {
return len(inc.arr)
}

func (inc *Queue) Update(v float64) {
inc.arr = append(inc.arr, v)
if len(inc.arr) > inc.size {
inc.arr = inc.arr[len(inc.arr)-inc.size:]
}
}

type HeikinAshi struct {
Close *Queue
Open *Queue
High *Queue
Low *Queue
Volume *Queue
}

func NewHeikinAshi(size int) *HeikinAshi {
return &HeikinAshi{
Close: NewQueue(size),
Open: NewQueue(size),
High: NewQueue(size),
Low: NewQueue(size),
Volume: NewQueue(size),
}
}

func (s *HeikinAshi) Print() string {
return fmt.Sprintf("Heikin c: %.3f, o: %.3f, h: %.3f, l: %.3f, v: %.3f",
s.Close.Last(),
s.Open.Last(),
s.High.Last(),
s.Low.Last(),
s.Volume.Last())
}

func (inc *HeikinAshi) Update(kline types.KLine) {
open := kline.Open.Float64()
cloze := kline.Close.Float64()
high := kline.High.Float64()
low := kline.Low.Float64()
newClose := (open + high + low + cloze) / 4.
newOpen := (inc.Open.Last() + inc.Close.Last()) / 2.
inc.Close.Update(newClose)
inc.Open.Update(newOpen)
inc.High.Update(math.Max(math.Max(high, newOpen), newClose))
inc.Low.Update(math.Min(math.Min(low, newOpen), newClose))
inc.Volume.Update(kline.Volume.Float64())
}
Loading

0 comments on commit 8652b4e

Please sign in to comment.