Community scripts › RAM FAILED BREAK v0.1.1

RAM FAILED BREAK v0.1.1

by RAMDEE · v1 · updated 18 h ago Order flowOpen source
RAM FAILED BREAK v0.1.1 by RAMDEE on the live order book chart

What it does

Identifies potential failed breakouts using real-time order flow. Displays LONG READY or SHORT READY when aggressive trading outside a previous 15-minute range is followed by a confirmed price recovery. Includes entry, stop and midpoint reference levels.

How it works

Combines native footprint volume with live trade sequencing to measure aggressive buying or selling beyond the previous 15-minute range. Outside pressure is evaluated relative to recent market activity, followed by confirmation through an opposite-side recovery in the trade flow.

How to use it

Designed for the 1-minute footprint chart across supported markets, operating 24/7. Allow the indicator to complete its initial data verification. Use READY markers as discretionary execution references alongside your own market analysis and risk management.

What's original

Rather than relying solely on candle wicks or delta divergence, RAM FAILED BREAK combines location-specific aggressive volume with the sequence of executed trades to identify a confirmed recovery. Released freely as an introduction to RAMDEE's ongoing order flow research and indicator development for the SwiftEdge community.

Limitations

Experimental tool for live order flow observation on the 1-minute timeframe. Requires continuous trade data and sufficient footprint history. Does not reconstruct historical signals or execute orders. Use independent judgment and risk management.

Source code

Open source under MPL-2.0. Copy it with credit: "Copy to my scripts" adds the credit for you.

// RAM FAILED BREAK v0.1.1 DEV — standalone native 1m tape/footprint research tool.
// No historical signal reconstruction, no automated orders, no STRIKE dependencies.
// READY is an observed recovery print, NOT an executable entry/fill or a prediction.
script({name:'RAM FAILED BREAK v0.1.1 DEV — Native 1m',bars:1500});

