103 lines
2.3 KiB
Go
103 lines
2.3 KiB
Go
|
|
package usmarket
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"log"
|
|||
|
|
"sync"
|
|||
|
|
"time"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
var (
|
|||
|
|
UsMarket *MarketIndex
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// lock
|
|||
|
|
type MarketIndex struct {
|
|||
|
|
sync.RWMutex
|
|||
|
|
LastIndex map[string]*StockDataIndex
|
|||
|
|
Stats int // -1:未开市,0:开市,数据不正常,1: 开市,数据正常
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func NewMarket() {
|
|||
|
|
UsMarket = &MarketIndex{
|
|||
|
|
LastIndex: make(map[string]*StockDataIndex),
|
|||
|
|
Stats: -1,
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (m *MarketIndex) Set(stats int, index map[string]*StockDataIndex) {
|
|||
|
|
m.Lock()
|
|||
|
|
defer m.Unlock()
|
|||
|
|
|
|||
|
|
m.Stats = stats
|
|||
|
|
m.LastIndex = index
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (m *MarketIndex) Get() (int, map[string]*StockDataIndex) {
|
|||
|
|
m.Lock()
|
|||
|
|
defer m.Unlock()
|
|||
|
|
|
|||
|
|
return m.Stats, m.LastIndex
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func CheckUsMarketOpen() int {
|
|||
|
|
var err error
|
|||
|
|
if !IsUSMarketOpen() {
|
|||
|
|
UsMarket.Set(-1, nil)
|
|||
|
|
return -1
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
newIndex, err := Baidu_UsIndex()
|
|||
|
|
if err != nil {
|
|||
|
|
log.Println("Baidu_UsIndex Error:", err)
|
|||
|
|
UsMarket.Set(-1, nil)
|
|||
|
|
return -1
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
_, LastStockIndex := UsMarket.Get()
|
|||
|
|
if len(LastStockIndex) == 0 {
|
|||
|
|
UsMarket.Set(0, newIndex)
|
|||
|
|
return 0
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// IXIC 纳斯达克,DJI 道琼斯,SPX 标普500
|
|||
|
|
if LastStockIndex["IXIC"].LastPrice == newIndex["IXIC"].LastPrice && LastStockIndex["DJI"].LastPrice == newIndex["DJI"].LastPrice && LastStockIndex["SPX"].LastPrice == newIndex["SPX"].LastPrice {
|
|||
|
|
// log.Println("ERROR #103, IXIC,DJI,SPX LastPrice no change")
|
|||
|
|
UsMarket.Set(-1, LastStockIndex)
|
|||
|
|
return 0
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if newIndex["IXIC"].Increase == "0.00" && newIndex["DJI"].Increase == "0.00" && newIndex["SPX"].Increase == "0.00" {
|
|||
|
|
// log.Println("ERROR #104, IXIC,DJI,SPX Increase is 0")
|
|||
|
|
UsMarket.Set(-1, LastStockIndex)
|
|||
|
|
return 0
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// jsonBytes, _ := json.Marshal(newIndex)
|
|||
|
|
// log.Println("USMarket:", string(jsonBytes))
|
|||
|
|
UsMarket.Set(1, newIndex)
|
|||
|
|
return 1
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 判断美股是否开市
|
|||
|
|
func IsUSMarketOpen() bool {
|
|||
|
|
// 获取当前时间
|
|||
|
|
now := time.Now()
|
|||
|
|
|
|||
|
|
// 设置美国东部时间
|
|||
|
|
loc, _ := time.LoadLocation("America/New_York")
|
|||
|
|
|
|||
|
|
// 将当前时间转换为美国东部时间
|
|||
|
|
nowInNY := now.In(loc)
|
|||
|
|
|
|||
|
|
// 判断是否为周末
|
|||
|
|
if nowInNY.Weekday() == time.Saturday || nowInNY.Weekday() == time.Sunday {
|
|||
|
|
return false
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 判断是否在交易时间内
|
|||
|
|
openTime := time.Date(nowInNY.Year(), nowInNY.Month(), nowInNY.Day(), 9, 30, 0, 0, loc)
|
|||
|
|
closeTime := time.Date(nowInNY.Year(), nowInNY.Month(), nowInNY.Day(), 16, 0, 0, 0, loc)
|
|||
|
|
|
|||
|
|
return nowInNY.After(openTime) && nowInNY.Before(closeTime)
|
|||
|
|
}
|