Strategy Time filtering  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
orionsturtle
Posts: 7
Joined: 17 Feb 2014
Has thanked: 2 times

Strategy Time filtering

Postby orionsturtle » 25 Feb 2014

I was wondering if anyone can give me a small block of C# code that will filter trades to just take place between 0930 and 1200 each day? ie if time >= 0930 or time < 1200 then trade.

Thank you!
Mike

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Strategy Time filtering  [SOLVED]

Postby Henry MultiСharts » 26 Feb 2014

Please find a sample code below:

Code: Select all

protected override void CalcBar()
{
double m_Avg = m_AverageFC[0];
if (PublicFunctions.DoubleGreater(Price[0], m_Avg))
{
++m_Counter.Value;
}
else
{
m_Counter.Value = 0;
}
TimeSpan _tStart = new TimeSpan(9, 30, 0);
TimeSpan _tEnd = new TimeSpan(12, 0, 0);

bool _Condition = DateTime.Now.TimeOfDay >= _tStart && DateTime.Now.TimeOfDay <= _tEnd;
if (PublicFunctions.DoubleGreater(Bars.CurrentBar, ConfirmBars)
&& m_Counter.Value == ConfirmBars && _Condition)
{
m_MACrossLE.Send();
}
}

orionsturtle
Posts: 7
Joined: 17 Feb 2014
Has thanked: 2 times

Re: Strategy Time filtering

Postby orionsturtle » 26 Feb 2014

Henry
I was trying to apply your sample time filter code to the canned RSI_LE signal and it complied and gave me the green check mark, but the ALL the signals disappeared from the chart. I was wondering if you might take a look at the CalcBar() block at the end (below) where I added your time filter code and see if you can tell me why all the signals disappeared from the chart.

Thanks for helping a noob!
Peace
OT

Code: Select all

using System;
using PowerLanguage.Function;

namespace PowerLanguage.Strategy
{
public class MT_RSI_TF2_LE : SignalObject
{
private RSI m_RSI;

private VariableSeries<Double> m_myrsi;

private IOrderMarket m_RsiLE;

public MT_RSI_TF2_LE(object ctx) :
base(ctx)
{
OverSold = 30;
Length = 14;
}

private ISeries<double> Price { get; set; }

[Input]
public int Length { get; set; }

[Input]
public double OverSold { get; set; }

protected override void Create(){
m_RSI = new RSI(this);
m_myrsi = new VariableSeries<Double>(this);
m_RsiLE =
OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, "RsiLE", EOrderAction.Buy));
}

protected override void StartCalc(){
Price = Bars.Close;
m_RSI.price = Price;
m_RSI.length = Length;
}


protected override void CalcBar(){
[b] TimeSpan _tStart = new TimeSpan(9, 30, 0);
TimeSpan _tEnd = new TimeSpan(12, 0, 0);
bool _Condition = DateTime.Now.TimeOfDay >= _tStart && DateTime.Now.TimeOfDay <= _tEnd;[/b]

m_myrsi.Value = m_RSI[0];
if (Bars.CurrentBar > 1 [b]&& _Condition[/b] ){
if (this.CrossesOver(m_myrsi,OverSold)){
m_RsiLE.Send();
}
}
}
}
}

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Strategy Time filtering

Postby Henry MultiСharts » 27 Feb 2014

The example above has been provided for Realtime trading:

Code: Select all

bool _Condition = DateTime.Now.TimeOfDay >= _tStart && DateTime.Now.TimeOfDay <= _tEnd;
For Backtesting please use the following code:

Code: Select all

bool _Condition = Bars.TimeValue.TimeOfDay >= _tStart && Bars.TimeValue.TimeOfDay <= _tEnd;

orionsturtle
Posts: 7
Joined: 17 Feb 2014
Has thanked: 2 times

Re: Strategy Time filtering

Postby orionsturtle » 27 Feb 2014

That worked.
I was looking at that "DateTime.Now.TimeOfDay" and thought it might be referencing just the current time. I tried to look up DateTime in the help but there doesn't seem to be a search function, and not really knowing my way around yet I couldn't find the info I was looking for.
Is there a comprehensive list explaining all the classes and methods? I'm not sure I asked that correctly.

Also, do you know of any video training for programming indicators and strategies for MC.NET?

