TheClassicTurtleTrader
Download scriptThe Classic Turtle Trader is a specialized technical analysis tool designed to replicate the "Turtle Trading" methodology on financial charts. Its primary purpose is to identify trend reversals and price breakouts, providing clear visual cues for when to enter or exit a trade based on historical price volatility and range boundaries.
Core Mechanics
The script operates by monitoring two different types of price "channels" simultaneously:
- Entry Channels (Trade Period): These are used to identify potential new opportunities. The script looks at the highest highs and lowest lows over a specific window of time (default is 20 periods). If the market breaks outside of these boundaries, it signals a possible trend shift.
- Exit Channels (Stop Period): These are much tighter windows (default is 10 periods) used specifically to manage active trades. They act as "safety nets" to tell you when a trend has likely exhausted itself and it is time to take profits or cut losses.
Key Trading Logic
1. Entry Signals (Buy/Sell Breakouts) The script monitors for price action that exceeds the established Entry Channels.
- A Buy Signal appears when the market price climbs above the highest point reached in the recent trading window.
- A Sell Signal appears when the market price drops below the lowest point reached in that same window.
2. Exit Logic and Risk Management Once a trade is active, the script manages it using three distinct layers of logic:
- Trend Exits: The script monitors the smaller Stop Period channels. If the price breaks out of these secondary boundaries, an exit signal is generated to protect gains or minimize losses.
- Volatility-Based Stop Losses (ATR): The script calculates the "Average True Range" (ATR)—a measure of how much a currency or asset usually moves in a day. It uses this to set a mathematical stop-loss level. If the price moves against you by a specific multiple of this volatility, the script marks the trade as finished.
- Greedy vs. Loose Mode: The tool includes a "Greedy" setting. When enabled, it prevents you from exiting a trade unless it is currently in profit or your hard stop-loss has been hit.
3. Visual and Alert Feedback To make the data actionable for the user, the script provides:
- On-Chart Markers: It places specific icons on the chart to pinpoint the exact candle where a breakout (Entry) or a reversal (Exit) occurred.
- Real-Time Alerts: If the user enables notifications, the script will trigger an audible/visual alert the moment a new entry or exit condition is met, allowing for immediate reaction without having to watch the screen constantly.
Summary of Functionality
In essence, this script automates the "trend following" philosophy: it waits for the market to prove it is moving in a new direction (the breakout), enters the trade, and then constantly monitors for signs that the momentum is fading (the exit), all while using mathematical volatility to ensure the risk remains controlled.
//+------------------------------------------------------------------+
//| TheClassicTurtleTrader.mq4
//| Copyright © Pointzero-indicator.com
//+------------------------------------------------------------------+
#property copyright "Copyright © Pointzero-indicator.com"
#property link "http://www.pointzero-indicator.com"
//---- indicator settings
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
#property indicator_color3 DodgerBlue
#property indicator_color4 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 3
#property indicator_width4 3
#property indicator_style1 STYLE_DOT
#property indicator_style2 STYLE_DOT
//---- constants to know if we are in or out of the market
#define IN_THE_MARKET 0
#define OUT_OF_MARKET 1
//---- indicator parameters
extern int TradePeriod = 20; // Donchian channel period for trading signals
extern int StopPeriod = 10; // Donchian channel period for exit signals
extern bool StrictEntry = false; // Apply strict entry parameters like the Turtles did
extern bool StrictExit = false; // Apply strict exit parameters like the Turtles did
extern bool StrictStop = false; // Apply strict stop-loss like the turtled did
extern bool Greedy = false; // Do not exit a trade unless it is in profit or the SL is hit
extern bool EvaluateStoploss = true; // Check if we have been stopped out and show future signals
extern int ATRPeriod = 20; // ATRPeriod to set the stop-loss
extern double ATRStopNumber = 2; // N.Factor to calculate the stop-loss
extern bool DisplayAlerts = false; // You know...
//---- indicator buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double LongInfo[];
double ShortInfo[];
double TrendDirection[];
//---- alerts
static datetime TimeStamp;
static int AlertCount = 1;
//---- internal
double long_price;
double short_price;
double long_stop;
double short_stop;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
// Three more buffers to store long/short trading info and trend
IndicatorBuffers(7);
// Drawing settings
SetIndexStyle(0,DRAW_ARROW); SetIndexArrow(0, 233);
SetIndexStyle(1,DRAW_ARROW); SetIndexArrow(1, 234);
SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2,159);
SetIndexStyle(3,DRAW_ARROW); SetIndexArrow(3,159);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
// Name and labels
IndicatorShortName("The Classic Turtle Trader ("+ TradePeriod +"-"+ StopPeriod +")");
SetIndexLabel(0,"Buy breakout "+ TradePeriod);
SetIndexLabel(1,"Sell breakout "+ TradePeriod);
SetIndexLabel(2,"Exit longs "+ StopPeriod);
SetIndexLabel(3,"Exit shorts "+ StopPeriod);
// Buffers
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexBuffer(2,ExtMapBuffer3);
SetIndexBuffer(3,ExtMapBuffer4);
SetIndexBuffer(4,LongInfo);
SetIndexBuffer(5,ShortInfo);
SetIndexBuffer(6,TrendDirection);
Comment("Copyright © http://www.pointzero-indicator.com");
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
// More vars here too...
int start = 0;
int limit;
int counted_bars = IndicatorCounted();
// check for possible errors
if(counted_bars < 0)
return(-1);
// Only check these
limit = Bars - 1 - counted_bars;
// Check the signal foreach bar
for(int i = limit; i >= start; i--)
{
// Highs and lows
double rhigh = iHigh(Symbol(),Period(),iHighest(Symbol(), Period(), MODE_HIGH, TradePeriod,i+1));
double rlow = iLow(Symbol(),Period(),iLowest(Symbol(), Period(), MODE_LOW, TradePeriod, i+1));
double shigh = iHigh(Symbol(),Period(),iHighest(Symbol(), Period(), MODE_HIGH, StopPeriod,i+1));
double slow = iLow(Symbol(),Period(),iLowest(Symbol(), Period(), MODE_LOW, StopPeriod, i+1));
// Candle value
double CLOSE = iClose(Symbol(),0, i);
double HIGH = iHigh(Symbol(),0, i);
double LOW = iLow(Symbol(),0, i);
// Stop-losses if we traded here
double ATRv = iATR(Symbol(), 0, ATRPeriod, i) * ATRStopNumber;
double ATRup = CLOSE + ATRv;
double ATRlow = CLOSE - ATRv;
// Respect old trend
LongInfo[i] = LongInfo[i+1];
ShortInfo[i] = ShortInfo[i+1];
TrendDirection[i] = TrendDirection[i+1];
// It might be recalculating bar zero
ExtMapBuffer1[i] = EMPTY_VALUE;
ExtMapBuffer2[i] = EMPTY_VALUE;
ExtMapBuffer3[i] = EMPTY_VALUE;
ExtMapBuffer4[i] = EMPTY_VALUE;
//
// 1) Check for exits
//
// Exit longs taking into account strict exits and the greedy mode
// Loose mode only applies after the bar has closed
if(((CLOSE < slow && i > 0) && (CLOSE > long_price || Greedy == false)) ||
(LOW < slow && StrictExit == true && (LOW > long_price || Greedy == false)) &&
LongInfo[i] == IN_THE_MARKET)
{
LongInfo[i] = OUT_OF_MARKET;
ExtMapBuffer3[i] = LOW;
}
// Exit shorts taking into account strict exits and the greedy mode
// Loose mode only applies after the bar has closed
if(((CLOSE > shigh && i > 0) && (CLOSE < short_price || Greedy == false)) ||
(HIGH > shigh && StrictExit == true && (HIGH < short_price || Greedy == false)) &&
ShortInfo[i] == IN_THE_MARKET)
{
ShortInfo[i] = OUT_OF_MARKET;
ExtMapBuffer4[i] = HIGH;
}
//
// 2) Check for stoploss hits
//
// Exit longs taking into account strict stoploss
// Loose mode only applies after the bar has closed
if(((CLOSE < long_stop && i > 0) || (LOW < long_stop && StrictStop == true)) && LongInfo[i] == IN_THE_MARKET && EvaluateStoploss == true)
{
LongInfo[i] = OUT_OF_MARKET;
ExtMapBuffer3[i] = LOW;
}
// Exit shorts taking into account strict stoploss
// Loose mode only applies after the bar has closed
if(((CLOSE > short_stop && i > 0) || (HIGH > short_stop && StrictStop == true)) && ShortInfo[i] == IN_THE_MARKET && EvaluateStoploss == true)
{
ShortInfo[i] = OUT_OF_MARKET;
ExtMapBuffer4[i] = HIGH;
}
//
// 3) Check for entries
//
// Enter long trade taking into account strict entries
// Loose mode only applies after the bar has closed
if(((CLOSE > rhigh && i > 0) || (HIGH > rhigh && StrictEntry == true)) && LongInfo[i] == OUT_OF_MARKET)
{
TrendDirection[i] = OP_BUY;
LongInfo[i] = IN_THE_MARKET;
ExtMapBuffer1[i] = LOW;
long_stop = ATRlow;
long_price = CLOSE;
}
// Enter short trade taking into account strict entries
if(((CLOSE < rlow && i > 0) || (LOW < rlow && StrictEntry == true)) && ShortInfo[i] == OUT_OF_MARKET)
{
TrendDirection[i] = OP_SELL;
ShortInfo[i] = IN_THE_MARKET;
ExtMapBuffer2[i] = HIGH;
short_stop = ATRup;
short_price = CLOSE;
}
}
// Alerts
if(TimeStamp != Time[0] && DisplayAlerts == true)
{
// Exit signals
if(LongInfo[1] == OUT_OF_MARKET && LongInfo[2] == IN_THE_MARKET && AlertCount == 0)
{
Alert("[Classic Turtle Trader "+ TradePeriod +"-"+ StopPeriod +"]["+ Symbol() +"] Close longs");
} else if (ShortInfo[1] == OUT_OF_MARKET && ShortInfo[2] == IN_THE_MARKET && AlertCount == 0) {
Alert("[Classic Turtle Trader "+ TradePeriod +"-"+ StopPeriod +"]["+ Symbol() +"] Close shorts");
}
// Enter signals
if(LongInfo[1] == IN_THE_MARKET && LongInfo[2] == OUT_OF_MARKET && AlertCount == 0)
{
Alert("[Classic Turtle Trader "+ TradePeriod +"-"+ StopPeriod +"]["+ Symbol() +"] BUY");
} else if (ShortInfo[1] == IN_THE_MARKET && ShortInfo[2] == OUT_OF_MARKET && AlertCount == 0) {
Alert("[Classic Turtle Trader "+ TradePeriod +"-"+ StopPeriod +"]["+ Symbol() +"] SELL");
}
TimeStamp = Time[0];
AlertCount = 0;
}
// Bye Bye
return(0);
}
Comments