Changes

Jump to navigation Jump to search
no edit summary
To start the data downloading process it is necessary to call the following method:
<syntaxhighlightlang="csharp">
IDataLoaderResult BeginLoadData(InstrumentDataRequest Request, LoadDataCallback Sink, object State)
</syntaxhighlight>
::Resolution structure helps to determine both regular and irregular resolutions.
* <div style="background-color: #E5F6FF;">'''Example of determining a regular resolution:'''</div><syntaxhighlightlang="csharp">::Resolution _res = new Resolution(EResolution.Tick, 10);
</syntaxhighlight>
::If the request resolution is set in this way then 10-tick data will be requested. </div>
* <div style="background-color: #E5F6FF;">'''Example of determining an irregular resolution:'''</div><syntaxhighlightlang="csharp">::Resolution _res = Resolution.CreatePointAndFigure(EResolution.Tick, 10, 0.001, 3, PointAndFigureBasis.HighLow, true);
</syntaxhighlight>
</div>
If the request resolution is set in this way, then the PointAndFigure resolution based on a 10-tick resolution, BoxSize = 0.001, Reversal = 3, HighLow Basis and BreakOnSession enabled will be returned.
:'''{{color|blue|LoadDataCallback}}''' – is a delegate with the following signature:<syntaxhighlightlang="csharp">
public delegate void LoadDataCallback(IDataLoaderResult Result);
</syntaxhighlight>
'''1.''' Create the new indicator with the standard pattern:
<syntaxhighlightlang="csharp">namespace PowerLanguage.Indicator{
public class DataLoader : IndicatorObject {
public DataLoader(object _ctx):base(_ctx){}
'''2.''' Add the plot statements that will plot spread values:
<syntaxhighlightlang="csharp">
private IPlotObject m_spreadPlot;
</syntaxhighlight>
'''3.''' Then create a corresponding object. This should be done in Create() function:
<syntaxhighlightlang="csharp">
protected override void Create() {
m_spreadPlot = AddPlot(new PlotAttributes("", EPlotShapes.Line, Color.Red));
}
</syntaxhighlight>
'''4.''' Now it is necessary to form the objects for data requests. To do that, InstrumentDataRequest is to be used but, there’s an easier workaround for this example indicator. Remember, that the Bars.Request property returns the reference to the object of this structure and its fields are filled in accordance with the main data series settings. So in this example, several parameters should be changed while the rest parameters can be left unchanged.
<syntaxhighlightlang="csharp">
InstrumentDataRequest Request = Bars.Request;
Request.Subscribe2RT = true;
'''5.''' Specify the request range. Here the bars back range is specified. Number of bars back in this example is equal to the number of all complete bars on the {{color|blue|main data series}} chart.
<syntaxhighlightlang="csharp">
DataRequest _DataRequest = new DataRequest();
_DataRequest.RequestType = DataRequestType.BarsBack;
For EUR/JPY the request will be as follows:
<syntaxhighlightlang="csharp">
Request.QuoteField = RequestQuoteField.Ask;
Request.Symbol = "EUR/JPY";
</syntaxhighlight>
For EUR/AUD the request will be as follows:
<syntaxhighlightlang="csharp">
Request.QuoteField = RequestQuoteField.Bid;
Request.Symbol = "EUR/AUD";
'''7.''' Before requesting the data, it is necessary to declare the call-back function that will be used for receiving the data into study:
<syntaxhighlightlang="csharp">
public void OnData(IDataLoaderResult Result){ }
</syntaxhighlight>
'''8.''' Having combined all of the above criteria, the following StartCalc() function can be retrieved. It will perform the creating of the requests and the requests themselves:
<syntaxhighlightlang="csharp"> protected override void StartCalc() { InstrumentDataRequest Request = Bars.Request; Request.Subscribe2RT = true; DataRequest _DataRequest = new DataRequest(); _DataRequest.RequestType = DataRequestType.BarsBack; _DataRequest.Count = Bars.FullSymbolData.Count; Request.Range = _DataRequest;
Request.QuoteField = RequestQuoteField.Ask; Request.Symbol = "EUR/JPY";
DataLoader.BeginLoadData(Request, OnData, null); Request.QuoteField = RequestQuoteField.Bid; Request.Symbol = "EUR/AUD";
DataLoader.BeginLoadData(Request, OnData, null); }
</syntaxhighlight>
'''9.''' Next step is to process the data. Accumulation logic should be done in the OnData() function.
First, add a reference to the System.Collections.Generic namespace:
<syntaxhighlight lang="csharp">using System.Collections.Generic;</syntaxhighlight>
Container Second, container fields where data will be stored should be added into indicator class: <syntaxhighlightlang="csharp"> private List<Bar> m_EURJPY = new List<Bar>(); private List<Bar> m_EURAUD = new List<Bar>();
</syntaxhighlight>
The '''IDataLoaderResult''' interface provides access to the call-back function of OnData()
The data processing function can be written in the following manner:
<syntaxhighlightlang="csharp">private void ProcessData(List<Bar> Container, IDataLoaderResult Result) { switch (Result.Event) { case DataLoadedEvent.History: { Container.AddRange(Result.Data); break;
}
case DataLoadedEvent.RTNewBar: { if (Result.RTData.HasValue) Container.Add(Result.RTData.Value); break;
}
}
}
</syntaxhighlight>
OnData function will be implemented the following:
<syntaxhighlightlang="csharp"> public void OnData(IDataLoaderResult Result){ if (Result.Request.Symbol == "EUR/JPY") { ProcessData(m_EURJPY, Result); }
if (Result.Request.Symbol == "EUR/AUD") { ProcessData(m_EURAUD, Result); }
}
}
</syntaxhighlight>
'''10.''' The final step is to make the plotting of the spreads once all of the required information has been obtained:
<syntaxhighlightlang="csharp">protected override void CalcBar(){ if (Bars.Status == EBarState.Close){ int index = Math.Min(Bars.CurrentBar, Math.Min(m_EURAUD.Count - 1, m_EURJPY.Count - 1)); if(index > 0){ m_spreadPlot.Set(Math.Abs(m_EURAUD[index].Close - m_EURJPY[index].Close));
}
}
}
</syntaxhighlight>
</div>
<syntaxhighlightlang="csharp"> public struct BarPriceLevels { public Bar Bar { get; set; } public PriceLevel[] Levels { get; set; } public int Index { get; set; } public DateTime Time{ get; set; } public uint TickID { get; set; } }
</syntaxhighlight>
[[Category:4. Understanding PowerLanguage .NET Programming Giude]]

Navigation menu