コード例 #1
0
func main() {

	info := gofighter.TradingInfo{
		Account:      ACCOUNT,
		Venue:        VENUE,
		Symbol:       SYMBOL,
		ApiKey:       KEY,
		BaseURL:      BASE_URL,
		WebSocketURL: WS_URL,
	}

	// The market and position will be watched by 2 goroutines. In order to get info
	// back from them, we create 2 channels that we can use to request the current
	// state from them. We request the state by sending... a channel, of course.

	market_queries := make(chan chan gofighter.Market)
	go gofighter.MarketWatch(info, market_queries)

	position_queries := make(chan chan gofighter.Position)
	go gofighter.PositionWatch(info, position_queries)

	for {
		market := gofighter.GetMarket(market_queries) // Behind the scenes, this sends a channel
		// and gets the response through it.

		// The .Last member of the quote gets set to -1 if not present in the JSON from the server,
		// which usually means there is no activity yet...

		if market.Quote.Last == -1 {
			fmt.Printf("Waiting for market action to start...\n")
			time.Sleep(1 * time.Second)
			continue
		}

		pos := gofighter.GetPosition(position_queries) // This works like .GetMarket() above, but for position

		pos.Print(market.Quote.Last) // The argument here is the price to use when calculating NAV

		var order gofighter.ShortOrder
		order.OrderType = "limit"
		order.Qty = 50 + rand.Intn(50)

		// Buy if short. Sell if long. If neither, flip a coin...

		if pos.Shares > 0 || (pos.Shares == 0 && rand.Intn(2) == 0) {
			order.Direction = "sell"
			order.Price = market.Quote.Last + 50
		} else {
			order.Direction = "buy"
			order.Price = market.Quote.Last - 50
		}
		if order.Price < 0 {
			order.Price = 0
		}

		go order_and_cancel(info, order)

		time.Sleep(500 * time.Millisecond)
	}
}
コード例 #2
0
func main() {

	// The following assumes game_start.exe has already been called, and thus that
	// there exists a file in the /gm/ directory which contains info about the level.

	info := gofighter.GetUserSelection("known_levels.json")

	position_queries := make(chan chan gofighter.Position)
	go gofighter.PositionWatch(info, position_queries)

	// How this works: the goroutine PositionWatch uses a WebSocket to keep track of
	// the position, and when we send a channel to it via its input channel, it sends
	// a copy of the position back to us along the channel we provided.

	for {
		position := gofighter.GetPosition(position_queries) // Behind the scenes, this sends a
		// one-time channel to the goroutine

		fmt.Printf(time.Now().Format("2006-01-02 15:04:05 ... "))
		fmt.Printf("Shares: %d, Dollars: $%d\n", position.Shares, position.Cents/100)

		time.Sleep(2 * time.Second)
	}
}