Instead, let’s recognize that this makes it very easy for any financial company to subscribe for stock quotes by creating a unique queue, binding to the stock system’s fanout exchange, and then write their own algorithm for making financial decision.ĪMQP is a nicely written spec. Pika python how to#This shows the basic pattern of connecting to our RabbitMQ broker, declaring a queue, binding it to the fanout exchange, and then registering a callback.īut let’s not getting all wrapped up in how to make this algorithm better at picking winners and losers. #ch.queue_bind(queue=qname, exchange=self.exchange_name, routing_key=qname)Ĭh.basic_consume(callback=callback, queue=qname) Print "Binding queue %s to exchange %s" % (qname, self.exchange_name) def monitor(self, qname, callback):Ĭonn = amqp.Connection(host="127.0.0.1", userid="guest", password="guest")Ĭh.queue_declare(queue=qname, durable=False, exclusive=False, auto_delete=False)Ĭh.queue_bind(queue=qname, exchange=self.exchange_name) The following function needs to be added to PyAmqpLibPublisher. self.client is our hook on the AMQP service coded earlier, and we need a way to bind our queue to the exchange to receive messages. The part missing from this is the function. However, if it already has shares, then it waits for the price to rise above what it originally paid for it. Wouldn’t you? Then it calculates the minimum and maximum of the trend, and if the price is relatively close to the minimum, it buys. It’s geared to collect a minimum number of quotes before making a decision. decide_whether_to_buy_or_sell splits up the tuple of the stock quote and then adds the price to its stock symbol history.handle de-pickles the opaque body of the message, acknowledges receipt of the message on the channel with the broker, and then fires off its algorithm about deciding whether to buy or sell.The reason for inserting this extra method call is to support swapping out py-amqplib with pika, which we’ll look at later. handle_pyamqplib_delivery pulls out the important parts of the message and hands them to handle.It registers handle_pyamqplib_delivery as the callback method to invoke every time a new quote arrives. monitor is the main hook to kick off listening for new stock quotes.This client has its strategy for buying and selling stocks nicely isolated from the machinery for receiving messages from RabbitMQ. (self.qname, self.handle_pyamqplib_delivery) #print "New price for %s => %s at %s" % quoteĬh.basic_ack(delivery_tag = delivery_tag) Self.handle(msg.delivery_info, msg.delivery_info, msg.body)ĭef handle(self, ch, delivery_tag, body): Print "Total net is %s" % (sale_value - self.holdings)ĭef handle_pyamqplib_delivery(self, msg): Print "Holdings value is %s" % self.holdings Print "+++++++ Price of %s is higher than my holdings, so I'm going to sell!" % symbol If price self.holdings and price > 0.99*price_max: Print "Net worth is %s + %s = %s" % (self.cash, value, self.cash + value) Value = sum(*self.history for symbol in ()]) # print "Value of %s is %s" % (symbol, float(self.holdings)*self.history) # print "self.holdings = %s" % self.holdings # print "self.history = %s" % self.history #print "Trending minimum/avg/max of %s is %s-%s-%s" % (symbol, price_low, price_avg, price_max) Print "%s quotes until we start deciding whether to buy or sell %s" % (end - len(self.history), symbol) #print "Recent history of %s is %s" % (symbol, self.history) New_quote = random.uniform(0.9*previous_quote, 1.1*previous_quote) Symbol = random.choice(self.stock_symbols) # This quickly creates four random stock symbolsĭef random_letter(): return chr(random.choice(chars)) The stock exchange publishes messages to the broker indicating stock name, price, and time. With that out of the way, let’s write some code!Ī good example for a messaging solution is a stock ticker system. Do not rely on the algorithms for financial advice. Pika python code#The code written in this blog entry is for demonstration purposes only. Pika python mac os x#Personally, I have it running on my Mac OS X machine (snow leopard). This assumes you have already installed RabbitMQ based on instructions for your platform and fired it up. You can find all the source code for this blog at. Compare pika, an AMQP library created by the RabbitMQ team, with py-amqplib.Create a brokerage Python application that decides when to buy and sell.Create a simple stock ticker Python application.Thanks to the neutral nature of the AMQP spec, it is easy to connect to it from many platforms, including Python. RabbitMQ is a powerful messaging broker based on the Advanced Message Queueing Protocol (AMQP).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |