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
Strategy Time filtering [SOLVED]
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2957 times
Re: Strategy Time filtering [SOLVED]
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();
}
}
-
- Posts: 7
- Joined: 17 Feb 2014
- Has thanked: 2 times
Re: Strategy Time filtering
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
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();
}
}
}
}
}
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2957 times
Re: Strategy Time filtering
The example above has been provided for Realtime trading:
For Backtesting please use the following code:
Code: Select all
bool _Condition = DateTime.Now.TimeOfDay >= _tStart && DateTime.Now.TimeOfDay <= _tEnd;
Code: Select all
bool _Condition = Bars.TimeValue.TimeOfDay >= _tStart && Bars.TimeValue.TimeOfDay <= _tEnd;
-
- Posts: 7
- Joined: 17 Feb 2014
- Has thanked: 2 times
Re: Strategy Time filtering
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
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
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2957 times
Re: Strategy Time filtering
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
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
-
- Posts: 7
- Joined: 17 Feb 2014
- Has thanked: 2 times
Re: Strategy Time filtering
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
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
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2957 times
Re: Strategy Time filtering
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.
Re: Strategy Time filtering
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...The example above has been provided for Realtime trading:For Backtesting please use the following code:Code: Select all
bool _Condition = DateTime.Now.TimeOfDay >= _tStart && DateTime.Now.TimeOfDay <= _tEnd;
Code: Select all
bool _Condition = Bars.TimeValue.TimeOfDay >= _tStart && Bars.TimeValue.TimeOfDay <= _tEnd;
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...