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