NQ CISD London 2R | Boxy | 10000 USD

Bystry1991 · strategy · 337 行 · 点赞 12 · TradingView 原页

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

Pine Script

//@version=6
// CISD — wariant badawczy testowany na CME_MINI:NQ1! M1.
// Domyślny okres: 01.05–31.10.2026.
// Zakodowany kalendarz: 01.11.2025–31.10.2026.
// Depozyt 1% nominału jest założeniem symulacji.
// Limit ryzyka SL nie zawiera prowizji, poślizgu ani luk.
 
strategy("NQ CISD London 2R | Boxy | 10000 USD",
     overlay=true,
     initial_capital=10000,
     currency=currency.USD,
     default_qty_type=strategy.fixed,
     default_qty_value=1,
     pyramiding=0,
     commission_type=strategy.commission.cash_per_contract,
     commission_value=3,
     slippage=1,
     backtest_fill_limits_assumption=1,
     use_bar_magnifier=true,
     process_orders_on_close=false,
     calc_on_order_fills=false,
     calc_on_every_tick=false,
     margin_long=1,
     margin_short=1,
     max_boxes_count=500)
 
// Ustawienia.
sessions=input.string("London","Sessions",
     options=["Both","New York","London"])
 
windowMin=input.int(60,"Entry window from open",
     options=[15,30,60])
 
maxRisk=input.float(1000,
     "Maximum planned stop-distance risk USD",
     minval=100,maxval=2000,step=100)
 
start=input.time(
     timestamp("01 May 2026 00:00 +0000"),
     "Start inclusive")
 
finish=input.time(
     timestamp("01 Nov 2026 00:00 +0000"),
     "Finish exclusive")
 
int effectiveStart=start
int effectiveFinish=finish
float tick=syminfo.mintick
int tf=int(timeframe.in_seconds()/60)
 
if barstate.isfirst and (syminfo.root!="NQ" or syminfo.pointvalue!=20 or not chart.is_standard or not timeframe.isminutes or tf>15)
    runtime.error("Use standard CME NQ futures minute candles.")
 
// Kalendarz i sesje.
int dayNY=year(time,"America/New_York")*10000+month(time,"America/New_York")*100+dayofmonth(time,"America/New_York")
int dayUK=year(time,"Europe/London")*10000+month(time,"Europe/London")*100+dayofmonth(time,"Europe/London")
 
bool closedNY=dayNY==20251127 or dayNY==20251225 or dayNY==20260101 or dayNY==20260119 or dayNY==20260216 or dayNY==20260403 or dayNY==20260525 or dayNY==20260619 or dayNY==20260703 or dayNY==20260907
bool closedUK=dayUK==20251225 or dayUK==20251226 or dayUK==20260101 or dayUK==20260403 or dayUK==20260406 or dayUK==20260504 or dayUK==20260525 or dayUK==20260831
bool earlyNY=dayNY==20251128 or dayNY==20251224
 
int nyEnd=earlyNY?780:960
 
bool calendarOK=time>=timestamp("01 Nov 2025 00:00 +0000") and time<timestamp("01 Nov 2026 00:00 +0000")
 
int ny=hour(time,"America/New_York")*60+minute(time,"America/New_York")
int wa=hour(time,"Europe/Warsaw")*60+minute(time,"Europe/Warsaw")
 
int sid=calendarOK and not closedUK and sessions!="New York" and wa>=540 and wa<720?1:calendarOK and not closedNY and sessions!="London" and ny>=570 and ny<nyEnd?2:0
 
int elapsed=sid==1?wa-540:sid==2?ny-570:-1
 
int dk=year(time,"Europe/Warsaw")*10000+month(time,"Europe/Warsaw")*100+dayofmonth(time,"Europe/Warsaw")
int key=sid==0?0:dk*10+sid
 
// Zbieranie płynności i potwierdzenie CISD.
float priorLow=ta.lowest(low,10)[1]
float priorHigh=ta.highest(high,10)[1]
float cr=high-low
 
bool bullDisp=cr>0 and close>open and close-open>=0.6*cr and high-close<=0.25*cr
bool bearDisp=cr>0 and close<open and open-close>=0.6*cr and close-low<=0.25*cr
 
var float bearOpen=na
var float bullOpen=na
 
if close<open
    bearOpen:=open
 
if close>open
    bullOpen:=open
 
