gitee.com/quant1x/engine@v1.8.4/realtime/moving_average_convergence_divergence_test.go (about)

     1  package realtime
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/engine/datasource/base"
     6  	"gitee.com/quant1x/engine/models"
     7  	pd "gitee.com/quant1x/pandas"
     8  	. "gitee.com/quant1x/pandas/formula"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestIncrementalMovingAverageConvergenceDivergence(t *testing.T) {
    14  	code := "002528"
    15  	date := "20240301"
    16  	klines := base.CheckoutKLines(code, date)
    17  	if len(klines) == 0 {
    18  		panic("no data")
    19  	}
    20  	df := pd.LoadStructs(klines)
    21  	if df.Nrow() == 0 {
    22  		panic("加载k线失败")
    23  	}
    24  	var (
    25  		DATE  = df.Col("date")
    26  		CLOSE = df.ColAsNDArray("close")
    27  	)
    28  
    29  	SHORT := 12
    30  	LONG := 26
    31  	MID := 9
    32  
    33  	short := EMA(CLOSE, SHORT)
    34  	long := EMA(CLOSE, LONG)
    35  	DIF := short.Sub(long)
    36  	DEA := EMA(DIF, MID)
    37  	MACD := DIF.Sub(DEA).Mul(2)
    38  
    39  	df = pd.NewDataFrame(
    40  		DATE,
    41  		pd.NewSeriesWithoutType("short", short),
    42  		pd.NewSeriesWithoutType("long", long),
    43  		pd.NewSeriesWithoutType("DIF", DIF),
    44  		pd.NewSeriesWithoutType("DEA", DEA),
    45  		pd.NewSeriesWithoutType("MACD", MACD),
    46  	)
    47  	fmt.Println(df)
    48  	fmt.Println("==============================================================================================================")
    49  	lastClose := CLOSE.IndexOf(-1).(float64)
    50  	fmt.Println("确定最新收盘价:", lastClose)
    51  	df1 := df.IndexOf(-2)
    52  	fmt.Println(df1)
    53  	//date0 := df1["date"]
    54  	lastShort := df1["short"].(float64)
    55  	lastLong := df1["long"].(float64)
    56  	lastDif := df1["DIF"].(float64)
    57  	lastDea := df1["DEA"].(float64)
    58  	fmt.Println("lastDif", lastDif)
    59  	dif, dea, macd := IncrementalMovingAverageConvergenceDivergence(lastClose, lastShort, lastLong, lastDea, SHORT, LONG, MID)
    60  
    61  	fmt.Println("date:", DATE.IndexOf(-1))
    62  	fmt.Println(" dif:", dif)
    63  	fmt.Println(" dea:", dea)
    64  	fmt.Println("macd:", macd)
    65  }
    66  
    67  func TestDynamicMovingAverageConvergenceDivergence(t *testing.T) {
    68  	code := "002528"
    69  	date := "20240126"
    70  	klines := base.CheckoutKLines(code, date)
    71  	if len(klines) == 0 {
    72  		panic("no data")
    73  	}
    74  	df := pd.LoadStructs(klines)
    75  	if df.Nrow() == 0 {
    76  		panic("加载k线失败")
    77  	}
    78  	var (
    79  		DATE  = df.Col("date")
    80  		CLOSE = df.ColAsNDArray("close")
    81  	)
    82  
    83  	SHORT := 12
    84  	LONG := 26
    85  	MID := 9
    86  
    87  	short := EMA(CLOSE, SHORT)
    88  	long := EMA(CLOSE, LONG)
    89  	DIF := short.Sub(long)
    90  	DEA := EMA(DIF, MID)
    91  	MACD := DIF.Sub(DEA).Mul(2)
    92  
    93  	df = pd.NewDataFrame(
    94  		DATE,
    95  		pd.NewSeriesWithoutType("short", short),
    96  		pd.NewSeriesWithoutType("long", long),
    97  		pd.NewSeriesWithoutType("DIF", DIF),
    98  		pd.NewSeriesWithoutType("DEA", DEA),
    99  		pd.NewSeriesWithoutType("MACD", MACD),
   100  	)
   101  	fmt.Println(df)
   102  	fmt.Println("==============================================================================================================")
   103  	lastClose := CLOSE.IndexOf(-1).(float64)
   104  	fmt.Println("确定最新收盘价:", lastClose)
   105  	df1 := df.IndexOf(-2)
   106  	fmt.Println(df1)
   107  	//date0 := df1["date"]
   108  	lastShort := df1["short"].(float64)
   109  	lastLong := df1["long"].(float64)
   110  	lastDif := df1["DIF"].(float64)
   111  	lastDea := df1["DEA"].(float64)
   112  	fmt.Println("lastDif", lastDif)
   113  	snapshot := models.GetTick(code)
   114  	now := time.Now()
   115  	macd, macdHigh, macdLow := DynamicMovingAverageConvergenceDivergence(*snapshot, lastShort, lastLong, lastDea, SHORT, LONG, MID)
   116  	fmt.Println("   times:", time.Since(now))
   117  	fmt.Println("    date:", DATE.IndexOf(-1))
   118  	fmt.Println("    macd:", macd)
   119  	fmt.Println("macdHigh:", macdHigh)
   120  	fmt.Println(" macdLow:", macdLow)
   121  }