Easylanguage | Trading System: Turtles

Discussion in 'Automated Trading' started by daanipd, Aug 28, 2023.

  1. daanipd

    daanipd

    Hey everyone, I have seen a plethora of publications about turtle trading strategies where rules and code are provided. The codes range from a mere Donchian breakout to a rather close representation. Without dynamic portfolio feedback, programming portfolio constraints is quite impossible as Curtis Faith explains in his well-received "Way of the Turtle." But the other components can be programmed quite close to Curtis' descriptions. I wanted to provide this code in a concise manner to illustrate some of the esoteric EasyLanguage constructions and some clean shortcuts. First, let's take a look at how the turtle trading system has performed in crude oil over the past 15 years.


    [​IMG]
    TradeStation chart of the turtle system

    If there are trends in the market, the turtle trading system will catch them. Look at how the market recovered in 2007 and immediately rebounded in 2008, and see how the Turtle caught the movements – impressive. But notice how the system stops working in congestion. It took a small portion of the 2014 downward movement and has done a great job of catching the pandemic collapse and rebound. In my last post, I programmed the LTL (Last Trader Loser) function to determine the success/failure of the Turtle System 1 entry. I modified it slightly for this post and used it in conjunction with Turtle System 2 Entry and the 1/2N pyramiding trade addon to get as close as possible to the core turtle entry/exit logic. I'll provide the ELD so you can review it in your free time, but here are the important pieces of code that you may not be able to derive without extensive programming experience.


    [​IMG]
    Crude oil turtle trading system chart in TradeStation


    [​IMG]


    This code determines if the current market position is not flat and is different from the previous bar's market position. If this is the case, then a new trade has been executed. This information is necessary to know which exit to apply without forcibly linking them using EasyLanguage Entry keywords. I just need to know the entry name. The entryPrice is the entryPrice. Here, I know if the LTL is false, and the entryPrice is equal to or greater/lesser than (based on the current market position) the System 2 entry levels, then I know that System 2 got us into the trade.


    [​IMG]


    The key to this logic is the keywords currentShares shares. This code tells TradeStation to liquidate all current shares or contracts at the stop levels. You could use currentContracts if you're more comfortable with futures. The key to this logic is the keywords currentShares shares. This code tells TradeStation to liquidate all current shares or contracts at the stop levels. You could use currentContracts if you prefer.

    Enabling the pyramiding option in TradeStation

    Before you can pyramid, you need to activate it in the Strategy Properties.
    [​IMG]
    Dialog box in TradeStation to configure pyramiding

    This logic adds positions from the originalPrice entry in increments of 1/2N. The description of this logic is a bit fuzzy. Is the value of N the ATR reading when the first contract was placed, or is it recalculated dynamically? I erred on the cautious side and used the N when the first contract was placed. Therefore, to calculate the long AddOn entries, it's enough to take the original entryPrice and add currentShares * 0.5N. So if currentShares is 1, then the next pyramid level would be entryPrice + 1 * 0.5N. If currentShares is 2, then entryPrice + 2 * 0.5N, and so on. The stop will be at 2N from the last entry. So if you put 4 contracts (as specified in Curtis' book), then the final exit would be 2N from where you added the 4th contract. Here's the code for that.


    [​IMG]

    I introduce a new variable here: cs. CS stands for currentShares, and I track it from bar to bar. If currentShares or cs is less than or equal to 1, I know that the last entryPrice was the original entryPrice. Things get a bit more complicated when you start adding positions – initially, I couldn't remember if the EasyLanguage entryPrice contained the last entryPrice or the original – turns out it's the original – good to know. So, if currentShares is greater than one and the current bar's currentShares is greater than the previous bar's currentShares, then I know I added another contract and thus should update lastEntryPrice. lastEntryPrice is calculated by taking the original entryPrice and adding (currentShares-1) * 0.5N. Now this is the theoretical entryPrice because I don't account for slippage on entry. You could make this adjustment. Therefore, once I know the lastEntryPrice, I can determine 2N from that price.

    Exiting at 2N Trailing Stop

    [​IMG]

    That's all the clever code. Below is the function and ELD for my implementation of the dual entry Turtle system. You'll see quite sophisticated code when it comes to System 1 entry, and this is due to these scenarios:

    What if you're theoretically short and are theoretically stopped out for a true loser and can enter on the same bar on a long trade? What if you're theoretically short and the reversal point would result in a losing trade? You wouldn't record the loser in time to enter the long position at the reversal point. What if you're actually short, and the reversal point would result in a true loser? Then you would want to allow the reversal at that point. There are probably other scenarios, but I think I covered all the bases. Just let me know if that's not the case. What I did to validate the entries was that I programmed a 20/10-day breakout/failure with a 2N stop and then reviewed the list and removed trades that followed a non-2N loss (10-bar exit for a loss or a win). Then I made sure those trades weren't in the full system output. There was a bit of trial and error. If you see an error, as I said, let me know.
     
  2. traider

    traider

    Does this make money?
    So complicated overfitting rulez
     
  3. daanipd

    daanipd

    It does hehe
     
  4. danielc1

    danielc1

    Can you send the code in a pdf or Word document? It looks like you have covered every step... do you also have the possibility to do it a cross multiple instruments and have a portfolio overview? I finally went for Mechanica, due the difficulty to create this in tradestation. But is years and years ago. Maybe they have evolved their product.
     
  5. SunTrader

    SunTrader

    Where's my handy dandy James Webb Space Telescope to read that?
     
    murray t turtle likes this.
  6. ph1l

    ph1l

    One of these might help,
    upload_2023-8-28_21-21-29.png
    except the images don't have enough resolution.
     
    daanipd likes this.
  7. alistera

    alistera

    I know people who tested Turtles strategy extensively in early 2000s, it was breakeven and today cannot work, it cannot sustain the 80% whipsaws.

    That’s the funny thing about this, everyone is using 30-50yr knowledge, sometimes you are lucky you find someone using 15-20yr knowledge, but are still not compatible with today, the only part of Turtles that still works is money management.
     
    daanipd and murray t turtle like this.
  8. SunTrader

    SunTrader

    Yup but GIGO to start with.
     
  9. SunTrader

    SunTrader

    Hey, finally said something logical - and without chest thumping. Do that more often.
     
    murray t turtle likes this.
  10. alistera

    alistera

    Only because you now have a piece of knowledge for free, but this is how people work when they can't do it themselves, which is why they need it 'more often', sad to be them but it is what it is.
     
    #10     Aug 29, 2023
    murray t turtle likes this.