There is no doubt that moving averages are great when the market is trending. They keep you in the trend. If you are trading with moving averages you should try to specialize in determining when a trending or ranging market is to be expected. How can you know when the market will be trending or ranging? Well here are some guidelines that may help you getting started.
Most chance of ranging market:
- when there is only 1 market open, this can be London, New York, Sydney or Tokyo
- after a strong move up or down, most of the time the market needs to consolidate after a big move
- when no data or news is released
- in or close to holiday periods
Most chance of trending market:
- when different markets are overlapping
- after a long consolidation period
- when important data or news is released
There are so many kinds of moving averages. They all have their own characteristics. The moving average that suits you best depends on your trading style and on the time frame you are trading. The purpose of this post is not to discuss them all. This post would get pages long and you would probably be bored before having read the end.
A reader asked me to write something with displaced SMA. So lets make a displaced SMA strategy and lets see how it performs
Strategy description :
Go long when the simple moving average period 13 (displaced +3) crosses the simple moving average period 13 (displaced -3) upwards
Go short when the simple moving average period 13 (displaced +3) crossed the simple moving average period 13 (displaced -3) downwards
Money management :
Use a maximum loss and a profit level. Maximum loss should be max 2% of balance. Profit level should be at least 2 times or more the maximum loss. Exact and ideal values depend on the time frame and pair you are trading.
The strategy written in CTL:
strategy SMA_displace_strat;
/*written by ctlprogrammer*/
/*Warning for demonstration purpose only, no live trading please*/
/*you can freely copy the code but plz leave this header*/
/*http://ctltrading.blogspot.com*/
input lots =1, maxloss=50, takeprofit=150, takepartialprofit=true;
begin
integer currentbar;
series smaF,smaB;
if back(close) < front(close) + 14 then return;
currentbar := back(close);
smaF := displace(sma(close,13), 3);
smaB := displace(sma(close, 13),-3);
if smaF[currentbar-4]<smaB[currentbar-4] and smaF[currentbar-3]>smaB[currentbar-3] and not long() and
not short() then
begin
buy(lots);
alert("sms","long");
end;
if smaF[currentbar-4]>smaB[currentbar-4] and smaF[currentbar-3]<smaB[currentbar-3] and not long() and not short() then
begin
sell(lots);
alert("sms","short");
end;
if long() and smaF[currentbar-3]<smaB[currentbar-3] then exitlong();
if short() and smaF[currentbar-3]>smaB[currentbar-3] then exitshort();
if fpl()>maxloss or fpl()>takeprofit then
begin
if long() then exitlong();
if short() then exitshort();
end;
end.
Same strategy but for older versions of dealbook or prostation:
strategy SMA_displace_strat;
/*written by ctlprogrammer*/
/*Warning for demonstration purpose only, no live trading please*/
/*you can freely copy the code but plz leave this header*/
/*http://ctltrading.blogspot.com*/
input lots =1, maxloss=50, takeprofit=150, takepartialprofit=true;
vars smaF(series),smaB(series),currentbar(number);
begin
if back(close) < front(close) + 14 then return;
currentbar := back(close);
smaF := displace(sma(close,13), 3);
smaB := displace(sma(close, 13),-3);
if smaF[currentbar-4]<smaB[currentbar-4] and smaF[currentbar-3]>smaB[currentbar-3] and not long() and
not short() then
begin
buy(lots);
alert("sms","long");
end;
if smaF[currentbar-4]>smaB[currentbar-4] and smaF[currentbar-3]<smaB[currentbar-3] and not long() and not short() then
begin
sell(lots);
alert("sms","short");
end;
if long() and smaF[currentbar-3]<smaB[currentbar-3] then exitlong();
if short() and smaF[currentbar-3]>smaB[currentbar-3] then exitshort();
if fpl()>maxloss or fpl()>takeprofit then
begin
if long() then exitlong();
if short() then exitshort();
end;
end.
1 month back test on EU 30 min chart with minilots:
winning trades = 26
losing trades = 9
consecutive winners = 12
consecutive losers = 3
average win/loss ratio = 0,73
profit factor = 2,11
Conclusion:
This kind of trading can be profitable. Of course you have to spend some time optimizing the maximum loss and profit taking level. This hasn't been done for the code above ! If you optimize the different parameters and you specialize in determining when a trending market is to be expected you will have a winning strategy.
I want to try a RSI filter where there is a simple moving average of the RSI signal line.
ReplyDeleteTake only a long when the RSI signal line is above the simple moving average of RSI itself and vice versa for shorts.
relative_strength(close,14);
rsi:=Relative_Strength.line;
Mov_Avg_Simple(Relative_Strength.line,20);
rsiMA:=Mov_Avg_Simple.line;
rsi[currentbar-1]>rsiMA[currentbar] //long//
I get a system error when I try to add it to the strategy....
Can you post the exact system error you are receiving? Or send the complete code so i can debug it.
ReplyDeleteinput lots =1, maxloss=50, takeprofit=150, takepartialprofit=true;
ReplyDeletebegin
integer currentbar;
series smaF,smaB, rsi, rsiMA;
if back(close) < front(close) + 14 then return;
currentbar := back(close);
smaF := displace(sma(close,13), 3);
smaB := displace(sma(close, 13),-3);
relative_strength(close,14);
rsi:=Relative_Strength.line;
Mov_Avg_Simple(Relative_Strength.line,20);
rsiMA:=Mov_Avg_Simple.line;
//Long Position//
if smaF[currentbar-4]smaB[currentbar-3] and
rsi[currentbar-1]>rsiMA[currentbar] and
not long() and
not short() then
begin
buy(lots);
alert("sms","long");
end;
//Short Position//
if smaF[currentbar-4]>smaB[currentbar-4] and smaF[currentbar-3]smaB[currentbar-3] then exitshort();
if fpl()>maxloss or fpl()>takeprofit then
begin
if long() then exitlong();
if short() then exitshort();
end;
end.
Hi,
ReplyDeleteI see your problem.
if back(close) < front(close) + 14 then return;
if back(close) < front(close) + 33 then return;
Because you can't start calculating the MA after 13 bars and for the MA you need 20 bars.
Of course there are some characters lost. There are always characters disappearing when posting code into the comments.
That makes a total of 33 bars.
That always jams me up -- thanks. Is there any way for the SMS alert to say what the profit or loss is in the message?
ReplyDeleteUse
ReplyDeletealert("sms","profit/loss="+numbertostring(fpl()));
just before closing the position.
Should work just fine.
Hello was trying the original and it seems like the longs and shorts are backwards when I ran it. FYI
ReplyDeleteI checked it again. It trades exactly like it should. But i made an error in the strategy description. I corrected it.
ReplyDeleteI added those displaced 13's on my chart and ran the strategy then looked at where positions were being taken. There were times when positions were taken taking opposite of the clear trend. Both displaced MA were heading up or down and it took opposite positions is the reason why I thought it was backwards....
ReplyDeleteHi,
ReplyDeleteI got an error when installing. Does that strategy need something else to work ?
Error is :
Ln8, Col9 : Error : Unexpected Identifier, expecting '[' or ':='
I'm using ChartStudio from WHS.
Thx for helping !
L
Are you using the code from the post or from the comments. In the comments there are always characters that are getting lost. That error looks like a typo error. You don't need anything special to run the code.
ReplyDeleteStraight from the post.
ReplyDeleteHere is the part that give an error :
_________
integer currentbar;
series smaF,smaB;
__________
there is no := or something ?
Hi again,
ReplyDeleteI finally get it working.
I juts commented the lines 9 and 10 and added :
vars currentbar(number), smaF(series), smaB(series); just before the begin.
Is it the same ?
Hi LH,
ReplyDeleteYou are WHselfinvest client and you are using prostation. Check the post again i added the code for prostation. There is a small difference in syntax. I tested it on your platform (prostation). It should work now.
And yes you did great. :-) You had found it yourself.
ReplyDeleteYeah :)
ReplyDeleteI used to be a coder (asp,php,java) since almost 15y. Basics are the same !
Thx !
Is there anyway to say the below line to just take the max data on any given time frame? I noticed that if I play around with different time frames and periods the below causes me problems because I don't really know how much to increase it. I guess I just don't fully understand it. I get that you need 14 periods of data but if I start adding other indicators and moving averages it get out of wack with it (sorry noob here).
ReplyDeleteif back(close) < front(close) + 14 then return;
Read http://ctltrading.blogspot.com/2011/05/what-does-front-back-mean.html
ReplyDeleteIt will help to understand. If you use another timeframe you don't need to change the value 14.
14 is the minimum numbers of candles needed to calculate all the indicators that are used in the strategy. If the strategy would use a 200 moving average the value would be 200 of course.
I want to thank ctlprogrammer for this blog and the willingness to help. With it I would have never been able to make a strategy!
ReplyDeleteSo I want to share a strategy I made based on what I learned here. I think it does well and it setup for 10M EUR/USD chart. If anyone has any suggestions on improvements please let me know.
https://docs.google.com/document/d/1tgGFGRgOzmOpWqUop6ztl4MO2UwbEMS2iv381IcKZeg/edit?hl=en_US
That's very nice to say. Thank you.
ReplyDeleteCan i ask you a favor? Could you explain the strategy and give some details like..
- best time frame to use
- what currency pair gives good result
- what the back test result are
-.....
I would like to post your strategy on the blog in full detail. Full credit to you of course
ctlprogrammer@gmail.com