Click here to Login

                                                   Peak / trough indicators and issue with Functions


2016-05-01 13:26:41

1) The Peak and Trough indicators have a "Change" parameter - what does that represent? Where can i find the code for this indicator, so i can inspect the logic?

2) Also, i'm Creating a Function, but it gives me an error on this line "troughs[i] = QSFunctions.Trough(low,_window, i);", saying that an Object reference is required for the nonstatic field, method or property. Cannot implicitly convert VectorD to double.
a) Trough indicator returns a double so not sure why it's complaining. Also, adding [0] at the end doesn't help - i.e. "troughs[i] = QSFunctions.Trough(low,_window, i)[0];"
b) Why does result is an array "result [i] = " in some examples but a double "result = " in others? Thought functions always return a double
c) Can i run this in debug mode to step through the logic and print variable values?

double _band = band[0];
int _window = (int)window[0];
int _minTouches = (int)minTouches[0];
VectorD low = cFunctions.Low;

double[] troughs = new double[5];
int[] touches = new int[5];

double sup;

for (int i=0; i<5; i++) {
troughs[i] = QSFunctions.Trough(low,_window, i);

for (int i=0; i<5; i++) {
for (int j=i+1; j<5; j++) {
if (-1*_band<(troughs[j]-troughs[i]) || (troughs[j]-troughs[i])<_band) {
if (touches[i]>= _minTouches){
result = troughs[i];

2016-05-02 04:11:10


1/ The code for built-in indicators is not available. The "Change" is the percentage of move needed to consider a change in the security direction.

2/ There is no "QSFunctions.Trough" function. It should be: "TA.Trough"
Also, that function returns a vector (VectorD) and you are assigning it to a double.

The custom function is not executed for each trading bar. It is executed just once. You will need to loop through each bar in the script to return a time-series. Please take a look at some custom functions available on the sharing server to see how they work.

2016-05-02 13:42:58


Just to clarify the Peak/Trough logic to ensure there's not peek-ahead issue with strategies using Peak/Trough indicator ..

For Peak(High,1), the indicator draws only after ..
1) High is higher than the previous Peak by 1% (say this happens at Bar10, and the previous Peak was at Bar5) AND
2) High has dropped by 1% from Bar10 - this might happen in Bar14.

So, the new Peak at Bar10 is drawn only at Bar14, and the old Peak remains constant from Bars5->13.
If i develop a strategy that uses Peak indicator as a Buy rule in QS script, does the strategy use
A) Values of Old Peak at bars 10-13 (which would be correct implementation without peak-ahead issues OR
B) New Peak values at bars 10-13 (as displayed in the chart), which is peaking ahead and the strategy is invalid.

-> If the strategy is using B, how do i address the peak-ahead issue and get a genuine backtest?

-> Also, doesn't this indicator simulate Renko bricks? Can this be used as Renko bricks in QS (since QS doesn't support Renko)?

- if it's using the O

2016-05-03 04:19:33


Please note that the Peak/Trough functions have a look-ahead bias, just like the ZigZag function.

There is nothing you can do here about these functions. They should not be used in backtesting.
You need to write your own implementation of a Peak/Trough function that doesn't look into the future.

2016-05-09 17:35:13


I'm trying to create a simplified Renko function (code below) which compiled fine but doesn't plot when i run
- Plot(renko(1),"renko",colorGreen);

1) It's likely because i haven't invoked the bar number correctly (neither result.Length or result.Length-1 works) Pl advise how to make this work
2) In Quantscript, how do i detect whether the previous brick was higher or lower than current brick - since the width of the brick is unknown? Something like ..
bool step_up = iff(Searchfor(renko[i] != renko[i]), _renko[i] > _renko[i-1], _renko[i-1]<_renko[i]);

//Renko brick function ...
double brick = _brick[0]; // brick height of Renko chart
VectorD close = cFunctions.Close;
VectorD renko = close;

