github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/examples/ws/cancel-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  					// notify auth channel about successful login
    35  					auth <- true
    36  				}
    37  			case order.New:
    38  				// new order received, can cancel it now
    39  				log.Printf("%T: %+v\n", v, v)
    40  				m.Send(&order.CancelRequest{ID: v.ID})
    41  			case order.Cancel:
    42  				// order cancel performed, exiting
    43  				log.Printf("%T: %+v\n", v, v)
    44  				close(crash)
    45  			}
    46  		})
    47  	}()
    48  
    49  	for {
    50  		select {
    51  		case err := <-crash:
    52  			fmt.Printf("err: %s\n", err)
    53  			os.Exit(1)
    54  		case <-auth:
    55  			// authenticated, safe to submit orders etc
    56  			if err := m.Send(&order.NewRequest{
    57  				CID:    788,
    58  				Type:   "EXCHANGE LIMIT",
    59  				Symbol: "tBTCUSD",
    60  				Price:  33,
    61  				Amount: 0.001,
    62  			}); err != nil {
    63  				fmt.Printf("err submitting new order: %s\n", err)
    64  			}
    65  		}
    66  	}
    67  }