修改 Fib 脚本以遇到会话
Modify a Fib script to encounter sessions
Drawing/explenation 我需要根据会话“1700-1700”调整以下脚本 (© TradeChartist),我不确定如何以正确的方式输入时间公式。已设置为每天使用,但这并不能解决我的问题...
2 如果 61,8 超过 15 点,第二个需要调整为两个时段。
虽然第一个问题最紧迫...
将不胜感激。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradeChartist
//@version=4
study("Fib Retracement", overlay=true, max_bars_back=5000)
Fibs = input("Plot Fibs based on Lookback", options = ["Plot Fibs based on Lookback", "Plot Fibs based on Price Input"], title = "Fibonacci Plot Type")
FIBS = Fibs == "Plot Fibs based on Lookback"?1:Fibs == "Plot Fibs based on Price Input"?2:na
Foption = input(defval = "1. Candles" , title = "Fibonacci Plot Lookback Type", options=["2. Days", "1. Candles"])
FP = input(defval = 100 , title = "Days/Candles to Lookback")
Reverse = input(defval = false , title = "Reverse Fibonacci Levels?")
ExtraFibs = input(false, "Show 0.886 and 1.113 Fibs")
Note = input(true, "════ ════")
High = input(0., minval = 0, title = "High - Enter Value")
Low = input(-1., minval = -1, title = "Low - Enter Value")
Note2 = input(true, "══════ / ══════")
Bull_Color = input(#008000, type=input.color, title = "Support Fibs Color")
Bear_Color = input(#ff0000, type=input.color, title = "Resistance Fibs Color")
CurrentFib = input(false, "Show Fib Level of Current Price")
Current_Color = input(color.orange, type=input.color, title = "Current Fib Label Color")
LineStyle = input("Dotted", options = ["Dotted", "Solid"], title = "Fib Line Style")
LineWidth = input(1, minval=1, maxval=3,title = "Fib Line Width")
Ext = input(false, "Extend Lines Left")
// Transparency = input("Low", options = ["High", "Medium", "Low"], title="Fib Line Transparency")
BullColor = Bull_Color//Transparency == "High"?color.new(#008000,75):Transparency == "Medium"?color.new(#008000,50):Bull_Color
BearColor = Bear_Color//Transparency == "High"?color.new(#ff0000,75):Transparency == "Medium"?color.new(#ff0000,50):Bear_Color
FPeriod = timeframe.isintraday and Foption=="2. Days"? (1440/timeframe.multiplier)*FP:
timeframe.isdaily and Foption=="2. Days"? FP/timeframe.multiplier:
timeframe.isweekly and Foption=="2. Days"? FP/(7*timeframe.multiplier):
timeframe.ismonthly and Foption=="2. Days"? FP/(28*timeframe.multiplier):
Foption=="1. Candles"? FP:100
Fhigh = FIBS==1? highest(FPeriod) : FIBS == 2 and High == 0? highest(high,100): FIBS == 2 and High!=0? High:na
Flow = FIBS==1? lowest(FPeriod) : FIBS == 2 and Low == -1? lowest(low,100): FIBS == 2 and High!=-1? Low:na
FH = FIBS == 1?highestbars(high,FPeriod): 1
FL = FIBS == 1?lowestbars(low,FPeriod): 2
revfibs = not Reverse? FL>FH : FL<FH
Fib_x(n) =>
revfibs ? (Fhigh-Flow)*n+Flow : Fhigh-(Fhigh-Flow)*n
Current = revfibs?(close-Flow)/(Fhigh-Flow):(Fhigh-close)/(Fhigh-Flow)
var label Current_Fib_Label = na
label.delete(Current_Fib_Label)
if(CurrentFib and barstate.islast)
Current_Fib_Label := label.new(bar_index, close, tostring(Current, "##.##"), textcolor = Current_Color, color = color.new(#000000,100), style=label.style_label_left, yloc=yloc.price)
EXTEND = Ext?extend.left:extend.none
STYLE = LineStyle=="Dotted"?line.style_dotted:line.style_solid
WIDTH = LineWidth
BB = FIBS==1?(FL<FH?bar_index[-FL]:bar_index[-FH]):FIBS==2?bar_index[50]:bar_index[50]
Fib_line(x) =>
var line ln = na
line.delete(ln)
ln:=line.new(BB, x, bar_index, x, color = close>x? BullColor:BearColor, extend=EXTEND ,style=STYLE, width = WIDTH)
Fib_label(x,_txt) =>
var label lbl = na
label.delete(lbl)
lbl:=label.new(bar_index, x, _txt + tostring(x, "##.########") + " )", textcolor = close>x?BullColor:BearColor, color = color.new(#000000,100), style=label.style_label_left, yloc=yloc.price)
Fib0 = Fib_line(Fib_x(0))
Fib236 = Fib_line(Fib_x(0.236))
Fib382 = Fib_line(Fib_x(0.382))
Fib500 = Fib_line(Fib_x(0.500))
Fib618 = Fib_line(Fib_x(0.618))
Fib786 = Fib_line(Fib_x(0.786))
Fib1000 = Fib_line(Fib_x(1.000))
Fib886 = ExtraFibs?Fib_line(Fib_x(0.886)):na
if(FIBS==2)
Fib1113 = ExtraFibs?Fib_line(Fib_x(1.113)):na
Fib1272 = Fib_line(Fib_x(1.272))
Fib1618 = Fib_line(Fib_x(1.618))
Fib2000 = Fib_line(Fib_x(2.000))
Fib2236 = Fib_line(Fib_x(2.236))
Fib2618 = Fib_line(Fib_x(2.618))
Fib3236 = Fib_line(Fib_x(3.236))
Fib3618 = Fib_line(Fib_x(3.618))
Fib4236 = Fib_line(Fib_x(4.236))
Fib4618 = Fib_line(Fib_x(4.618))
LFib0 = Fib_label(Fib_x(0), "0 ( ")
LFib236 = Fib_label(Fib_x(0.236), "0.236 ( ")
LFib382 = Fib_label(Fib_x(0.382), "0.382 ( ")
LFib500 = Fib_label(Fib_x(0.500), "0.500 ( ")
LFib618 = Fib_label(Fib_x(0.618), "0.618 ( ")
LFib786 = Fib_label(Fib_x(0.786), "0.786 ( ")
LFib1000 = Fib_label(Fib_x(1.000), "1.000 ( ")
LFib886 = ExtraFibs?Fib_label(Fib_x(0.886), "0.886 ( "):na
if(FIBS==2)
LFib1113 = ExtraFibs?Fib_label(Fib_x(1.113), "1.113 ( "):na
LFib1272 = Fib_label(Fib_x(1.272), "1.272 ( ")
LFib1618 = Fib_label(Fib_x(1.618), "1.618 ( ")
LFib2000 = Fib_label(Fib_x(2.000), "2.000 ( ")
LFib2236 = Fib_label(Fib_x(2.236), "2.236 ( ")
LFib2618 = Fib_label(Fib_x(2.618), "2.618 ( ")
LFib3236 = Fib_label(Fib_x(3.236), "3.236 ( ")
LFib3618 = Fib_label(Fib_x(3.618), "3.618 ( ")
LFib4236 = Fib_label(Fib_x(4.236), "4.236 ( ")
LFib4618 = Fib_label(Fib_x(4.618), "4.618 ( ")
我创建了这个:
//@version=4
study("Fib Retracement", overlay=true, max_bars_back=5000)
var int sessionStartHour = input(17, "Session start hour (UTC)", input.integer)
var bool extendLeft = input(false, "Fib line extend left", input.bool)
var bool extendRight = input(false, "Fib line extend right", input.bool)
var float fibBreakLevel = input(61.80, "Fib break level", input.float)
var int fibBreakDistance = input(10, "Level break distance (ticks)", input.integer)
var float fibBreakLevelPrice = na
var string extendType = extendLeft and extendRight ? extend.both : extendRight ? extend.right : extendLeft ? extend.left : extend.none
var float curSessionHi = na
var float curSessionLo = na
var float prevSessionHi = na
var float prevSessionLo = na
var int curHour = na
var int curBarIndexHi = na
var int curBarIndexLo = na
var int prevBarIndexHi = na
var int prevBarIndexLo = na
var int fibLevelCount = na
var bool lowFirst = na
var int fibStartBar = na
var arrFibLevels = array.new_float()
var arrFibColors = array.new_color()
var bool levelBroken = na
var l0 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l1 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l2 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l3 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l4 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l5 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l6 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l7 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l8 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l9 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
f_moveLine(_id, _x, _y, _color) =>
line.set_xy1(_id, _x, _y)
line.set_xy2(_id, bar_index, _y)
line.set_color(_id, _color)
fib_setLevels() =>
array.clear(arrFibLevels)
array.push(arrFibLevels, 00.00), array.push(arrFibColors, color.green)
array.push(arrFibLevels, 23.60), array.push(arrFibColors, color.blue)
array.push(arrFibLevels, 38.20), array.push(arrFibColors, color.blue)
array.push(arrFibLevels, 50.00), array.push(arrFibColors, color.blue)
array.push(arrFibLevels, 61.80), array.push(arrFibColors, color.yellow)
array.push(arrFibLevels, 78.60), array.push(arrFibColors, color.blue)
array.push(arrFibLevels, 100.00), array.push(arrFibColors, color.red)
array.size(arrFibLevels)
fib_retrace(_startBar, _levelLo, _levelHi, _lowFirst) =>
var float level0 = na
var float level = na
var line ln = na
var int dir = na
var color fibColor = na
var float breakPrice = na
var float fibLevel = na
dir := _lowFirst ? 1 : -1
fibRange = abs(_levelHi - _levelLo)
level0 := _lowFirst ? _levelLo : _levelHi
for i = 0 to fibLevelCount-1
ln := i==0 ? l0 : i==1 ? l1 : i==2 ? l2 : i==3 ? l3 : i==4 ? l4 : i==5 ? l5 : i==6 ? l6 : i==7 ? l7 : i==8 ? l8 : i==9 ? l9 : na
fibLevel := array.get(arrFibLevels, i)
level := level0 + (dir * fibRange * fibLevel/100)
if (fibLevel == fibBreakLevel)
breakPrice := level
fibColor := array.get(arrFibColors, i)
f_moveLine(ln, _startBar, level, fibColor)
breakPrice
if barstate.isfirst
fibLevelCount := fib_setLevels()
curHour := hour(time, 'UTC')
newSession = (curHour == sessionStartHour) and change(curHour)
if newSession
prevSessionHi := curSessionHi
prevSessionLo := curSessionLo
prevBarIndexHi := curBarIndexHi
prevBarIndexLo := curBarIndexLo
lowFirst := prevBarIndexLo < prevBarIndexHi
fibStartBar := lowFirst ? prevBarIndexLo : prevBarIndexHi
curSessionLo := na
curSessionHi := na
curBarIndexHi := na
curBarIndexLo := na
levelBroken := na
// Draw fibs for prev session
fibBreakLevelPrice := fib_retrace(fibStartBar, prevSessionLo, prevSessionHi, lowFirst)
if lowFirst
levelBroken := close > (fibBreakLevelPrice + (fibBreakDistance * syminfo.mintick))
else
levelBroken := close < (fibBreakLevelPrice - (fibBreakDistance * syminfo.mintick))
curSessionHi := max(high, nz(curSessionHi))
curSessionLo := min(low, nz(curSessionLo, 1.0e23))
curBarIndexHi := change(curSessionHi) ? bar_index : curBarIndexHi
curBarIndexLo := change(curSessionLo) ? bar_index : curBarIndexLo
if levelBroken
prevSessionHi := max(prevSessionHi, curSessionHi)
prevSessionLo := min(prevSessionLo, curSessionLo)
// Debug section
// plotchar(prevSessionLo, "prevSessionLo", "")
// plotchar(prevSessionHi, "prevSessionHi", "")
// plotchar(prevBarIndexLo, "prevBarIndexLo", "")
// plotchar(prevBarIndexHi, "prevBarIndexHi", "")
// plotchar(curBarIndexLo, "curBarIndexLo", "")
// plotchar(curBarIndexHi, "curBarIndexHi", "")
// plotchar(lowFirst, "lowFirst", "")
// plotchar(bar_index, "bar_index", "")
// plotchar(fibStartBar, "fibStartBar", "")
// plotchar(fibBreakLevelPrice, "fibBreakLevelPrice", "")
// plotchar(levelBroken, "levelBroken", "")
bgcolor(newSession ? color.yellow : na)
2020 年 12 月 2 日更新:
这是我到目前为止的解决方案。
它似乎主要工作,但它可能仍然包含一些错误。
如果您发现任何问题,请告诉我。
//@version=4
study("Fib retr 4", overlay=true, max_bars_back=5000)
// study("Fib retr 4", overlay=true)
// ======== INPUTS ========
// {
var bool i_extend_left = input(false, "Fib line extend left", input.bool)
var bool i_extend_right = input(false, "Fib line extend right", input.bool)
var float i_fib_break_level = input(61.80, "Fib break level", input.float)
var float i_fib_break_margin = input(00.00, "Fib break margin", input.float)
// } === END INPUTS ===
// ======== VARIABLES ========
// {
var bool newSession = na
var string extendType = i_extend_left and i_extend_right ? extend.both : i_extend_right ? extend.right : i_extend_left ? extend.left : extend.none
var int sessionsBack = 0
var int max_keep_sessions = 100
var float[] arr_fib_levels = array.new_float()
var color[] arr_fib_colors = array.new_color()
var float[] arr_fib_draw_coords = array.new_float() // (x1, y1, x2, y2)
var line l0 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l1 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l2 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l3 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l4 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l5 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l6 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l7 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l8 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l9 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var float fibLevelToBreak = na
var bool fibLevelBroken = na
var int[] arrNewSessionBars = array.new_int(na)
var int barsBack = na
// } === END VARIABLES ===
// ========= FUNCTIONS =========
// {
f_move_horizontal_line(_line_id, _bar_start, _level, _bar_stop, _color) =>
// {
line.set_xy1(_line_id, _bar_start, _level)
line.set_xy2(_line_id, _bar_stop, _level)
line.set_color(_line_id, _color)
// }
// Initializes fibonacci levels and colors.
f_fib_init_levels(_arr_fib_levels, _arr_fib_colors) =>
// {
array.clear(_arr_fib_levels)
array.push(_arr_fib_levels, 00.00), array.push(_arr_fib_colors, color.green)
// array.push(_arr_fib_levels, 23.60), array.push(_arr_fib_colors, color.blue)
// array.push(_arr_fib_levels, 38.20), array.push(_arr_fib_colors, color.blue)
// array.push(_arr_fib_levels, 50.00), array.push(_arr_fib_colors, color.blue)
array.push(_arr_fib_levels, 61.80), array.push(_arr_fib_colors, color.yellow)
// array.push(_arr_fib_levels, 78.60), array.push(_arr_fib_colors, color.blue)
array.push(_arr_fib_levels, 100.00), array.push(_arr_fib_colors, color.red)
// }
// Retrieves the direction of the fibonacci retracement for the given coords (point1, point2)
f_fib_get_direction_for_coords(_x1, _y1, _x2, _y2) => (_y1 < _y2) ? -1 : (_y1 > _y2) ? 1 : 0
// Retrieves the zero level of the fibonacci retracement for the given coords (point1, point2)
f_fib_get_level_zero_for_coords(_x1, _y1, _x2, _y2) => _y2
// Retrieves specifications from a fibonacci retracement for the given coords (point1, point2)
f_fib_get_specs_for_coords(_x1, _y1, _x2, _y2) =>
// {
fib_range = abs(_y1 - _y2)
fib_direction = f_fib_get_direction_for_coords(_x1, _y1, _x2, _y2)
fib_level_zero = f_fib_get_level_zero_for_coords(_x1, _y1, _x2, _y2)
[fib_range, fib_direction, fib_level_zero]
// }
// Draws a fibonacci retracement with given coordinates.
f_fib_draw_coords(_x1, _y1, _x2, _y2, _x_stop) =>
// {
// Fib is drawn from point1 to point2. Point1 = 100%-level, Point2 = 0%-level.
var int fib_linecount = array.size(arr_fib_levels)
if not fib_linecount
f_fib_init_levels(arr_fib_levels, arr_fib_colors)
fib_linecount := array.size(arr_fib_levels)
[fib_range, fib_direction, fib_level_zero] = f_fib_get_specs_for_coords(_x1, _y1, _x2, _y2)
fib_x_start = min(_x1, _x2)
fib_x_stop = max(max(_x1, _x2), nz(_x_stop) ? _x_stop : bar_index)
for i = 0 to fib_linecount-1
fib_line = i==0 ? l0 : i==1 ? l1 : i==2 ? l2 : i==3 ? l3 : i==4 ? l4 : i==5 ? l5 : i==6 ? l6 : i==7 ? l7 : i==8 ? l8 : i==9 ? l9 : na
fib_level = fib_level_zero + (fib_direction * fib_range * array.get(arr_fib_levels, i) / 100)
fib_color = array.get(arr_fib_colors, i)
f_move_horizontal_line(fib_line, fib_x_start, fib_level, fib_x_stop, fib_color)
// }
// Retrieves the value of the requested fib level for the given coords (point1, point2)
f_fib_get_level_value_for_coords(_requested_fib_level, _x1, _y1, _x2, _y2) =>
// {
[fib_range, fib_direction, fib_level_zero] = f_fib_get_specs_for_coords(_x1, _y1, _x2, _y2)
fib_level = fib_level_zero + (fib_direction * fib_range * _requested_fib_level/100)
// }
// f_getHiLoCoords(_sessionsBack) =>
f_getHiLoCoords(_len) =>
lo = lowest(_len)
hi = highest(_len)
bar_lo = bar_index + lowestbars(_len)
bar_hi = bar_index + highestbars(_len)
if bar_lo < bar_hi
[bar_lo, lo, bar_hi, hi]
else
[bar_hi, hi, bar_lo, lo]
f_setFibDrawCoords(_barsBack) =>
[x1, y1, x2, y2] = f_getHiLoCoords(_barsBack)
array.clear(arr_fib_draw_coords)
array.push(arr_fib_draw_coords, x1), array.push(arr_fib_draw_coords, y1)
array.push(arr_fib_draw_coords, x2), array.push(arr_fib_draw_coords, y2)
f_getFibDrawCoords() =>
x1 = int(array.get(arr_fib_draw_coords, 0)), y1 = array.get(arr_fib_draw_coords, 1)
x2 = int(array.get(arr_fib_draw_coords, 2)), y2 = array.get(arr_fib_draw_coords, 3)
[x1, y1, x2, y2]
f_fib_draw(_x_stop) =>
[x1, y1, x2, y2] = f_getFibDrawCoords()
f_fib_draw_coords(x1, y1, x2, y2, _x_stop)
f_fib_get_level_value(_level) =>
[x1, y1, x2, y2] = f_getFibDrawCoords()
f_fib_get_level_value_for_coords(_level, x1, y1, x2, y2)
f_get_bars_back(_sessionsBack) =>
arrSize = array.size(arrNewSessionBars)
result = bar_index - (arrSize > 0 ? array.get(arrNewSessionBars, min(_sessionsBack, arrSize ? arrSize-1 : 0)) : bar_index) + 1 // number of bars to look back for high/low
// } ==== END FUNCTIONS ====
// ======== MAIN ========
// {
newSession := change(time("D"))
if newSession
array.unshift(arrNewSessionBars, bar_index) // Most recent addition always at index 0
if array.size(arrNewSessionBars) > max_keep_sessions
array.pop(arrNewSessionBars) // remove last element
sessionsBack := 0
f_setFibDrawCoords(f_get_bars_back(sessionsBack))
fibLevelToBreak := f_fib_get_level_value(i_fib_break_level)
fibLevelBroken := cross(close, fibLevelToBreak - i_fib_break_margin) or cross(close, fibLevelToBreak + i_fib_break_margin)
if fibLevelBroken
sessionsBack := sessionsBack + 1
f_fib_draw(bar_index)
// } === END MAIN ===
// ======== DEBUG ========
// {
// plotchar(bar_index, "bar_index", "")
// plotchar(myFibLevel64, "myFibLevel64", "")
// plotchar(myFibLevelBroken, "myFibLevelBroken", "")
// plotchar(barsBack, "barsBack", "")
// plot(myFibLevel64)
// bgcolor(newSession ? color.white : na, 70)
// } === END DEBUG ===
Drawing/explenation 我需要根据会话“1700-1700”调整以下脚本 (© TradeChartist),我不确定如何以正确的方式输入时间公式。已设置为每天使用,但这并不能解决我的问题...
2 如果 61,8 超过 15 点,第二个需要调整为两个时段。
虽然第一个问题最紧迫...
将不胜感激。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TradeChartist
//@version=4
study("Fib Retracement", overlay=true, max_bars_back=5000)
Fibs = input("Plot Fibs based on Lookback", options = ["Plot Fibs based on Lookback", "Plot Fibs based on Price Input"], title = "Fibonacci Plot Type")
FIBS = Fibs == "Plot Fibs based on Lookback"?1:Fibs == "Plot Fibs based on Price Input"?2:na
Foption = input(defval = "1. Candles" , title = "Fibonacci Plot Lookback Type", options=["2. Days", "1. Candles"])
FP = input(defval = 100 , title = "Days/Candles to Lookback")
Reverse = input(defval = false , title = "Reverse Fibonacci Levels?")
ExtraFibs = input(false, "Show 0.886 and 1.113 Fibs")
Note = input(true, "════ ════")
High = input(0., minval = 0, title = "High - Enter Value")
Low = input(-1., minval = -1, title = "Low - Enter Value")
Note2 = input(true, "══════ / ══════")
Bull_Color = input(#008000, type=input.color, title = "Support Fibs Color")
Bear_Color = input(#ff0000, type=input.color, title = "Resistance Fibs Color")
CurrentFib = input(false, "Show Fib Level of Current Price")
Current_Color = input(color.orange, type=input.color, title = "Current Fib Label Color")
LineStyle = input("Dotted", options = ["Dotted", "Solid"], title = "Fib Line Style")
LineWidth = input(1, minval=1, maxval=3,title = "Fib Line Width")
Ext = input(false, "Extend Lines Left")
// Transparency = input("Low", options = ["High", "Medium", "Low"], title="Fib Line Transparency")
BullColor = Bull_Color//Transparency == "High"?color.new(#008000,75):Transparency == "Medium"?color.new(#008000,50):Bull_Color
BearColor = Bear_Color//Transparency == "High"?color.new(#ff0000,75):Transparency == "Medium"?color.new(#ff0000,50):Bear_Color
FPeriod = timeframe.isintraday and Foption=="2. Days"? (1440/timeframe.multiplier)*FP:
timeframe.isdaily and Foption=="2. Days"? FP/timeframe.multiplier:
timeframe.isweekly and Foption=="2. Days"? FP/(7*timeframe.multiplier):
timeframe.ismonthly and Foption=="2. Days"? FP/(28*timeframe.multiplier):
Foption=="1. Candles"? FP:100
Fhigh = FIBS==1? highest(FPeriod) : FIBS == 2 and High == 0? highest(high,100): FIBS == 2 and High!=0? High:na
Flow = FIBS==1? lowest(FPeriod) : FIBS == 2 and Low == -1? lowest(low,100): FIBS == 2 and High!=-1? Low:na
FH = FIBS == 1?highestbars(high,FPeriod): 1
FL = FIBS == 1?lowestbars(low,FPeriod): 2
revfibs = not Reverse? FL>FH : FL<FH
Fib_x(n) =>
revfibs ? (Fhigh-Flow)*n+Flow : Fhigh-(Fhigh-Flow)*n
Current = revfibs?(close-Flow)/(Fhigh-Flow):(Fhigh-close)/(Fhigh-Flow)
var label Current_Fib_Label = na
label.delete(Current_Fib_Label)
if(CurrentFib and barstate.islast)
Current_Fib_Label := label.new(bar_index, close, tostring(Current, "##.##"), textcolor = Current_Color, color = color.new(#000000,100), style=label.style_label_left, yloc=yloc.price)
EXTEND = Ext?extend.left:extend.none
STYLE = LineStyle=="Dotted"?line.style_dotted:line.style_solid
WIDTH = LineWidth
BB = FIBS==1?(FL<FH?bar_index[-FL]:bar_index[-FH]):FIBS==2?bar_index[50]:bar_index[50]
Fib_line(x) =>
var line ln = na
line.delete(ln)
ln:=line.new(BB, x, bar_index, x, color = close>x? BullColor:BearColor, extend=EXTEND ,style=STYLE, width = WIDTH)
Fib_label(x,_txt) =>
var label lbl = na
label.delete(lbl)
lbl:=label.new(bar_index, x, _txt + tostring(x, "##.########") + " )", textcolor = close>x?BullColor:BearColor, color = color.new(#000000,100), style=label.style_label_left, yloc=yloc.price)
Fib0 = Fib_line(Fib_x(0))
Fib236 = Fib_line(Fib_x(0.236))
Fib382 = Fib_line(Fib_x(0.382))
Fib500 = Fib_line(Fib_x(0.500))
Fib618 = Fib_line(Fib_x(0.618))
Fib786 = Fib_line(Fib_x(0.786))
Fib1000 = Fib_line(Fib_x(1.000))
Fib886 = ExtraFibs?Fib_line(Fib_x(0.886)):na
if(FIBS==2)
Fib1113 = ExtraFibs?Fib_line(Fib_x(1.113)):na
Fib1272 = Fib_line(Fib_x(1.272))
Fib1618 = Fib_line(Fib_x(1.618))
Fib2000 = Fib_line(Fib_x(2.000))
Fib2236 = Fib_line(Fib_x(2.236))
Fib2618 = Fib_line(Fib_x(2.618))
Fib3236 = Fib_line(Fib_x(3.236))
Fib3618 = Fib_line(Fib_x(3.618))
Fib4236 = Fib_line(Fib_x(4.236))
Fib4618 = Fib_line(Fib_x(4.618))
LFib0 = Fib_label(Fib_x(0), "0 ( ")
LFib236 = Fib_label(Fib_x(0.236), "0.236 ( ")
LFib382 = Fib_label(Fib_x(0.382), "0.382 ( ")
LFib500 = Fib_label(Fib_x(0.500), "0.500 ( ")
LFib618 = Fib_label(Fib_x(0.618), "0.618 ( ")
LFib786 = Fib_label(Fib_x(0.786), "0.786 ( ")
LFib1000 = Fib_label(Fib_x(1.000), "1.000 ( ")
LFib886 = ExtraFibs?Fib_label(Fib_x(0.886), "0.886 ( "):na
if(FIBS==2)
LFib1113 = ExtraFibs?Fib_label(Fib_x(1.113), "1.113 ( "):na
LFib1272 = Fib_label(Fib_x(1.272), "1.272 ( ")
LFib1618 = Fib_label(Fib_x(1.618), "1.618 ( ")
LFib2000 = Fib_label(Fib_x(2.000), "2.000 ( ")
LFib2236 = Fib_label(Fib_x(2.236), "2.236 ( ")
LFib2618 = Fib_label(Fib_x(2.618), "2.618 ( ")
LFib3236 = Fib_label(Fib_x(3.236), "3.236 ( ")
LFib3618 = Fib_label(Fib_x(3.618), "3.618 ( ")
LFib4236 = Fib_label(Fib_x(4.236), "4.236 ( ")
LFib4618 = Fib_label(Fib_x(4.618), "4.618 ( ")
我创建了这个:
//@version=4
study("Fib Retracement", overlay=true, max_bars_back=5000)
var int sessionStartHour = input(17, "Session start hour (UTC)", input.integer)
var bool extendLeft = input(false, "Fib line extend left", input.bool)
var bool extendRight = input(false, "Fib line extend right", input.bool)
var float fibBreakLevel = input(61.80, "Fib break level", input.float)
var int fibBreakDistance = input(10, "Level break distance (ticks)", input.integer)
var float fibBreakLevelPrice = na
var string extendType = extendLeft and extendRight ? extend.both : extendRight ? extend.right : extendLeft ? extend.left : extend.none
var float curSessionHi = na
var float curSessionLo = na
var float prevSessionHi = na
var float prevSessionLo = na
var int curHour = na
var int curBarIndexHi = na
var int curBarIndexLo = na
var int prevBarIndexHi = na
var int prevBarIndexLo = na
var int fibLevelCount = na
var bool lowFirst = na
var int fibStartBar = na
var arrFibLevels = array.new_float()
var arrFibColors = array.new_color()
var bool levelBroken = na
var l0 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l1 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l2 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l3 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l4 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l5 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l6 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l7 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l8 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var l9 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
f_moveLine(_id, _x, _y, _color) =>
line.set_xy1(_id, _x, _y)
line.set_xy2(_id, bar_index, _y)
line.set_color(_id, _color)
fib_setLevels() =>
array.clear(arrFibLevels)
array.push(arrFibLevels, 00.00), array.push(arrFibColors, color.green)
array.push(arrFibLevels, 23.60), array.push(arrFibColors, color.blue)
array.push(arrFibLevels, 38.20), array.push(arrFibColors, color.blue)
array.push(arrFibLevels, 50.00), array.push(arrFibColors, color.blue)
array.push(arrFibLevels, 61.80), array.push(arrFibColors, color.yellow)
array.push(arrFibLevels, 78.60), array.push(arrFibColors, color.blue)
array.push(arrFibLevels, 100.00), array.push(arrFibColors, color.red)
array.size(arrFibLevels)
fib_retrace(_startBar, _levelLo, _levelHi, _lowFirst) =>
var float level0 = na
var float level = na
var line ln = na
var int dir = na
var color fibColor = na
var float breakPrice = na
var float fibLevel = na
dir := _lowFirst ? 1 : -1
fibRange = abs(_levelHi - _levelLo)
level0 := _lowFirst ? _levelLo : _levelHi
for i = 0 to fibLevelCount-1
ln := i==0 ? l0 : i==1 ? l1 : i==2 ? l2 : i==3 ? l3 : i==4 ? l4 : i==5 ? l5 : i==6 ? l6 : i==7 ? l7 : i==8 ? l8 : i==9 ? l9 : na
fibLevel := array.get(arrFibLevels, i)
level := level0 + (dir * fibRange * fibLevel/100)
if (fibLevel == fibBreakLevel)
breakPrice := level
fibColor := array.get(arrFibColors, i)
f_moveLine(ln, _startBar, level, fibColor)
breakPrice
if barstate.isfirst
fibLevelCount := fib_setLevels()
curHour := hour(time, 'UTC')
newSession = (curHour == sessionStartHour) and change(curHour)
if newSession
prevSessionHi := curSessionHi
prevSessionLo := curSessionLo
prevBarIndexHi := curBarIndexHi
prevBarIndexLo := curBarIndexLo
lowFirst := prevBarIndexLo < prevBarIndexHi
fibStartBar := lowFirst ? prevBarIndexLo : prevBarIndexHi
curSessionLo := na
curSessionHi := na
curBarIndexHi := na
curBarIndexLo := na
levelBroken := na
// Draw fibs for prev session
fibBreakLevelPrice := fib_retrace(fibStartBar, prevSessionLo, prevSessionHi, lowFirst)
if lowFirst
levelBroken := close > (fibBreakLevelPrice + (fibBreakDistance * syminfo.mintick))
else
levelBroken := close < (fibBreakLevelPrice - (fibBreakDistance * syminfo.mintick))
curSessionHi := max(high, nz(curSessionHi))
curSessionLo := min(low, nz(curSessionLo, 1.0e23))
curBarIndexHi := change(curSessionHi) ? bar_index : curBarIndexHi
curBarIndexLo := change(curSessionLo) ? bar_index : curBarIndexLo
if levelBroken
prevSessionHi := max(prevSessionHi, curSessionHi)
prevSessionLo := min(prevSessionLo, curSessionLo)
// Debug section
// plotchar(prevSessionLo, "prevSessionLo", "")
// plotchar(prevSessionHi, "prevSessionHi", "")
// plotchar(prevBarIndexLo, "prevBarIndexLo", "")
// plotchar(prevBarIndexHi, "prevBarIndexHi", "")
// plotchar(curBarIndexLo, "curBarIndexLo", "")
// plotchar(curBarIndexHi, "curBarIndexHi", "")
// plotchar(lowFirst, "lowFirst", "")
// plotchar(bar_index, "bar_index", "")
// plotchar(fibStartBar, "fibStartBar", "")
// plotchar(fibBreakLevelPrice, "fibBreakLevelPrice", "")
// plotchar(levelBroken, "levelBroken", "")
bgcolor(newSession ? color.yellow : na)
2020 年 12 月 2 日更新:
这是我到目前为止的解决方案。
它似乎主要工作,但它可能仍然包含一些错误。
如果您发现任何问题,请告诉我。
//@version=4
study("Fib retr 4", overlay=true, max_bars_back=5000)
// study("Fib retr 4", overlay=true)
// ======== INPUTS ========
// {
var bool i_extend_left = input(false, "Fib line extend left", input.bool)
var bool i_extend_right = input(false, "Fib line extend right", input.bool)
var float i_fib_break_level = input(61.80, "Fib break level", input.float)
var float i_fib_break_margin = input(00.00, "Fib break margin", input.float)
// } === END INPUTS ===
// ======== VARIABLES ========
// {
var bool newSession = na
var string extendType = i_extend_left and i_extend_right ? extend.both : i_extend_right ? extend.right : i_extend_left ? extend.left : extend.none
var int sessionsBack = 0
var int max_keep_sessions = 100
var float[] arr_fib_levels = array.new_float()
var color[] arr_fib_colors = array.new_color()
var float[] arr_fib_draw_coords = array.new_float() // (x1, y1, x2, y2)
var line l0 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l1 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l2 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l3 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l4 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l5 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l6 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l7 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l8 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var line l9 = line.new(x1=na, y1=na, x2=na, y2=na, extend=extendType)
var float fibLevelToBreak = na
var bool fibLevelBroken = na
var int[] arrNewSessionBars = array.new_int(na)
var int barsBack = na
// } === END VARIABLES ===
// ========= FUNCTIONS =========
// {
f_move_horizontal_line(_line_id, _bar_start, _level, _bar_stop, _color) =>
// {
line.set_xy1(_line_id, _bar_start, _level)
line.set_xy2(_line_id, _bar_stop, _level)
line.set_color(_line_id, _color)
// }
// Initializes fibonacci levels and colors.
f_fib_init_levels(_arr_fib_levels, _arr_fib_colors) =>
// {
array.clear(_arr_fib_levels)
array.push(_arr_fib_levels, 00.00), array.push(_arr_fib_colors, color.green)
// array.push(_arr_fib_levels, 23.60), array.push(_arr_fib_colors, color.blue)
// array.push(_arr_fib_levels, 38.20), array.push(_arr_fib_colors, color.blue)
// array.push(_arr_fib_levels, 50.00), array.push(_arr_fib_colors, color.blue)
array.push(_arr_fib_levels, 61.80), array.push(_arr_fib_colors, color.yellow)
// array.push(_arr_fib_levels, 78.60), array.push(_arr_fib_colors, color.blue)
array.push(_arr_fib_levels, 100.00), array.push(_arr_fib_colors, color.red)
// }
// Retrieves the direction of the fibonacci retracement for the given coords (point1, point2)
f_fib_get_direction_for_coords(_x1, _y1, _x2, _y2) => (_y1 < _y2) ? -1 : (_y1 > _y2) ? 1 : 0
// Retrieves the zero level of the fibonacci retracement for the given coords (point1, point2)
f_fib_get_level_zero_for_coords(_x1, _y1, _x2, _y2) => _y2
// Retrieves specifications from a fibonacci retracement for the given coords (point1, point2)
f_fib_get_specs_for_coords(_x1, _y1, _x2, _y2) =>
// {
fib_range = abs(_y1 - _y2)
fib_direction = f_fib_get_direction_for_coords(_x1, _y1, _x2, _y2)
fib_level_zero = f_fib_get_level_zero_for_coords(_x1, _y1, _x2, _y2)
[fib_range, fib_direction, fib_level_zero]
// }
// Draws a fibonacci retracement with given coordinates.
f_fib_draw_coords(_x1, _y1, _x2, _y2, _x_stop) =>
// {
// Fib is drawn from point1 to point2. Point1 = 100%-level, Point2 = 0%-level.
var int fib_linecount = array.size(arr_fib_levels)
if not fib_linecount
f_fib_init_levels(arr_fib_levels, arr_fib_colors)
fib_linecount := array.size(arr_fib_levels)
[fib_range, fib_direction, fib_level_zero] = f_fib_get_specs_for_coords(_x1, _y1, _x2, _y2)
fib_x_start = min(_x1, _x2)
fib_x_stop = max(max(_x1, _x2), nz(_x_stop) ? _x_stop : bar_index)
for i = 0 to fib_linecount-1
fib_line = i==0 ? l0 : i==1 ? l1 : i==2 ? l2 : i==3 ? l3 : i==4 ? l4 : i==5 ? l5 : i==6 ? l6 : i==7 ? l7 : i==8 ? l8 : i==9 ? l9 : na
fib_level = fib_level_zero + (fib_direction * fib_range * array.get(arr_fib_levels, i) / 100)
fib_color = array.get(arr_fib_colors, i)
f_move_horizontal_line(fib_line, fib_x_start, fib_level, fib_x_stop, fib_color)
// }
// Retrieves the value of the requested fib level for the given coords (point1, point2)
f_fib_get_level_value_for_coords(_requested_fib_level, _x1, _y1, _x2, _y2) =>
// {
[fib_range, fib_direction, fib_level_zero] = f_fib_get_specs_for_coords(_x1, _y1, _x2, _y2)
fib_level = fib_level_zero + (fib_direction * fib_range * _requested_fib_level/100)
// }
// f_getHiLoCoords(_sessionsBack) =>
f_getHiLoCoords(_len) =>
lo = lowest(_len)
hi = highest(_len)
bar_lo = bar_index + lowestbars(_len)
bar_hi = bar_index + highestbars(_len)
if bar_lo < bar_hi
[bar_lo, lo, bar_hi, hi]
else
[bar_hi, hi, bar_lo, lo]
f_setFibDrawCoords(_barsBack) =>
[x1, y1, x2, y2] = f_getHiLoCoords(_barsBack)
array.clear(arr_fib_draw_coords)
array.push(arr_fib_draw_coords, x1), array.push(arr_fib_draw_coords, y1)
array.push(arr_fib_draw_coords, x2), array.push(arr_fib_draw_coords, y2)
f_getFibDrawCoords() =>
x1 = int(array.get(arr_fib_draw_coords, 0)), y1 = array.get(arr_fib_draw_coords, 1)
x2 = int(array.get(arr_fib_draw_coords, 2)), y2 = array.get(arr_fib_draw_coords, 3)
[x1, y1, x2, y2]
f_fib_draw(_x_stop) =>
[x1, y1, x2, y2] = f_getFibDrawCoords()
f_fib_draw_coords(x1, y1, x2, y2, _x_stop)
f_fib_get_level_value(_level) =>
[x1, y1, x2, y2] = f_getFibDrawCoords()
f_fib_get_level_value_for_coords(_level, x1, y1, x2, y2)
f_get_bars_back(_sessionsBack) =>
arrSize = array.size(arrNewSessionBars)
result = bar_index - (arrSize > 0 ? array.get(arrNewSessionBars, min(_sessionsBack, arrSize ? arrSize-1 : 0)) : bar_index) + 1 // number of bars to look back for high/low
// } ==== END FUNCTIONS ====
// ======== MAIN ========
// {
newSession := change(time("D"))
if newSession
array.unshift(arrNewSessionBars, bar_index) // Most recent addition always at index 0
if array.size(arrNewSessionBars) > max_keep_sessions
array.pop(arrNewSessionBars) // remove last element
sessionsBack := 0
f_setFibDrawCoords(f_get_bars_back(sessionsBack))
fibLevelToBreak := f_fib_get_level_value(i_fib_break_level)
fibLevelBroken := cross(close, fibLevelToBreak - i_fib_break_margin) or cross(close, fibLevelToBreak + i_fib_break_margin)
if fibLevelBroken
sessionsBack := sessionsBack + 1
f_fib_draw(bar_index)
// } === END MAIN ===
// ======== DEBUG ========
// {
// plotchar(bar_index, "bar_index", "")
// plotchar(myFibLevel64, "myFibLevel64", "")
// plotchar(myFibLevelBroken, "myFibLevelBroken", "")
// plotchar(barsBack, "barsBack", "")
// plot(myFibLevel64)
// bgcolor(newSession ? color.white : na, 70)
// } === END DEBUG ===