# Geometric Brownian Motion using Python.

This post is going to be a little different than what I usually post. I have recently joined a new organization in the finance domain. They are into trading and stuff. So you can trade Forex pairs, indices, commodities etc on their website. They have also generated their own underline named as volatility which can also be used for trading and to my surprise that is the most traded underline on our website. Not Forex, not metals but volatility indices are the most traded underline.

So I just thought can I also generate my own trading underline something similar. After some googling and consulting my seniors, I came across Geometric Brownian Motion  which can be used to generate such feeds.

The formula for feed generation is quite scary –

So you start with any initial value and then generate next values using the above formula. Most of which are constants.

To explain the formula –

S  is the current value of the feed.
So – is the previous value of the feed.
µ- 0
sigma – 0.01
Wt – random number between -1, 1.

So that’s it with all this ingredients, we are ready to generate our own feed. Now, this needs to be a continuous process. So we would also require a loop for that. Just for the records I am also writing each value into a text file.

```from __future__ import division
import matplotlib.pyplot as plt
import math
import random
import time
#Generating random number using geometric Brownian motion
def dataListener() :
#Initializations of the constants
startingIndex = 1000
mu = 0
sigma = 0.01
flag = True
i = 1
try:
while(flag):
file = open('example.txt', 'a')
expression = (mu - (0.5 * sigma ** 2)) * (2/(365 * 86400)) + (sigma *
random.uniform(-1, 1))
startingIndex = startingIndex * math.exp(expression)
file.write(str(startingIndex) + "," + str(i) + "\n")
print str(startingIndex) + "," + str(i)
i = i + 1
file.close()
time.sleep(2)
except KeyboardInterrupt:
print("\nDone!!")

if __name__ == "__main__":
dataListener()
```

This generates a continuous feed and writes the value in the console and revolves exactly the way I wanted. Goes positive/negative in both the ways. Now, the next target was to draw a graph which shows this continuous value. Finding an appropriate method for this one too took lot of time. Tried and tested various things and you know what, finally it was the basic `matplotlib` which came to my rescue.

From this  python programming tutorial, I tweaked it into something which I can use. The above code writes the data into a text file, whereas this data reads it from the same text file and plots a graph which changes real time.

```import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib import style
import randomNumberGeneration
style.use('fivethirtyeight')
fig = plt.figure()

def animate(i):
lines = graph_data.split('\n')
xs = []
ys = []
for line in lines:
if len(line) > 1:
x, y = line.split(',')
xs.append(x)
ys.append(y)
ax1.clear()
ax1.plot(ys, xs)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
```