---
title: "Discover the fascinating &#8220;Finite State Machine&#8221;"
description: "Providing the code/structure to configure a Finite State Machine using the EasyLanguage Switch/Case structure."
url: https://www.quantifiedmodels.com/finite-state-machine-easylanguage/
date: 2022-08-20
modified: 2023-05-04
author: "Quantified Models"
image: https://www.quantifiedmodels.com/wp-content/uploads/2022/08/BLOG-WEB-Quantified-Models.png
categories: ["EasyLanguage"]
tags: ["algorithm", "code", "EasyLanguage", "filter", "FSM", "George Pruitt", "LTL", "market", "mechanism", "monitor", "operation", "portfolio", "programming", "strategy", "template", "trading", "Turtle"]
type: post
lang: en
---

# Discover the fascinating &#8220;Finite State Machine&#8221;

## Finite State Machine: Premise

An important component of the Turtle algorithm was to skip the next 20-day breakout if the previous one was a winner. I assume Dennis believed that the success/failure of a trade had an impact on the outcome of the subsequent trade. I’ve written about how you can implement this in EasyLanguage in previous posts, but I’ve gotten some questions about implementing FSM in commerce and thought this post might kill two birds with one stone: 1) provide a template that can be adapted to any The LTL mechanism and 2) provide the code/structure to configure an FSM using the EasyLanguage Switch/Case structure.

## Turtle Specific LTL Logic

Turtle LTL logic states that a trade is a loser if there is a loss of 2N after entry. N is basically an exponential moving average of TrueRange. So if the market moves 2N against a long or short position and you stop it, you have a losing trade. What makes the Turtle algorithm a bit more difficult is that it can also exit at a new 10 day low/high depending on your position. The final departure of 10 days does not mean a loss. Well, at least in this post it’s not like that. I have code that says any loss is a loss, but for this explanation, stick to a 2N loss to determine the failure of an operation.

## How to monitor operations when some of them are skipped

This is another additional layer of complexity. You have to do your own trade bookkeeping behind the scenes to determine if a losing trade occurs. Because if you have a winning trade, you skip the next trade, and if you skip that trade, how do you know if you would have been a winner or a loser? You have to run a theoretical system in parallel with the code of the real system.

Ok, let’s start by assuming that the last trade was a winner. So we disabled real trading. As the bars go by, we look for a 20-day penetration high or low. Suppose a new 20-day high is placed and a long position is established at the previous 20-day high. At this point, calculate an amount of 2N and subtract si from the theoretical entry price to get the theoretical exit price. So you have a theoMP (marketPosition) and a theoEX (exit price). This task seems simple enough, so go ahead and start looking for a day that makes a new 10-day low or crosses below your theoEX price. If a new 10-day low is placed then continue to look for a new entry and a subsequent 2N loss. If a loss of 2N occurs, it turns operations back on and continues to monitor operations, turning operations off and on again when necessary. In the following code I use these variables:

- **state – 0:** looking for an input or 1: looking for an output
- **lep** – long entry price
- **sep** – short entry price
- **seekLong** – I am looking for a long position
- **seekShort** : I am looking for a short position
- **theoMP** – theoretical market position
- **theoEX** – theoretical starting price
- **lxp** – long starting price
- **sxp** – short starting price

Let’s jump into the Switch / Case structure when state = 0:

```cpp
Switch(state)
	Begin
		Case 0:
			lep = highest(h[1],20) + minMove/priceScale;
			sep = lowest(l[1],20) - minMove/priceScale;
			If seekLong and h >= lep then 
			begin
				theoMP = 1;
				theoEX = maxList(lep,o) - 2 * atr; 
//				print(d," entered long >> exit at ",theoEX," ",atr);
			end;
			If seekShort and l  1 then n = (n*19 + trueRange)/20;

If useLTLFilter then
Begin
	if ltl then buy next bar at highest(h,20) + minMove/priceScale stop;
	if ltl then sellShort next bar at lowest(l,20) -minMove/priceScale stop;
end
Else
Begin
	buy next bar at highest(h,20) + minMove/priceScale stop;
	sellShort next bar at lowest(l,20) -minMove/priceScale stop;
end;

mp = marketPosition;

If mp  0 and mp[1]  mp then NLossAmt = 2 * n;

If mp = 1 then
Begin
	Sell("LL10-LX") next bar at lowest(l,10) - minMove/priceScale stop;
	Sell("2NLS-LX") next bar at entryPrice - NLossAmt stop;
end;
If mp =-1 then
Begin
	buyToCover("HH10-SX") next bar at highest(h,10) + minMove/priceScale stop;
	buyToCover("2NLS-SX") next bar at entryPrice + NLossAmt stop;
end;
```

Strategy Code Using LTL filter

This code basically replicates what we did in the FSM, but places actual orders based on the fact that the last trade was a loser (ltl.)

### **Works? Only trade after a 2N loss**

[![LTLExample 1](https://www.quantifiedmodels.com/wp-content/uploads/2022/08/LTLExample-1.webp)](https://www.quantifiedmodels.com/wp-content/uploads/2022/08/LTLExample-1.webp)

[![NOLTLCrude](https://www.quantifiedmodels.com/wp-content/uploads/2022/08/NOLTLCrude.webp)](https://www.quantifiedmodels.com/wp-content/uploads/2022/08/NOLTLCrude.webp)

No filter in the last 10 years in crude

[![LTLCrude](https://www.quantifiedmodels.com/wp-content/uploads/2022/08/LTLCrude.webp)](https://www.quantifiedmodels.com/wp-content/uploads/2022/08/LTLCrude.webp)

With filter in the last 10 years in crude

I have programmed this into my TradingSimula-18 software and will show portfolio performance with this filter a bit later on www.trendfollowingsystems.com.

I had to do a bit of tinkering with some codes due to the fact that you can exit and re-enter the same bar. In the next post of this blog I will tell you about those machinations. With this template, you should be able to recreate any last trade that was a losing mechanism and see if it can help you with your own trading algorithms.

Article courtesy of George Pruitt

## Quantified Models Youtube Channel

On our YouTube channel we have several videos available that you may find very useful for developing trading systems. To access, click this link: Quantified Models YouTube Channel

We hope this information has been useful to you.

[Watch our videos](https://www.youtube.com/channel/UC1aM_BnnBAWRSjiApHVGD5w)
