Click here to Login

                                                   How does Simulator work?


Obuli Govindaraju
2012-02-17 22:57:29

I wrote a function that returns the value at the last bar ( result[result.length-1] )

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?

2012-02-18 02:28:59


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
2012-02-19 21:56:11


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.


2012-02-20 04:24:10



You should use "numbar[0]" to get the current bar value (not numbar.Count)

Also make sure you add a semi-colon after "indi=NumBars()".

Obuli Govindaraju
2012-02-20 07:29:47


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.

2012-02-20 08:57:23


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

Obuli Govindaraju
2012-02-20 10:43:03


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.Length-1].
When you assign indi to numbar, then the last value of numbar should also correspond to result[result.Length-1] correct?

If the above is true then how come numbar[0] returns the no. of bars in the index as opposed to numbar[numbar.Count-1] ? (as you asked me to do in the above post?)

2012-02-21 06:41:49


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
2012-02-21 22:55:32


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?

2012-02-22 04:28:19


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
2012-02-22 17:58:02


I have a function that returns value only at result[result.Length-1].

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?

2012-02-23 03:56:49


Best Answer
You have an error in the formula inside "ParseFormula" ("))")

The correct code is:

Obuli Govindaraju
2012-02-23 07:59:44


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

No more messages


No html code. URLs turn into links automatically.

Type in the trading objects you want to include: - Add Objects
To add a trading object in your message, type in the object name, select it and then click on "Add Objects"


Trading Items
How far are the close prices from the support line
Fixed Fractional Position Sizing
Industry Equity
R Squared of a Trading System Equity Curve
Scale-in Trading Strategy

How-to Lessons
How to debug a downloader
How to add a metric in the trading system simulation report
How to optimize the stop limit of a trading system
How to download a trading item from the Sharing Server
How to create a custom trendline

Related Forum Threads
How to force simulator to close open positions when data ends?
How to get the last day of the month in C#
Debugging System in Simulator
Simulator question
Simulator algorithm change/option

Blog Posts
How Does QuantShare Work?
Buy the best/top rated stocks or how to create powerful rank base...
How to create a trading system, screen and composite using earnin...
How to build and backtest a robust stock trading system
How to use QuantShare screener to trade penny stocks

Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2021
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Follow us on Google+
RSS Trading Items

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.