Trading Protocols

Have you been curious how trading websites pass on critical information about stocks to users? Did you know certain privileged corporate can get all the information faster that you or me? Do you wonder how critical information is and how safe the secrets are guarded – while the stock price goes up or down?

If you are stick reading, here are the basic questions we should answer in the first place:
What are trading protocols?
How do trading protocols work?
What are the popular protocols?
How is it designed?
How does it make sure the latency is low?
How does it make sure the bandwidth used is low?
How does it improve efficiency?
How do certain groups get faster information that general public?
How is security managed?

FIX is Financial Information Exchange
Here is an example of FIX protocol:
8=FIX.4.2 | 9=178 | 35=8 | 49=PHLX | 56=PERS | 52=20071123-05:30:00.000 | 11=ATOMNOCCC9990900 | 20=3 | 150=E | 39=E | 55=MSFT | 167=CS | 54=1 | 38=15 | 40=2 | 44=15 | 58=PHLX EQUITY TESTING | 59=0 | 47=C | 32=0 | 31=0 | 151=15 | 14=0 | 6=0 | 10=128 |

8 (BeginString), 9 (BodyLength), 35 (MsgType), 49 (SenderCompID), 56 (TargetCompID) and 1128 (ApplVerID) followed by actual Body consisting for trade information and trailer with checksum for security.

With these tag value pairs, it can send information it needs to the other end

https://en.wikipedia.org/wiki/Financial_Information_eXchange
Refer to above Wiki for diagram.

The problem here is that is uses TCP which might affect its latency.
Hence a very popular protocol called FAST was developed as an enhancement. FAST protocol is used in loads of Exchanges all over the world and is a open source code that is free available.

FIX is used more so with in the company. However FAST is used for ultra fast dissemination of market data.
To know more on FAST
http://ftp.moex.com/pub/FAST/Spectra/test/spectra_fastgate_en.pdf

Advertisements

Soil Hygrometer

Tags

, , , , , , , , , , , , , , , , , , , , , , , ,

Hygro_plant

Now, to automate further, we would add a soil hygrometer to know if the soil is dry or wet. This is of great help as one could water plants on need basis. I bought a Y69 (made in china – also comes along Y38).

hygrometer

The connections a fairly simple.

VCC –> Raspberry PI’s 3.3V

GND –> Raspberry PI’s GND

DO –>Pin GPIO 07 of RPi

AO –>Unused

For initial testing to see if the hygrometer is fine, connect about 3 volts DC + to VCC, – to GND and check the voltage between DO and multimeter. You will see that when hygrometer probes are in the open, Voltage would be VCC. As soon as you dip the probes in water, the Voltage becomes 0.

Here is the code to test if the signal received is fine:

#!/usr/bin/env python
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
HYGRO = 7
GPIO.setup(HYGRO, GPIO.IN)
print GPIO.input(HYGRO)

We are almost done. We could use this result to take decisions on when to water the plants. This code can be combined with previous project on solenoid for a complete sensor based plant watering system.

Water the Garden!

Tags

, , , , , , , , , , , , , , , , , ,

Garden_pi

If you have seen the project on watering plants using motor, this should be fairly a minor extension.

Things you require:-

1. Potentiometer

2. Rectifier

3. Solenoid (12 V)

4. Transistors (TIP122)

5. 12 V DC (Make sure it has good current rating. (0.3 or 0.4 Amps or so should so))

6. Wires and breadboard

Q.How to choose a transistor based on solenoid?
I used TIP122, a darlington pair, whose beta is about 1000. This was more than enough to get the required current to drive solenoid. Make sure you read TP122 spec prior to connecting them in a circuit – note the order of Base, Collector and Emitter. In our case collector is connected to one end of solenoid. Base is connected to raspberry pi using a resistor. Emitter is connected to GnD.

Q. How to choose a resistor based on solenoid?
Most solenoid have wattage ratings along with Voltage ratings. One can manually measure the resistance of the coil. Given Resistance, Voltage and Wattage, determine the current. This is the current needed to drive the solenoid.
In my case this turned out to be approximately 300mA (4W, 12 V, 36 Ohm).
Raspberry pi would drive about 8mA (Base current of transistor) and given a huge B of transistor, we would get about 8mA * 1000 = 8A of collector current – that is more than good for solenoid to work!

Circuit diagram:

IMG_20140923_215456530

IMG_20140924_173751694IMG_20140924_173742608

#!/usr/bin/env python
import RPi.GPIO as GPIO, time
GPIO.setmode(GPIO.BCM)
GPIO.setup(SOLENOID_ENABLE, GPIO.OUT)
while 1:
    GPIO.output(SOLENOID_ENABLE, True)
    time.sleep(60)
    GPIO.output(SOLENOID_ENABLE, False)
    time.sleep(3600)

The above bit of code will water for 1 minute every 1 hour.
I suggest you enhance the code as per your requirements. You could also combine the code from the project on watering plants through pump to control the watering from gmail.

Image

Water plants through gmail.

Tags

, , , , , , , , , , , , , ,

This is a small project I did with Raspberry pi to be able to water plants with an email sent to do so!

Untitled

Here is what you need:

1. Rapberry pi

2. Hobby Motor

3. L293D

4. Rubber tube and some bottle cap

Below is the connection diagram between raspberry pi and motor chip (L293D). Credit goes to Adafruit lesson on DC motor. I followed the same connections and used the occidentalis software provided by them.

For the software to extract gmail titles to look for “Waterr” and water the plant, I followed the below code (With due credits to https://pythonadventures.wordpress.com/tag/gmail/)

 


import urllib2

import untangle

#from motor1 import run_motor

import RPi.GPIO as io
io.setmode(io.BCM)

in1_pin = 4
in2_pin = 17

io.setup(in1_pin, io.OUT)
io.setup(in2_pin, io.OUT)

def set(property, value):
try:
f = open("/sys/class/rpi-pwm/pwm0/" + property, 'w')
f.write(value)
f.close()
except:
print("Error writing to: " + property + " value: " + value)

set("delayed", "0")
set("mode", "pwm")
set("frequency", "500")
set("active", "1")

def clockwise():
io.output(in1_pin, True)
io.output(in2_pin, False)

def counter_clockwise():
io.output(in1_pin, False)
io.output(in2_pin, True)

clockwise()

def run_motor():
while True:
clockwise()
set("duty", "99")

FEED_URL = 'https://mail.google.com/mail/feed/atom'

def get_unread_msgs(user, passwd):
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(
realm='New mail feed',
uri='https://mail.google.com',
user='{user}@gmail.com'.format(user=user),
passwd=passwd
)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
feed = urllib2.urlopen(FEED_URL)
return feed.read()

##########

if __name__ == "__main__":
import getpass

# user = raw_input('Username: ')
# passwd = getpass.getpass('Password: ')
# print get_unread_msgs(user, passwd)

#make sure you change the below to yours
USER = 'USERNAME'
PASSWORD = PASSWORD'

xml = get_unread_msgs(USER, PASSWORD)
o = untangle.parse(xml)
try:
for e in o.feed.entry:
title = e.title.cdata
# print title
if title == "Waterr":
print 'Watering the raspberry pi now'
#Call the motor function here to water.
run_motor()

except IndexError:
pass # no new mail

Send email with subject as “Waterr” to see your raspberry pi watering your plants.

Untitled1

If you want to use cron tab, please follow the blog below (one of the best blogs I know on python)
http://pythonadventures.wordpress.com/2012/03/31/calling-a-python-script-from-crontab/

If you want to execute it every few minutes, it is better to use code such as below than crontab (which is better used for boot time activities).

while true:
<do something>
sleep <sometime>