var int prevKey=0
var bool attempted=false
var int swL=na
var int swS=na
var float stopL=na
var float stopS=na
var float cisdL=na
var float cisdS=na
 
if key!=prevKey
    strategy.cancel_all()
    attempted:=false
    swL:=na
    swS:=na
    prevKey:=key
 
bool validDate=time>=effectiveStart and time_close<effectiveFinish
 
bool canSignal=barstate.isconfirmed and sid!=0 and validDate and elapsed+tf<windowMin and not attempted and strategy.position_size==0 and strategy.equity>0
 
if canSignal
    if low<priorLow and high<priorHigh
        swL:=bar_index
        stopL:=low-tick
        cisdL:=bearOpen
 
    if high>priorHigh and low>priorLow
        swS:=bar_index
        stopS:=high+tick
        cisdS:=bullOpen
 
    bool activeL=not na(swL) and bar_index-swL<=7
    bool activeS=not na(swS) and bar_index-swS<=7
 
    if activeL
        stopL:=math.min(stopL,low-tick)
 
    if activeS
        stopS:=math.max(stopS,high+tick)
 
    bool longSignal=activeL and bar_index>swL and not na(cisdL) and close>cisdL and close[1]<=cisdL and bullDisp
    bool shortSignal=activeS and bar_index>swS and not na(cisdS) and close<cisdS and close[1]>=cisdS and bearDisp
 
    float riskL=math.ceil((close+tick-stopL)/tick)
    float riskS=math.ceil((stopS-close+tick)/tick)
 
    bool canL=longSignal and riskL>0 and riskL*tick*20<=maxRisk
    bool canS=shortSignal and riskS>0 and riskS*tick*20<=maxRisk
 
    string lg=sid==1?"L_LON":"L_NY"
    string sh=sid==1?"S_LON":"S_NY"
 
    if canL and not canS
        strategy.entry(lg,strategy.long,
             comment="riskUSD="+str.tostring(riskL*tick*20))
 
        strategy.exit("XL",lg,
             loss=riskL,
             profit=2*riskL)
 
        attempted:=true
 
    else if canS and not canL
        strategy.entry(sh,strategy.short,
             comment="riskUSD="+str.tostring(riskS*tick*20))
 
        strategy.exit("XS",sh,
             loss=riskS,
             profit=2*riskS)
 
        attempted:=true
 
// Zamknięcie przed końcem sesji — bez overnight.
if strategy.position_size!=0 and (sid==0 or (sid==1 and wa+tf>=720) or (sid==2 and ny+tf>=nyEnd) or time_close>=effectiveFinish)
    strategy.close_all(comment="TIME",immediately=true)
 
// Kontrola dat, godzin i limitu transakcji.
if barstate.islastconfirmedhistory and strategy.opentrades>0
    runtime.error("AUDIT open position at end")
 
if barstate.islastconfirmedhistory and strategy.closedtrades>0
    int lastSessionKey=na
    int lastDayKey=na
    int tradesToday=0
 
    for i=0 to strategy.closedtrades-1
        int et=strategy.closedtrades.entry_time(i)
        int xt=strategy.closedtrades.exit_time(i)
 
        bool lon=str.contains(
             strategy.closedtrades.entry_id(i),"_LON")
 
        string tz=lon?"Europe/Warsaw":"America/New_York"
        int em=hour(et,tz)*60+minute(et,tz)
        int op=lon?540:570
 
        int ed=year(et,"Europe/Warsaw")*10000+month(et,"Europe/Warsaw")*100+dayofmonth(et,"Europe/Warsaw")
        int xd=year(xt,"Europe/Warsaw")*10000+month(xt,"Europe/Warsaw")*100+dayofmonth(xt,"Europe/Warsaw")
        int sk=ed*10+(lon?1:2)
 
        if et<effectiveStart or et>=effectiveFinish
            runtime.error("AUDIT entry outside interval: "+str.format_time(et,"yyyy-MM-dd HH:mm","Europe/Warsaw"))
 
        int xm=hour(xt,tz)*60+minute(xt,tz)
        int cutoff=lon?720:((ed==20251128 or ed==20251224)?780:960)
 
        if xt>=effectiveFinish or xm>cutoff
            runtime.error("AUDIT exit outside bounds/cutoff")
 
        if em<op or em>=op+windowMin or ed!=xd
            runtime.error("AUDIT session/overnight violation")
 
        if sk==lastSessionKey
            runtime.error("AUDIT multiple entries in session")
 
        tradesToday:=ed==lastDayKey?tradesToday+1:1
 
        if tradesToday>2
            runtime.error("AUDIT more than2entries/day")
 
        lastSessionKey:=sk
        lastDayKey:=ed
 
