Recreating code from "Weekend trend trader" book

Hi,

I'm trying to recreate the code used in Nick Radge's "Weekend Trend Trader" book. This is what i came up with so far:

Index = Foreign("$SPX", "C", True);

IndexMA = MA(Index, 20);

MarketUpTrend = Null;

IIf(Index > IndexMA, MarketUpTrend = True, MarketUpTrend = False);

NewHigh = Null;

IIf(Close > HHV(Close, 20), NewHigh = True, NewHigh = False);

Buy = ROC(Close, 20, False) > 30 AND MarketUpTrend == True AND NewHigh == True;

Sell = 0;

Here you can see i have only coded the Buy signal so far, however when testing i've noticed that when i use the "Scan" button to check for any signals, nothing comes up. Nothing comes up when backtesting also. Any help would be appreciated.

@cgchaves many errors in your code. Try testing something like below (run it in Weekly analysis as intended by Nick Radge with these parameter values).

Index = Foreign("$SPX", "C");

IndexMA = MA(Index, 20);

MarketUpTrend = Index > IndexMA;

Breakout = Close > Ref(HHV(Close, 20), -1);

Buy = ROC(Close, 20) > 30 AND MarketUpTrend AND Breakout;

Sell = 0;

// explore those newly created variables
// are they calculating what you intended?
Filter = 1; // all symbols and all bars
// Filter = Buy; // uncomment this just to see all the "buys"
AddColumn(Index, "SPX");
AddColumn(IndexMA, "MA(Index, 20)");
AddColumn(MarketUpTrend, "MarketUpTrend", 1.0, colorDefault, IIf(MarketUpTrend, colorLime, colorDefault));
AddColumn(Breakout, "Breakout", 1.0, colorDefault, IIf(Breakout, colorYellow, colorDefault));
AddColumn(ROC(Close, 20), "ROC(Close, 20)", 1.2, colorDefault, IIf(ROC(Close, 20) > 30, colorGreen, colorDefault));

And read this post carefully.

3 Likes

Thank you very much for your help. Also, i've been trying to implement the sell signal, however it doesen't seem to be working as well. Could you help me out once more? Thank you in advance

Index = Foreign("$SPX", "C");

IndexMA = MA(Index, 20);

MarketUpTrend = Index > IndexMA;

Breakout = Close > Ref(HHV(Close, 20), -1);

Buy = ROC(Close, 20) > 30 AND MarketUpTrend AND Breakout;

//___________________________
//This is the part i added

Stop = HHV(Close, 1) * 0.6;

IIf(MarketUpTrend, Stop = HHV(Close, 1) * 0.6, Stop = HHV(Close, 1) * 0.1);

Sell = Stop;

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

//___________________

// explore those newly created variables
// are they calculating what you intended?
Filter = 1; // all symbols and all bars
// Filter = Buy; // uncomment this just to see all the "buys"
AddColumn(Index, "SPX");
AddColumn(IndexMA, "MA(Index, 20)");
AddColumn(MarketUpTrend, "MarketUpTrend", 1.0, colorDefault, IIf(MarketUpTrend, colorLime, colorDefault));
AddColumn(Breakout, "Breakout", 1.0, colorDefault, IIf(Breakout, colorYellow, colorDefault));

You are using IIf incorrectly. You urgently need to read about difference between IIf and if-else

Also Sell expects TRUE/FALSE array returning 0 or 1.
Your Stop variable is not that but it is percentage of some price... 60% of... 10% of....
So it is always greater zero.

Also HHV(Close, 1) is equal to just Close.

You seem to look for variable stop depending on market condition. So e.g. you may use ApplyStop with volatile being set to true.

stop_amount = IIf(MarketUpTrend, 60, 10);// percent stop amount

Sell = 0;
SellPrice = Close;
ApplyStop(stopTypeLoss, stopModePercent, stop_amount, exitatstop = 0, volatile = 1);

or trailing stop type

stop_amount = IIf(MarketUpTrend, 60, 10);// percent stop amount

Sell = 0;
SellPrice = Close;
ApplyStop(stopTypeTrailing, stopModePercent, stop_amount, exitatstop = 0, volatile = 1);

Note: to evaluate stops in non backtester environment (Chart, Exploration,..) you need to use Equity() function. Please read ApplyStop documentation.

No ExRem required then.

1 Like

Thank you very much for your help.
One last thing: In order to check if the Buy/Sell signals are correct, i've been trying to plot the Trailing stop in a chart, and when i was searching the internet, i found a page in the AmiBroker Knowledge Base that has a sample code already ready.
How would i go about adapting this code to use the same paramaters used in the ApplyStop function?

I have been using AmiBroker for a little under two weeks, so this sort of thing is new to me. I really appreciate all the help you all have given me.

@cgchaves you must put in some effort. The Knowledge Base article has already coded an answer for you, and all you need to do is plug in a couple of small changes to match your code. Try a few, experiment a bit. That is the best way to learn.

I realize you are a new user, hey welcome to AmiBroker! But take some time reading over the Knowledge Base, many many great forum examples and work your way through the resources on this thread

Two additional points to make though. I don't use the ApplyStop functionality so I am not really confident in making any comments. But either you or @fxshrat may have misinterpreted the initial stop loss %, it is meant to be 40% so this line should have a 40 instead of a 60.

StopLevel = IIf(MarketUpTrend, 40, 10); // percent stop amount

Secondly, the book you are referring to also mentions that the stop loss will require slightly more complicated coding. The scenario to consider: If the Market regime filter was negative but you have not been stopped out of your stock, and then the Market trend turns UP again. The stop price does not drop from 10% to 40%.

In fact the stop is not to decrease at all. As long as your stock rises, the stop is gradually permitted to widen. If your stock climbs far enough then your trailing stop will begin to rise once it has reached the 40% level again (so it gradually widens from a 10% stop loss to a 40% stop loss again).

I don't know if that can be done with ApplyStop (I admit I know very little about ApplyStop) but it can with some modifications to the looping example that @cgchaves just listed.

Good luck and once again welcome to AmiBroker!

PS I think you used to be able to purchase a full AmiBroker coded Weekend Trend Trader from Nick Radge.

1 Like

Nothing has been misinterpreted on my end. My post was not about percentage amount of some book but about proper AFL code. His posted sell stop code is not proper one as well as the use of Iif function. That's what it is about. So whatever percentage number you insert is up to you! Also ApplyStop examples are just examples. Which stop he wants to apply is up to him also.

Besides I don't care for "traders" for "traders" books out there. Maybe others do but I don't. I have my own brain to use. So I could care less which "magic" number ("that but only that number") is mentioned in some ... book.


BTW, if you want to play number games then you should do according to his post.

StopLevel = IIf(MarketUpTrend, 40, 90); // percent stop amount

Since in his post he used 0.1*price -> 1-0.1 = 0.9 or 90%
(Maybe just maybe @chchaves does not want to use numbers of books but wants to apply different ones. Or maybe not... Well, only he knows what he actually looks for).


But some advertised scam artists here too who (by their own words on website) scan this forums for other peoples codes (and then go on promotion tour). :face_vomiting:

2 Likes