Jake Bernstein code

Jake Bernstein has a momentum strategy that I'd like to backtest but being relatively new to AFL and coding in general, it's beyond my knowledge base and I'm not finding anything in the forum or online that helps, so if anyone can assist with code for this, I'd appreciate it!

  1. The first rule is price must make a 60 day low: L==LLV(L,60);
  2. 28 period Momentum indicator must be higher than the lowest low of Momentum in the prior 60 bars(in other words, diverging with price).
    I can create Momentum(C,28)>LLV(Momentum, 28) but I don't know how to create the syntax of what the rule is, of being higher than the lowest the indicator has been in the prior 60 bars.
  3. The lowest low of Momentum must occur 6 bars or more from rule 1.
  4. Find the highest Momentum value between today(rule 1) and the lowest Momentum low in the prior 60 bars.
  5. Entry trigger is when Momentum closes above the highest Momentum value found in rule 4.
    That's it. Thanks for any assistance.

Please go here first then you may get that code assistance.

13


BTW, you are missing exit rule

Thanks fxshrat. I did complete the account verification. I can do my own exit code. I just need help with the rest. Thanks.

Below is entry rule translation.

Note: I think rule 4 of post #1 rather has to be:
"4. Find the highest Momentum value between today(rule 1) and the violation/cross of the lowest Momentum low of the prior 60 bars (see rule 2 and following)."

Otherwise it would make no sense as rule 2 would have no connection then.

/// Jake Bernstein Momentum strategy entry rules
/// Rules translation to AFL by fxshrat@gmail.com feat. Dougrox
/// @link https://forum.amibroker.com/t/jake-bernstein-code/19301/4
/// The first rule is price must make a 60 day low
LLV_period = 60;
mom_period = 28;
rule1 = L == LLV(L,-LLV_period);
/// 28 period Momentum indicator must be higher than the lowest low of Momentum 
/// in the prior 60 bars(in other words, diverging with price).
mom = C - Ref(C,-mom_period);
mom_llv = Ref(LLV(mom,LLV_period), -1);
rule2 = mom > mom_llv;
/// The lowest low of Momentum must occur 6 bars or more from rule 1.
bars_rule1 = BarsSince(rule1);
bars_llv = Ref(LLVBars(mom,LLV_period), -1);
rule3 = rule2 AND bars_rule1-bars_llv-1 >= 6;
/// Find the highest Momentum value between today(rule 1) and the cross of lowest Momentum low in the prior 60 bars.
highest_mom = ValueWhen(rule3, HighestSince(rule1, mom));
/// Entry trigger is when Momentum closes above the highest Momentum value
rule4 = Cross(mom, Ref(highest_mom, -1));
///
Buy = Ref(rule4, -1);
BuyPrice = Open;
///
Plot( C, "Price", colorDefault, styleBar );
Plot( mom, "mom", colorGold, styleLeftAxisScale );
Plot( highest_mom, "highest_mom", colorGreen, styleLeftAxisScale );
PlotShapes(Buy*shapeUpArrow, colorGreen, layer = 0, y = L );
8 Likes

There is a small typo in rule1.
It rather should be

rule1 = L == LLV(L,LLV_period);

So replace it or simply remove minus sign.

1 Like

Thanks so much fxshrat! Greatly appreciated!

Although the Momentum indicator is a function in Amibroker and can be placed on a chart, it's not recognized for coding, as is RSI or Stoch. It's saying it needs an identifier. Can some other code be added so the formula editor recognizes the Momentum indicator? Thanks.

@Dougrox,

Momentum is C - C x periods ago.

momentum = c-ref(C,-10);  // 10 period momentum

@Dougrox & @snoopy.pa30,

momentum already is in the code of post#4!
It is just difference between current array to previous array.
There is zero magic about it. See,

Quote:

To construct a 10-day momentum line, simply subtract the closing price 10 days ago from the last closing price.


There is zero syntax error in that code of post #4 also there is no momentum function in that code.
So I have no idea what you are referring to.

No one knows your screen and no one can see your screen.
Please read here urgently.

The Momentum function you are talking about is AFL function and part of Indicators folder of Charts window.
It is not natively inbuilt function because it is too simple (I guess that's the reason).

You have to copy that function of Indicators folder over to upper code placed at top.
But what for?
You just need single line for that code (already there) since it is just difference as explained above.
Functions are rather used for multiple calls.


As for the error you are getting in your editor...
Since you do not name the error number (Why not?) I guess you are talking about Error 30.
13

You can't do that since Momentum is not part of list of AFL function reference.
So you have to add custom function definition before.

function Momentum(array, period) {
	/// part of Charts - Indicators
	return array - Ref(array,-period);
}

mom = Momentum(C, 20);

or place custom function(s) to an Include file and then include that file via #Include command

13

2 Likes