Australian (ASX) Stock Market Forum

Developing a mechanical system from scratch

Typical of shorts, we need to take our profits quicker. So we should have a separate stop for the short trades. Any suggestions on how to differentiate the codefor the two differentstops?

Heres the code i'm currently using (i changes the liquidity threshold):

Hi canuck,

I can't seem to figure out what the entry + exit criteria is:confused:

I know we are looking for a reduction in the daily range as one of the parameters, but what actually triggers the entry?

Lesm's code doesn't make much sense to me(that's saying something about me, not the code Les:))
 
Hi canuck,

I can't seem to figure out what the entry + exit criteria is:confused:

I know we are looking for a reduction in the daily range as one of the parameters, but what actually triggers the entry?

Lesm's code doesn't make much sense to me(that's saying something about me, not the code Les:))

It looks for 3 inside days with this:

InsideDay1 = H < Ref(High,-1) AND Low > Ref(Low,-1);
InsideDay2 = Ref(High,-1)< Ref(High,-2) AND Ref(Low,-1)> Ref(Low,-2);
InsideDayCond = InsideDay1 == 1 AND InsideDay2 == 1;

Then determines whether to go long or short with this:

an EMA (35), but it goes long on a close > than "EMABuyCond = EMA(C, 35);" and shorts on a low less than "EMAShortCond = EMA(L, 35);"

Here's the buy signal...it has a volumecondition too.

BuySig = InsideDayCond AND HVRatioCond AND VolumeCond AND C > EMABuyCond ;

Heres the short signal:

ShortSig = InsideDayCond AND HVRatioCond AND VolumeCond AND Low < EMAShortCond;


I'm trying to figure out how to change the cover signal...so it gets out of the short trade sooner. It identifies short really well in what i've seen so far, it just hangs on too long turning a profit into a loss.

This is kinda fun...:)

cos i'm on holidays!

Cheers,
 
It looks for 3 inside days with this:

InsideDay1 = H < Ref(High,-1) AND Low > Ref(Low,-1);
InsideDay2 = Ref(High,-1)< Ref(High,-2) AND Ref(Low,-1)> Ref(Low,-2);
InsideDayCond = InsideDay1 == 1 AND InsideDay2 == 1;

Then determines whether to go long or short with this:

an EMA (35), but it goes long on a close > than "EMABuyCond = EMA(C, 35);" and shorts on a low less than "EMAShortCond = EMA(L, 35);"

Here's the buy signal...it has a volumecondition too.

BuySig = InsideDayCond AND HVRatioCond AND VolumeCond AND C > EMABuyCond ;

Heres the short signal:

ShortSig = InsideDayCond AND HVRatioCond AND VolumeCond AND Low < EMAShortCond;


I'm trying to figure out how to change the cover signal...so it gets out of the short trade sooner. It identifies short really well in what i've seen so far, it just hangs on too long turning a profit into a loss.

This is kinda fun...:)

cos i'm on holidays!

Cheers,

OK gotcha. There doesn't seem to be any exit conditions as yet. Correct?
 
OK gotcha. There doesn't seem to be any exit conditions as yet. Correct?

Only a stop loss, so yes thats correct:
ApplyStop(stopTypeLoss, stopModePercent, amount = 10 );
But he's got this ready to be considered:

// Cover Condition

// CoverSig = condition .......

Short = ShortSig;

//Short = ExRem(ShortSig, CoverSig);
//Cover = ExRem(CoverSig, ShortSig);

...its only the "//" comment markers that disable it, it needs a condition to cover defined...so what should we use...it needs to be something that covers once you have three strong closes...like three up days...like this
 

Attachments

  • rio short and cover.jpg
    rio short and cover.jpg
    96.4 KB · Views: 197
CanOz said:
Only a stop loss, so yes thats correct:

ApplyStop(stopTypeLoss, stopModePercent, amount = 10 );
But he's got this ready to be considered:
// Cover Condition

// CoverSig = condition .......

Short = ShortSig;

//Short = ExRem(ShortSig, CoverSig);
//Cover = ExRem(CoverSig, ShortSig);
...its only the "//" comment markers that disable it, it needs a condition to cover defined...so what should we use...it needs to be something that covers once you have three strong closes...like three up days...like this

Ok thanks canuck.



Looking for 3 up days before covering a short would mean you are almost looking for a medium term trend to make a decent profit. That doesn't seem to fit in with the idea of a short term trade(well to me anyway).

I quite like the inside day idea as a set up for a short term trade, but it seems to me that it doesn't help on picking direction at all. Whilst the EMA condition will help to keep you on the right side of the overall trend, I don't think the overall trend is really that big of an issue if we are only looking at holding for a few days. My first thought would to be looking at jumping on a breakout of the group of inside days.

As for exits, I think it was Nick that mentioned it earlier in the thread- looking for range expansion as a possible exit. I quite like this idea, but have absolutely no idea how you would code it:eek:

Chart below is a good example of what I'm talking about. I know it's a long trade, but it was the first setup I saw- but it does illustrate one point fairly well- look at the difference in hold times there is when looking for a range expansion to exit, as opposed to waiting for price to move for consecutive days against you. You would be out in half the time in this case.

Looking at the RIO example you just posted, I'd be looking to code an exit to take advantage of either the first or second thrust down during the downtrend. Might be a more efficient use of capital- you'd save yourself 2 weeks hold time and achieve a similar result- you'd be able to get the system trading more often this way, which would make a pretty big difference to the overall profitability of the system.
 

Attachments

  • CBA.jpg
    CBA.jpg
    64.5 KB · Views: 192
Further to this and along Bany's lines of progress, its suggested that before we move on we select an objective function. The objective function will be used to measure the effectiveness of our system in the tests that we run. To me this makes sense as you need to decide what factors are most important to you before you start testing, otherwise what are you trying to achieve by testing?

Can we have some discussion about this?

We want things like:
1.) low drawdown
2.) frequent trades
3.) short holding period
4.) high percent annual gain
etc.
Here's my suggestions to gauge whether it is a (short term) system worth trading.
1) Drawdown < 40% - I think it will become very hard to sit through anything worse than that without wanting to intervene.
2) > 100 trades (Assuming 1 year of trades)
3) Average holding < 5 days and (average hold for losing trades) < (average hold winning trades)
4) I don't think we want to set a target for annual % gain, that will come automatically once we've taken care of the rest. Maybe consider setting targets on % winning trades (target > 35%) and win/loss ratio > 2.
:2twocents
 
Here's my suggestions to gauge whether it is a (short term) system worth trading.
1) Drawdown < 40% - I think it will become very hard to sit through anything worse than that without wanting to intervene.
2) > 100 trades (Assuming 1 year of trades)
3) Average holding < 5 days and (average hold for losing trades) < (average hold winning trades)
4) I don't think we want to set a target for annual % gain, that will come automatically once we've taken care of the rest. Maybe consider setting targets on % winning trades (target > 35%) and win/loss ratio > 2.
:2twocents
For my :2twocents here, i think the system should shoot for drawdown WAY less than 40%.

Short term systems don't have the same risk/reward parameters as long term systems and work because of greater frequency of trades. This means a recovery from say 35% DD can be a long slog.

Also that much DD on a short term system would seriously mess with your head.

I don't get much DD at all with my trading, but with a mechanical system, "some" must be accepted. I don't know what the figure should be, but perhaps 20%ish?
 
Prof...excellent thinking...could be applied to the shorts at the very least.

More on this later...got to get some dinner groceries...just woke up from a nap....gotta love holidays:)

RUB92me and Wayne...can one of you start a matrix scorecard on Excel to use as our Obj.Function...we need to propose the items such as what you listed, then weight them according to importance to us....then we can score each test.

Eg. DD, weight 10, AR - weight 4, Win rate >40% weight 6 ...etc.


Cheers,
 
Can,

Below is another variation. Think it's using Docj's suggestion from memory. Needed to add the volume/liquidity condition though.

Using the IG Markets Shares List and the period is 2/1/2006 to 18/6/2007.

Code:
/* 

ASF Sample System Development Version 1.0

*/

// Define system options can modify these as required

SetChartOptions( 2, chartShowDates|chartShowArrows ); 
SetBarsRequired(10000,10000); 
SetTradeDelays( 1, 2, 1, 2);
SetOption( "initialequity", 100000 );
SetOption( "MaxOpenPositions", 10 ); 
SetOption( "PriceBoundChecking", 1 ); 
SetOption( "CommissionMode", 1 ); 
SetOption( "CommissionAmount", 0.1 ); 

BuyPrice = SellPrice = Open;
ShortPrice = CoverPrice = Open;
Buy = Sell = Short = Cover = 0;
PositionSize = -10; // always invest 10% of the current Equity 

// Setup Conditions

VolumeCond = EMA(V * C, 21) > 500000; // Ensure at least $500k of money flow for 21 periods

// Long side - buy and sell conditions

 
BuySig = Open > Ref(C,-1)<O AND Ref(V,-1)> (2*Ref(EMA(V,13),-1)) AND C>Ref(C,-1) AND ( Ref(H,-1)-Ref(L,-1))< Ref(EMA(H-L,13),-1) AND VolumeCond;
SellSig = 1;

Buy = ExRem(BuySig, SellSig);
Sell = ExRem(SellSig, BuySig);

// Short side - Short and Cover conditions

ShortSig = Open < Ref(C,-1)<O AND Ref(V,-1)> (2*Ref(EMA(V,13),-1)) AND C>Ref(C,-1) AND ( Ref(H,-1)-Ref(L,-1))< Ref(EMA(H-L,13),-1) AND VolumeCond;
CoverSig = 1;

Short = ExRem(ShortSig, CoverSig);
Cover = ExRem(CoverSig, ShortSig);

//Define Stoploss Conditions

ApplyStop(stopTypeLoss, stopModePercent, amount = 10 );

//Lists exploration results conforming to our Buy/Sell OR Short/Cover criteria
 
Filter = Buy OR Sell OR Short OR Cover;

AddColumn(Buy, "Buy", 1, colorDefault,IIf(Buy,colorGreen,colorDefault));
//AddColumn(Sell, "Sell", 1, colorDefault,IIf(Sell,colorRed,colorDefault));
AddColumn(Short, "Short", 1, colorDefault,IIf(Short,colorGreen,colorDefault));
//AddColumn(Cover, "Cover", 1, colorDefault,IIf(Cover,colorRed,colorDefault));
AddColumn(Open, "Open", 4.3); 
AddColumn(Low, "Low", 4.3); // 
AddColumn(High, "High", 4.3); // 
AddColumn(Close, "Close", 4.3); //
 

Attachments

  • ASF Sample Development System Version 1.0.jpg
    ASF Sample Development System Version 1.0.jpg
    60.4 KB · Views: 198
I was wondering if that was going to resurface. I'm rather fond of it, but think its a fair way off being tradeable. Also, I don't think going short is as simple as reversing the buy condition. The logic for the system was based on something intended for long trades.

Need to tend to dinner, but will come back with more later. Will also run it thru tradesim.
 
I was wondering if that was going to resurface. I'm rather fond of it, but think its a fair way off being tradeable. Also, I don't think going short is as simple as reversing the buy condition. The logic for the system was based on something intended for long trades.

Need to tend to dinner, but will come back with more later. Will also run it thru tradesim.

Doc,

As you originally mentioned it was a bit quick and rough at the time, but it is a starting point.

Yes, quite true, going short is not necessarily as simple as reversing the buy condition.

If you are going to run it through Tradesim and you have the Enterprise edition, would you mind doing a quick Monte Carlo analysis on it?

Cheers.
 
Doc,

As you originally mentioned it was a bit quick and rough at the time, but it is a starting point.

Yes, quite true, going short is not necessarily as simple as reversing the buy condition.

If you are going to run it through Tradesim and you have the Enterprise edition, would you mind doing a quick Monte Carlo analysis on it?

Cheers.

Les, can you explain the differences to me...like i'm a two year old:eek:

I see you uncommented the short and cover conds too...

Cheers,
 
lesm, I'm in the process of translating your exits to metastock language. It's been a while since I've used AB, so I'm struggling a little.

I see your stop loss, but how does your code account for exiting a winning trade (a trailing stop)?
 
lesm, I'm in the process of translating your exits to metastock language. It's been a while since I've used AB, so I'm struggling a little.

I see your stop loss, but how does your code account for exiting a winning trade (a trailing stop)?

Doc,

There is only the stop loss (10%).

The AB setting sets up the buy, sell, short and cover delays, as in:

SetTradeDelays(1, 2, 1, 2) or (n=BuyDelay, n=SellDelay, n=ShortDelay, n=CoverDelay)

Buy, Sell, Short and Cover all occur on the respective open.

The buy and short will occur on the next days open or delayed by one day from the original signal.

The sell and cover are delayed by two days from the original signal.

What I am seeing in the AB backtest report is that the trade is being exited two days after the buy or short signal is being activated/executed.

