Following the kind comments and suggestions by George (gaheitman), I've refined the on-chart indicator code, to make it a little more efficient. It draws everything once on startup, and only recalculates/redraws everything when the chart is altered by incoming data, such as a new bar or changes to the range. This turned out to be a good exercise, which improved my understanding of Empty4's lesser-used functions. Maybe it - or part of it - will prove useful.
Code: Select all
extern double Level1=0.2, Level2=0.3, Level3=0.5, Level4=0.7, Level5=0.8;
extern int StochasticK=14, StochasticD=3, StochasticSlowing=7, RSIPeriod=9;
extern int EA_DrawingMem;
extern double EA_WindowPriceRangeMem;
//-------------------------------------------------------------------------------------------------------------------------------
int init(){ //This section contains code which executes only on startup
EA_DrawingMem=Time[WindowFirstVisibleBar()]; // Identify the time of the oldest bar on the screen. This is for determining when a redraw is necessary, for the on-screen indicators, at the start of a new bar.
EA_WindowPriceRangeMem=WindowPriceMax()-WindowPriceMin(); // Identify the range of the chart.
// This routine paints gridlines on the screen every 100 pips.
double a,b; // Define counting variables for this loop.
while (a<2000)
{b=a/100; ObjectCreate("_PriceLines"+a,OBJ_HLINE,0,Time[0],b); ObjectSet("_PriceLines"+a,OBJPROP_COLOR,Silver); ObjectSet("_PriceLines"+a,OBJPROP_STYLE,STYLE_DOT);a++;
} // End of this loop.
// These next two lines create two black boxes on the screen, to allow comments to be displayed without being obscured by the chart.
ObjectCreate("LabelBox1", OBJ_LABEL, 0, 0, 0); ObjectSetText("LabelBox1","g",150,"Webdings",Black); ObjectSet("LabelBox1", OBJPROP_XDISTANCE, 0); ObjectSet("LabelBox1", OBJPROP_YDISTANCE, 13); ObjectSet("LabelBox1",OBJPROP_BACK,0);
ObjectCreate("LabelBox2", OBJ_LABEL, 0, 0, 0); ObjectSetText("LabelBox2","g",150,"Webdings",Black); ObjectSet("LabelBox2", OBJPROP_XDISTANCE, 0); ObjectSet("LabelBox2", OBJPROP_YDISTANCE, 213); ObjectSet("LabelBox1",OBJPROP_BACK,0);
RepetitiveDrawing();// Call the drawing routine, to paint level lines, indicators, etc.
} // End of init routine
//-------------------------------------------------------------------------------------------------------------------------------
int start(){
if(Time[WindowFirstVisibleBar()] != EA_DrawingMem || (WindowPriceMax()-WindowPriceMin()) != EA_WindowPriceRangeMem) {
RepetitiveDrawing(); // Call the drawing routine, to re-paint level lines, indicators, etc.
} // End of the if routine
Comment("\n","Light blue: Stochastic main ",StochasticK,",",StochasticD,",",StochasticSlowing,"\nDark blue: Stochastic signal ",StochasticK,",",StochasticD,",",StochasticSlowing,"\nViolet: RSI ",RSIPeriod);
} // End of start routine
//-------------------------------------------------------------------------------------------------------------------------------
int deinit(){ //This section contains code which executes only on shutdown of the EA. Things like deleting drawing objects, etc.
ObjectsDeleteAll();
} // End of deinit routine
//-------------------------------------------------------------------------------------------------------------------------------
int RepetitiveDrawing(){
Print("Redrawing all graphical objects, to match new chart data.");
//This paints level lines on the chart, at a percentage of the chart range.
double WindowPriceRange=WindowPriceMax()-WindowPriceMin(); // Determine the range of the chart window
ObjectCreate("_Level1",OBJ_HLINE,0,Time[0],WindowPriceMin()+WindowPriceRange*Level1); ObjectSet("_Level1",OBJPROP_PRICE1,WindowPriceMin()+WindowPriceRange*Level1); ObjectSet("_Level1",OBJPROP_COLOR,Tan); ObjectSet("_Level1",OBJPROP_STYLE,STYLE_SOLID);
ObjectCreate("_Level2",OBJ_HLINE,0,Time[0],WindowPriceMin()+WindowPriceRange*Level2); ObjectSet("_Level2",OBJPROP_PRICE1,WindowPriceMin()+WindowPriceRange*Level2); ObjectSet("_Level2",OBJPROP_COLOR,Tan); ObjectSet("_Level2",OBJPROP_STYLE,STYLE_SOLID);
ObjectCreate("_Level3",OBJ_HLINE,0,Time[0],WindowPriceMin()+WindowPriceRange*Level3); ObjectSet("_Level3",OBJPROP_PRICE1,WindowPriceMin()+WindowPriceRange*Level3); ObjectSet("_Level3",OBJPROP_COLOR,Tan); ObjectSet("_Level3",OBJPROP_STYLE,STYLE_SOLID);
ObjectCreate("_Level4",OBJ_HLINE,0,Time[0],WindowPriceMin()+WindowPriceRange*Level4); ObjectSet("_Level4",OBJPROP_PRICE1,WindowPriceMin()+WindowPriceRange*Level4); ObjectSet("_Level4",OBJPROP_COLOR,Tan); ObjectSet("_Level4",OBJPROP_STYLE,STYLE_SOLID);
ObjectCreate("_Level5",OBJ_HLINE,0,Time[0],WindowPriceMin()+WindowPriceRange*Level5); ObjectSet("_Level5",OBJPROP_PRICE1,WindowPriceMin()+WindowPriceRange*Level5); ObjectSet("_Level5",OBJPROP_COLOR,Tan); ObjectSet("_Level5",OBJPROP_STYLE,STYLE_SOLID);
// This routine paints a 0-100 stochastic oscillator on the main chart, instead of creating a separate chart window.
int c; // define a counting variable for a loop
double Indicator1Val, Indicator1ValPrev, Indicator2Val, Indicator2ValPrev, Indicator3Val, Indicator3ValPrev, Indicator4Val, Indicator4ValPrev;
while (c<WindowBarsPerChart()){ // define the limits of the loop
Indicator1Val=(WindowPriceMin()+((iStochastic(NULL,0,StochasticK,StochasticD,StochasticSlowing,MODE_SMA,0,MODE_MAIN,c)/100)*WindowPriceRange));
Indicator1ValPrev=(WindowPriceMin()+((iStochastic(NULL,0,StochasticK,StochasticD,StochasticSlowing,MODE_SMA,0,MODE_MAIN,c+1)/100)*WindowPriceRange));
Indicator2Val=(WindowPriceMin()+((iStochastic(NULL,0,StochasticK,StochasticD,StochasticSlowing,MODE_SMA,0,MODE_SIGNAL,c)/100)*WindowPriceRange));
Indicator2ValPrev=(WindowPriceMin()+((iStochastic(NULL,0,StochasticK,StochasticD,StochasticSlowing,MODE_SMA,0,MODE_SIGNAL,c+1)/100)*WindowPriceRange));
Indicator3Val=(WindowPriceMin()+((iRSI(NULL,0,RSIPeriod,0,c)/100)*WindowPriceRange));
Indicator3ValPrev=(WindowPriceMin()+((iRSI(NULL,0,RSIPeriod,0,c+1)/100)*WindowPriceRange));
Indicator4Val=NULL;
Indicator4ValPrev=NULL;
ObjectCreate("_Indicator1"+c,OBJ_TREND,0,Time[c],Low[c]); ObjectSet("_Indicator1"+c,OBJPROP_PRICE1,Indicator1ValPrev); ObjectSet("_Indicator1"+c,OBJPROP_PRICE2,Indicator1Val); ObjectSet("_Indicator1"+c,OBJPROP_TIME1,Time[c+1]); ObjectSet("_Indicator1"+c,OBJPROP_TIME2,Time[c]); ObjectSet("_Indicator1"+c,OBJPROP_RAY,0); ObjectSet("_Indicator1"+c,OBJPROP_COLOR,DodgerBlue); ObjectSet("_Indicator1"+c,OBJPROP_WIDTH,1); ObjectSet("_Indicator1"+c,OBJPROP_BACK,1);
ObjectCreate("_Indicator2"+c,OBJ_TREND,0,Time[c],Low[c]); ObjectSet("_Indicator2"+c,OBJPROP_PRICE1,Indicator2ValPrev); ObjectSet("_Indicator2"+c,OBJPROP_PRICE2,Indicator2Val); ObjectSet("_Indicator2"+c,OBJPROP_TIME1,Time[c+1]); ObjectSet("_Indicator2"+c,OBJPROP_TIME2,Time[c]); ObjectSet("_Indicator2"+c,OBJPROP_RAY,0); ObjectSet("_Indicator2"+c,OBJPROP_COLOR,DarkBlue); ObjectSet("_Indicator2"+c,OBJPROP_WIDTH,1); ObjectSet("_Indicator2"+c,OBJPROP_BACK,1);
ObjectCreate("_Indicator3"+c,OBJ_TREND,0,Time[c],Low[c]); ObjectSet("_Indicator3"+c,OBJPROP_PRICE1,Indicator3ValPrev); ObjectSet("_Indicator3"+c,OBJPROP_PRICE2,Indicator3Val); ObjectSet("_Indicator3"+c,OBJPROP_TIME1,Time[c+1]); ObjectSet("_Indicator3"+c,OBJPROP_TIME2,Time[c]); ObjectSet("_Indicator3"+c,OBJPROP_RAY,0); ObjectSet("_Indicator3"+c,OBJPROP_COLOR,Violet); ObjectSet("_Indicator3"+c,OBJPROP_WIDTH,1); ObjectSet("_Indicator3"+c,OBJPROP_BACK,1);
ObjectCreate("_Indicator4"+c,OBJ_TREND,0,Time[c],Low[c]); ObjectSet("_Indicator4"+c,OBJPROP_PRICE1,Indicator4ValPrev); ObjectSet("_Indicator4"+c,OBJPROP_PRICE2,Indicator4Val); ObjectSet("_Indicator4"+c,OBJPROP_TIME1,Time[c+1]); ObjectSet("_Indicator4"+c,OBJPROP_TIME2,Time[c]); ObjectSet("_Indicator4"+c,OBJPROP_RAY,0); ObjectSet("_Indicator4"+c,OBJPROP_COLOR,Wheat); ObjectSet("_Indicator4"+c,OBJPROP_WIDTH,1); ObjectSet("_Indicator4"+c,OBJPROP_BACK,1);
c++;
} // End of this loop
EA_DrawingMem=Time[WindowFirstVisibleBar()]; // Redefine the time of the oldest bar on the screen, to prevent unnecessary repetition of this function.
EA_WindowPriceRangeMem=WindowPriceMax()-WindowPriceMin(); // Redefine the range of the chart, to prevent unnecessary repetition of this function.
} // End of Drawing routine
//-------------------------------------------------------------------------------------------------------------------------------
Thanks!
youcrazykids
[/size]
