Risk warning

All articles, strategies and indicators are just reflecting a single traders opinion and should be viewed as that.
I advice everybody to trade with a DEMO account!

Tuesday, January 11, 2011

Price channel strategy

Nice little strategy that can be profitable in a ranging market or when you got lots of whipsaws:

How does  this work. Add the price channel to your chart. Its one of the indicators you get with your trading platform. Then add a simple moving average with period 5 and with displace (shift) 5.
 Try this strategy on different time frames. You will see that it can be profitable on some time frames and bad on others. Actually its a counter trend strategy. Don't think only a trend strategy can be profitable. It all depends on the market conditions.. A good trader uses the right strategy at the right time!!!

How does it work:

Entry conditions for short:
When price goes above the upper price channel line

Entry conditions for long:
When price goes below the lower price channel line

Exit conditions for a long:
When the price crosses the simple moving average up.
(moving average with displace 5)

Exit conditions for a short:
When the price crosses the simple moving average down.
(moving average with displace 5)

Exit conditions for long and short:
When loss is bigger than the average true range * 10000

The code for traders using the older versions of dealbook or prostation:

strategy Price_channel_trading;
/* 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  period_price_channel = 14 ,lots =1 , periodsma = 5, shift = 5;
vars i(number),currentbar(number),top(series),bot(series),ma(series),atr(series);
begin
  Price_Channel(period_price_channel);
  Average_True_Range();
  top:=Price_Channel.line_top;
  bot:=Price_Channel.line_bot;
  atr:=Average_True_Range.line;
 
  if back(close) <  front(close) + 14 then return;
  currentbar := back(close);
 
  {entry conditions}
  if not long() and not short() and crossup (close, top) then  sell(lots); 
  if not long() and not short() and crossdown(close, bot) then  buy(lots);         
  {exit conditions}
  ma := displace(sma(close, periodsma), shift); 
  if crossdown(close, ma) and short() then  exitshort();
  if crossup  (close, ma) and long() then  exitlong();
 
  if fpl()<-10000*atr[currentbar] then
  begin
  if long() then exitlong();
  if short() then exitshort();
  end;
 
  end.

The code for traders that are using a new version of dealbook or prostation:

strategy Price_channel_trading;
/* written by ctlprogrammer for EUR/USD  */
/*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  period_price_channel = 14 ,lots =1 , period_sma = 5, shift_sma = 5;
begin
  integer i,periodpc,periodsma,shift,currentbar;
  series top, bot ,ma,atr;
  periodpc:=period_price_channel;
  periodsma:=period_sma;
  shift:=shift_sma;
  Price_Channel(period_price_channel);
  Average_True_Range();
  top:=Price_Channel.line_top;
  bot:=Price_Channel.line_bot;
  atr:=Average_True_Range.line;
 
  if back(close) <  front(close) + 14 then return;
  currentbar := back(close);
 
  {entry conditions}
  if not long() and not short() and crossup (close, top) then  sell(lots); 
  if not long() and not short() and crossdown(close, bot) then  buy(lots);         
  {exit conditions}
  ma := displace(sma(close, periodsma), shift); 
  if crossdown(close, ma) and short() then  exitshort();
  if crossup  (close, ma) and long() then  exitlong();
 
  if fpl()<-10000*atr[currentbar] then
  begin
  if long() then exitlong();
  if short() then exitshort();
  end;

Small backtest results form today on 15 min eur/usd.

24 comments:

  1. I dont understand the XL and XS..

    ReplyDelete
  2. The XL means exit long.
    The XS means exit short.

    ReplyDelete
  3. If I wanted to do crossup of a displaced moving average in a strategy how would I write that?

    In other words I want to take a long position on a crossup of a displaced 13 SMA (close, -3) and 13 SMA (open, 3)?

    Thank you

    ReplyDelete
  4. There are 2 methods you can use.

    First.

    Create 2 new displaced series and check for crossup or crossdown.

    Second

    Instead of using crossup and crossdown you just check if a line was above or under another.

    ReplyDelete
  5. That's the problem I don't know how to write those out in CTL. Could you please provide the code?

    Thank you!

    ReplyDelete
  6. I can't post code into the comments. There are always problems. You can download the code here

    https://docs.google.com/leaf?id=0B4qVe5jNBQJiYTMxOWM0YTEtMzFjMS00MzVhLWE5ODYtMzdlMGRlY2EwNzYx&hl=en_US

    If you don't understand the code. Just ask for help.

    ReplyDelete
  7. Thanks for the help! Could I trouble you for one more thing? Is it possible to add an SMS alert when an entry is made and is it possible to also SMS an alert when the position is closed and the amount of pips taken or lost?

    ReplyDelete
  8. Corrected an error in the code

    https://docs.google.com/leaf?id=0B4qVe5jNBQJiZDI4ODY0YzctODE1Yy00M2M1LWIyZmYtZDlhMjQxYWI4Mzkw&hl=en_US

    Will add SMS alert and some MM... will write post

    ReplyDelete
  9. Question: why is this line necessary

    if back(close) < front(close) + 14 then return;
    currentbar := back(close);

    ReplyDelete
  10. Because you can't calculate a simple moving average period 13 if you don't have at least 13 bars.

    ReplyDelete
  11. ha duh! thanks :-)

    ReplyDelete
  12. Hello,
    How do I compare the value of the last close with the value of a movingaverage ?
    I want to find out if a candle is above or underneath a moving average.
    (not crossup or crossdown)

    input MA1 = 10 ;
    vars ema1(series);
    Like: if close > ema1 then AND
    if close-1 < email then...

    I know that ema1 is a series, i just don't know how to access a numbervalue in it.
    thanks a lot.

    ReplyDelete
  13. First step: initialise your moving average
    Mov_Avg_Exponential();

    Then you need to get the line you need:
    ma:=Mov_Avg_Exponential.line();

    After that you can use that line:
    if close[currentbar]>ma[currentbar] then ....

    ReplyDelete
  14. Hello ,
    thank you for your quick response.

    I am still wondering about this part:
    if close[currentbar]>ma[currentbar] then ....

    close[currentbar] is the last bar in the array of all the bars, as far as I understood this.
    ma is the array of the values of the moving average, which start later, because it takes the period number of bars to calculate the first average value for that array.
    doesn't if have to be something like this:
    if close[currentbar] > ma[currentbar - period] ?

    I installed my strategy, but wheen I use it tells me "Strategy is not active".
    Looks like I forgot something.
    This is my code:


    input MA1 = 10, lot = 1 ;
    vars ema1(series), i(number);


    begin
    ema1 := ema(close, MA1);
    i := back(close);
    //if last candle closes above ema1 and
    if (close[i] > back(ema1)) AND
    //if the candle before the last closedunderneath
    // the ema1
    close[i-1] < back(ema1)-1
    then begin
    alert("", "");
    end;
    end.

    thanks again

    ReplyDelete
  15. Hi, the code corrected.

    strategy test;

    input MA1 = 10, lot = 1 ;
    vars ema1(series), i(number);

    begin
    if back(close) < front(close) + MA1 then return;

    ema1 := ema(close, MA1);
    i := back(close);
    //if last candle closes above ema1 and
    if (close[i] > ema1[i]) AND
    //if the candle before the last closedunderneath
    // the ema1
    close[i-1] < ema1[i-1]
    then begin
    alert("", "");
    end;
    end.

    ReplyDelete
  16. thanks so much for your response.
    I installed it without problems, but I am still getting the message "Strategy is not active".

    Which are the best resources to learn this ctl ?

    thanks again.

    ReplyDelete
  17. ? I am not getting that message. Where exactly are you getting that message? Which version are you using? Can you show screenshot of message. On the CTL forum you can easily add a screenshot of error message. Of course the strategy isn't really active cause you aren't buying or selling. Does it show the alarms?

    Best place to learn CTL is this blog and of course the CTL forum.

    ReplyDelete
  18. you are right, it works. I have to use chartstudio in administration modus , otherwise the new changed version of the strategy is not working. My mistake, sorry !
    Thanks for your expertice.

    ReplyDelete
  19. Hello
    Im Searching for a trailing bar exit Strategy

    for prostation. Thanks a lot for code.

    ReplyDelete
  20. trailing_stop_buy and trailing_stop_sell are the commands you can use. Check that the strategy did open a position before you order a trailer stop.

    The big disadvantage of trailing stops are that you can not do backtest so testing should be done on a demo account.

    ReplyDelete
  21. Hello
    thanks for help. So i search for a code , where the Sl stay on the last ((2) Variable) Candles/ Bars at the high/low ((2 Pips) Variable.
    everytime two bars /Candles behind the cours

    thanks

    ReplyDelete
  22. Hello
    This is my strategy but id didnt work, do you now why?

    strategy trailing;
    input periode1 = 14, periode2 =50 , lots=1;
    vars ema1(series), ema2(series),lots2=0;

    begin

    ema1 := EMA(open, periode1);
    ema2 := EMA(open, periode2);
    lots2 := 2*lots;

    if crossup(ema1,ema2)
    and not short() and not long() then
    buy(lots);

    if long () then begin trailing_stop_buy() ;


    if crossup(ema1,ema2)
    and short () then
    buy(lots2);



    if crossdown(ema1,ema2)
    and not long() and not short() then
    sell(lots);
    if long () then begin trailing_stop_sell () ;


    if crossdown(ema1,ema2)
    and long () then
    sell (lots2);



    end;
    end;
    end.


    regards

    ReplyDelete
  23. this is my second and dont work on pro station
    Why?

    strategy Trendexit;
    input periode1 = 14, periode2 =50, periode3 =5, periode4 =8, lots=1;
    vars ema1(series), ema2(series), ema3(series),ema4(series), lots2=0;

    begin

    ema1 := EMA(open, periode1);
    ema2 := EMA(open, periode2);
    ema3 := EMA(open, periode3);
    ema4 := EMA(open, periode4);
    lots2 := 2*lots;

    if crossup(ema1,ema2) then begin
    buy(lots);

    if crossdown(ema3,ema4)
    and long () then begin
    exitlong () ;


    end;

    if crossdown(ema1,ema2) then begin
    sell(lots);

    if crossup(ema3,ema4)
    and short () then begin
    exitshort ();

    end;
    end;

    end;
    end.


    regards

    ReplyDelete
  24. Hi,

    First of all add :

    barsneeded(integer) to your variables

    add this to your code:

    barsneeded:=max(max(periode1,periode2),max(periode3,periode4));
    if back(close) < front(close) + barsneeded - 1 then return;

    Also i see you buy and sell without checking if there is already a position open.

    don't write buy(lots) instead write
    if not long() then buy(lots);
    if not short() then sell(lots);

    ReplyDelete