Announcement

Collapse
No announcement yet.

drawShapeRelative

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • drawShapeRelative

    Help. I can't find the problem. Trying to put a shape on the bottom of the chart that shows what bar the signal occured. drawShapeRelative will not plot my diamond. It works for Absolute (which of course doesn't do what I want) but not Relative.

    Code:
    /*********************************************************
    Plot Profit Targets and Stop Loss
    **********************************************************/
    debugClear();
    var aFPArray = new Array();
    var SL, PT1, PT2;
    var sDir = "long";
    var AdjSL = 0;
    var PT1TS, PT2TS;	// exit triggers for PT 1&2
    var nSig = 0.00;	// the signal price
    var nE = 0;		// actual entry price
    var nX = 0;		// calculated eXit after PT reached
    var nTS = 0;	// trailing stop for runner
    var o1, h1, l1, c1, nPrc;
    var sym, desc;
    var bPT2Flag = false;
    var bPT2TS = false;
    var bPT1Flag = false;
    var bPT1TS = false;
    var bAlerts = false;
    var hh = 0;		// highest high
    var LL = 9999;	// lowest low
    var nB = 0;
    function preMain() {
        setShowTitleParameters( false );
        setPriceStudy(true);
        setStudyTitle("Targets2");
    	var x=0;
    	aFPArray[x] = new FunctionParameter( "fpSig", FunctionParameter.NUMBER);
    	with( aFPArray[x] ) {
    		setName("Signal Price");
    		setLowerLimit( 0 );
    		setDefault( 0 );
    	}
    	x++;
    	aFPArray[x] = new FunctionParameter( "fpE", FunctionParameter.NUMBER);
    	with( aFPArray[x] ) {
    		setName("Actual Entry");
    		setLowerLimit( 0 );
    		setDefault( 0 );
    	}
    	x++;
    	aFPArray[x] = new FunctionParameter( "fpSL", FunctionParameter.NUMBER);
    	with( aFPArray[x] ) {
    		setName("Stop Loss");
    		setLowerLimit( 0 );
    		//setDefault( 0 );
    	}
    	x++;
    	aFPArray[x] = new FunctionParameter( "fpPT1", FunctionParameter.NUMBER);
    	with( aFPArray[x] ) {
    		setName("Profit Target 1");
    		setLowerLimit( 0 );
    		//setDefault( 0 );	
    	}
    	x++;
    	aFPArray[x] = new FunctionParameter( "fpPT2", FunctionParameter.NUMBER);
    	with( aFPArray[x] ) {
    		setName("Profit Target 2");
    		setLowerLimit( 0 );
    		//setDefault( 0 );
    	}
    	x++;
    	aFPArray[x] = new FunctionParameter( "fpAdj", FunctionParameter.NUMBER);
    	with( aFPArray[x] ) {
    		setName("Adjusted Stop");
    		setLowerLimit( 0 );
    		setDefault( 0 );
    	}
    	x++;
    	aFPArray[x] = new FunctionParameter( "fpBO", FunctionParameter.NUMBER);
    	with( aFPArray[x] ) {
    		setName("Entry bar offset");
    		setLowerLimit( 0 );
    		setDefault( 0 );
    	}
            askForInput( "Targets" );
    }
    var bInit = false;
    var xbar
    function main(fpSig, fpE, fpSL, fpPT1, fpPT2, fpAdj, fpBO ) {
    	xbar = getCurrentBarIndex();
    	sFGup = Color.lime;
    	sBGup = Color.darkgreen;
    	sFGdn = Color.red;
    	sBGdn = Color.lightyellow;
            nB = fpBO;
    
    	if ( getBarState() == BARSTATE_ALLBARS ) {
    		var ts = getSymbol();
    		askForInput( ts );
    		if( fpSL == null || fpPT1 == null || fpPT2 == null) {return;}
    		bAlerts = true;
    		if (fpSig == 0) { 
    			nSig = (fpPT1+fpSL)/2;
    		} else {
    			nSig = fpSig;
    		}
    		if (fpE == 0) {
    			nE = nSig;
    		} else {
    			nE = fpE;
    		}
    		drawShapeAbsolute( 2, nE, Shape.LEFTARROW, null, Color.black, Shape.ONTOP );
    		drawLineAbsolute( 5, nE, 2, nE, PS_SOLID, 2, Color.black, "E" );
    //drawShapeRelative(0-nB, BottomRow2, Shape.DIAMOND, null, Color.black, Shape.ONTOP | Shape.PRESET, 101);
    //drawShapeAbsolute(0-nB, BottomRow2, Shape.DIAMOND, null, Color.black, Shape.ONTOP | Shape.PRESET, 101);
    
    		nX = 0;
    		nPrc = 0;
    		SL = fpSL;
    		PT1 = fpPT1;
    		PT2 = fpPT2;
    		AdjSL = fpAdj;
    		sym = getSymbol();
    		addLineTool(LineTool.HORZ, nSig, 1, Color.RGB(240, 240, 100), "Sig");
    		addLineTool(LineTool.HORZ, fpSL, 1, Color.red, "SL");
    		addLineTool(LineTool.HORZ, fpPT1, 1, Color.aqua, "PT1");
    		addLineTool(LineTool.HORZ, fpPT2, 1, Color.blue, "PT2");
                    sFlag = ""
                    drawTextAbsolute( 5, SL, SL, Color.red, Color.lightgrey, Text.ONTOP | Text.TOP | Text.RIGHT, "Calibri", 10, 111 );
                    drawTextAbsolute( 5, PT1, PT1, Color.black, Color.aqua, Text.ONTOP | Text.BOTTOM | Text.RIGHT, "Calibri", 10, 222 );
                    drawTextAbsolute( 5, PT2, PT2, Color.lightyellow, Color.blue, Text.ONTOP | Text.BOTTOM | Text.RIGHT, "Calibri", 10, 333 );
    
    		if (SL > PT1) {
    			sDir = "short";
    		} else {
    			sDir = "long";
    		}
    	}
    
    // this doesn't work either
            if (bInit == false) {
                drawShapeRelative(0-nB, BottomRow2, Shape.DIAMOND, null, Color.black, Shape.ONTOP | Shape.PRESET, 101);
                bInit = true;
            }
    
    	if (getBarState() == BARSTATE_NEWBAR) {
    
    		if( fpSL == 0 && fpPT1 == 0 && fpPT2 == 0) {return;}
    		// nX is used as a Trailing Stop once a target value is reached.
    		switch (sDir) {
    			case "long":
    				if (close(-1) < nE ) {
    					// pink background means trade is losing 
    					setBarBgColor( Color.RGB(255, 245, 245), 0 );
    				} else {
    					setBarBgColor( Color.white, 0 );
    				}
    				if (nX < low(-1)) {nX = low(-1);}		//  set the exit trigger
    				break;
    			case "short":
    				if (close(-1) > nE ) {
    					// pink background means trade is losing 
    					setBarBgColor( Color.RGB(255, 245, 245), 0 );
    				} else {
    					setBarBgColor( Color.white, 0 );
    				}
    				if (nX > high(-1)) {nX = high(-1);}		//  set the exit trigger
    				break;
    			default:
    				setBarBgColor( Color.white, 0 );
    				break;
    		}
    
    		if ( AdjSL !== 0 ) {
    			addLineTool(LineTool.HORZ, AdjSL, 1, Color.fushcia, "AdjSL");
    		}
    	}
    
    	if (getBarState() == BARSTATE_CURRENTBAR) {
    		if (bAlerts == true) {
    			nPrc = close(0);
    			if (sDir == "short") {
    				fCheckShortTargets();
    			} else {
    				fCheckLongTargets();
    			}
    		}
    	}
        return;
    }
    
    function fAlert(fg, bg) {
    debugPrintln("adding alert");
    	Alert.addToList( sym, desc, fg, bg );
    	return;
    }
    
    function fCheckLongTargets() {		// 3 tiered exit + stop loss
    	if (bPT1Flag == true) {	// price reached PT1
    		if (bPT1TS == true) {
    			if (nPrc <= PT1TS) {
    				// PT1 exit has been triggerred
    				desc = "PT1 sell first 1/3 at: "+nPrc;
    				fAlert(sFGup, sBGup);
    				Alert.playSound( "cowbell.wav" );
    				AdjSL = nE; 			// move Stop to entry
    				bPT1TS = false;
    			} else { 	// keep raising exit price until  price turns down
    				if (PT1TS < nX) {PT1TS = nX;}
    			}
    		} else {	//  PT1Flag is T & TS is F means PT1 exit triggered
    			if (bPT2Flag == true) {
    				if (bPT2TS == true) {
    					if (nPrc <= PT2TS) {	// exit triggered
    						// PT2 exit triggered
    						desc = "PT2 sell second 1/3 at: "+nPrc;
    						fAlert(sFGup, sBGup);
    						Alert.playSound( "cowbell.wav" );
    						bPT2TS = false;
    						nTS = 0;	// make sure we set TS for the runner
    						PT2TS = 0;
    						// setup the runner
    						hh = Math.max(hh, nPrc);
    						nTS = .75 * (hh - nE) + nE;	
    					} else {	// keep raising exit price until  price turns down
    						if (PT1TS < nX) {PT1TS = nX;}
    					}
    				} else {
    					// at this point PT2 was triggered and we have a runner
    					if (nPrc <= nTS) {
    						// exit last third
    						desc = "Exit Runner at: "+nPrc;
    						fAlert(sFGup, sBGup);
    						
    						//  which vars need to be reset???
    						bAlerts = false;
    						
    					} else {
    						// update the Trailing Stop for the runner
    						hh = Math.max(hh, nPrc);
    						nTS = .75 * (hh - PT2) + PT2;
    						// the TS should never give up more than 25% of the gain from PT2 (or 1or entry)
    					}
    				}
    			} else {  // don't have a PT2 flag yet
    				if (nPrc >= PT2) {	// hooray finally got to PT2
    					bPT2Flag = true;
    					bPT2TS = true;
    					PT2TS = nX;
    					AdjSL = PT1;		// move stop to PT1
    				} else if ( nPrc < AdjSL) {
    					desc = "Adj SL triggered";
    					fAlert(sFGdn, sBGdn);
    					Alert.playSound( "cowbell.wav" );
    					bAlerts = false;		
    					//  turn off alerts & exit all
    					// cleanup vars
    				}
    			}
    		}
    	} else {	// haven't set the flag for first PT 
    		if (nPrc >= PT1) {		// ok PT1 has been reached
    			bPT1Flag = true;	// set the flag 
    			bPT1TS = true;		// exit when price stops moving up
    			PT1TS = nX;			// the exit trigger  
    		} else {
    			if (nPrc < SL) {	// exit all
    				// oh crap - stop loss triggered
    				desc = "SL triggered";
    				fAlert(sFGdn, sBGdn);
    				Alert.playSound( "cowbell.wav" );
    				bAlerts = false;
    				// fix any other vars
    			}
    			// else still waiting for price to hit SL or 1st PT
    		}
    	}
    	return;
    }
    
    function fCheckShortTargets() {		// 3 tiered exit + stop loss
    	if (bPT1Flag == true) {	// price reached PT1
    		if (bPT1TS == true) {
    			if (nPrc >= PT1TS) {
    				// PT1 exit has been triggerred
    				desc = "PT1 sell first 1/3 at: "+nPrc;
    				fAlert(sFGup, sBGup);
    				Alert.playSound( "cowbell.wav" );
    				AdjSL = nE; 			// move Stop to entry
    				bPT1TS = false;
    			} else { 	// keep lowering exit price until  price turns up
    				if (PT1TS > nX) {PT1TS = nX;}
    			}
    		} else {	//  PT1Flag is T & TS is F means PT1 exit triggered
    			if (bPT2Flag == true) {
    				if (bPT2TS == true) {
    					if (nPrc >= PT2TS) {	// exit triggered
    						// PT2 exit triggered
    						desc = "PT2 sell second 1/3 at: "+nPrc;
    						fAlert(sFGup, sBGup);
    						Alert.playSound( "cowbell.wav" );
    						bPT2TS = false;
    						nTS = 0;	// make sure we set TS for the runner
    						PT2TS = 0;
    						// setup the runner
    						LL = Math.min(LL, nPrc);
    						nTS = nE - ( 0.75 * (nE-LL) );	
    					} else {	// keep raising exit price until  price turns down
    						if (PT1TS > nX) {PT1TS = nX;}
    					}
    				} else {
    					// at this point PT2 was triggered and we have a runner
    					if (nPrc >= nTS) {
    						// exit last third
    						desc = "Exit Runner at: "+nPrc;
    						fAlert(sFGup, sBGup);
    						
    						//  which vars need to be reset???
    						bAlerts = false;
    						
    					} else {
    						// update the Trailing Stop for the runner
    						LL = Math.min(LL, nPrc);
    						nTS = PT2 - ( 0.75 * (hh - PT2) );
    						// the TS should never give up more than 25% of the gain from PT2 (or 1or entry)
    					}
    				}
    			} else {  // don't have a PT2 flag yet
    				if (nPrc >= PT2) {	// hooray finally got to PT2
    					bPT2Flag = true;
    					bPT2TS = true;
    					PT2TS = nX;
    					AdjSL = PT1;		// move stop to PT1
    				} else if ( nPrc > AdjSL) {
    					desc = "Adj SL triggered";
    					fAlert(sFGdn, sBGdn);
    					Alert.playSound( "cowbell.wav" );
    					bAlerts = false;		
    					//  turn off alerts & exit all
    					// cleanup vars
    				}
    			}
    		}
    	} else {	// haven't set the flag for first PT 
    		if (nPrc <= PT1) {		// ok PT1 has been reached
    			bPT1Flag = true;	// set the flag 
    			bPT1TS = true;		// exit when price stops moving up
    			PT1TS = nX;			// the exit trigger  
    		} else {
    			if (nPrc > SL) {	// exit all
    				// oh crap - stop loss triggered
    				desc = "SL triggered";
    				fAlert(sFGdn, sBGdn);
    				Alert.playSound( "cowbell.wav" );
    				bAlerts = false;
    				// fix any other vars
    			}
    			// else still waiting for price to hit SL or 1st PT
    		}
    	}
    	return;
    }
Working...
X