|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/antihax/optional" |
| 10 | + "github.com/gateio/gateapi-go/v5" |
| 11 | + "github.com/shopspring/decimal" |
| 12 | +) |
| 13 | + |
| 14 | +func FuturesDemo(config *RunConfig) { |
| 15 | + settle := "usdt" |
| 16 | + contract := "BTC_USDT" |
| 17 | + |
| 18 | + client := gateapi.NewAPIClient(gateapi.NewConfiguration()) |
| 19 | + client.ChangeBasePath(config.BaseUrl) |
| 20 | + ctx := context.WithValue(context.Background(), gateapi.ContextGateAPIV4, gateapi.GateAPIV4{ |
| 21 | + Key: config.ApiKey, |
| 22 | + Secret: config.ApiSecret, |
| 23 | + }) |
| 24 | + |
| 25 | + // update position leverage |
| 26 | + leverage := "3" |
| 27 | + _, _, err := client.FuturesApi.UpdatePositionLeverage(ctx, settle, contract, leverage) |
| 28 | + if err != nil { |
| 29 | + panicGateError(err) |
| 30 | + } |
| 31 | + |
| 32 | + // retrieve position size |
| 33 | + positionSize := int64(0) |
| 34 | + position, _, err := client.FuturesApi.GetPosition(ctx, settle, contract) |
| 35 | + if err != nil { |
| 36 | + if e, ok := err.(gateapi.GateAPIError); ok { |
| 37 | + // ignore position not found error |
| 38 | + if e.Label != "POSITION_NOT_FOUND" { |
| 39 | + panicGateError(e) |
| 40 | + } |
| 41 | + } else { |
| 42 | + panicGateError(err) |
| 43 | + } |
| 44 | + } else { |
| 45 | + positionSize = position.Size |
| 46 | + } |
| 47 | + |
| 48 | + futuresContract, _, err := client.FuturesApi.GetFuturesContract(ctx, settle, contract) |
| 49 | + if err != nil { |
| 50 | + panicGateError(err) |
| 51 | + } |
| 52 | + orderSize := int64(10) |
| 53 | + if orderSize < futuresContract.OrderSizeMin { |
| 54 | + orderSize = futuresContract.OrderSizeMin |
| 55 | + } |
| 56 | + if positionSize < 0 { |
| 57 | + orderSize = 0 - orderSize |
| 58 | + } |
| 59 | + |
| 60 | + // example to update risk limit |
| 61 | + riskLimit := decimal.RequireFromString(futuresContract.RiskLimitBase).Add(decimal.RequireFromString(futuresContract.RiskLimitStep)) |
| 62 | + _, _, err = client.FuturesApi.UpdatePositionRiskLimit(ctx, settle, contract, riskLimit.String()) |
| 63 | + if err != nil { |
| 64 | + panicGateError(err) |
| 65 | + } |
| 66 | + |
| 67 | + // retrieve last price to calculate margin needed |
| 68 | + tickers, _, err := client.FuturesApi.ListFuturesTickers(ctx, settle, &gateapi.ListFuturesTickersOpts{ |
| 69 | + Contract: optional.NewString(contract), |
| 70 | + }) |
| 71 | + lastPrice := tickers[0].Last |
| 72 | + logger.Printf("last price of contract %s: %s", contract, lastPrice) |
| 73 | + |
| 74 | + margin := decimal.NewFromInt(orderSize).Mul( |
| 75 | + decimal.RequireFromString(lastPrice)).Mul( |
| 76 | + decimal.RequireFromString(futuresContract.QuantoMultiplier)).DivRound( |
| 77 | + decimal.RequireFromString(leverage), 8).Mul( |
| 78 | + decimal.RequireFromString("1.1")).Round(8) |
| 79 | + logger.Printf("needs margin amount: %s\n", margin.String()) |
| 80 | + |
| 81 | + // if balance is not enough, transfer from spot account |
| 82 | + available := "0" |
| 83 | + futuresAccount, _, err := client.FuturesApi.ListFuturesAccounts(ctx, settle) |
| 84 | + if err != nil { |
| 85 | + if e, ok := err.(gateapi.GateAPIError); ok { |
| 86 | + if e.Label != "USER_NOT_FOUND" { |
| 87 | + panicGateError(e) |
| 88 | + } |
| 89 | + } else { |
| 90 | + panicGateError(err) |
| 91 | + } |
| 92 | + } else { |
| 93 | + available = futuresAccount.Available |
| 94 | + } |
| 95 | + logger.Printf("futures account available: %s %s\n", available, strings.ToUpper(settle)) |
| 96 | + if margin.GreaterThan(decimal.RequireFromString(available)) { |
| 97 | + if config.UseTestNet { |
| 98 | + logger.Fatal("testnet account balance not enough, make a transferal on web") |
| 99 | + } |
| 100 | + transfer := gateapi.Transfer{ |
| 101 | + Currency: strings.ToUpper(settle), |
| 102 | + From: "spot", |
| 103 | + To: "futures", |
| 104 | + Amount: margin.String(), |
| 105 | + } |
| 106 | + _, err := client.WalletApi.Transfer(ctx, transfer) |
| 107 | + if err != nil { |
| 108 | + panicGateError(err) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + // example to cancel all open orders in contract |
| 113 | + _, _, err = client.FuturesApi.CancelFuturesOrders(ctx, settle, contract, nil) |
| 114 | + if err != nil { |
| 115 | + panicGateError(err) |
| 116 | + } |
| 117 | + |
| 118 | + // order using market price |
| 119 | + order := gateapi.FuturesOrder{Contract: contract, Size: orderSize, Price: "0", Tif: "ioc"} |
| 120 | + orderResponse, _, err := client.FuturesApi.CreateFuturesOrder(ctx, settle, order) |
| 121 | + if err != nil { |
| 122 | + panicGateError(err) |
| 123 | + } |
| 124 | + logger.Printf("order %d created with status: %s\n", orderResponse.Id, orderResponse.Status) |
| 125 | + if orderResponse.Status == "open" { |
| 126 | + futuresOrder, _, err := client.FuturesApi.GetFuturesOrder(ctx, settle, strconv.FormatInt(orderResponse.Id, 10)) |
| 127 | + if err != nil { |
| 128 | + panicGateError(err) |
| 129 | + } |
| 130 | + logger.Printf("order %d status %s, total size %d, left %d\n", |
| 131 | + futuresOrder.Id, futuresOrder.Status, futuresOrder.Size, futuresOrder.Left) |
| 132 | + _, _, err = client.FuturesApi.CancelFuturesOrder(ctx, settle, strconv.FormatInt(orderResponse.Id, 10)) |
| 133 | + if err != nil { |
| 134 | + panicGateError(err) |
| 135 | + } |
| 136 | + logger.Printf("order %d cancelled\n", orderResponse.Id) |
| 137 | + } else { |
| 138 | + time.Sleep(time.Millisecond * 200) |
| 139 | + orderTrades, _, err := client.FuturesApi.GetMyTrades(ctx, settle, &gateapi.GetMyTradesOpts{ |
| 140 | + Contract: optional.NewString(contract), |
| 141 | + Order: optional.NewInt64(orderResponse.Id), |
| 142 | + }) |
| 143 | + if err != nil { |
| 144 | + panicGateError(err) |
| 145 | + } |
| 146 | + for _, t := range orderTrades { |
| 147 | + logger.Printf("order %s filled size %d with price %s\n", t.OrderId, t.Size, t.Price) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // example to update position margin |
| 152 | + _, _, err = client.FuturesApi.UpdatePositionMargin(ctx, settle, contract, "0.01") |
| 153 | + if err != nil { |
| 154 | + panicGateError(err) |
| 155 | + } |
| 156 | +} |
0 commit comments