Designing and Building a Profitable Automated Trading System

Quote from ScottD:

Here is the code for version 0.1 of the CashCow ATS. The name is nice and it's red rags for bull. In order for me to code stuff, I have to make the concepts very simple to match my lack of fluency in the programming language. As a side effect, it should be very easy to read.

I have started to bring some MACD language into the code so that later I can form some logical constructs with it, but for now it's commented out. Anything in between curly brackets { ... } is commented out for now.

Attached is a screen shot of the automated trade decisions. On the chart, you'll see labels for Long Entry (...LE), Short Entry (...SE), Long Exit (...LX), Short Exit (...SX) and, for today, End of Day Short Exit (EODSX). You will also see color coded vertical arrows showing the exact bar of entry and exit and horizontal arrows showing the exact price of entry and exit.

Technically, it made net profits today with a 4/6 win rate. Realistically the P&L is irrelevant at this stage because the engine is extremely rudimentary and not yet even close to what Jack is describing. Having a rudimentary engine to improve is a nice start though.

{
CashCow automated trading system
Architect: Jack Hershey
Version 0.1
11 Dec 2008
}

variables:
FastK5( 0 ), FastD5( 0 ), SlowK5( 0 ), SlowD5( 0 ),
FastK14( 0 ), FastD14( 0 ), SlowK14( 0 ), SlowD14( 0 ) ;

if Time > 945 and Time < 1600 then begin

Value1 = Stochastic( H, L, C, 5, 2, 3, 1, FastK5, FastD5, SlowK5, SlowD5 ) ;
Value2 = Stochastic( H, L, C, 14, 1, 3, 1, FastK14, FastD14, SlowK14, SlowD14 ) ;

condition1 = CurrentBar > 2 and FastK5 crosses over 50 ;
if condition1 and marketposition = 0 then
Buy ( "FastK5LE" ) 1 contract this bar at close ;

condition2 = CurrentBar > 2 and FastK5 crosses below 50 ;
if condition2 and marketposition = 0 then
Sellshort ( "FastK5SE" ) 1 contract this bar at close ;

condition3 = CurrentBar > 2 and (SlowK14 < SlowD14) and (SlowK14 crosses below 80) ;
if condition3 and marketposition > 0 then
Sell ( "SlowK5LX" ) 1 contract this bar at close ;

condition4 = CurrentBar > 2 and (SlowK14 > SlowD14) and (SlowK14 crosses above 20) ;
if condition4 and marketposition < 0 then
buytocover ( "SlowK5SX" ) 1 contract this bar at close ;


variables: MACDVal( 0 ), MACDMA( 0 ), MACDHist( 0 ) ;

MACDVal = MACD( Close, 5, 13 ) ;
MACDMA = XAverage( MACDVal, 6 ) ;
MACDHist = MACDVal - MACDMA ;
{
condition5 = CurrentBar > 2 and absvalue( MACDVal ) < 1.4 ;
if condition5 and marketposition > 0 then
Sell ( "MACDLX" ) this bar at close ;
if condition5 and marketposition < 0 then
Buytocover ( "MACDSX" ) this bar at close ;
}

end;

if Time > 1600 and marketposition > 0 then sell ( "EODLX" ) next bar at market;
if Time > 1600 and marketposition < 0 then buytocover ( "EODSX" ) next bar at market;

I see this is set up for a rocket entry (MACD hist .1.4) based on STOCH5 direction after a three bar time synch.

Conservative but useful for first pass.

I see the exit is STOCH 14 stuff.

You need to slip in a way to do a reversal if the new entry is BEFORE the old exit.

you said you had two losses. throw up the chart and I will focus on the coding here to eliminate losses as a starter. then we go to work on coding.
 
Quote from ScottD:

Here is the code for version 0.1 of the CashCow ATS. The name is nice and it's red rags for bull. In order for me to code stuff, I have to make the concepts very simple to match my lack of fluency in the programming language. As a side effect, it should be very easy to read.

I have started to bring some MACD language into the code so that later I can form some logical constructs with it, but for now it's commented out. Anything in between curly brackets { ... } is commented out for now.

Attached is a screen shot of the automated trade decisions. On the chart, you'll see labels for Long Entry (...LE), Short Entry (...SE), Long Exit (...LX), Short Exit (...SX) and, for today, End of Day Short Exit (EODSX). You will also see color coded vertical arrows showing the exact bar of entry and exit and horizontal arrows showing the exact price of entry and exit.

Technically, it made net profits today with a 4/6 win rate. Realistically the P&L is irrelevant at this stage because the engine is extremely rudimentary and not yet even close to what Jack is describing. Having a rudimentary engine to improve is a nice start though.

