ORB Break-Return-Cross Entry

Trend_MyFrend · study · 204 行 · 点赞 905 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Trend_MyFrend (Modified for Time-Limited Retest, Extension Control & Half-Range SL)
//@version=6
indicator("ORB Retest Entry", "ORB_TMF", true, max_labels_count = 500, max_lines_count = 500, max_boxes_count =  500)
 
// --- Inputs ---
tf = input.timeframe("15", "ORB Timeframe",inline = "time")
lastentryTime = input.string("10:15", "Entry Valid Time",inline = "time")
ishist = input(true, "", inline = "History")
hist = input.int(10, "History Signals Show", minval = 1, inline = "History", active = ishist)
bfrRange = input.float(0.00, "ORB buffer %",step=0.01,inline = "buffer") / 100
bfrorder = input.float(-0.02, "Buffer for Position %",step=0.01,inline = "buffer") / 100
rrInput = input.float(2, "Set Risk-Reward Ratio",step = 1)
 
 
var bool activeRange = true
var float hv = na
var float lv = na
var bool validTime = false 
if session.isfirstbar
    activeRange := true
 
if timeframe.change(tf) and (not session.isfirstbar)
    activeRange := false
 
var HighArray = array.new<float>()
var LowArray = array.new<float>()
 
if session.isfirstbar
    HighArray.clear()
    LowArray.clear()
 
if activeRange
    validTime := true
    array.push(HighArray, high)
    array.push(LowArray, low)
 
startLine = (activeRange[1]) and (not activeRange)
 
 
// --- Types & Orders ---
type LL
    line Line
    label Label
 
type order
    LL entry
    LL sl
    LL tg
    float slPrice
    float tpPrice
    bool active      
    bool isBuy       
 
var order orderBuy = na
var order orderSell = na
var orderHistory = array.new<order>()
var signalLabels = array.new<label>()
 
drawLL(y, clr, txt) =>
    Line = line.new(bar_index, y, bar_index, y, color = clr, width = 2)
    Label = label.new(bar_index, y, text = txt, color = clr, style = label.style_label_left)
    LL.new(Line, Label)
 
orderList(ev, sv, tv, isBuy) =>
    E = drawLL(ev, color.blue, "Entry : " + str.tostring(ev, format.mintick))
    S = drawLL(sv, color.red, "SL : " + str.tostring(sv, format.mintick))
    T = drawLL(tv, color.green, "TP : " + str.tostring(tv, format.mintick))
    newOrder = order.new(E, S, T, sv, tv, true, isBuy)
    array.push(orderHistory, newOrder)
    newOrder
 
extend(_this) =>
    if not na(_this) and _this.active 
        line.set_x2(_this.entry.Line, bar_index)
        label.set_x(_this.entry.Label, bar_index)
        line.set_x2(_this.sl.Line, bar_index)
        label.set_x(_this.sl.Label, bar_index)
        line.set_x2(_this.tg.Line, bar_index)
        label.set_x(_this.tg.Label, bar_index)
 
deleteOrder(_this) =>
    if not na(_this)
        line.delete(_this.entry.Line)
        label.delete(_this.entry.Label)
        line.delete(_this.sl.Line)
        label.delete(_this.sl.Label)
        line.delete(_this.tg.Line)
        label.delete(_this.tg.Label)
 
// --- ORB Box 
var box orbBox = na   
if session.isfirstbar
    orbBox := box.new(left=bar_index, top=high, right=bar_index, bottom=low, border_width=1,bgcolor = color.new(color.blue, 90),border_color =color.new(color.blue, 90))
 
if startLine
    hv := array.max(HighArray) + (array.max(HighArray) * bfrRange)
    lv := array.min(LowArray) - (array.min(LowArray) * bfrRange)
    if not na(orbBox)
        box.set_top(orbBox, hv)
        box.set_bottom(orbBox, lv)
        box.set_bgcolor(orbBox, color.new(color.blue, 95))
        box.set_border_color(orbBox, color.new(color.teal, 90))
 
if not na(orbBox)
    box.set_right(orbBox, bar_index)
 
