// Add pattern to completed pattern structures
addValidPattern(t,h1,h2,h3,h4,h5,h6,xX,xY,aX,aY,bX,bY,cX,cY,dX,dY) =>
string[] s = na
bool lasteH = na
int lasteX = na
float lasteY = na
float lastScore = na
tp = tp(h1,h2,h3,h4,h5,h6)
m = typeToMatrix(t,tp)
if matrix.rows(m) > 0
// check last pattern of same type
last = matrix.row(m,matrix.rows(m)-1)
[lt,lh1,lh2,lh3,lh4,lh5,lh6,lxX,lxY,laX,laY,lbX,lbY,lcX,lcY,ldX,ldY,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,lt1H,lt2H,leH,leX,leY,lscore]
= unstringify(last)
lastScore := lscore
// if A, B or C is different = new pattern
if aX!=laX or bX!=lbX or cX!=lcX
s := stringify(t,h1,h2,h3,h4,h5,h6,xX,xY,aX,aY,bX,bY,cX,cY,dX,dY)
addCompleted(t,h1,h2,h3,h4,h5,h6,s)
// if ABC are same but D is beyond last pattern's D, replace it with this one. We want to draw the
// new/updated pattern and calculate its updated score, but maintain any entry/targets that have
// already been hit.
else if (t and dY < ldY) or (t==false and dY > ldY)
if leH and na(lt1H)
lasteH := true // inherit entry from last pattern (trade is active)
lasteX := leX
lasteY := leY
s := stringify(t,h1,h2,h3,h4,h5,h6,xX,xY,aX,aY,bX,bY,cX,cY,dX,dY,lt1H,lt2H,leH,leX,leY)
else
s := stringify(t,h1,h2,h3,h4,h5,h6,xX,xY,aX,aY,bX,bY,cX,cY,dX,dY)
//
if lastScore > getScore(s) // if lower score, keep last pattern
s := na
else if not na(lt1H) // if last pattern trade closed, just add the new pattern
addCompleted(t,h1,h2,h3,h4,h5,h6,s)
else // else, remove the last pattern in favor of the new one
lpid = pid(tp,lxX,laX,lbX,lcX,ldX)
removeCompleted(lpid,t,tp)
addCompleted(t,h1,h2,h3,h4,h5,h6,s)
else
s := stringify(t,h1,h2,h3,h4,h5,h6,xX,xY,aX,aY,bX,bY,cX,cY,dX,dY)
addCompleted(t,h1,h2,h3,h4,h5,h6,s)
// Update newly added pattern
if not na(s)
// update entry, if necessary
if lasteH
draw.eHitLbl(lasteX,lasteY,dX,dY,t,true)
else
score = getScore(s)
[eHit,eX,eY] = entryHit(t,tp,xX,cX,xY,aY,bY,cY,dX,dY,score)
if eHit
draw.eHitLbl(eX,eY,dX,dY,t)
pid = pid(tp,xX,aX,bX,cX,dX)
s := setEntry(s,eHit,eX,eY)
l = updateCompleted(pid,t,tp,s)