Code:
{
CashCow automated trading system
Architect: Jack Hershey
Version 0.1
11 Dec 2008
}

variables:
FastK5( 0 ), FastD5( 0 ), SlowK5( 0 ), SlowD5( 0 ),
FastK14( 0 ), FastD14( 0 ), SlowK14( 0 ), SlowD14( 0 ) ;

if Time > 945 and Time < 1600 then begin

Value1 = Stochastic( H, L, C, 5, 2, 3, 1, FastK5, FastD5, SlowK5, SlowD5 ) ;
Value2 = Stochastic( H, L, C, 14, 1, 3, 1, FastK14, FastD14, SlowK14, SlowD14 ) ;

condition1 = CurrentBar > 2 and FastK5 crosses over 50 ;
if condition1 and marketposition = 0 then                                                     
	Buy ( "FastK5LE" ) 1 contract this bar at close ;

condition2 = CurrentBar > 2 and FastK5 crosses below 50 ;
if condition2 and marketposition = 0 then                                                     
	Sellshort ( "FastK5SE" ) 1 contract this bar at close ;
	
condition3 = CurrentBar > 2 and (SlowK14 < SlowD14) and (SlowK14 crosses below 80)  ;
if condition3 and marketposition > 0 then                                                     
	Sell ( "SlowK5LX" ) 1 contract this bar at close ;

condition4 = CurrentBar > 2 and (SlowK14 > SlowD14) and (SlowK14 crosses above 20)  ;
if condition4 and marketposition < 0  then                                                     
	buytocover ( "SlowK5SX" ) 1 contract this bar at close ;


variables:  MACDVal( 0 ), MACDMA( 0 ), MACDHist( 0 ) ;

MACDVal = MACD( Close, 5, 13 ) ;
MACDMA = XAverage( MACDVal, 6 ) ;
MACDHist = MACDVal - MACDMA ;
{
condition5 = CurrentBar > 2 and absvalue( MACDVal ) < 1.4 ;
if condition5 and marketposition > 0 then                                   
	Sell ( "MACDLX" ) this bar at close ;
if condition5 and marketposition < 0 then                                   
	Buytocover ( "MACDSX" ) this bar at close ;
}
	
end;

if Time > 1600 and marketposition > 0 then sell ( "EODLX" ) next bar at market;
if Time > 1600 and marketposition < 0 then buytocover ( "EODSX" ) next bar at market;


<img src=http://elitetrader.com/vb/attachment.php?s=&postid=2215939 width=800>
click on attachment to enlarge image
 
LOL that is yesteday.... Two trades before I recognized it.

can we set up to post a list of trades as they occur like a log of some sort.

The STOCH5 X over 50 and the STOCH14 80/20 exit are in good synch. I'll find the two "losers" and see if the cases from yesterday fixed them.
 
At this point it looks like the code is cycling at the end of every bar (once evey five minutes). This is like the screen is the source of the data and not an external stream of data.

Dark ages type use of PC.

Can you confirm that your ATS coding doesn't get information very often?

Where I am going with the conversation is dealing with forming bars and how to code C and D and.........both entwine and how to do suppression with MLR's etc.

So far we are just doing "chunking" with slugs of H, L and C's at the end of a 5 minute bar. Display type historical data. Talking head type stuff on TV.
 
Updated 10 Dec 2008 (yesterday) chart attached, showing trades from updated logic engine.

As desired, we can focus on the same day to see how the code changes impact the same time series of data.

Current screen technology allows one to see this stuff in its native resolution.

It's nice not to have to downgrade the image in the feedback information loop.

24" or 30" screens are useful for trading.

17" was a common size for a black and white TV when I was a young lad, so I do have some experience with it.

17"x5 may not be as effective as 30"x1.

Chart labels
S5FKLE: Stoch5 FastK Long Entry
S5FKSE: Stoch5 FastK Short Entry
S5FKRL: Stoch5 FastK Reverse Long
S5FKRS: Stoch5 FastK Reverse Short
S14KDLX: Stoch14 KxD Long Exit
S14KDSX: Stoch14 KxD Short Exit
and so on...

Long Entry bar and price: Green arrows
Long Exit bar and price: Blue arrows
Short Entry bar and price: Purple arrows
Short Exit bar and price: Dark Yellow arrows

In the next post I will attach a log of trades.

Version 0.2 of the logic engine below.

Additional capability and enhancement as follows:

- Built the Reversal logic, using the Stoch5 FastK.
System now takes many more trades.

