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

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"log"
     6  	"os"
     7  	"time"
     8  
     9  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/order"
    10  	"github.com/bitfinexcom/bitfinex-api-go/v2/websocket"
    11  )
    12  
    13  func SubmitTestOrder(c *websocket.Client) {
    14  	log.Printf("Submitting new order")
    15  	err := c.SubmitOrder(context.Background(), &order.NewRequest{
    16  		Symbol: "tBTCUSD",
    17  		CID:    time.Now().Unix() / 1000,
    18  		Amount: 0.02,
    19  		Type:   "EXCHANGE LIMIT",
    20  		Price:  5000,
    21  	})
    22  	if err != nil {
    23  		log.Fatal(err)
    24  	}
    25  }
    26  
    27  func UpdateTestOrder(orderId int64, c *websocket.Client) {
    28  	log.Printf("Updating order")
    29  	err := c.SubmitUpdateOrder(context.Background(), &order.UpdateRequest{
    30  		ID:     orderId,
    31  		Amount: 0.04,
    32  	})
    33  	if err != nil {
    34  		log.Fatal(err)
    35  	}
    36  }
    37  
    38  func main() {
    39  	key := os.Getenv("BFX_KEY")
    40  	secret := os.Getenv("BFX_SECRET")
    41  	p := websocket.NewDefaultParameters()
    42  	p.URL = "wss://test.bitfinex.com/ws/2"
    43  	c := websocket.NewWithParams(p).Credentials(key, secret)
    44  
    45  	err := c.Connect()
    46  	if err != nil {
    47  		log.Fatalf("connecting authenticated websocket: %s", err)
    48  	}
    49  	defer c.Close()
    50  
    51  	// Begin listening to incoming messages
    52  
    53  	for obj := range c.Listen() {
    54  		switch obj.(type) {
    55  		case error:
    56  			log.Fatalf("channel closed: %s", obj)
    57  			break
    58  		case *websocket.AuthEvent:
    59  			// on authorize create new order
    60  			SubmitTestOrder(c)
    61  		case *order.New:
    62  			// new order received so update it
    63  			id := obj.(*order.New).ID
    64  			UpdateTestOrder(id, c)
    65  		default:
    66  			log.Printf("MSG RECV: %#v", obj)
    67  		}
    68  	}
    69  
    70  	time.Sleep(time.Second * 10)
    71  }