// ───────────── BOXY POZYCJI ─────────────
// Czerwony: wejście–SL.
// Zielony: wejście–TP 2R.
// Wizualizacja nie zmienia zleceń ani wyniku strategii.
 
showBoxes=input.bool(true,
     "Pokaż boxy pozycji",group="Boxy")
 
keepTrades=input.int(200,
     "Liczba widocznych zamkniętych pozycji",
     minval=1,maxval=249,group="Boxy")
 
transparency=input.int(82,
     "Przezroczystość",
     minval=0,maxval=95,group="Boxy")
 
var array<box> slHistory=array.new<box>()
var array<box> tpHistory=array.new<box>()
 
var box liveSL=na
var box liveTP=na
var int liveEntryTime=na
var int seenClosed=0
 
int barMs=int(timeframe.in_seconds()*1000)
 
f_riskPoints(string entryComment)=>
    str.tonumber(str.replace(entryComment,"riskUSD=",""))/syminfo.pointvalue
 
f_boxes(int leftTime,int rightTime,float entryPrice,float direction,float riskPoints)=>
    float stopPrice=entryPrice-direction*riskPoints
    float targetPrice=entryPrice+direction*2*riskPoints
 
    box sb=box.new(
         left=leftTime,
         top=math.max(entryPrice,stopPrice),
         right=rightTime,
         bottom=math.min(entryPrice,stopPrice),
         xloc=xloc.bar_time,
         border_color=color.red,
         bgcolor=color.new(color.red,transparency),
         text="SL",
         text_color=color.red,
         text_size=size.tiny)
 
    box tb=box.new(
         left=leftTime,
         top=math.max(entryPrice,targetPrice),
         right=rightTime,
         bottom=math.min(entryPrice,targetPrice),
         xloc=xloc.bar_time,
         border_color=color.lime,
         bgcolor=color.new(color.lime,transparency),
         text="TP 2R",
         text_color=color.lime,
         text_size=size.tiny)
 
    [sb,tb]
 
// Także transakcje otwarte i zamknięte na tej samej świecy.
if strategy.closedtrades>seenClosed
    box.delete(liveSL)
    box.delete(liveTP)
 
    liveSL:=na
    liveTP:=na
    liveEntryTime:=na
 
    if showBoxes
        for i=seenClosed to strategy.closedtrades-1
            float points=f_riskPoints(
                 strategy.closedtrades.entry_comment(i))
 
            if not na(points) and points>0
                int lt=strategy.closedtrades.entry_time(i)
                int rt=math.max(
                     strategy.closedtrades.exit_time(i),
                     lt+barMs)
 
                float dir=strategy.closedtrades.size(i)>0?1.0:-1.0
 
                [sb,tb]=f_boxes(
                     lt,rt,
                     strategy.closedtrades.entry_price(i),
                     dir,points)
 
                array.push(slHistory,sb)
                array.push(tpHistory,tb)
 
                if array.size(slHistory)>keepTrades
                    box.delete(array.shift(slHistory))
                    box.delete(array.shift(tpHistory))
 
    seenClosed:=strategy.closedtrades
 
// Aktualizacja boxów otwartej pozycji.
if showBoxes and strategy.opentrades>0
    int et=strategy.opentrades.entry_time(0)
 
    float points=f_riskPoints(
         strategy.opentrades.entry_comment(0))
 
    if not na(points) and points>0
        int rt=math.max(time_close,et+barMs)
 
        if na(liveEntryTime) or liveEntryTime!=et
            box.delete(liveSL)
            box.delete(liveTP)
 
            float dir=strategy.opentrades.size(0)>0?1.0:-1.0
 
            [sb,tb]=f_boxes(
                 et,rt,
                 strategy.opentrades.entry_price(0),
                 dir,points)
 
            liveSL:=sb
            liveTP:=tb
            liveEntryTime:=et
        else
            box.set_right(liveSL,rt)
            box.set_right(liveTP,rt)

← 返回列表