Building an Automated FOREX System: Writing the Pseudo Code
0In our last article we examined the basics of writing pseudo code and briefly looked at strategy design. If you have not read Designing a simple strategy and Pseudo Code you will need read that before continuing. In this article we will be translating the original Turtle Trader rules into pseudo code. This will ensure we have a good understanding of the trading system we will be implementing.
Writing Pseudo Code in MeataEditor
For simplicity reasons we will write our trend following system pseudo code in MetaEditor. Open MetaEditor from “[drive]:/Program Files/MetaTrader4/” folder. Alternatively you can open MetaTrader and press F4 or Tools –> Meta Quotes Language Editor.
Step One
Create a New File of type Expert Advisor. File –> New or CTRL +N.
Step Two
Click Next an enter appropriate details for the system. Call your Expert Advisor Turtle System.
Finally, you should see a code window similar to the one below:
We will be following the pseudo code convention outlined in the pseudo standard presented in the previous article. We will enter the pseudo code as comments in MetaEditor, for simplicity we will use the quote modifier. Below is sample pseudo code I have written to implement most parts of the Turtle Trading system. This is the first iteration and there are errors present in the code. Can you find the errors in the pseudo code?
// Calculate True Range
FUNCTION trueRange WITH todaysHigh AND todaysLow AND pDayClose
RETURN CALL findMax((todaysHigh - todaysLow), abs(todaysHigh - pDayClose), abs(pDayClose - todaysLow))
END FUNCTION
// Calculate N
FUNCTION calcN WITH prevN and trueRange
RETURN (19 * prevN + trueRange) / 20
END FUNCTION
// Detrmine the dollar value of a pip
// 1 lot = 100,000 base currency
// PPV = Per Pip Value
FUNCTION PPV WITH lotSize, currentPrice, accCurrency, pipVal
inCurr = pipVal / currentPrice
RETURN accCurrency * inCurr * (lotSize *100000)
END FUNCTION
// Calculate the dollar volatility of a currency pair
FUNCTION dollarVol WITH n AND ppv
RETURN n * ppv
END FUNCTION
// Calculate unit value for given account
FUNCTION calcUnit WITH dollarVol, accBalance
return (0.01 * accBalance) / dollarVol
END FUNCTION
// Misc rules
// max 12 units in one direction
// if account drops 10% reduce available equity by 20%
////////////////////// Entries ///////////////////
// System One A shorter term 20 period breakout
// buy or sell 20 period breakout
// buy or sell 1 unit
// if previous breakout winner skip trade
// losing if previous breakout 2N loss
// stops set at 2n below entry
IF currentPrice > 20periodHigh AND prevLongTradeLoser THEN
CALL buyCurr
ELSE IF currentPrice < 20periodLow AND prevShortTradeLoser THEN
CALL sellCurr
END
// System Two 55 period breakout entry
IF currentPrice > 55periodHigh AND THEN
CALL buyCurr
ELSE IF currentPrice < 55periodLow THEN
CALL sellCurr
END
// Adding units
// Additional units added stops raised by half n
// add a unit every 1n move
IF tradeLong AND currentPrice > (lngEntPrice + n) THEN
CALL buyCurr
CALL adjStop
ELSE IF tradeShort AND currentPrice < (lngEntPrice - n) THEN
CALL sellCurr
CALL adjStop
END
///////////////////Exits ///////////////////////
// System one
// 10 period low for long posn
// 10 period high for short posn
IF s1tradeLong AND currentPrice < 10periodLow THEN
CALL sellCurr
ELSE IF s1tradeShort AND currentPrice > 10periodHigh THEN
CALL buyCurr
END
// System two exit
// 20 period low for long posn
// 20 period high for short posn
IF s2tradeLong AND currentPrice < 20periodLow THEN
CALL sellCurr
ELSE IF s2tradeShort AND currentPrice > 20periodHigh THEN
CALL buyCurr
END
As you can see this code gives us an excellent starting point for the development of our Turtle Trading system. In the next article we will go through the specifics of coding in MQL. I have provided a basic implementation of the code for this trading system but you will derive most benefit from creating your own implementations of this system.
Related Articles
Category Education, FOREX, Fundamentals, MetaTrader, Programming, Trading | Tags: Code, Fundamentals, MetaEditor, MetaTrader, Pseudo Code, Turtle Trading


