C# Mark the close of Bar at specific time.
C# Mark the close of Bar at specific time.
I am trying to create a simple indicator that would mark the chart with a horizontal line at 2pm every day. That is, at the close of the bar at 2pm, make a horizontal line. I have found Bars.Time[0], but this is for the current bar. I would like to do an "If Bars.Time[0] == 2pm". What is the format that would equate Bars.Time to 2pm? Any help is appreciated.
- Henry MultiСharts
- Posts: 9165
- Joined: 25 Aug 2011
- Has thanked: 1264 times
- Been thanked: 2958 times
Re: C# Mark the close of Bar at specific time.
Hello alex650,
You can use the following code:
You can use the following code:
Code: Select all
TimeSpan needTime = new TimeSpan(14,0,0);
if(Bars.TimeValue.TimeOfDay == needTime)
{
// TO DO
}
Re: C# Mark the close of Bar at specific time.
Thank you for that bit of code. Now I'm trying to figure out how to plot a horizontal line at that price. I have tried various "EPlotShapes." but none give a horizontal line. I also experimented with "if(Bars.TimeValue.TimeOfDay == needTime)" changing it to >=, but then that will recalculate the close of each candle after 1400 and plot a leftTick there.
I also attempted this:
" private decimal whitec()
{
TimeSpan needTime = new TimeSpan(12,0,0);
if(Bars.TimeValue.TimeOfDay == needTime)
return (decimal)( Bars.Close[0]) ;
else
return 0;
}
" but this will not work as "plot1.Set (whitec());" will not work with a decimal.
The code i currently have only plots a left tick at the 1400 price.
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
namespace PowerLanguage.Indicator{
public class wtest : IndicatorObject {
public wtest(object _ctx):base(_ctx){}
private IPlotObject plot1;
private VariableObject<Double> m_timeline;
protected override void Create() {
// create variable objects, function objects, plot objects etc.
plot1 = AddPlot(new PlotAttributes("TimeLine", EPlotShapes.LeftTick,
Color.Magenta, Color.Empty, 2,
0,
true));
}
protected override void StartCalc() {
// assign inputs
}
protected override void CalcBar(){
// indicator logic
TimeSpan needTime = new TimeSpan(14,0,0);
if(Bars.TimeValue.TimeOfDay == needTime)
{
// TO DO
plot1.Set(Bars.Close[0]);
}
}
}
}
I also attempted this:
" private decimal whitec()
{
TimeSpan needTime = new TimeSpan(12,0,0);
if(Bars.TimeValue.TimeOfDay == needTime)
return (decimal)( Bars.Close[0]) ;
else
return 0;
}
" but this will not work as "plot1.Set (whitec());" will not work with a decimal.
The code i currently have only plots a left tick at the 1400 price.
using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;
namespace PowerLanguage.Indicator{
public class wtest : IndicatorObject {
public wtest(object _ctx):base(_ctx){}
private IPlotObject plot1;
private VariableObject<Double> m_timeline;
protected override void Create() {
// create variable objects, function objects, plot objects etc.
plot1 = AddPlot(new PlotAttributes("TimeLine", EPlotShapes.LeftTick,
Color.Magenta, Color.Empty, 2,
0,
true));
}
protected override void StartCalc() {
// assign inputs
}
protected override void CalcBar(){
// indicator logic
TimeSpan needTime = new TimeSpan(14,0,0);
if(Bars.TimeValue.TimeOfDay == needTime)
{
// TO DO
plot1.Set(Bars.Close[0]);
}
}
}
}
- Alex MultiCharts
- Posts: 194
- Joined: 09 Aug 2013
- Has thanked: 43 times
- Been thanked: 77 times
Re: C# Mark the close of Bar at specific time.
If you want to draw a horizontal line using plot, then you have 2 options:
1. Use plot style not LeftTick, but Line.
2. To draw not plots, but drawings - trendlines.
1. Use plot style not LeftTick, but Line.
2. To draw not plots, but drawings - trendlines.
Re: C# Mark the close of Bar at specific time.
Thanks, I'm not a programmer so I am having a hard time with this.
Replacing Eplotshapes.LeftTick with "EPlotStyle.DashDotDot" or "EPlotShapes.Line" still does not create the horrizontal line. EPlotShapes.Line only marks a small dash and EPlotStyle gives a few errors:
'PowerLanguage.EPlotStyle.DashDotDot' is a 'field' but is used like a 'type' "wtest" [Indicator] Ln 16
The best overloaded method match for 'PowerLanguage.PlotAttributes.PlotAttributes(string, PowerLanguage.EPlotShapes, System.Drawing.Color, System.Drawing.Color, int, PowerLanguage.EPlotStyle, bool)' has some invalid arguments "wtest" [Indicator] Ln 17
Argument 2: cannot convert from 'PowerLanguage.EPlotStyle' to 'PowerLanguage.EPlotShapes' "wtest" [Indicator] Ln 17
plot1 = AddPlot(new PlotAttributes("TimeLine", EPlotShapes.Line,
All help is appreciated and I will give thanks.
Replacing Eplotshapes.LeftTick with "EPlotStyle.DashDotDot" or "EPlotShapes.Line" still does not create the horrizontal line. EPlotShapes.Line only marks a small dash and EPlotStyle gives a few errors:
'PowerLanguage.EPlotStyle.DashDotDot' is a 'field' but is used like a 'type' "wtest" [Indicator] Ln 16
The best overloaded method match for 'PowerLanguage.PlotAttributes.PlotAttributes(string, PowerLanguage.EPlotShapes, System.Drawing.Color, System.Drawing.Color, int, PowerLanguage.EPlotStyle, bool)' has some invalid arguments "wtest" [Indicator] Ln 17
Argument 2: cannot convert from 'PowerLanguage.EPlotStyle' to 'PowerLanguage.EPlotShapes' "wtest" [Indicator] Ln 17
plot1 = AddPlot(new PlotAttributes("TimeLine", EPlotShapes.Line,
All help is appreciated and I will give thanks.
- Alex MultiCharts
- Posts: 194
- Joined: 09 Aug 2013
- Has thanked: 43 times
- Been thanked: 77 times
Re: C# Mark the close of Bar at specific time.
To achive what you need, it is necessary to write the script correctly and it will be difficult in C# as programming experience is required. MultiCharts .Net is oriented to experienced programmers. In case you have any questions regarding C# coding we recommend referencing MSDN for information. 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
Wiki format: https://www.multicharts.com/trading-sof ... ming_Giude
MC .NET FAQ: viewtopic.php?f=19&t=45848
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
Wiki format: https://www.multicharts.com/trading-sof ... ming_Giude
MC .NET FAQ: viewtopic.php?f=19&t=45848