- Added a quick and clean Medium volume threshold qualifier for entry and reversal.

- Cleaned up and standardized the nomenclature as discussed.

- Better structured the code for intuitive understanding and natural reading.

- Added some commentary behind double forward slash // symbol

Comments:

At the moment I deliberately have the system configured to make decisions and to take actions at the end of each bar, instead of at every tick.

This makes the logic much easier to validate in the chart.

When we are ready for prime time, I can toggle the switch and change to tick-by-tick decisions and actions.

If I were to do it now, it would plot entries that don't appear to be validated in the chart
(temporal intrabar crosses that don't close as crosses, and that sort of thing).

Code:
{
CashCow automated trading system
Architect: Jack Hershey
Version 0.2
Date 11 Dec 2008
}

variables:
Stoch5FastK( 0 ), Stoch5FastD( 0 ), Stoch5SlowK( 0 ), Stoch5SlowD( 0 ),
Stoch14FastK( 0 ), Stoch14FastD( 0 ), Stoch14SlowK( 0 ), Stoch14SlowD( 0 ) ;

if CurrentBar > 2 and Time > 945 and Time < 1600 then
begin
	Value1 = Stochastic( H, L, C, 5, 2, 3, 1, Stoch5FastK, Stoch5FastD, Stoch5SlowK, Stoch5SlowD ) ;
	Value2 = Stochastic( H, L, C, 14, 1, 3, 1, Stoch14FastK, Stoch14FastD, Stoch14SlowK, Stoch14SlowD ) ;

	//Entry logic
	if Volume of 1 bar ago > 20000 then  //enter only when volume greater than 'Medium'
	begin
		if CurrentBar > 2 and Stoch5FastK crosses above 50 
		and Stoch5FastK > Stoch5FastD and marketposition = 0 then

			Buy ( "S5FKLE" ) 1 contract this bar at close ;

		if CurrentBar > 2 and Stoch5FastK crosses below 50 
		and Stoch5FastK < Stoch5FastD and marketposition = 0 then

			Sellshort ( "S5FKSE" ) 1 contract this bar at close ;
	end;

	//Reverse logic
	If Volume of 1 bar ago > 20000 then
	begin
		if marketposition > 0 and Stoch5FastK crosses below 50 then
			sellshort ( "S5FKRS" ) 1 contracts this bar at close ;

		if marketposition < 0 and Stoch5FastK crosses above 50 then
			buy ( "S5FKRL" ) 1 contracts this bar at close ;
	end;

	//Exit logic
	if {(Stoch14SlowK < Stoch14SlowD) and} 
	(Stoch14SlowK crosses below 80) and marketposition > 0 then                  
		Sell ( "S14SKLX" ) 1 contract this bar at close ;

	if {(Stoch14SlowK > Stoch14SlowD) and} 
	(Stoch14SlowK crosses above 20) and marketposition < 0  then                                  
		buytocover ( "S14SKSX" ) 1 contract this bar at close ;
	
	if marketposition > 0 and Stoch14SlowK crosses below Stoch14SlowD then
		sell ( "S14KDLX" ) 1 contract this bar at close ;

	if marketposition < 0 and Stoch14SlowK crosses above Stoch14SlowD then
		buytocover ( "S14KDSX" ) 1 contract this bar at close ;

	if marketposition > 0 and Stoch5FastK crosses below 50 then
		sell ( "S5FKSX" ) 1 contracts this bar at close ;

	if marketposition < 0 and Stoch5FastK crosses above 50 then
		buytocover ( "S5FKLX" ) 1 contracts this bar at close ;

	{
	MACD Exit logic stub
	Commented out.  Will be brought to life later.
	variables: MACDVal( 0 ), MACDMA( 0 ), MACDHist( 0 ) ;
	MACDVal = MACD( Close, 5, 13 ) ;
	MACDMA = XAverage( MACDVal, 6 ) ;
	MACDHist = MACDVal - MACDMA ;
	if absvalue( MACDVal ) < 1.4 and marketposition > 0 then 
		Sell ( "MACDLX" ) this bar at close ;
	if absvalue( MACDVal ) < 1.4 and marketposition < 0 then     
			Buytocover ( "MACDSX" ) this bar at close ;
	}
end;

//Flatten at end of day
if Time > 1600 and marketposition > 0 then sell ( "EODLX" ) next bar at market;
if Time > 1600 and marketposition < 0 then buytocover ( "EODSX" ) next bar at market;

<img src=http://elitetrader.com/vb/attachment.php?s=&postid=2216805 width=800>
click on attachment link to enlarge image
 

Attachments

Back
Top