|
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
|
|