Function to only Update return Variable if new Value is higher than current value assigned to the return


I have the following function below, which is being used in a for loop. Essential, in its current form it will hit an API which will give me a bunch of information in return including the “Bid” and “Ask” price of a stock.

My Goal is to only “update” the return variables IF the new information is a higher price than the previous information.

For example, say I trigger the function and it returns a Bid price of 1.00. Then we I run the function again the new Bid price is 1.02. Since 1.02 is higher than the previous Bid of 1.00, then it updates the variable to 1.02. Then say the function is run again and the bid price is now 0.95. In this case I do not want to the variable to be updated and the Bid variable should still be 1.02 since that was the “highest number”.

I was thinking I could do an if/else statement, but it does not seem to work as expected. Any assistance in the direction I should look would be much appreciated.

func optionHighQuote() (bidH, askH float64) {
url := ""
payload := strings.NewReader("symbols=")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("accept", "application/json")

res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)

//parse response into Json Data
json.Unmarshal([]byte(body), &jsonStockResponse)

var hBid = jsonStockResponse.Quotes.Quote.Bid
var hAsk = jsonStockResponse.Quotes.Quote.Ask

return hBid, hAsk

This is what the Return information looks like

type QUOTE 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"`

I have not read your code, but isn’t new := maximum(old, pull_new_value()) sufficient (with a proper implementation of maximum).

I appreciate your reply. I did not even think of something like that. I am going to work on it.Thank you.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.