Daily Open, Hourly Open
Hi!
I'm building the Trading System.
Sorry for the trivial question, what is the best way to know on quote
the daily and hourly open. And open price of specific hour?
public override void OnQuote()
{
Instrument inst = Instruments.GetInstrument("EUR/USD");
double dailyOpen = 0;
double hourlyOpen = 0;
BarData dailyBars = GetHistoricalData(new HistoricalDataRequest(inst, Period.Day)) as BarData;
BarData hourlyBars = GetHistoricalData(new HistoricalDataRequest(inst, Period.Hour)) as BarData;
if (dailyBars != null)
{
dailyOpen = dailyBars.Open();
}
if (hourlyBars != null)
{
hourlyOpen = hourlyBars.Open();
}
}
There the dailyOpen = dailyBars.Open(); is giving error:
A first chance exception of type 'System.IndexOutOfRangeException' occurred in "TEST2", Line 54:
Cannot get the value from the empty data
Can anyone help with such problem?
With regards
Peep Palts
Hi, Peep Palts!
Firstly, you forget to download both HistoricalData's. Please, check out this reference HistoricalData.Load(DateTime from, DateTime to)
The code
BarData dailyBars = GetHistoricalData(new HistoricalDataRequest(inst, Period.Day)) as BarData;
only determinates in which aggregation(daily/hourly) you wish to pull trading data.
Also you are free to join in skype chat https://join.skype.com/lolJj7qEVTNH for resolving PTMC C# API questions.
Working example for getting hourly, daily open:
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Drawing; using PTLRuntime.NETScript; namespace TEST2 { /// <summary> /// TEST2 /// /// </summary> public class TEST2 : NETStrategy { public TEST2() : base() { #region Initialization base.Author = ""; base.Comments = ""; base.Company = ""; base.Copyrights = ""; base.DateOfCreation = "07.06.2017"; base.ExpirationDate = 0; base.Version = ""; base.Password = "66b4a6416f59370e942d353f08a9ae36"; base.ProjectName = "TEST2"; #endregion } Instrument inst; BarData dailyBars, hourlyBars; double dailyOpen, hourlyOpen; bool hisoryLoaded; /// <summary> /// This function will be called after creating /// </summary> public override void Init() { inst = Instruments.GetInstrument("GBP/USD"); dailyBars = GetHistoricalData(new HistoricalDataRequest(inst, Period.Day)) as BarData; hourlyBars = GetHistoricalData(new HistoricalDataRequest(inst, Period.Hour)) as BarData; } /// <summary> /// Entry point. This function is called when new quote comes /// </summary> public override void OnQuote() { // instrument doesn't exist if (inst == null) return; var curBarTime = CurrentData.Time(); // loading data for each timeframe - only once! if (!historyLoaded) { dailyBars.Load(curBarTime); hourlyBars.Load(curBarTime); historyLoaded = true; // skip one quote - bar doesn't exist yet (casting to BarData) return; } dailyOpen = dailyBars.Open(); hourlyOpen = hourlyBars.Open(); Print(string.Format ("{0} {1} {2} {3} {4}", curBarTime, hourlyOpen, dailyOpen, Bid, Ask)); } /// <summary> /// This function will be called before removing /// </summary> public override void Complete() { // free resources inst = null; dailyBars = null; hourlyBars = null; } } }