I think the PRESET flag is afffecting my drawShape commands. When a setup is detected I'm want to put a square on BottomRow1 at current bar and an arrow BelowBar2 on the previous bar. I want to color the setp bar purple but the crossover can't be confirmed until NEWBAR by checking the previous 2 bars. Cannot setPriceBarColor for a previous bar so monitor and set color tic-by-tic. (3-bAR Trigger to be added later)
PHP Code:
/* 8 open / close crossover
Show the direction of the trend on the bottom row:
green if 8 close > 8 open and red if open > close
Trigger bar is the cross over. Show the direction
of the cross using a Red arrow above the price bar or
green arrow below the bar & color the price bar purple.
if not triggered price bar will be aqua (use a 3-bar trigger)
*/
function preMain() {
setPriceStudy(true);
setStudyTitle("Test drawShape");
setCursorLabelName("8-Open", 0);
setDefaultBarFgColor(Color.olive, 0);
setCursorLabelName("8-Close", 1);
setDefaultBarFgColor(Color.magenta, 1);
}
var xCls = null;
var xOpn = null;
var bInit = false;
var bSetup = false;
var n8Opn, n8Cls, n8Cls_1, n8Cls_2, n8Opn_1, n8Opn_2;
function main(nLen) {
if (bInit == false) {
xCls = sma(8, close() );
xOpn = sma(8, open() );
bInit = true;
}
if (getBarState() == BARSTATE_NEWBAR) {
n8Cls_1 = xCls.getValue(-1);
n8Cls_2 = xCls.getValue(-2);
n8Opn_1 = xOpn.getValue(-1);
n8Opn_2 = xOpn.getValue(-2);
if (n8Cls_1 == null || n8Cls_2 == null ||
n8Opn_1 == null || n8Opn_2 == null) {return;}
if ( bSetup == true ) {
bSetup = false;
} else {
if (n8Cls_2 <= n8Opn_2 && n8Cls_1 > n8Opn_1) {
drawShapeRelative( -1, BelowBar2, Shape.UPARROW, null,
Color.green, Shape.ONTOP | Shape.PRESET );
// will not draw the green arrow
setPriceBarColor(Color.aqua);
drawShapeRelative( 0, BottomRow1, Shape.SQUARE, null,
Color.green, Shape.ONTOP | Shape.PRESET );
// but will draw the green sqaure below price bar
bSetup = true;
}
if (n8Cls_2 >= n8Opn_2 && n8Cls_1 < n8Opn_1) {
drawShapeRelative( -1, AboveBar2, Shape.DOWNARROW, null,
Color.red, Shape.ONTOP | Shape.PRESET );
// if 2nd draw turned off will draw the arrow
// above the purple bar (like its supposed to)
setPriceBarColor(Color.aqua);
// if this draw is turned on - no arrow & square moves
//drawShapeRelative( 0, BottomRow1, Shape.SQUARE, null,
// Color.red, Shape.ONTOP | Shape.PRESET );
bSetup = true;
}
}
}
n8Cls = xCls.getValue(0);
n8Opn = xOpn.getValue(0);
if (bSetup == true) {
// do stuff
} else { // check tic-by-tic to see if the crossover occured on this bar
if (n8Cls_1 < n8Opn_1) { // prev bar was bearish
if (n8Cls > n8Opn) { // bullish crossover
setPriceBarColor(Color.purple);
} else { // trend is still bearish
drawShapeRelative( 0, BottomRow1, Shape.SQUARE, null,
Color.red, Shape.ONTOP | Shape.PRESET );
setPriceBarColor(Color.red);
}
} else if (n8Cls_1 > n8Opn_1) { // prev bar was bullish
if ( n8Cls < n8Opn) { // bearish crossover
setPriceBarColor(Color.purple);
} else { // trend is still bullish
drawShapeRelative( 0, BottomRow1, Shape.SQUARE, null,
Color.green, Shape.ONTOP | Shape.PRESET );
setPriceBarColor(Color.green);
}
}
}
return new Array(n8Opn, n8Cls);
}
Comment