int i = result.Length-1;
if (i<=0) renko[i]=close[i];
else {
if (close[i] > renko[i-1]+brick) //step up
else {
if (close[i] > renko[i-1]-brick) //step down
renko[i] = renko[i-1]; // same step
result[i] = renko[i];
cFunctions.SetForwardAndBackwardBars(5, 0);

2016-05-10 04:28:17


1/ You need to set every value in the vector "result". Currently, you are just setting the last value (result.Length - 1)
You need to loop through every bar in the vector. Please check other indicators shared to see how they work or read our documentation.

2/ In QuantShare language (not C#), to detect whether a value is higher than its previous bar value
a = iff(close > close[1], 1, 0);

2016-06-19 23:20:13


I removed the look-ahead bias of peak and trough by offsetting by 1 bar i.e. ref(peak,1), as below ..
However my simple backtest below doesn't trigger any trades. res and sup both plot fine on the charts and i can see the Buy and Sell conditions successful on the chart

Can the QS backtest script not access peak and trough functions? Pl advise how to fix this issue.


buy = low<sup; //headroom;
sell = high>res; //close>r-0.5*atr(20);
SetSimStop(StopLoss, _Point, 1.5*atr(20), 0);

2016-06-20 09:18:53


It can access the peak and trough functions. Just double check your trading system or create a new one.
Also, make sure the "buy" and "sell" variables are generating signals on a chart (not the res and sup).

Finally, offsetting the peak and trough by one bar will not remove the look-ahead bias.

2016-06-20 13:41:30


1) I plotted the Buy and Sell signals on the chart as shown below, and it plots a signal on every bar even though the conditions are not satisfied (i.e. buy1 is plotted when low> sup+0.5). Pl advise ..
buy1 = low<=(sup+0.5);
sell1 = high>=(res-0.5);
PlotArrow(buy1, "", AboveHigh, colorGreen);
PlotArrow(sell1, "", BelowLow, colorRed);

2) Also, why do you say look-ahead bias still exists after i offset the peak and trough by 1 bar? After offset, it's only using previous bar data to calculate the new peak/trough?


2016-06-20 19:28:53


1/ If it returns 1 on every bar then the rule "low<=(sup+0.5)" is satisfied on every bar.
Plot the "low" and plot the "sup+0.5" and you will see.

2/ The default peak/trough formula does not look at just one bar ahead. The number of future bars is variable.

2017-04-03 23:46:37


Another issue with peak and trough functions ..
1) peak(high, 1); //this plots correctly as steps, as tracks the price chart as price moves up and down
2) peak(high,natr(20)); // however, this function plots the absolute high reached ever, for the life of the curve and doesn't track the price changes - it's incorrect

I need to use function (2) in strategy, because the Change parameter is variable (% ATR of the symbol), not an absolute number.
How do i implement this?


2017-04-04 04:35:45


It is not incorrect. It is just that the peak function takes only fixed values as "change" parameter.

If you want it to take dynamic values then you will need to implement your own version of the "peak" function.

No more messages


No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"


Trading Items
Export Indicators Data with Filtering and Column Labels Support
Top 20 stocks with the highest 10 day rate of change - Current an...
Consecutive Up and Down Bars - Works With Any Trading Indicator
Predict financial and stock markets with textual analysis
Design and backtest a trading system with two strategies

How-to Lessons
How to speed up watchlist and screener plug-ins when working with...
How to save and restore charts
Difference between the watchlist and the screener tools
How to download and use U.S. stocks earnings data
How to drag & drop indicators in charts

Related Forum Threads
Basic features and some issues with the background
Set Global variable and running calculation Functions in MM scrip...
Issues with indicators on backtesting (CCI)
ADX di+ and Di- indicators incorrect
charting issue with new version 2.91

Blog Posts
Trading Indicators using the Rank and Percentile functions
New Ranking and Percentile Composite Functions
Screening with the composite indicators
Adding Trading Indicators and Formulas to a Chart
Fundamental analysis: How to track economic indicators in the For...

Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2020
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Follow us on Google+
RSS Trading Items

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.