HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Get Bitcoin Price and Advice

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pricegetadviceandbitcoin

Problem

I wrote this in the last hour because I was bored. I was hoping I could get some feedback on this. It is a function that returns the price of a bitcoin and whether you should buy or sell it. I am thinking of setting it up as a function within a chat bot to spice up the chat a little.

import urllib2
import random
import json

def bitcoin(currency, amt = 1):
    url = "https://api.coindesk.com/v1/bpi/currentprice.json"
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    the_page = response.read()
    data = json.loads(the_page)
    if currency in ["USD","US","dollar","$"]:
        conversion = data['bpi']['USD']['rate_float']
        symbol = "USD"
    elif currency in ["EUR","EU","euro","€"]:
        conversion = data['bpi']['EUR']['rate_float']
        symbol = "EUR"
    elif currency in ["GBP","GB","UK","pound","£"]:
        conversion = data['bpi']['GBP']['rate_float']
        symbol = "GBP"
    else:
        stop("Currency not recognized.")
    advice = random.choice(["It's so LOW!!!!!!! \r\nBuy!!! Buy!!! Buy!!!!!","It's so HIGH!!!!!! \r\nSell!!! Sell!!! Sell!!!!!"])
    return "{0} Bitcoin = {1} {2} \r\n{3}".format(amt,conversion * amt, symbol, advice)

Solution

This is Schrödinger code

It can be both correct and incorrect at the same time.

random.choice is not how we offer financial advice.

What I would suggest is to monitor previous data, the longer the better, and monitor the fluctuation in price.

See this graph (Note, a 5 year graph):

at two points on the chart you can see separate events that can be somewhat predicted by the speed at which it falls/rises in comparison to previous events.

Although this could potentially expand to be a large solution, it's about as close to accurate as you can get. (Not really, but I'm not a market analyst)

These guys are, though.

Note that the graph is 5 years; data doesn't fluctuate that much in small amounts.

Although Bitcoin can, given it's financially unstable and volatile nature.

Beyond that, your python code is nice :-)

I would suggest writing it to PEP8 however, as that is the official style guide of Python, and then you can use an online checker like pep8online.

Context

StackExchange Code Review Q#116272, answer score: 11

Revisions (0)

No revisions yet.