// --- Signal Logic ---
var bool waitingForBuyRetest = false
var bool waitingForSellRetest = false
var bool buySignalShown = false
var bool sellSignalShown = false
var int breakoutBar = 0
 
if not activeRange and validTime and not startLine
    if not buySignalShown and not waitingForBuyRetest and high > hv
        waitingForBuyRetest := true
        breakoutBar := bar_index
    if not sellSignalShown and not waitingForSellRetest and low < lv
        waitingForSellRetest := true
        breakoutBar := bar_index
 
buyRetest  = validTime and waitingForBuyRetest and bar_index > breakoutBar and low <= hv 
sellRetest = validTime and waitingForSellRetest and bar_index > breakoutBar and high >= lv 
// sl adjest 
float rangeHalf = (hv - lv) / 2
 
if buyRetest and not buySignalShown
    entryPrice = hv + hv * bfrorder
    slPrice = entryPrice - rangeHalf
    tpPrice = entryPrice + (rangeHalf * rrInput)
    
    lbl = label.new(bar_index, low, "Buy", style=label.style_label_up, color=color.blue, textcolor=color.white)
    
    // Set Box Color to Green on Buy
    if not na(orbBox)
        box.set_bgcolor(orbBox, color.new(color.green, 95))
        box.set_border_color(orbBox, color.new(color.green, 80))
        
    array.push(signalLabels, lbl)
    orderBuy := orderList(entryPrice, slPrice, tpPrice, true)
    buySignalShown := true
    waitingForBuyRetest := false
 
if sellRetest and not sellSignalShown
    entryPrice = lv - lv * bfrorder
    slPrice = entryPrice + rangeHalf
    tpPrice = entryPrice - (rangeHalf * rrInput)
    
    lbl = label.new(bar_index, high, "Sell", style=label.style_label_down, color=#811212, textcolor=color.white)
    
    // Set Box Color to Red on Sell
    if not na(orbBox)
        box.set_bgcolor(orbBox, color.new(color.red, 95))
        box.set_border_color(orbBox, color.new(color.red, 80))
        
    array.push(signalLabels, lbl)
    orderSell := orderList(entryPrice, slPrice, tpPrice, false)
    sellSignalShown := true
    waitingForSellRetest := false
 
// --- Check for SL/TP Hits ---
checkExit(order chk) =>
    if not na(chk) and chk.active
        if chk.isBuy
            if low <= chk.slPrice 
                chk.active := false
                label.set_text(chk.sl.Label, "SL Hit : " + str.tostring(line.get_y1(chk.entry.Line), format.mintick))
                label.set_color(chk.sl.Label, color.maroon)
                
            else if high >= chk.tpPrice
                chk.active := false
                label.set_text(chk.tg.Label, "TP Hit : " + str.tostring(line.get_y1(chk.tg.Line), format.mintick))
                label.set_color(chk.tg.Label, #175c0e)
        else
            // Sell logic with text updates
            if high >= chk.slPrice
                chk.active := false
                label.set_text(chk.sl.Label, "SL Hit : " + str.tostring(line.get_y1(chk.entry.Line), format.mintick))
                label.set_color(chk.sl.Label, color.maroon)
            else if low <= chk.tpPrice
                chk.active := false
                label.set_text(chk.tg.Label, "TP Hit : " + str.tostring(line.get_y1(chk.tg.Line), format.mintick))
                label.set_color(chk.tg.Label, #175c0e)
checkExit(orderBuy)
checkExit(orderSell)
 
// --- Extensions & History Control ---
extend(orderBuy)
extend(orderSell)
 
if ishist and array.size(orderHistory) > hist
    deleteOrder(array.shift(orderHistory))
if ishist and array.size(signalLabels) > hist
    label.delete(array.shift(signalLabels))
 
if session.isfirstbar
    orderBuy := na
    orderSell := na
    buySignalShown := false
    sellSignalShown := false
 
if lastentryTime == str.format_time(time, "HH:mm", "GMT+0530")
    validTime := false

← 返回列表