
Obuli Govindaraju
20120217 22:57:29


I wrote a function that returns the value at the last bar ( result[result.length1] )
The purpose of this function to use in the simulator.
The reason I wrote such a function is because I thought the simulator traverses a chart from left to right one bar at a time. So whenever a simulator takes a new bar I thought that new bar will be the last bar so my function should work.
But when I run the simulator I see no trades at all even though there are conditions in the chart that meets my entry/exit criteria.
Does the simulator does not work as I thought? Should my function return values for all bars for the simulator to work?




QuantShare
20120218 02:28:59
0


The trading system formula (in QS language), including your function, will be executed only once.
When done, the simulator will take the "buy" variable and analyze the signals bar by bar.
If you want to execute a C# code for each trading bar, you must use the money management script.




Obuli Govindaraju
20120219 21:56:11
0


Thank you. Now how about parsing a function in the OnEndPeriod event?
For example I tried to print the number of bars in the chart. I wrote a function called NumBars() whose code is
for(int i=0;i<result.Length;i++) result[i] = i;
Then I created a formula that says
indi = NumBars();
PlotSymbol(1, indi, 0, 0, AboveHigh, colorTransparent, colorBlack, PlotSymbolNone);
When I apply the above formula to a chart it displays the bar index number right above the bar.
Now I wrote the following in the OnEndPeriod event.
string[] symbols = Data.GetSymbols();
TimeSeries numbar;
MMParser mmp;
int i;
mmp = Data.ParseFormula("indi=NumBars()");
numbar = mmp.GetTimeSeries(symbols[0],"indi");
Global.Trace("Total Bars =" +numbar.Count);
The above is printing only Total Bars = 1 as the output for all bars.
What is wrong? I was expecting to see Total Bars count increase when the money management script goes bar by bar.
Thanks,
Obuli




QuantShare
20120220 04:24:10
0


Hi,
You should use "numbar[0]" to get the current bar value (not numbar.Count)
Also make sure you add a semicolon after "indi=NumBars()".




Obuli Govindaraju
20120220 07:29:47
0


Thank you, that is giving me the output I expect. However I see some repeat, for example the first few lines of output looks like this. Why is that the number 2,7,12 are repeated?
Total Bars =0
Total Bars =1
Total Bars =2
Total Bars =2
Total Bars =2
Total Bars =3
Total Bars =4
Total Bars =5
Total Bars =6
Total Bars =7
Total Bars =7
Total Bars =7
Total Bars =8
Total Bars =9
Total Bars =10
Total Bars =11
Total Bars =12
Total Bars =12
Total Bars =12
Total Bars =13
Total Bars =14
Total Bars =15
I ran this on another security and now different sets of numbers repeat.
For the first one I used QQQ and for the second I used IWM. I am just running this simulation on one security since I am in learning mode.




QuantShare
20120220 08:57:23
0


On Saturday and Sunday, the value of Friday is used. That is why these numbers are repeated.




Obuli Govindaraju
20120220 10:43:03
0


Great thanks for the reply.
One last question. Lets look at this code
MMParser mmp = Data.ParseFormula("indi=NumBars();");
TimeSeries numbar = mmp.GetTimeSeries(symbols[0],"indi");
The formula variable indi is a vector. The last value in indi corresponds to result[result.Length1].
When you assign indi to numbar, then the last value of numbar should also correspond to result[result.Length1] correct?
If the above is true then how come numbar[0] returns the no. of bars in the index as opposed to numbar[numbar.Count1] ? (as you asked me to do in the above post?)




QuantShare
20120221 06:41:49
0


In the money management script, time series are indexed differently.
numbar[0] > Gets current bar value
numbar[1] > Gets previous bar value
numbar[1] > Gets next bar value
And thus, each time the "OnEndPeriod" event is executed, the value of "numbar[0]" changes




Obuli Govindaraju
20120221 22:55:32
0


Great! Thanks.
Now when you call a function using Data.ParseFormula does this function work on All bars everytime the onEndPeriod is called or works on only from starting bar to the current bar in consideration by the event?




QuantShare
20120222 04:28:19
0


The function calculates the formula for all bars. The result is cached and it is synchronized with the current bar date each time "OnEndPeriod" event is called.




Obuli Govindaraju
20120222 17:58:02
0


I have a function that returns value only at result[result.Length1].
So I assign this to a variable say mmparser = Data.ParseFormula("indi=myfunction());");
TimeSeries varTS = mmparser.GetTimeSeries(symbols[0],"indi");
since varTS[0] represents the current bar, I was expecting it to display the result, but it is not displaying anything. At what array location will my data be displaced?




QuantShare
20120223 03:56:49
0


Best Answer
You have an error in the formula inside "ParseFormula" ("))")
The correct code is:
indi=myfunction();




Obuli Govindaraju
20120223 07:59:44
0


Thanks for the wonderful discussion. I was able to accomplish what I wanted using global variables.