Thanks again!
peace,
OT

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Strategy Time filtering

Postby Henry MultiСharts » 27 Feb 2014

Our Wiki section provides detailed up-to-date information about current MultiCharts functionality. You can reference this information as the platform help guide when using both products MultiCharts and MultiCharts .NET as they have the same functionality but are using different programming languages.

MC .NET into video can be found here.

As for the .Net programming guide - MultiCharts .Net is oriented to experienced programmers. In case you have any questions regarding C# coding we recommend referencing MSDN for information. DateTime.Now.TimeOfDay is the native C# functionality. Description can be found @ MSDN.

Interfaces and classes available in MultiCharts .Net are described in MC .Net help file (PowerLanguage .Net Editor->Help tab-> PowerLanguage .Net Help). You can also find the code examples in the source code of the prebuilt MultiCharts studies (PowerLanguage .Net Editor->File tab->Open).

MultiCharts .NET Programming guide that contains important and useful information not only for beginners but for experienced MC .Net users as well. This is the recommended "Getting started for programming in MultiCharts .Net": https://www.multicharts.com/downloads/M ... e-v1.0.pdf

MultiCharts .NET Programming guide in Wiki: https://www.multicharts.com/trading-sof ... ming_Giude

MC .NET FAQ: viewtopic.php?f=19&t=45848

orionsturtle
Posts: 7
Joined: 17 Feb 2014
Has thanked: 2 times

Re: Strategy Time filtering

Postby orionsturtle » 27 Feb 2014

That's great Henry. I now know I am not missing anything.

BTW...I came a cross this great video series of C# training if anyone is interested.
http://channel9.msdn.com/Series/C-Sharp ... -Beginners

An aside for the marketing department:
I currently "program" in .afl, Excel VBA, and Easy Language. For what it's worth, as someone new to the platform I would feel comfortable with the platform quicker and come to a buying decision a lot sooner if I didn't have to go through another learning curve of learning a hard core programming language like C# just to get the platform functioning for my needs. I don't mind doing this, as I like learning new things, but if there was a powerlanguage SE edition it would move me to purchase a lot sooner.
One of your competitors also gives a full working version of their software but you just can't save any of your work until you register it. I don't know all the concerns of MC management, but having the vision to get the software in as many hands as possible with the greatest ease of operation might not be a bad goal. Just a thought.

I really love MC, so far it is by far the most comprehensive platform out there that I have seen. Well worth the effort to learn C#.

Thanks again Henry
peace
OT

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Strategy Time filtering

Postby Henry MultiСharts » 28 Feb 2014

orionsturtle, thank you for your feedback. It has been forwarded to the management of our company. You can sign up for a free 30-day MultiCharts with PowerLanguage trial at this page.

DonDuck
Posts: 6
Joined: 01 Apr 2021

Re: Strategy Time filtering

Postby DonDuck » 13 Jan 2022

The example above has been provided for Realtime trading:

Code: Select all

bool _Condition = DateTime.Now.TimeOfDay >= _tStart && DateTime.Now.TimeOfDay <= _tEnd;
For Backtesting please use the following code:

Code: Select all

bool _Condition = Bars.TimeValue.TimeOfDay >= _tStart && Bars.TimeValue.TimeOfDay <= _tEnd;
As this is an old thread, anyone going crazy trying to get it to work in 2021/2022, you first have to declare the TimeSpan variables at the strategy class level (top of the strategy) and then initialize/assign the values in the CalcBar...
Example:
Strategy Class level:
private TimeSpan tStart0;
private TimeSpan tEnd0;
private TimeSpan tStart1;
private TimeSpan tEnd1;
private TimeSpan tStart2;
private TimeSpan tEnd2;
private TimeSpan tStart3;
private TimeSpan tEnd3;
.
.
.
CalcBar:
tStart0 = new TimeSpan(00, 00, 00);
tEnd0 = new TimeSpan(04, 00, 00);
tStart1 = new TimeSpan(06, 30, 00);
tEnd1 = new TimeSpan(13, 00, 00);
tStart2 = new TimeSpan(14, 45, 00);
tEnd2 = new TimeSpan(16, 10, 00);
tStart3 = new TimeSpan(19, 30, 00);
tEnd3 = new TimeSpan(23, 00, 00);

I hope this helps...


Return to “MultiCharts .NET”