-- 作者:excel
-- 发布时间:2014/1/17 14:47:42
--
function Init() indicator:name(resources:get("R_NAME")); indicator:description(resources:get("R_DESCRIPTION")); indicator:requiredSource(core.Bar); indicator:type(core.Indicator); indicator:setTag("group", "Support/Resistance");
indicator.parameters:addGroup(resources:get("R_PARAMS")); indicator.parameters:addString("BS", resources:get("R_PERIOD"), resources:get("R_PERIOD1"), "D1"); indicator.parameters:setFlag("BS", core.FLAG_BARPERIODS);
indicator.parameters:addString("CalcMode", resources:get("R_MODE"), resources:get("R_MODE1"), "Pivot"); indicator.parameters:addStringAlternative("CalcMode", resources:get("R_MODE_O1"), "", "Pivot"); indicator.parameters:addStringAlternative("CalcMode", resources:get("R_MODE_O2"), "", "Camarilla"); indicator.parameters:addStringAlternative("CalcMode", resources:get("R_MODE_O3"), "", "Woodie"); indicator.parameters:addStringAlternative("CalcMode", resources:get("R_MODE_O4"), "", "Fibonacci"); indicator.parameters:addStringAlternative("CalcMode", resources:get("R_MODE_O5"), "", "Floor"); indicator.parameters:addStringAlternative("CalcMode", resources:get("R_MODE_O6"), "", "FibonacciR");
indicator.parameters:addGroup(resources:get("R_STYLE")); indicator.parameters:addString("ShowMode", resources:get("R_SMODE"), resources:get("R_SMODE1"), "TODAY"); indicator.parameters:addStringAlternative("ShowMode", resources:get("R_SMODE_O1"), "", "TODAY"); indicator.parameters:addStringAlternative("ShowMode", resources:get("R_SMODE_O2"), "", "HIST"); indicator.parameters:addString("LabelLoc", resources:get("R_LABEL_LOC"), resources:get("R_LABEL_LOC_DESC"), "E"); indicator.parameters:addStringAlternative("LabelLoc", resources:get("R_LABEL_LOC_1"), "", "E"); indicator.parameters:addStringAlternative("LabelLoc", resources:get("R_LABEL_LOC_2"), "", "B"); indicator.parameters:addStringAlternative("LabelLoc", resources:get("R_LABEL_LOC_3"), "", "A");
indicator.parameters:addColor("clrP", string.format(resources:get("R_color_of_PARAM_name"), resources:get("R_P_name")), string.format(resources:get("R_color_of_PARAM_description"), resources:get("R_P_desc")), core.rgb(192, 192, 192)); indicator.parameters:addInteger("widthP", string.format(resources:get("R_width_of_PARAM_name"), resources:get("R_P_name")), string.format(resources:get("R_width_of_PARAM_description"), resources:get("R_P_desc")), 1, 1, 5); indicator.parameters:addInteger("styleP", string.format(resources:get("R_style_of_PARAM_name"), resources:get("R_P_name")), string.format(resources:get("R_style_of_PARAM_description"), resources:get("R_P_desc")), core.LINE_SOLID); indicator.parameters:setFlag("styleP", core.FLAG_LEVEL_STYLE);
indicator.parameters:addColor("clrS1", string.format(resources:get("R_color_of_PARAM_name"), resources:get("R_S1_name")), string.format(resources:get("R_color_of_PARAM_description"), resources:get("R_S1_desc")), core.rgb(255, 0, 0)); indicator.parameters:addInteger("widthS1", string.format(resources:get("R_width_of_PARAM_name"), resources:get("R_S1_name")), string.format(resources:get("R_width_of_PARAM_description"), resources:get("R_S1_desc")), 1, 1, 5); indicator.parameters:addInteger("styleS1", string.format(resources:get("R_style_of_PARAM_name"), resources:get("R_S1_name")), string.format(resources:get("R_style_of_PARAM_description"), resources:get("R_S1_desc")), core.LINE_SOLID); indicator.parameters:setFlag("styleS1", core.FLAG_LEVEL_STYLE); indicator.parameters:addBoolean("showS1", resources:get("R_SHOW_S1"), resources:get("R_SHOW_S1_1"), true);
indicator.parameters:addColor("clrS2", string.format(resources:get("R_color_of_PARAM_name"), resources:get("R_S2_name")), string.format(resources:get("R_color_of_PARAM_description"), resources:get("R_S2_desc")), core.rgb(224, 0, 0)); indicator.parameters:addInteger("widthS2", string.format(resources:get("R_width_of_PARAM_name"), resources:get("R_S2_name")), string.format(resources:get("R_width_of_PARAM_description"), resources:get("R_S2_desc")), 1, 1, 5); indicator.parameters:addInteger("styleS2", string.format(resources:get("R_style_of_PARAM_name"), resources:get("R_S2_name")), string.format(resources:get("R_style_of_PARAM_description"), resources:get("R_S2_desc")), core.LINE_SOLID); indicator.parameters:setFlag("styleS2", core.FLAG_LEVEL_STYLE); indicator.parameters:addBoolean("showS2", resources:get("R_SHOW_S2"), resources:get("R_SHOW_S2_1"), true);
indicator.parameters:addColor("clrS3", string.format(resources:get("R_color_of_PARAM_name"), resources:get("R_S3_name")), string.format(resources:get("R_color_of_PARAM_description"), resources:get("R_S3_desc")), core.rgb(192, 0, 0)); indicator.parameters:addInteger("widthS3", string.format(resources:get("R_width_of_PARAM_name"), resources:get("R_S3_name")), string.format(resources:get("R_width_of_PARAM_description"), resources:get("R_S3_desc")), 1, 1, 5); indicator.parameters:addInteger("styleS3", string.format(resources:get("R_style_of_PARAM_name"), resources:get("R_S3_name")), string.format(resources:get("R_style_of_PARAM_description"), resources:get("R_S3_desc")), core.LINE_SOLID); indicator.parameters:setFlag("styleS3", core.FLAG_LEVEL_STYLE); indicator.parameters:addBoolean("showS3", resources:get("R_SHOW_S3"), resources:get("R_SHOW_S3_1"), true);
indicator.parameters:addColor("clrS4", string.format(resources:get("R_color_of_PARAM_name"), resources:get("R_S4_name")), string.format(resources:get("R_color_of_PARAM_description"), resources:get("R_S4_desc")), core.rgb(160, 0, 0)); indicator.parameters:addInteger("widthS4", string.format(resources:get("R_width_of_PARAM_name"), resources:get("R_S4_name")), string.format(resources:get("R_width_of_PARAM_description"), resources:get("R_S4_desc")), 1, 1, 5); indicator.parameters:addInteger("styleS4", string.format(resources:get("R_style_of_PARAM_name"), resources:get("R_S4_name")), string.format(resources:get("R_style_of_PARAM_description"), resources:get("R_S4_desc")), core.LINE_SOLID); indicator.parameters:setFlag("styleS4", core.FLAG_LEVEL_STYLE); indicator.parameters:addBoolean("showS4", resources:get("R_SHOW_S4"), resources:get("R_SHOW_S4_1"), true);
indicator.parameters:addColor("clrR1", string.format(resources:get("R_color_of_PARAM_name"), resources:get("R_R1_name")), string.format(resources:get("R_color_of_PARAM_description"), resources:get("R_R1_desc")), core.rgb(0, 255, 0)); indicator.parameters:addInteger("widthR1", string.format(resources:get("R_width_of_PARAM_name"), resources:get("R_R1_name")), string.format(resources:get("R_width_of_PARAM_description"), resources:get("R_R1_desc")), 1, 1, 5); indicator.parameters:addInteger("styleR1", string.format(resources:get("R_style_of_PARAM_name"), resources:get("R_R1_name")), string.format(resources:get("R_style_of_PARAM_description"), resources:get("R_R1_desc")), core.LINE_SOLID); indicator.parameters:setFlag("styleR1", core.FLAG_LEVEL_STYLE); indicator.parameters:addBoolean("showR1", resources:get("R_SHOW_R1"), resources:get("R_SHOW_R1_1"), true);
indicator.parameters:addColor("clrR2", string.format(resources:get("R_color_of_PARAM_name"), resources:get("R_R2_name")), string.format(resources:get("R_color_of_PARAM_description"), resources:get("R_R2_desc")), core.rgb(0, 224, 0)); indicator.parameters:addInteger("widthR2", string.format(resources:get("R_width_of_PARAM_name"), resources:get("R_R2_name")), string.format(resources:get("R_width_of_PARAM_description"), resources:get("R_R2_desc")), 1, 1, 5); indicator.parameters:addInteger("styleR2", string.format(resources:get("R_style_of_PARAM_name"), resources:get("R_R2_name")), string.format(resources:get("R_style_of_PARAM_description"), resources:get("R_R2_desc")), core.LINE_SOLID); indicator.parameters:setFlag("styleR2", core.FLAG_LEVEL_STYLE); indicator.parameters:addBoolean("showR2", resources:get("R_SHOW_R2"), resources:get("R_SHOW_R2_1"), true);
indicator.parameters:addColor("clrR3", string.format(resources:get("R_color_of_PARAM_name"), resources:get("R_R3_name")), string.format(resources:get("R_color_of_PARAM_description"), resources:get("R_R3_desc")), core.rgb(0, 192, 0)); indicator.parameters:addInteger("widthR3", string.format(resources:get("R_width_of_PARAM_name"), resources:get("R_R3_name")), string.format(resources:get("R_width_of_PARAM_description"), resources:get("R_R3_desc")), 1, 1, 5); indicator.parameters:addInteger("styleR3", string.format(resources:get("R_style_of_PARAM_name"), resources:get("R_R3_name")), string.format(resources:get("R_style_of_PARAM_description"), resources:get("R_R3_desc")), core.LINE_SOLID); indicator.parameters:setFlag("styleR3", core.FLAG_LEVEL_STYLE); indicator.parameters:addBoolean("showR3", resources:get("R_SHOW_R3"), resources:get("R_SHOW_R3_1"), true);
indicator.parameters:addColor("clrR4", string.format(resources:get("R_color_of_PARAM_name"), resources:get("R_R4_name")), string.format(resources:get("R_color_of_PARAM_description"), resources:get("R_R4_desc")), core.rgb(0, 160, 0)); indicator.parameters:addInteger("widthR4", string.format(resources:get("R_width_of_PARAM_name"), resources:get("R_R4_name")), string.format(resources:get("R_width_of_PARAM_description"), resources:get("R_R4_desc")), 1, 1, 5); indicator.parameters:addInteger("styleR4", string.format(resources:get("R_style_of_PARAM_name"), resources:get("R_R4_name")), string.format(resources:get("R_style_of_PARAM_description"), resources:get("R_R4_desc")), core.LINE_SOLID); indicator.parameters:setFlag("styleR4", core.FLAG_LEVEL_STYLE); indicator.parameters:addBoolean("showR4", resources:get("R_SHOW_R4"), resources:get("R_SHOW_R4_1"), true);
indicator.parameters:addBoolean("ShowMP", resources:get("R_SHOW_MIDPOINT"), resources:get("R_SHOW_MIDPOINT_1"), false); indicator.parameters:addColor("clrMP", string.format(resources:get("R_color_of_PARAM_name"), resources:get("R_MIDPOINT_name")), string.format(resources:get("R_color_of_PARAM_description"), resources:get("R_MIDPOINT_desc")), core.rgb(128, 128, 128)); indicator.parameters:addInteger("widthMP", string.format(resources:get("R_width_of_PARAM_name"), resources:get("R_MIDPOINT_name")), string.format(resources:get("R_width_of_PARAM_description"), resources:get("R_MIDPOINT_desc")), 1, 1, 5); indicator.parameters:addInteger("styleMP", string.format(resources:get("R_style_of_PARAM_name"), resources:get("R_MIDPOINT_name")), string.format(resources:get("R_style_of_PARAM_description"), resources:get("R_MIDPOINT_desc")), core.LINE_DOT); indicator.parameters:setFlag("styleMP", core.FLAG_LEVEL_STYLE); end
|
-- 作者:excel
-- 发布时间:2014/1/17 14:49:40
--
-- check whether the requested candle is before -- the reference collection start if (candle < ref:date(0)) then return ; end
-- find the lastest completed period which is not saturday\'s period (to avoid -- collecting the saturday\'s data local prev_i = nil; local start;
if (pday == nil) then start = 0; elseif ref:date(pday) >= candle then start = 0; else start = pday; end
for i = start, ref:size() - 1, 1 do local td; -- skip nontrading candles if BSLen > 1 or not(core.isnontrading(ref:date(i), offset)) then if (ref:date(i) >= candle) then break; else prev_i = i; end end end
if (prev_i == nil) then -- assert(false, "prev_i is nil"); return ; end
pday = prev_i; if CalcMode == O_PIVOT or CalcMode == O_FIB or CalcMode == O_FLOOR then P[period] = (ref.high[prev_i] + ref.close[prev_i] + ref.low[prev_i]) / 3; elseif CalcMode == O_CAM then -- P[period] = (ref.high[prev_i] + ref.close[prev_i] + ref.low[prev_i]) / 3; P[period] = ref.close[prev_i]; elseif CalcMode == O_FIBR then P[period] = (ref.high[prev_i] + ref.low[prev_i]) / 2; elseif CalcMode == O_WOOD then local open; if (prev_i == ref:size() - 1) then -- for a live day take close as open of the next period open = ref.open[prev_i]; else open = ref.open[prev_i + 1]; end P[period] = (ref.high[prev_i] + ref.low[prev_i] + open * 2 ) / 4; end H[period] = ref.high[prev_i]; L[period] = ref.low[prev_i];
CalculateLevels(period); if ShowMode == O_HIST then local nb; nb = false; if P:hasData(period - 1) and math.abs(P[period - 1] - P[period]) > eps and not(SameSizeBar) then nb = true; end
for i = S1, R4, 1 do if show[i] and d[i] ~= 0 then stream[i][period] = d[i]; if nb then stream[i]:setBreak(period, true); end end end end if (period == source:size() - 1) then ShowLevels(d, period); end end
-- the function is called when the async operation is finished function AsyncOperationFinished(cookie) if cookie == 100 then pday = nil; loading = false; instance:updateFrom(0); elseif cookie == 101 then loading = true; end end
function CalculateLevels(period) local h, l, p, r; p = P[period]; h = H[period]; l = L[period]; r = h - l;
if CalcMode == O_PIVOT then d[R4] = p + r * 3; d[R3] = p + r * 2; d[R2] = p + r; d[R1] = p * 2 - l;
d[S1] = p * 2 - h; d[S2] = p - r; d[S3] = p - r * 2; d[S4] = p - r * 3; elseif CalcMode == O_CAM then d[R4] = p + r * 1.1 / 2; d[R3] = p + r * 1.1 / 4; d[R2] = p + r * 1.1 / 6; d[R1] = p + r * 1.1 / 12;
d[S1] = p - r * 1.1 / 12; d[S2] = p - r * 1.1 / 6; d[S3] = p - r * 1.1 / 4; d[S4] = p - r * 1.1 / 2; elseif CalcMode == O_WOOD then d[R4] = h + (2 * (p - l) + r); d[R3] = h + 2 * (p - l); d[R2] = p + r; d[R1] = p * 2 - l;
d[S1] = p * 2 - h; d[S2] = p - r; d[S3] = l - 2 * (h - p); d[S4] = l - (r + 2 * (h - p)); elseif CalcMode == O_FIB then d[R4] = p + 1.618 * (h - l); d[R3] = p + 1 * (h - l); d[R2] = p + 0.618 * (h - l); d[R1] = p + 0.382 * (h - l);
d[S1] = p - 0.382 * (h - l); d[S2] = p - 0.618 * (h - l); d[S3] = p - 1 * (h - l); d[S4] = p - 1.618 * (h - l); elseif CalcMode == O_FLOOR then d[R4] = 0; d[R3] = h + (p - l) * 2; d[R2] = p + r; d[R1] = p * 2 - l;
d[S1] = p * 2 - h; d[S2] = p - r; d[S3] = l - (h - p) * 2; d[S4] = 0; elseif CalcMode == O_FIBR then d[R4] = l + (h - l) * fibr[R4]; d[R3] = l + (h - l) * fibr[R3]; d[R2] = l + (h - l) * fibr[R2]; d[R1] = l + (h - l) * fibr[R1];
d[S1] = l + (h - l) * fibr[S1]; d[S2] = l + (h - l) * fibr[S2]; d[S3] = l + (h - l) * fibr[S3]; d[S4] = l + (h - l) * fibr[S4]; end
return ; end
function round(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / mult end
function ShowLevels(data, period) local i, d1, d2;
--d1 = source:date(0); d2 = source:date(period); d1, d2 = core.getcandle(BS, d2, offset, weekoffset);
host:execute("drawLine", PID, d1, P[period], d2, P[period], clrP, styleP, widthP, "P(" .. round(P[period], source:getPrecision()) .. ")"); if LabelLoc == O_END or LabelLoc == O_BOTH then host:execute("drawLabel", PID, d2, P[period], name[RP]); end if LabelLoc == O_BEG or LabelLoc == O_BOTH then host:execute("drawLabel", PID + 100, d1, P[period], name[RP]); end
for i = S1, R4, 1 do if show[i] and data[i] ~= 0 then host:execute("drawLine", i, d1, data[i], d2, data[i], clr[i], style[i], width[i], name[i] .. "(" .. round(data[i], source:getPrecision()) .. ")"); if LabelLoc == O_END or LabelLoc == O_BOTH then host:execute("drawLabel", i, d2, data[i], name[i]); end if LabelLoc == O_BEG or LabelLoc == O_BOTH then host:execute("drawLabel", i + 100, d1, data[i], name[i]); end else host:execute("removeLine", i); host:execute("removeLabel", i); host:execute("removeLabel", i + 100); end end
if ShowMP then ShowMPP(0, d1, d2, d[S2], d[S3], "M0"); ShowMPP(1, d1, d2, d[S1], d[S2], "M1"); ShowMPP(2, d1, d2, P[period], d[S1], "M2"); ShowMPP(3, d1, d2, P[period], d[R1], "M3"); ShowMPP(4, d1, d2, d[R1], d[R2], "M4"); ShowMPP(5, d1, d2, d[R2], d[R3], "M5"); end end
function ShowMPP(i, d1, d2, p1, p2, l) if p1 ~= 0 and p2 ~= 0 then local p = (p1 + p2) / 2; host:execute("drawLine", PID + 10 + i, d1, p, d2, p, clrMP, styleMP, widthMP, l .. "(" .. round(p, source:getPrecision()) .. ")"); if LabelLoc == O_END or LabelLoc == O_BOTH then host:execute("drawLabel", PID + 10 + i, d2, p, l); end if LabelLoc == O_BEG or LabelLoc == O_BOTH then host:execute("drawLabel", PID + 110 + i, d1, p, l); end end end
|