The attached code works when loaded, but when run in real time the lines draw in the wrong places.
Announcement
Collapse
No announcement yet.
Lines not drawing in real time
Collapse
X
-
Hello Mike2,
The problem revolves around your logic for the "lineY" variable. The reason the lines draw where you want them on reload is because the bar indexes during the loading process do not change. When getCurrentBarIndex() is executed on a historical bar during the loading process it will return -###, or the current bar index relative to the 0 bar at the time of the reload. Once the formula starts processing in real time getCurrentBarIndex() is returning -1 (because of setComputeOnClose() in preMain). Every new bar that arrives after one of your lines are drawn is leaving "lineY" at the value of -1. You need to manually update this variable at BARSTATE_NEWBAR to reflect is new bar index. At the end of main() before your return statement, try adding the following logic which will look for BARSTATE_NEWBAR in real time.
PHP Code:if (getBarState() == BARSTATE_NEWBAR && getCurrentBarIndex() == -1) {
lineY--;
}
Jason K.
Project Manager
eSignal - an Interactive Data company
EFS KnowledgeBase
JavaScript for EFS Video Series
EFS Beginner Tutorial Series
EFS Glossary
Custom EFS Development Policy
New User Orientation
-
Making money from this code ?.
Thanks Jason, I think you've pointed me in the right direction, however the code still doesn't work. I have modified the code to work with Stochastics, it's very neat and can be quite predictive. If anyone makes money from this code, please let me know how you went about it. The code is good, but trying to pick which line is best is difficult. Send email to [email protected].
I have tried making it work as a range indicator (quite successfully on occassions) and as a directional indicator (quite successfully at times). But consistency is the usual problem with most indicators. If you think you know a price is going to trend, it's very good at picking highs and lows. I mostly use it on the Euro.
I have another version of the code which works with the offset of Moving Averages, it's also quite good (see attached).
Warning: this code looks great in backtesting, but is tricky to use when live. Definately paper trade it first.
PHP Code://Mike's FREAK index, bizzare, but works a total treat
//{{EFSWizard_Description
//
// This formula was generated by the Alert Wizard
//
//}}EFSWizard_Description
//{{EFSWizard_Declarations
var vStoch14_1 = new StochStudy(14, 1, 3);
var vStoch28_1 = new StochStudy(28, 1, 3);
var vStoch7_1 = new StochStudy(7, 1, 3);
var vStoch56_1 = new StochStudy(56, 1, 3);
var vLastAlert = -1;
//}}EFSWizard_Declarations
var barcount = 0;
var lineX = 0;
var lineY = 0;
var lineX2 = 0;
var lineY2 = 0;
var FirstTick = 0;
var noPrint = true;
var NoBeep = true;
var NoAlert = true;
var NoShow = true;
function preMain() {
setComputeOnClose();
setPriceStudy(true); // false if want to show array
setStudyTitle("Line");
setCursorLabelName("h1", 0);
setCursorLabelName("h2", 1);
setCursorLabelName("h3", 2);
setCursorLabelName("h4", 3);
setDefaultBarStyle(PS_SOLID, 0);
setDefaultBarStyle(PS_SOLID, 1);
setDefaultBarStyle(PS_SOLID, 2);
setDefaultBarStyle(PS_SOLID, 3);
setDefaultBarFgColor(Color.red, 0);
setDefaultBarFgColor(Color.darkgrey, 1);
setDefaultBarFgColor(Color.navy, 2);
setDefaultBarFgColor(Color.fuchsia, 3);
setDefaultBarThickness(4, 0);
setDefaultBarThickness(4, 1);
setDefaultBarThickness(4, 2);
setDefaultBarThickness(4, 3);
setPlotType(PLOTTYPE_HISTOGRAM, 0);
setPlotType(PLOTTYPE_HISTOGRAM, 1);
setPlotType(PLOTTYPE_HISTOGRAM, 2);
setPlotType(PLOTTYPE_HISTOGRAM, 3);
}
function main() {
barcount ++;
if(FirstTick == 2) {
FirstTick =3;
noPrint = false;
}
if (getCurrentBarIndex() > -2 && FirstTick ==1) FirstTick = 2;
if (getCurrentBarIndex() == -2) FirstTick = 1;
//if (!noPrint){
if (vStoch7_1.getValue(StochStudy.FAST, -1) < 7){
if (lineX > 0) {
//removeLineTool( LineTool.SEGMENT, "segment1" );
//addLineTool( LineTool.SEGMENT, lineY - barcount, lineX, 0, close(), 2, Color.blue, "segment1" );
//addLineTool( LineTool.SEGMENT, - barcount + lineY , lineX, getCurrentBarIndex(), close(), 2, Color.blue, "segment1" ) // does same thing as following line
addLineTool( LineTool.SEGMENT, lineY -1 , lineX, getCurrentBarIndex() -1 + getCurrentBarIndex() - lineY -1, close(-1) + close(-1) - lineX, 2, Color.blue, "segment1" )
// addLineTool( LineTool.SEGMENT, lineY -1 , lineX, getCurrentBarIndex(), close() , 4, Color.blue, "segment2" )
//drawTextRelative(0, +5, barcount, Color.green, null,Text.RELATIVETOTOP|Text.TOP|Text.CENTER|Text.FRAME, "Comic Sans MS", 8, (barcount+"text"+1));
//drawTextRelative(0, +5, getCurrentBarIndex(), Color.red, null,Text.RELATIVETOTOP|Text.TOP|Text.CENTER|Text.FRAME, "Comic Sans MS", 8, (barcount+"text"+1));
lineX = open(-1);
if (noPrint) lineY = getCurrentBarIndex(); //barcount;
if(!NoBeep) Alert.playSound("c:\\program files\\netmeeting\\blip.wav");
if(!NoAlert) Alert.addToList(getSymbol()+" "+getInterval(),"Line "+close(),Color.blue,Color.blue);
}
else {
lineX = open(-1);
if (noPrint) lineY = barcount;
}
}
if (vStoch7_1.getValue(StochStudy.FAST, -1) > 90){
if (lineX > 0) {
//removeLineTool( LineTool.SEGMENT, "segment1" );
//addLineTool( LineTool.SEGMENT, lineY - barcount, lineX, 0, close(), 2, Color.blue, "segment1" );
//addLineTool( LineTool.SEGMENT, - barcount + lineY -1, lineX, getCurrentBarIndex()-1, close(), 2, Color.blue, "segment1" ) // does same thing as following line
addLineTool( LineTool.SEGMENT, lineY2 -1 , lineX2, getCurrentBarIndex() -1 + getCurrentBarIndex() - lineY2 -1, close(-1) + close(-1) - lineX2, 2, Color.green, "segment1" )
// addLineTool( LineTool.SEGMENT, lineY2 -1 , lineX2, getCurrentBarIndex(), close() , 4, Color.green, "segment2" )
//drawTextRelative(0, +5, barcount, Color.green, null,Text.RELATIVETOTOP|Text.TOP|Text.CENTER|Text.FRAME, "Comic Sans MS", 8, (barcount+"text"+1));
//drawTextRelative(0, +5, getCurrentBarIndex(), Color.red, null,Text.RELATIVETOTOP|Text.TOP|Text.CENTER|Text.FRAME, "Comic Sans MS", 8, (barcount+"text"+1));
lineX2 = open(-1);
if (noPrint) lineY2 = getCurrentBarIndex(); //barcount;
}
else {
lineX = open(-1);
if (noPrint) lineY = barcount;
}
}
if (getBarState() == BARSTATE_NEWBAR && getCurrentBarIndex() == -1 && !noPrint) {
lineY--;
}
//else lineY = barcount;
var v100 = 100
if (!NoShow){
return new Array(
vStoch56_1.getValue(StochStudy.FAST),
vStoch28_1.getValue(StochStudy.FAST),
vStoch14_1.getValue(StochStudy.FAST),
vStoch7_1.getValue(StochStudy.FAST),
v100
);
}
}
Attached Files
Comment
-
Hello Mike2,
In the formula you attached to your post, you did not have the code solution I gave you in that version. If you add it, it should solve the problem.
For your Stochastic code, you are using an additional variable, lineY2, that will also need to follow the same logic as lineY. There is a new problem with this formula introduced with your usage of the noPrint variable. First, you have changed the conditional statement I gave you and added ... && !noPrint . You need to remove that because your formula logic is currently setting noPrint to false once it reaches bar -1, which prevents the code solution from being executed in real time. Change the condition to look like below.
PHP Code:if (getBarState() == BARSTATE_NEWBAR && getCurrentBarIndex() == -1) {
lineY--;
lineY2--;
}
Jason K.
Project Manager
eSignal - an Interactive Data company
EFS KnowledgeBase
JavaScript for EFS Video Series
EFS Beginner Tutorial Series
EFS Glossary
Custom EFS Development Policy
New User Orientation
Comment
-
Jason, appreciate your help. The noprint routine is a standard routine I have in all my programs, because quite often I write an output to file and use it to trade with. I found it the only way to stop writing data before a tick is made in live trading, otherwise I was getting outputs before the first tick (using barstate, etc). I'm sure there's a more efficient way of doing this, but this worked for me.
I still can't get the routine to work. I changed the code per your suggestions.
Regards,
MikeAttached FilesLast edited by Mike2; 07-27-2005, 04:48 AM.
Comment
-
Hi Mike2,
There is still a if (noPrint) condition on line 80 that is preventing lineY from being updated. Remove that condition and try it again.Jason K.
Project Manager
eSignal - an Interactive Data company
EFS KnowledgeBase
JavaScript for EFS Video Series
EFS Beginner Tutorial Series
EFS Glossary
Custom EFS Development Policy
New User Orientation
Comment
Comment