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!

Wednesday, December 8, 2010

Third CTL strategy example. Fast Stochastics 1min + 5min + time entry with SMS alert + emergency stop.

This code had some serious errors found by a reader. This code has been corrected on 27/05/2011. The error was in the lines

if t>=t1 and t<=t2 and stoch5min[currentbar] < 24 and stoch5min[currentbar] < 17
if t>=t1 and t<=t2 and stoch1min[currentbar] < 24 and stoch5min[currentbar] < 17

if t>=t1 and t<=t2 and stoch5min[currentbar] > 76 and stoch5min[currentbar] > 83
if t>=t1 and t<=t2 and stoch1min[currentbar] > 76 and stoch5min[currentbar] > 83

not long() and not long() then
not long() and not short() then

This greatly improves the performance of the strategy.

Many thanks to the anonymous reader for pointing out the error.

First the code:
************************************************************************
strategy min1min5stochastics;


/* written by ctlprogrammer for EUR/USD timeframe 1min*/
/*Warning for demonstration purpose only, no live trading please*/
/* http://ctltrading.blogspot.com*/


input h1=6,h2=23,MaxLoss=50;
/*Warning MaxLoss must be optimized in backtest first*/


vars currentbar = 1, lots = 1,stoch1min(series),stoch5min(series),t=0,t1=0,t2=0;
begin
  currentbar := back(close);
  if currentbar < 40 then return;
 
  Fast_Stochastics(close,5,3);
  stoch1min:=Fast_Stochastics.line_d;
 
  Fast_Stochastics(close,25,15);
  stoch5min:=Fast_Stochastics.line_d;
 
  t:=hour(timestamp[currentbar])*60;
  t1:=h1*60;
  t2:=h2*60;
 
 
  if t>=t1 and t<=t2 and stoch1min[currentbar] < 24 and stoch5min[currentbar] < 17 and
  not short() and not long() then
  begin
  buy(lots);
  alert("sms","long");
  end;
  if t>=t1 and t<=t2 and stoch1min[currentbar] > 76 and stoch5min[currentbar] > 83 and
  not short() and not long() then
  begin
  sell(lots);
  alert("sms","short");
  end;


  if stoch1min[currentbar]>82 and long() then exitlong();
  if stoch1min[currentbar]<18 and short() then exitshort();
 
  if fpl()<-MaxLoss then
  begin
  if long() then exitlong();
  if short() then exitshort();
  end;
 
  end.
************************************************************************
This strategy goes long between h1 en h2 when
Fast_Stochastics(close,5,3).line_d is lower than 24 and
Fast_Stochastics(close,25,15).line_d lower then 17

This strategy goes short between h1 en h2 when
Fast_Stochastics(close,5,3).line_d is higher than 76 and
Fast_Stochastics(close,25,15).line_d higher then 83
In both case trader is warned with SMS signal.
Long position is closed when Fast_Stochastics(close,5,3).line_d > 82
Short position is closed when Fast_Stochastics(close,5,3).line_d < 18
For protection startegy has a maximum loss protection. This should be optimized before using the strategy.
Too small ==> many small losses and few wins
Too big ==> many wins but big loosers

8 comments:

  1. Hi,
    Can you elaborate on this line of coding?
    if t>=t1 and t<=t2 and stoch5min[currentbar] < 24 and stoch5min[currentbar] < 17 and
    not long() and not long() then
    I dont really understand how does the t, t1 and t2 works and most part of the latter.

    Why must there be a "<24" and which after another "< 17"? Was thinking that just a <17 would be enough.

    And there was 2 not long() statement.

    Please explain, as I'm relatively new to programming coding, thanks =)

    ReplyDelete
  2. Hi,

    Actually it is normal that you don't understand. I just saw there are multiple errors in the code. See post to see corrections. Sorry about that. Don't know how i did not see.

    t:=hour(timestamp[currentbar])*60;
    the strategy runs from 1 candle to another, this line checks what hour it is at each candle. We multiply with 60 to have a normal 24 hour format

    if t>=t1 and t<=t2
    this line checks that the hour this candle is in is between t1 and t2

    The use of timestamp,hour,minute is very bad explained in manual. I will make an entire post on this subject soon.

    ReplyDelete
  3. Hi, thanks for the correction,
    Best Regards =)

    ReplyDelete
  4. Hi,
    Would appreciate if you could post some codings or example on backtesting and optimizing of strategy as well.
    Thanks and Best Regards =)
    Emmanuel

    ReplyDelete
  5. Hi,

    I just wonder why are you so interested in this specific strategy? I wrote this when i was a total newbie. There are indeed a lot of ways to optimize this.

    Anyway you will see a post about strategy optimization soon. That topic hasn't been discussed yet. I will need a strategy to use as an example to explain. I can use this one and see what comes out after some optimization.

    ReplyDelete
  6. Hi ctlprogrammer,

    Actually i wasnt really interested in any of the specific strategies. I was just going through all the strategies that you wrote and chance upon this strategy which i didnt really understand certain points.

    As most of the people uses mt4 trader, there is little help on writing CTL language. And being a beginner in ctl, i decided to go through what you had written, line by line to have a better understanding in the codings of ctl.

    As dealbook doesnt have a backtesting program unlike mt4, (and i had been using dealbook to trade all along) i was hoping to try to program out a backtesting and optimizing program (probably with the use of a notepad to store the profit and losses since i'm not very good with database).

    Anyway, Really thanks for all the help provided to a newbie like me. I did learn much of ctl language from your site. Please do inform me if you have any other source of ctl programming which could further enrich my knowledge on ctl

    Once again, thanks for all the help provided =) Just letting you know that it is greatly appreciated by me and keep up with the good work !

    Best Regards, Emmanuel =)

    ReplyDelete
  7. There seem to be something wrong with your strategy. There isnt any green/red arrows when i run it on dealbook.
    Can you post an entry on the timestamp as well? Cant seem to get it working, thanks bro !

    ReplyDelete
  8. Hi,

    The strategy doesn't make much trades. I just copied and paste the strategy from the blog and ran it on the 1 min chart of the last 2 days. From 13/6 to 114/6 it made only 3 trades but all 3 profitable.

    Entries:
    13/06 11:17 short@1,43882
    13/06 11:39 exitshort@1,43798
    14/06 10:54 long@1,44286
    14/06 11:20 exitlong@1,44407
    14/06 14:35 short@1,44800
    14/06 15:03 exitshort@1,44716

    I used the default settings. Are you running it on EU? Do you have 5 digits? Did you load enough data?
    Most people think that a strategy that runs on a 1 min chart makes lots of trades but that isn't always true. This strategy doesn't make much trades. It isn't a scalp strategy. It makes very few trades but most of them are profitable.

    Careful with adjusting the stochastics levels 24, 17 and 76, 83 They are optimized levels for the 1 min EU chart. Other values can give the impression of performing better but in the long run the default levels will give the best result.

    You can adjust the stop loss but don't make the stop loss to small or you will kill the strategy performance.

    ReplyDelete