Okay let’s see how I was able to add some eloquence to the brute force approach to this pyramiding algorithm. The original code included multiple entry directives and a ton of hard coded numerical values. So let me show you how I was able to refine the logic/code and in doing so make it much more flexible. We might lose a little bit of the readability, but we can compensate by using extra commentary.
First off, let’s add flexibility by employing input variables. In this case, we need to inform the algorithm the distance from the open to add additional positions and the max number of entries allowed for the day.
Now we need to set somethings up for the first bar of the day. Comparing the date of today with the date of yesterday is a good way to do this.
Here is a neat way to keep track of the number of entries as they occur throughout the trading day. Remember the function EntriesToday(date) will not provide the information we need.
If the last bar’s mp is not equal to the current bar’s mp then and mp is not equal to zero then we know we have added on another entry. Okay now let’s think about eliminating the “brute force” approach.
Instead of placing multiple order entry directives I only want to use one with a variable stop level. This stop level will be guided by the variable SellMult. We start the day with a wacky sell stop level and then calculate it based on the SellMult variable and PyramidDistance input.
So on the first bar of the day the sellStop = openD(0) – sellMult * pyramidDistance or sellStop = openD(0) – 1 * 5. Or 5 handles below the open. Note you an change the pyramidDistance input and make it three to match the previous examples.
Ok, we need to tell the computer to turn off the ability to place orders if one of two things happens: 1) we have reached the maxDailyEntries or 2) time >= sess1EndTime. You could make the time to stop entering trades an input as well. If neither criteria applies then place an order to sellShort at our sellStop level. If price goes below our sell stop level then we know we have been filled and the new sellStop level needs to be recalculated. See how we use a calculation to adapt the stop level with a single order placement directive? This is where the eloquence comes into play. QED.
Now you code the opposite side and then see if you can make money (hypothetically speaking of course) with it. If you think about it, why does this not work. And the not so obvious reason is that it trades too much. Other than trading too much it makes perfect sense – buy or sell by taking a nibbles at the market. If the market takes off then take a big bite. The execution costs of the nibbles are just way too great. So we need to think of a filtering process to determine when it is either better to buy or sell or when to trade at all. Good Luck with this ES [emini S&P ]day trading algorithm!