JDanieldelaPaz
Posts: 1
Joined: Apr 29, 2014
|
Posted: Apr 29, 2014 @ 12:41 PM
Msg. 1 of 2
Does OEC have any function or method to identify if this is the first tick of a new bar?
If there isn't one, is there a way to have a persistent variable that can at least hold the last bar number so the indicator can identify if a new bar has formed.
John Daniel de la Paz
|
VPfau
Moderator
Posts: 164
Joined:
|
Posted: May 06, 2014 @ 10:44 AM
Msg. 2 of 2
Hi John,
See the indicator below. Please note that it is good practice to calculate bar on each Calculate call. Otherwise you will skip calculation very last bar on each tick.
using System; using System.Collections.Generic; using System.ComponentModel; using OEC.UI; using OEC.Chart; using OEC.Chart.Indicators; using OEC.Chart.Details; using OEC.Chart.BaseSeries; using OEC.EL.Runtime; using System.Runtime.InteropServices; using System.Drawing.Drawing2D;
namespace OEC.Chart.Custom.Sandbox {
[TimeSeries("OncePerBar", "OncePerBar", "My Indicators", AreaScale.Any, SourceScale=AreaScale.Price)] public sealed class MyInd : OEC.Chart.Custom.CustomIndicator { //use one of these two collections to track calculated bars private Dictionary<int, object> _barNumbers = new Dictionary<int, object>(); private Dictionary<DateTime, object> _timestamps = new Dictionary<DateTime, object>(); private void InitIndicator() { _barNumbers.Clear(); //_timestamps.Clear() //Do not clear timestamps if you need calculate bar once per lifetime } protected override void Refresh(DataChangedMode Mode) { if (Mode == DataChangedMode.Insert) InitIndicator(); base.Refresh(Mode); }
protected override void Calculate() { //here we have //CurrentBar: // the number of bar in the series; the pair CurrentBar/TimeStamp could // be changed after additional data loaded; Use this property if you need // calculate a bar once per recalculation process if (!_barNumbers.ContainsKey(CurrentBar)) { _barNumbers.Add(CurrentBar, null); //TODO: past your code here }
//InputData.Timestamps[CurrentBar] // TimeStamp of Current Bar. Use timestamps if you need calculate a bar // at certain timestamp once per indicator lifetime if (!_timestamps.ContainsKey(InputData.Timestamps[CurrentBar])) { _timestamps.Add(InputData.Timestamps[CurrentBar], null); //TODO: past your code here } } protected override DataSeries[] DefaultSubseries { get { return new DataSeries[]{ new DataSeries(this, "Value", new ChartStyle(SeriesChartType.Line,System.Drawing.Color.Red,1,DashStyle.Solid)) }; } }
protected Series ValueSeries { get { return RArray[0]; } } } }
Vitaliy Pfau
|