Is there a way (or the best way) to record each historical bar's full time stamp (beginning and ending date/hour/minute) as an EFS is being loaded? to be saved for later use, such as to a file? Thanks.
Announcement
Collapse
No announcement yet.
Historical Begin and End Time Stamps
Collapse
X
-
z11,
here is a method I used after all bars were loaded to accomplish something very similar to what you want.
PHP Code:function time_and_price_data(){
nbars = Math.abs(getOldestBarIndex());
dbar = Initialize_2X_Array(10,nbars);
var n;
for (n=0;n<nbars;n++){
dbar[0][n] = (getHour(n-nbars)*60) + getMinute(n-nbars); // # of minutes
dbar[1][n] = open(n-nbars);
dbar[2][n] = close(n-nbars);
dbar[3][n] = high(n-nbars);
dbar[4][n] = low(n-nbars);
}
function Initialize_2X_Array(n1,n2){//initialize 2 dim array
var i; var j;var newArray = new Array;
if (n1 == null){n1 =2;}if (n2 == null){n2 = 2;}
for (i=0;i<n1; i++){newArray[i] = new Array();
for (j=0;j<n2; j++){newArray[i][j]=0;
}}
return newArray;
}
PHP Code://define as a global
dbar = new array();
//in Main every new bar
//you define the starttime and date variables
dbar[barcount] = new time_price_data(starttime, date);
// as a separate function
function time_price_data(s,d){
this.start_time = s;
this.date = d;
}
// then store the data
function store_data(){
var i;
file1 = new File("x");
file1.open("wt+");
for (i=0;i<dbar.length;i++){// writes to file
var a = dbar[i].start_time+","+dbar[i].date;
file1.writeln(a);
file1.flush();
}
file1.close();
}
I hope this helps.
-
Hi Z11,
Thanks for the kind words.
Yes, you can. I am assuming you would want this for some sort of iterative loop.
Alternatively, you can also use a for/in statement loop with objects as well. See this link http://www.quirksmode.org/js/associative.html.
Regarding a 2 dimensional array, dbar[x][y], where x and y are the lengths of their respective dimensions
dbar.length would equal x
dbar[0].length would be equal to y (assuming element zero was defined).
the same goes for dbar[1].length, dbar[2].length, etc. Depending how these elements were defined, their length may each be different. In the first example in my post below, they would all be the same length.
Comment
-
Thanks Steve. I have not tried it but I presume the operations of Pop and Unshift will also work on the 2 dimensional arrays as well...
On a unrelated note, how would you prevent a system from re-enter a position it just exited on the same bar (but different ticks)... say it was short, and its logic exited the short ...but on the next tick it goes back in short.... I tried to set a flag on the bar it exited but it stopped all trades since the bar index is zero for the current bar but on each iteration of ticks you can't increment that flag since it is still the same bar...don't know if I am explaining myself correctly...
Comment
-
Z11,
An easy way would be to create a bool that is set to true when you exit and set to false initially and on each NEWBAR. All you would have to do is check the value of the bool before entering a new trade.
Alternatively - only enter or exit trades at the close of the bar.
GarthGarth
Comment
Comment