github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/examples/ws/submit-order/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os"
     7  
     8  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/event"
     9  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/order"
    10  	"github.com/bitfinexcom/bitfinex-api-go/pkg/mux"
    11  )
    12  
    13  func main() {
    14  	m := mux.
    15  		New().
    16  		TransformRaw().
    17  		WithAPIKEY("YOUR_API_KEY").
    18  		WithAPISEC("YOUR_API_SECRET").
    19  		Start()
    20  
    21  	crash := make(chan error)
    22  	auth := make(chan bool)
    23  
    24  	go func() {
    25  		// if listener will fail, program will exit by passing error to crash channel
    26  		crash <- m.Listen(func(msg interface{}, err error) {
    27  			if err != nil {
    28  				log.Printf("error received: %s\n", err)
    29  			}
    30  
    31  			switch v := msg.(type) {
    32  			case event.Info:
    33  				if v.Event == "auth" && v.Status == "OK" {
    34  					auth <- true
    35  				}
    36  			case order.New:
    37  				log.Printf("%T: %+v\n", v, v)
    38  				close(crash)
    39  			}
    40  		})
    41  	}()
    42  
    43  	for {
    44  		select {
    45  		case err := <-crash:
    46  			fmt.Printf("err: %s\n", err)
    47  			os.Exit(1)
    48  		case <-auth:
    49  			// authenticated, safe to submit orders etc
    50  			if err := m.Send(&order.NewRequest{
    51  				CID:    788,
    52  				Type:   "EXCHANGE LIMIT",
    53  				Symbol: "tBTCUSD",
    54  				Price:  33,
    55  				Amount: 0.001,
    56  			}); err != nil {
    57  				fmt.Printf("err submitting new order: %s\n", err)
    58  			}
    59  		}
    60  	}
    61  }