Monthly Archives: May 2018

Keeping track of intra-month profit or loss

In real time trading I have noticed that once you reach a certain loss for the month its best, sometimes, to circle the wagons and quit trading until the beginning of the next month. This concept works best for very short term or day trade algorithms, as its very easy to get started back up. You can do this with Trend Following, but you must build a logical and replicable process for re-entering existing positions. Let’s assume a trading algorithm whose averaging losing month is $1500 and you are currently down $2000 – what are the chances that you will revert to the mean or draw down further? Probably 50/50. Who knows you might turn around and actually make money by month’s end. If you review a track record of a hedge fund manager, trader, or algorithm and they show a bar chart of monthly returns and there sticking out like a sore thumb is a big down bar, that kind of makes you think that could happen again. If you can control the monthly downside without sacrificing the existing Profit:DrawDown ratio, then why not do it.

Sample Code To Monitor IntraMonth $P/L

Here I am comparing the month of the current bar against the month of the prior bar. If they are not equal, then we have a new month. Store the netProfit in the variable begMonthProf. All you have to do is compare the current bar’s netProfit to begMonthProf and make a decision. Here is some code:

If the day of the month is greater than 15 (month half over) and the difference between the current netProfit and begMonthProfit is greater than a negative intraMonthMaxLoss then quit trading for the month. Only turn it back on the first bar of the next month. See how this works for your algos.

John Ehlers used the following EasyLanguage code to calculate the Dominant Cycle in a small sample of data. If you are interested in cycles and noise reduction, definitely check out the books by John Ehlers – “Rocket Science for Traders” or “Cybernetic Analysis for Stocks and Futures.” I am doing some research in this area and wanted to share how I programmed the indicator/function in Python. I refer you to his books or online resources for an explanation of the code. I can tell you it involves an elegantly simplified approach using the Hilbert Transform.

In my Python based back tester an indicator of this type is best programmed by using a class. A class is really a simple construct, especially in Python, once you familiarize yourself with the syntax. This indicator requires you to refer to historical values to calculate the next value in the equation: Value1[4], inPhase[1], re[2], etc.,. In EasyLanguage these values are readily accessible as every variable is defined as a BarArray – the complete history of a variable is accessible by using indexing. In my PSB I used lists to store values for those variables most often used such as Open, High, Low, Close. When you need to store the values of let’s say the last five bars its best to just create a list on the fly or build them into a class structure. A Class stores data and data structures and includes the methods (functions) that the data will be pumped into. The follow code describes the class in two sections: 1) data declaration and instantiation and 2) the function to calculate the Dominant Cycle. First off I create the variables that will hold the constant values: imult and qmult. By using the word self I make these variables class members and can access them using “.” notation. I will show you later what this means. I also make the rest of the variables class members, but this time I make them lists and instantiate the first five values to zero. I use list comprehension to create the lists and zero out the first five elements – all in one line of code. This is really just a neat short cut, but can be used for much more powerful applications. Once you create a dominantCycleClass object the object is constructed and all of the data is connected to this particular object. You can create many dominantCycleClass objects and each one would maintain its own data. Remember a class is just a template that is used to create an object.

class dominantCycleClass(object):
def __init__(self):
self.imult = 0.635
self.qmult = 0.338
self.value1 = [0 for i in range(5)]
self.inPhase = [0 for i in range(5)]
self.quadrature = [0 for i in range(5)]
self.re = [0 for i in range(5)]
self.im = [0 for i in range(5)]
self.deltaPhase = [0 for i in range(5)]
self.instPeriod = [0 for i in range(5)]
self.period = [0 for i in range(5)]

Data Portion of Class

The second part of the class template contains the method or function for calculating the Dominant Cycle. Notice how I index into the lists to extract prior values. You will also see the word self. preceding the variable names used in the calculations. Initially I felt like this redundancy hurt the readability of the code and in this case it might. But by using self. I know I am dealing with a class member. This is an example of the ” . ” notation I referred to earlier. Basically this ties the variable to the class.

Here I assign domCycle the object created by calling the dominantCycleClass constructor. TempVal1 is assigned the Dominant Cycle when the function or method is called using the objects name (domCycle) and the now familiar ” . ” notation. See how you can also access the imult variable using the same notation.

Here is the code in its entirety. I put this in the indicator module of the PSB.