Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Python Object Oriented Programming
#1

I was coding a reinforcement learning algorithmic trading strategy in Python. I had to create a number of classes for the reinforcement learning trading trading. If you want to use Python for algorithmic trading then you should have a good grasp over its object oriented programming features. Some say Python is weak in object oriented programming department. I don't agree with that. I decided to starts a thread on Python Object Oriented Programming so as to reinforce my knowledge in this department. Design patterns are very important when it comes to coding. The focus should be on writing code that is efficient. Most new coders will learn this skill over time. When you get time, read a good book on Python Design Patterns.

Algorithmic trading is all about simulation, simulating different trading scenarios and choosing the best scenario. Object oriented programming was also born when someone tried to write simulation code. As I said in the first paragraph, writing efficient code is very important when it comes to algorithmic trading. Latency should be reduced to the minimum. Latency means the time taken to execute a set of commands. When we are coding an algorithmic trading strategy for high frequency data like 1 minute, writing efficient code becomes extremely important.  So our aim should be to write code that runs fast and doesn't waste the system resources. Let's write a small program that measure the time it taken for a program to execute:

from time import sleep, time
#define a function we use sleep for now instead of actual execution
def TradeOpen(sleepTime=0.2):
    sleep(sleepTime)
#define a function to measure how much time taken to execute
def TimeTaken(func, *args, **kwargs):
    t=time()
    func(*args, **kwargs)
    print(func.__name__, 'Time Taken For Execution', time()-t)

TimeTaken(TradeOpen, sleepTime=1)

We already know what is the time taken by this program as it is doing nothing just sleeping. However the above program can be modified and used to measure the time taken by any program to execute. This was the output:

def TimeTaken(func, *args, **kwargs):
    t=time()
    func(*args, **kwargs)
    print(func.__name__, 'Time Taken For Execution', time()-t)


TimeTaken(TradeOpen, sleepTime=1)
TradeOpen Time Taken For Execution 1.0007438659667969

Did you notice *args and *kwargs in the above code? What is this? When you don't know before hand how many parameters to pass in a function definition, *args lets you pass a variable number of parameters in the function definition. Most Python coders use *args for this. You can use *vars if you want just keep this in mind * is important rest you can change. In the same manner **kwargs is an unknown dictionary that you don't know beforehand. Everything in Python is an object. You should remember this. This is very important. Let's define a very simple class:

class AlgorithmicTrading():
    pass

print(type(AlgorithmicTrading()))
<class '__main__.AlgorithmicTrading'>

Most of the time we will be using this pass command when defining algorithmic trading strategies. We will use pass in the beginning when we don't have enough idea about our algorithmic trading strategy.

Subscribe My YouTube Channel:
https://www.youtube.com/channel/UCUE7VPo...F_BCoxFXIw

Join Our Million Dollar Trading Challenge:
https://www.doubledoji.com/million-dolla...challenge/
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)