Click here to Login





                                                   sp() function

  0

0
Obuli Govindaraju
2012-09-18 05:12:07


SP() function returns the swing high or low. Is it possible for it to return the bar number (index) of the swing low/high as well (maybe using another function)?

Thanks,
Obuli



SystemTrade
2012-09-18 07:38:27

  0

Hi Obuli,

I like this question, below is the answer. Changes compared to the original version are commented // v 2.0.
Just add a numeric parameter "output" to the function. If this is 0, than the function will return the price level as before - if it is 1, it will return the lag.

Enjoy!


// ------------------------ C# --------------- sp function v 2.0 with bar lag output ------------------------------

VectorD lows = cFunctions.Low * (1 - TA.Sign(TA.LlvLb(cFunctions.Low, bars)));
VectorD highs = cFunctions.High * (1 - TA.Sign(TA.HhvLb(cFunctions.High, bars)));

QSFormula ll = cFunctions.CreateFormula("lowest_low = LowestSince(condition, array);");
ll.SetParameter("condition", TA.Ref(highs, 1));
ll.SetParameter("array", cFunctions.Low);
ll.Compile();

QSFormula hh = cFunctions.CreateFormula("highest_high = HighestSince(condition, array);");
hh.SetParameter("condition", TA.Ref(lows, 1));
hh.SetParameter("array", cFunctions.High);
hh.Compile();

lows = TA.Iff(lows, ll.GetVectorDouble("lowest_low"), 0);
lows[0] = 0;
highs = TA.Iff(highs, hh.GetVectorDouble("highest_high"), 0);
highs[0] = 0;

VectorD sti = highs - lows;
sti = TA.Iff(TA.Sign(lows) * TA.Sign(highs) * TA.Sign(cFunctions.Open - cFunctions.Close), highs, sti);
sti = TA.Iff(TA.Sign(lows) * TA.Sign(highs) * TA.Sign(cFunctions.Close - cFunctions.Open), lows, sti);
sti = TA.Iff(TA.Sign(lows) * TA.Sign(highs) * (1 - TA.Absolute(TA.Sign(cFunctions.Open - cFunctions.Close))), 0, sti);
sti = TA.ValueWhen(sti != 0, sti);

VectorD sti_bars = TA.Iff(sti > 0, TA.BarsSince(highs) + 1, -1 * TA.BarsSince(lows) - 1); // v 2.0
for (int i = 0; i < point[0]; i++)
{
sti_bars = TA.Iff(sti > 0, -1 * TA.BarsSince(sti < 0) + TA.ValueWhen(sti < 0, sti_bars), TA.BarsSince(sti > 0) + TA.ValueWhen(sti > 0, sti_bars)); // v 2.0
sti = TA.Iff(sti > 0, TA.ValueWhen(sti < 0, sti), TA.ValueWhen(sti > 0, sti));
}
if (output[0] == 0) result = (TA.Iff(point < 0, sti, TA.Absolute(sti)));
if (output[0] == 1) result = (sti_bars); // v 2.0



Obuli Govindaraju
2012-09-19 05:08:54

  0

Thank you very much for your reply. I see that you have modified the code to include the no. of bars between two swing points. However what I was referring to is that the bar index number of the swing point high/low bar in relation to total bars in the chart.

For example if the chart has 3000 bars then the sp(3,0) bar number could be 2800, sp(3,1) bar number could be 2792, sp(3,2) could be 2788 etc.,

The reason I am asking for bar index is because once I get this number I can calculate the number of days between the total bars and the swing point bar (using NbDays) and then use that info to automatically draw Support/Resistance lines using the AutoSR function.

Hope you can understand what I am trying to do here.

Thanks.



SystemTrade
2012-09-19 08:37:03

  0

Hi Obuli,

sp(3, 0, 1) already returns the number of bars since the last swingpoint.
In AutoSR(SR, lag, length) you could use something like sp(3, 0, 1) as lag and (sp(3, 2, 1) - lag) as length if you want to draw a line between the two points.
Note that sp() returns negative values if the swingpoint is a low.

Please be aware that AutoSR will calculate the lowest angle between the two points, which might not be a direct line between the points, for example:
bar high
1 1000 (this is supposed to be the first swing high)
2 990
3 999 (not a swing high, if period is set to 3 as in your example)
4 990
5 990
6 990
7 991 (second swing high)
AutoSR will probably draw a line between bar 3 and bar 7 and not between 1 and 7 as expected.



No more messages
0




Reply:

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"










QuantShare

Trading Items
News parser function
Advanced Quotes and Custom Databases Function
Swing Point Function (SP)
NORMDIST - Normal Probability Density Function
Day of week and Week number function

How-to Lessons
Difference between the watchlist and the screener tools
How to plot two different time frames in the same chart
How to set order type of a trading system programmatically
How to display the bar index of a stock on a chart
How to screen for stocks having a high correlation with the Dow J...

Related Forum Threads
Question on Swing Point function sp()
Calling a Function from another Function
Comp Function
Logging the Comp() function
Composite Function

Blog Posts
How to create market indicators using the composite function - Pa...
The 'inside period' function
How to create market indicators using the composite function - Pa...
How to create market indicators using the composite function - Pa...
Backtesting Your Trading Systems: Symbol Segmentation









QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
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.