我仍在学习用Golang编写代码,这可能是一个简单的问题,但是我已经在网上和Go网站上进行了搜索,但还没有解决它的能力。我下面有以下代码。运行时,它实际上将运行option_quote
函数,该函数将打印出选项的"Ask"
和"Bid"
。现在for
只是一个无休止的循环。
但是,如果要根据c_bid
函数中的option_quote
变量满足某些条件,我想执行新操作。
我的目标是:
程序将继续循环执行option_quote
函数,以获取该期权的当前价格。如果该期权的当前价格大于或等于特定值,请执行其他操作。
类似
for c_bid > target_price {
continue getting looping through quotes
}
if target_price >= c_bid {
close_trade
}
我当前拥有的代码:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
var json_stock_response Json
//endless loop
func main() {
for {
option_quote()
}
}
//This function is used to get the quotes
func option_quote() {
url := "https://api.tradier.com/v1/markets/quotes"
payload := strings.NewReader("symbols=AAPL180629C00162500")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("accept", "application/json")
req.Header.Add("Authorization", "Bearer XXX")
req.Header.Add("Cache-Control", "no-cache")
req.Header.Add("Postman-Token", "9d669b80-0ed2-4988-a225-56b2f018c5c6")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
//parse response into Json Data
json.Unmarshal([]byte(body), &json_stock_response)
//fmt.Println(res)
// This will print out only the "Ask" value
var option_symbol string = json_stock_response.Quotes.Quote.Symbol
var c_bid float64 = json_stock_response.Quotes.Quote.Bid
var c_ask float64 = json_stock_response.Quotes.Quote.Ask
fmt.Println("Option:", option_symbol, "Bid:", c_bid, "Ask:", c_ask)
}
//Structure of the Json Response back from Traider when getting an option quote
type Json struct {
Quotes struct {
Quote struct {
Symbol string `json:"symbol"`
Description string `json:"description"`
Exch string `json:"exch"`
Type string `json:"type"`
Last float64 `json:"last"`
Change float64 `json:"change"`
ChangePercentage float64 `json:"change_percentage"`
Volume int `json:"volume"`
AverageVolume int `json:"average_volume"`
LastVolume int `json:"last_volume"`
TradeDate int64 `json:"trade_date"`
Open interface{} `json:"open"`
High interface{} `json:"high"`
Low interface{} `json:"low"`
Close interface{} `json:"close"`
Prevclose float64 `json:"prevclose"`
Week52High float64 `json:"week_52_high"`
Week52Low float64 `json:"week_52_low"`
Bid float64 `json:"bid"`
Bidsize int `json:"bidsize"`
Bidexch string `json:"bidexch"`
BidDate int64 `json:"bid_date"`
Ask float64 `json:"ask"`
Asksize int `json:"asksize"`
Askexch string `json:"askexch"`
AskDate int64 `json:"ask_date"`
OpenInterest int `json:"open_interest"`
Underlying string `json:"underlying"`
Strike float64 `json:"strike"`
ContractSize int `json:"contract_size"`
ExpirationDate string `json:"expiration_date"`
ExpirationType string `json:"expiration_type"`
OptionType string `json:"option_type"`
RootSymbol string `json:"root_symbol"`
} `json:"quote"`
} `json:"quotes"`
}
答案 0 :(得分:0)
您可以尝试从option_quote函数返回结果,这是一个示例:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
var json_stock_response Json
//endless loop
func main() {
var target_price = 1.0
for {
bid, ask := option_quote()
fmt.Println("Bid:", bid, "Ask:", ask)
if bid <= target_price {
fmt.Printf("Closing trade, bid is: %f\n", bid)
break
}
}
}
//This function is used to get the quotes
func option_quote() (bid, ask float64) {
url := "https://api.tradier.com/v1/markets/quotes"
payload := strings.NewReader("symbols=AAPL180629C00162500")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("accept", "application/json")
req.Header.Add("Authorization", "Bearer XXX")
req.Header.Add("Cache-Control", "no-cache")
req.Header.Add("Postman-Token", "9d669b80-0ed2-4988-a225-56b2f018c5c6")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
//parse response into Json Data
json.Unmarshal([]byte(body), &json_stock_response)
var c_bid float64 = json_stock_response.Quotes.Quote.Bid
var c_ask float64 = json_stock_response.Quotes.Quote.Ask
return c_bid, c_ask
}
//Structure of the Json Response back from Traider when getting an option quote
type Json struct {
Quotes struct {
Quote struct {
Symbol string `json:"symbol"`
Description string `json:"description"`
Exch string `json:"exch"`
Type string `json:"type"`
Last float64 `json:"last"`
Change float64 `json:"change"`
ChangePercentage float64 `json:"change_percentage"`
Volume int `json:"volume"`
AverageVolume int `json:"average_volume"`
LastVolume int `json:"last_volume"`
TradeDate int64 `json:"trade_date"`
Open interface{} `json:"open"`
High interface{} `json:"high"`
Low interface{} `json:"low"`
Close interface{} `json:"close"`
Prevclose float64 `json:"prevclose"`
Week52High float64 `json:"week_52_high"`
Week52Low float64 `json:"week_52_low"`
Bid float64 `json:"bid"`
Bidsize int `json:"bidsize"`
Bidexch string `json:"bidexch"`
BidDate int64 `json:"bid_date"`
Ask float64 `json:"ask"`
Asksize int `json:"asksize"`
Askexch string `json:"askexch"`
AskDate int64 `json:"ask_date"`
OpenInterest int `json:"open_interest"`
Underlying string `json:"underlying"`
Strike float64 `json:"strike"`
ContractSize int `json:"contract_size"`
ExpirationDate string `json:"expiration_date"`
ExpirationType string `json:"expiration_type"`
OptionType string `json:"option_type"`
RootSymbol string `json:"root_symbol"`
} `json:"quote"`
} `json:"quotes"`
}