Click here to Login

                                                   Get the Previous N bar Date/DateTime in Money Management script


Borbely Zsolt
2020-06-02 19:28:31


In the Advanced Money Management script I can get the current bar date using the following function: Divers.CurrentDate.
Is there a way to get the previous n bar Date?
As not all weekdays are trading days, I can't find a strait forward way to get the previous n bar date.

I was trying to create a custom function (BarDateTime) and return the Date string in the result vector, but when I try to call my custom function with ParseFormula, the function always returns 0;

BarDateTime function:

int n = (int)N[0];
for(int i = 0;i<cFunctions.Date.Length;i++)
int shift = i + n;
if(shift > 0 && shift < cFunctions.Date.Length)
result.SetValue(i, cFunctions.Date[shift].ToString());
result.SetValue(i, String.Empty);

This is how I try to call in the Money Management script using the Data.ParseFormula:
TimeSeries twoDaysBeforeToday = Data.ParseFormula("a = BarDateTime(2);").GetTimeSeries(symbol, "a");
twoDaysBeforeToday[0] is always 0 even if I write the BarDateTime function result to the Global.Trace then I can see that I have valid values in the result object.

for(int index=0;index<result.Length;index++)
Global.Trace("Index: " + index + " - Value: " + result[index]);

Am I using the Data.ParseFormula correctly?
Is there an easier way to get the previous n bar Date?

Please advice!


Borbely Zsolt
2020-06-03 05:26:30


Meantime I have found a working solution for my problem, I have used the day, month, year QS functions to get the date for the N bar.

Here is the code:
public DateTime BarDate(MMData data, string symbol, int N){
TimeSeries day = data.ParseFormula("a = ref(day(), " + N + ") ;").GetTimeSeries(symbol, "a");
TimeSeries month = data.ParseFormula("a = ref(month(), " + N + ") ;").GetTimeSeries(symbol, "a");
TimeSeries year = data.ParseFormula("a = ref(year(), " + N + ") ;").GetTimeSeries(symbol, "a");
DateTime result = new DateTime((int)year[0], (int)month[0], (int)day[0]);
return result;

This works for me as I expected.
Could you please confirm that this is a good or the best solution for my problem.


2020-06-03 12:53:12


Best Answer
Hi Zsolt,

Another solution, would be to create a list in the Global event and store the Divers.CurrentDate date on each "OnEndPeriod" event.
Then you can access the previous N item of the list easily.

Borbely Zsolt
2020-06-03 17:23:52


Thank you for your response.
The solution I have found looks more flexible than the one with "OnEndPeriod" because it can be used with bars in past and in the future as well. With the "OnEndPeriod" you can access only the previous bars but not the future bars.


2020-06-04 10:12:44


Yes, you are right. But the one I proposed will run much faster.

Borbely Zsolt
2020-06-04 18:35:37


Thank you for your support. Your argument makes sense.

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
Long strategy created with the money management tool
Return per Bar Stop - Money Management Strategy
Investing in stocks using the Kelly criterion money management st...
Money management to Reject Trading Positions Based on their Past ...
Money Management System to Split Order Into Smaller Pieces

How-to Lessons
How to calculate the average of a time series using the money man...
How to get stocks for a particular index using the global script
How to debug a trading system using the money management tool
How to install the previous version of QuantShare
How to change volume bar colors when the price closes down

Related Forum Threads
Money Management script and exit options in the wizard
Advance Money Management Script debug.
Can a Money Management script access a custom function?
Money Management Script variables and more
Advanced Money Management

Blog Posts
Basic trading system implemented using the money management tool
Create a trading strategy using the money management tool - Part ...
Create a trading strategy using the money management tool - Part ...
Money Management: Optimize the scale-in strategy
Several money management strategies in a trading system

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.