// Keep all private bindings inside a local scope to avoid SwiftEdge API name collisions.
(function ramFailedBreakRun(){
const cfg={pressure:input.number(10,'Outside net pressure | % of median 1m USD',{min:1,max:100,step:1}),
 life:input.int(60,'Attempt | max seconds',{min:10,max:120}),
 ready:input.int(15,'READY display | max seconds',{min:3,max:60}),
 levels:input.bool(true,'Show frozen 15m range'), midpoint:input.bool(true,'Show midpoint'),
 guides:input.bool(true,'Show active recovery / stop guide'), history:input.bool(false,'Show prior observed recovery markers'),
 diag:input.bool(true,'Show status panel'), reset:input.int(1,'Run ID | change to reset',{min:1,max:9999}),
 long:input.color('#9ED8FF','Long color'),short:input.color('#D3D5DB','Short color')};
const M=60,BLOCK=900,BASE=30,MAX_BATCH=12000,MAX_MARKS=40,TOL=.01,MAX_GAP=60;
const finite=x=>typeof x==='number'&&Number.isFinite(x), pos=x=>finite(x)&&x>0;
const ramFBMinuteBucket=t=>Math.floor(t/M)*M, block=t=>Math.floor(t/BLOCK)*BLOCK;
function tradeTime(x){return !pos(x)?null:x>1e12?x/1000:x;}
function validBar(b){return b&&finite(b.time)&&[b.open,b.high,b.low,b.close].every(pos)&&b.high>=Math.max(b.open,b.close,b.low)&&b.low<=Math.min(b.open,b.close,b.high);}
function newState(sig){return {sig,edge:null,last:null,prev:null,range:null,L:null,S:null,active:null,marks:[],unit:null,votes:[],agg:null,queue:[],baseline:null,baseAt:null,matched:0,mismatch:0,confirmed:0,discarded:0,lastReason:'Startup: waiting for fresh tape',first:true};}
function indexAt(b,t){let l=0,r=b.length-1;while(l<=r){const m=(l+r)>>1;if(b[m].time===t)return m;if(b[m].time<t)l=m+1;else r=m-1;}return -1;}
function nativeSides(b,fp,t,edge){if(t>=edge||!fp||fp.ready===false||Number(fp.rows)!==1)return null;
 const i=indexAt(b,t);if(i<0||!validBar(b[i])||!b[i+1]||b[i+1].time!==t+M)return null;
 try{if(typeof fp.has==='function'&&!fp.has(i))return null;
  let buy=fp.buy&&fp.buy[i],sell=fp.sell&&fp.sell[i];
  if(!finite(buy)||!finite(sell)||buy<0||sell<0||buy+sell<=0){
   let rows=fp.bar(i);if(rows&&!Array.isArray(rows))rows=rows.rows;if(!Array.isArray(rows)||!rows.length||rows.length>20000)return null;
   buy=0;sell=0;for(const r of rows){if(!r||!finite(r.buy)||!finite(r.sell)||r.buy<0||r.sell<0)return null;buy+=r.buy;sell+=r.sell;}
  }
  return buy+sell>0&&finite(buy+sell)?[buy,sell]:null;
 }catch(e){return null;}}
function getBaseline(b,fp,t,edge){const a=[];for(let j=BASE;j>=1;j--){const v=nativeSides(b,fp,t-j*M,edge);if(!v)return null;a.push(v[0]+v[1]);}a.sort((x,y)=>x-y);return (a[14]+a[15])/2;}
function getRange(b,k){let lo=Infinity,hi=-Infinity;for(let j=15;j>=1;j--){const i=indexAt(b,k-j*M),x=b[i];if(i<0||!validBar(x))return null;lo=Math.min(lo,x.low);hi=Math.max(hi,x.high);}return hi>lo?{k,lo,hi,mid:(lo+hi)/2}:null;}
function edgeState(spent){return {spent:!!spent,a:null};}
function setRange(s,b,k,bootstrap,edge){const r=getRange(b,k);s.range=r;s.prev=null;s.active=null;s.L=edgeState(!r);s.S=edgeState(!r);
 if(!r)return;
 // On startup/reconnect, any edge already breached in the visible current block is spent.
 // On normal rollover, no developing OHLC is used to infer prior events.
 if(bootstrap){for(let t=k;t<=edge;t+=M){const x=b[indexAt(b,t)];if(!validBar(x)){s.L.spent=true;s.S.spent=true;break;}if(x.low<r.lo)s.L.spent=true;if(x.high>r.hi)s.S.spent=true;}}
}
function resetTape(s,why){s.prev=null;s.active=null;if(s.L){s.L.a=null;s.L.spent=true;}if(s.S){s.S.a=null;s.S.spent=true;}s.unit=null;s.votes=[];s.agg=null;s.queue=[];s.baseline=null;s.baseAt=null;s.last=null;s.lastReason=why;}
function pushAggregate(s,t,side,size,price){const m=ramFBMinuteBucket(t);if(!s.agg||s.agg.m!==m){const old=s.agg;if(old&&old.complete&&m===old.m+M)s.queue.push(old);if(s.queue.length>8)s.queue.shift();s.agg={m,complete:!!old&&m===old.m+M,raw:[0,0],usd:[0,0],n:0};}
 const a=s.agg,j=side==='buy'?0:1;a.raw[j]+=size;a.usd[j]+=size*price;a.n++;}
function closeEnough(a,b){return a.every((v,i)=>Math.abs(v-b[i])<=Math.max(1e-6,TOL*Math.max(Math.abs(v),Math.abs(b[i]))));}
function calibrate(s,b,fp,edge){const keep=[];for(const a of s.queue){
 // Wait one full extra minute for footprint finalization; don't score partial capture.
 if(a.m>edge-2*M){keep.push(a);continue;}const f=nativeSides(b,fp,a.m,edge);
 if(!f){if(a.m>=edge-5*M)keep.push(a);continue;}
 const raw=closeEnough(a.raw,f),usd=closeEnough(a.usd,f),vote=raw&&!usd?'USD':usd&&!raw?'BASE':null;
 if(!vote){s.mismatch++;s.unit=null;s.votes=[];s.active=null;if(s.L)s.L.a=null;if(s.S)s.S.a=null;s.lastReason=raw&&usd?'Units ambiguous':'Tape / footprint mismatch';continue;}
 s.matched++;if(s.unit&&s.unit!==vote){s.unit=null;s.active=null;if(s.L)s.L.a=null;if(s.S)s.S.a=null;}
 const prev=s.votes[s.votes.length-1];if(prev&&(prev.m+M!==a.m||prev.vote!==vote))s.votes=[];
 s.votes.push({m:a.m,vote});if(s.votes.length>3)s.votes.shift();if(s.votes.length===3)s.unit=vote;
 }s.queue=keep;}
function cancel(s,e,why){e.a=null;s.discarded++;s.lastReason=why;}
function inRange(r,p){return p>=r.lo&&p<=r.hi;}
function attempt(s,e,dir,t,p,side,usd,threshold,canMark,step){const r=s.range,level=dir===1?r.lo:r.hi;
 const outside=dir===1?p<level:p>level,inside=dir===1?p>level:p<level;
 let a=e.a,justRecovered=false;if(a&&t-a.start>=cfg.life){cancel(s,e,'Attempt expired');a=null;}
 if(!a&&!e.spent&&outside&&(s.prev===null||!inRange(r,s.prev))){e.spent=true;s.lastReason='Edge already outside before fresh inside-to-outside break';return;}
 if(!a&&!e.spent&&outside&&s.prev!==null&&inRange(r,s.prev)){
  e.spent=true; // first fresh break only; even unqualified attempts consume this edge
  if(!pos(threshold)||!pos(usd)){s.lastReason='First break missed: baseline / tape units unavailable';return;}
  a=e.a={start:t,extreme:p,net:0,threshold,phase:'OUT',recovered:null};s.lastReason=dir===1?'Watching outside selling':'Watching outside buying';
 }
 if(!a)return;
 if(a.phase==='OUT'){
  if(outside){a.extreme=dir===1?Math.min(a.extreme,p):Math.max(a.extreme,p);a.net+=(side===(dir===1?'sell':'buy')?1:-1)*usd;return;}
  if(p===level)return; // equality is neither strict outside nor strict recovery
  if(inside){if(!inRange(r,p)){cancel(s,e,'Recovery crossed entire range');return;}
   if(t<=a.start){cancel(s,e,'Break/recovery ordering unresolved at same timestamp');return;}
   if(a.net<a.threshold){cancel(s,e,'Outside net pressure below threshold');return;}
   a.phase='REC';a.recovered=t;justRecovered=true;s.lastReason=dir===1?'Recovered: waiting for seller above low':'Recovered: waiting for buyer below high';
   // A first inside print with the required side is itself a valid confirming print.
  }
 }else if(a.phase==='REC'&&(outside||!inRange(r,p))){cancel(s,e,'Renewed breach or opposite edge before confirmation');return;}
 if(a.phase==='REC'&&inside&&side===(dir===1?'sell':'buy')){
  if(!justRecovered&&t<=a.recovered){cancel(s,e,'Recovery confirmation ordering unresolved');return;}
  if(!inRange(r,p)){cancel(s,e,'Confirmation outside opposite range edge');return;}
  const stop=dir===1?a.extreme-step:a.extreme+step;
  const reward=dir*(r.mid-p),risk=dir*(p-stop);
  e.a=null;
  if(!canMark||!pos(step)||!pos(risk)||reward<=0){s.discarded++;s.lastReason='Recovery observed; missed / midpoint passed / stop unavailable';return;}
  const z={t,dir,p,extreme:a.extreme,stop,mid:r.mid,level,net:a.net,threshold:a.threshold,rr:reward/risk,k:r.k};
  s.marks.push(z);if(s.marks.length>MAX_MARKS)s.marks.shift();s.active=z;s.confirmed++;s.lastReason=dir===1?'LONG READY — recovery print':'SHORT READY — recovery print';
 }
}
function process(s,b,fp,edge,now,batch){
 // Reject malformed/out-of-order batches before publishing any marker. No sorting or inferred prints.
 let last=s.last;const clean=[];for(const x of batch){const t=tradeTime(x&&x.time),p=x&&x.price,size=x&&x.size,side=x&&x.side;
  if(t===null||!pos(p)||!pos(size)||!['buy','sell'].includes(side)||t>now+5||(last!==null&&t<last)){resetTape(s,'Tape invalid / out of order; rechecking units');s.first=true;return;}
  last=t;clean.push({t,p,size,side});}
 if(s.first){s.first=false;s.last=null;setRange(s,b,block(edge),true,edge);s.lastReason='Startup: first batch discarded; checking units';return;}
 for(const x of clean){const {t,p,size,side}=x;if(ramFBMinuteBucket(t)<edge-M||ramFBMinuteBucket(t)>edge){resetTape(s,'Tape outside chart clock; waiting for synchronization');s.first=true;return;}
  if(s.last!==null&&t-s.last>MAX_GAP){resetTape(s,'Tape gap >60s; rechecking units');setRange(s,b,block(t),true,edge);}
  const k=block(t);if(!s.range||s.range.k!==k)setRange(s,b,k,false,edge);
  pushAggregate(s,t,side,size,p);
  if(s.range){const m=ramFBMinuteBucket(t);if(s.baseAt!==m||(!pos(s.baseline)&&s.baseAt===m&&s.prev===null)){s.baseline=getBaseline(b,fp,m,edge);s.baseAt=m;}
   const usd=s.unit==='USD'?size:s.unit==='BASE'?size*p:null;
   const threshold=pos(s.baseline)?s.baseline*cfg.pressure/100:null;
   if(s.unit&&pos(usd)){const step=Number(fp&&fp.step),fresh=m===edge&&now-t<=cfg.ready;
    attempt(s,s.L,1,t,p,side,usd,threshold,fresh,step);attempt(s,s.S,-1,t,p,side,usd,threshold,fresh,step);
   }else{if(p<s.range.lo)s.L.spent=true;if(p>s.range.hi)s.S.spent=true;s.L.a=null;s.S.a=null;}
   s.prev=p;
  }s.last=t;
  if(s.active&&(s.active.k!==k||t-s.active.t>=cfg.ready||s.active.dir*(p-s.active.level)<=0||s.active.dir*(s.active.mid-p)<=0))s.active=null;
 }
 // Check prior completed capture AFTER processing so a new unit vote cannot retroactively validate trades.
 calibrate(s,b,fp,edge);
 if(s.active&&(now-s.active.t>=cfg.ready||!s.unit))s.active=null;
 for(const e of [s.L,s.S])if(e&&e.a&&now-e.a.start>=cfg.life)cancel(s,e,'Attempt expired');
}
function price(x){return finite(x)?String(Number(x.toPrecision(9))):'—';}
function money(x){return finite(x)?'$'+Math.round(x).toLocaleString('en-US'):'—';}
function mark(z,active){const c=z.dir===1?cfg.long:cfg.short,txt=z.dir===1?'LONG READY':'SHORT READY';
 if(typeof canvas!=='undefined'&&typeof canvas.circle==='function')canvas.circle(z.t,z.p,4,{fill:c,stroke:c,unit:'px'});
 if(typeof canvas!=='undefined'&&typeof canvas.text==='function')canvas.text(z.t,z.p,active?txt:(z.dir===1?'L':'S')+' recovery',{color:c,size:12,background:'#20252F'});
 else draw.text(z.t,z.p,active?txt:(z.dir===1?'L':'S')+' recovery');
 if(active&&cfg.guides){const end=z.t+cfg.ready;draw.line(z.t,z.p,end,z.p,{color:c,width:2,label:'Recovery print'});draw.line(z.t,z.stop,end,z.stop,{color:'#A2A9B5',style:'dashed',label:'Stop ref'});}
}
function render(s,edge,now){const r=s.range&&s.range.k===block(edge)?s.range:null;
 if(r&&cfg.levels){draw.line(r.k,r.hi,r.k+BLOCK,r.hi,{color:'#9299A5',style:'dashed',label:'Prior 15m H'});draw.line(r.k,r.lo,r.k+BLOCK,r.lo,{color:'#9299A5',style:'dashed',label:'Prior 15m L'});if(cfg.midpoint)draw.line(r.k,r.mid,r.k+BLOCK,r.mid,{color:'#606D80',style:'dotted',label:'Midpoint'});}
 const z=s.active&&r&&now-s.active.t<cfg.ready?s.active:null;
 if(cfg.history)for(const h of s.marks)if(h!==z&&(!z||h.t!==z.t||h.dir!==z.dir))mark(h,false);
 if(z)mark(z,true);
 if(cfg.diag){const a=s.L&&s.L.a||s.S&&s.S.a,status=z?(z.dir===1?'LONG READY':'SHORT READY'):!s.unit?'CHECKING TAPE UNITS':!r?'WAITING 15m RANGE':!pos(s.baseline)?'WAITING 30m FOOTPRINT':a?(a.phase==='OUT'?'OUTSIDE PRESSURE':'WAITING RECOVERY PRINT'):'WATCHING';
 plot.table([['Status',status],['Tape unit / matches', (s.unit||'Unverified')+' / '+s.votes.length+'/3'],['Reconciled / mismatch',s.matched+' / '+s.mismatch],['Median 1m / threshold',money(s.baseline)+' / '+money(pos(s.baseline)?s.baseline*cfg.pressure/100:null)],['15m low / high',r?price(r.lo)+' / '+price(r.hi):'—'],['First break L / S',(s.L&&s.L.spent?'Used':'Open')+' / '+(s.S&&s.S.spent?'Used':'Open')],['Outside net / required',a?money(a.net)+' / '+money(a.threshold):'—'],['Recovery / stop / midpoint',z?[z.p,z.stop,z.mid].map(price).join(' / '):'—'],['Gross R to midpoint',z?z.rr.toFixed(2)+'R (print proxy)':'—'],['Observed / discarded',s.confirmed+' / '+s.discarded],['Last event',s.lastReason]],{position:'top-right',title:'RAM FAILED BREAK | 1m DEV'});}}
function status(t){plot.table([['Status',t]],{position:'top-right',title:'RAM FAILED BREAK | 1m DEV'});}
function main(){const b=typeof bars!=='undefined'?bars:null;if(!Array.isArray(b)||b.length<31){status('WAITING CHART');return;}
 const tf=String(market.timeframe).toLowerCase().trim();if(!['1m','1','60','60s','1min'].includes(tf)||!validBar(b[b.length-1])||b[b.length-1].time-b[b.length-2].time!==M){status('Use native 1-minute chart');return;}
 if(typeof state!=='object'||!state||typeof onTrade!=='function'){status('Native state / tape API unavailable');return;}
 const edge=b[b.length-1].time,now=typeof Date!=='undefined'&&typeof Date.now==='function'?Date.now()/1000:null;
 if(!pos(now)||edge%M!==0||now<edge-5||now-edge>120){state.ramFailedBreak01=null;status('LIVE ONLY — chart clock not synchronized');return;}
 const sig=JSON.stringify([market.symbol,market.id||'',tf,cfg.reset,cfg.pressure,cfg.life,cfg.ready]);let s=state.ramFailedBreak01;
 if(!s||s.sig!==sig||s.edge!==null&&(edge<s.edge||edge-s.edge>M))s=newState(sig);
 const fp=request.footprint({rows:1});const batch=[];let overflow=false;
 onTrade(t=>{if(batch.length<MAX_BATCH)batch.push(t);else overflow=true;});
 if(overflow){resetTape(s,'Tape batch cap reached; rechecking units');s.first=true;}else process(s,b,fp,edge,now,batch);
 s.edge=edge;state.ramFailedBreak01=s;render(s,edge,now);
}
main();

})();

Comments · 0