If the buy or short is on the 4/4/2006 then the sell or cover is on the 6/4/2006.

SellSig = 1 and CoverSig = 1

Means that the both the sell and cover signals are set to 'True', hence 'SetTradeDelays' controls/determines the holding period and the trade is exited based on these settings.

A sell/cover condition isn't used, as an exit.

Hope this makes sense.
 
Apologies for not getting there sooner.

Monte Carlo Report
(ASF Master)

Simulation Summary
Simulation Date: 21/06/2007
Simulation Time: 10:25:18 PM
Simulation Duration: 189.38 seconds

Trade Parameters
Initial Capital: $50,000.00
Portfolio Limit: 100.00%
Maximum number of open positions: 100
Position Size Model: Fixed Percent Risk
Percentage of capital risked per trade: 2.00%
Position size limit: 100.00%
Portfolio Heat: 20.00%
Pyramid profits: No
Transaction cost (Trade Entry): $0.00
Transaction cost (Trade Exit): $0.00
Margin Requirement: 100.00%

Trade Preferences
Trading Instrument: Stocks
Break Even Trades: Process separately
Trade Position Type: Process all trades
Entry Order Type: Default Order
Exit Order Type: Default Order
Minimum Trade Size: $0.00
Accept Partial Trades: No
Volume Filter: Ignore Volume Information
Pyramid Trades: No
Use Level Zero trades only: Yes

Simulation Stats
Number of trade simulations: 20000
Trades processed per simulation: 4929
Maximum Number of Trades Executed: 2808
Average Number of Trades Executed: 2800
Minimum Number of Trades Executed: 2733
Standard Deviation: 5.16

Profit Stats
Maximum Profit: $57,227.30 (114.45%)
Average Profit: $33,368.44 (66.74%)
Minimum Profit: $9,128.87 (18.26%)
Standard Deviation: $6,088.64 (12.18%)
Probability of Profit: 100.00%
Probability of Loss: 0.00%

Percent Winning Trade Stats
Maximum percentage of winning trades: 47.04%
Average percentage of winning trades: 45.35%
Minimum percentage of winning trades: 43.69%
Standard Deviation: 0.43%

Percent Losing Trade Stats
Maximum percentage of losing trades: 43.16%
Average percentage of losing Trades: 41.17%
Minimum percentage of losing trades: 39.69%
Standard Deviation: 0.42%

Average Relative Dollar Drawdown Stats
Maximum of the Average Relative Dollar Drawdown: $146.87
Average of the Average Relative Dollar Drawdown: $125.80
Minimum of the Average Relative Dollar Drawdown: $111.38
Standard Deviation: $4.04

Average Relative Percent Drawdown Stats
Maximum of the Average Relative Percent Drawdown: 0.2650%
Average of the Average Relative Percent Drawdown: 0.2096%
Minimum of the Average Relative Percent Drawdown: 0.1640%
Standard Deviation: 0.0142%

Maximum Peak-to-Valley Dollar Drawdown Stats
Maximum Absolute Dollar Drawdown: $15,690.15
Average Absolute Dollar Drawdown: $7,673.25
Minimum Absolute Dollar Drawdown: $4,266.19
Standard Deviation: $1,101.20

Maximum Peak-to-Valley Percent Drawdown Stats
Maximum Absolute Percent Drawdown: 26.5318%
Average Absolute Percent Drawdown: 12.8804%
Minimum Absolute Percent Drawdown: 7.1799%
Standard Deviation: 1.8038%
 
Hmmm I smell a rat.

Perhaps someone else can run it while I work out what I did wrong :)
 
Doc..Not sure if you meant to leave it out but with $30 a side brokerage... might look different.

Average Trades (2800) * Brokerage (30) = $84,000.

With a max profit of $57,227.30 that would turn out to be a loss of @26,772.20
 
Indeed. Can you tell I don't use tradesim too often?:D I ran the simulation again just after and it returned a loss (I correctly included the commission the second time) hence why I posted something was amiss.

It's not a complete fowl up though. It does show the entry isn't entirely bad. The question now is whether there is anything practical we can change to make it profitable (position sizing, the exit, the universe of stocks)? Tweaking the entry to exclude stocks without an upward bias by including a long term MA would be my starting point. We need to establish whether or not the system has an edge we can exploit.

Or, alternatively, we decide the system is nice in theory, but isn't tradeable. Plenty of ideas like that around.
 
Top