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];
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.
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)?
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.
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
renko[i]=renko[i-1]+brick;
else {
if (close[i] > renko[i-1]-brick) //step down
renko[i]=renko[i-1]-brick;
else
renko[i] = renko[i-1]; // same step
}
}
result[i] = renko[i];
cFunctions.SetForwardAndBackwardBars(5, 0);
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);
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.
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.
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?
res=ref(peak(high,2*atr(150)),1);
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?
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.