StraTrader
Search:

Creating custom DataSource

All you have to do is to create DLL that implements IDataSource and IDataStream interfaces and put this DLL into StraTrader binary folder.

The following is the partial code of such DLL. Full version is available in StraTrader installation.

This is the DataSource class. It has 3 tickers with symbols "one", "two" and "three". It creates custom dialog boxes that allow user to assign tickers to session. It supports both historical and realtime data.

000: using System;
001: using System.Collections.Generic;
002: using System.Text;
003: using System.Threading;
004: using System.Runtime.Serialization;
005: using StraTrader;
006:
007: namespace SampleDataSource
008: {
009:     [RealtimeDataSource(true)]
010:     public class FakeDataSource : IDataSource
011:     {
012:         private Thread m_Thread;
013:         private List<FakeDataStream> m_Streams;
014:         private IEngine m_Engine;
015:
016:         public FakeDataSource(IEngine Engine, SerializationInfo info)
017:         {
018:             m_Engine = Engine;
019:             m_Streams = new List<FakeDataStream>();
020:
021:             CreateStream("ONE", 50);
022:             CreateStream("TWO", 100);
023:             CreateStream("THREE", 150);
024:         }
025:
026:         public void GetObjectData(SerializationInfo info, StreamingContext context)
027:         {
028:
029:         }
030:         public bool SupportsRealtime
031:         {
032:             get
033:             {
034:                 return true;
035:             }
036:         }
037:         public bool SupportsHistorical
038:         {
039:             get
040:             {
041:                 return true;
042:             }
043:         }
044:         public DateTime Now
045:         {
046:             get
047:             {
048:                 return DateTime.Now;
049:             }
050:         }
051:         public PropertyPage ProjectPropertiesControl
052:         {
053:             get
054:             {
055:                 return new SettingsPropertyPage(this);
056:             }
057:         }
058:
059:         public IDataStream BrowseOne()
060:         {
061:             dlgBrowseOne dlg = new dlgBrowseOne(this);
062:             if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) return null;
063:             return dlg.SelectedStream;
064:         }
065:         public IDataStream GetStream(string FileName)
066:         {
067:             foreach (FakeDataStream stream in m_Streams)
068:                 if (stream.FileName == FileName)
069:                     return stream;
070:
071:             return null;
072:         }
073:         public IDataStream GetStream(TickerID id)
074:         {
075:             foreach (FakeDataStream stream in m_Streams)
076:                 if (stream.ID == id)
077:                     return stream;
078:
079:             return null;
080:         }
081:         public PropertyPage GetDataPropertyPage(List<IDataStream> DataStreams)
082:         {
083:             return new DataPropertyPage(this, DataStreams);
084:         }
085:
086:         public void SwitchToRealtime()
087:         {
088:             m_Thread = new Thread(ThreadFunc);
089:             m_Thread.Start();
090:         }
091:         public void Dispose()
092:         {
093:         }
094:
095:
096:
097:         private void ThreadFunc()
098:         {
099:             List<IDataStream> projectStreams = new List<IDataStream>();
100:             foreach (ITicker ticker in m_Engine.Project.Tickers)
101:                 projectStreams.Add(ticker.DataStream);
102:
103:
104:
105:             while (true)
106:             {
107:                 foreach (FakeDataStream stream in m_Streams)
108:                 {
109:                     if (projectStreams.Contains(stream))
110:                     {
111:                         stream.NextRealtime();
112:                         Thread.Sleep(100);
113:                     }
114:                 }
115:             }
116:         }
117:         internal List<FakeDataStream> AllStreams
118:         {
119:             get
120:             {
121:                 return m_Streams;
122:             }
123:         }
124:         private void CreateStream(string symbol, double average)
125:         {
126:             TickerID ID1 = new TickerID(SecurityType.Stock, symbol, """"null);
127:
128:             TickerInfo Info1 = new TickerInfo();
129:             Info1.StartTime = new TimeSpan(10, 0, 0);
130:             Info1.EndTime = new TimeSpan(15, 0, 0);
131:             Info1.LotSize = 1;
132:             Info1.TickSize = 0.01;
133:             Info1.TickPrice = 0.01;
134:
135:             FakeGen FakeGen1 = new FakeGen(average, 1, 10000, 5, 20000, 10, 100000, 20);
136:
137:             m_Streams.Add(new FakeDataStream(this, ID1, Info1, FakeGen1));
138:         }
139:     }
140: }

This is the DataStream class. It generates random sequence of ticks.

000: using System;
001: using System.Collections.Generic;
002: using System.Text;
003: using System.Threading;
004: using StraTrader;
005:
006: namespace SampleDataSource
007: {
008:     public class FakeDataStream : IDataStream
009:     {
010:         private FakeDataSource m_DataSource;
011:         private TickerID m_TickerID;
012:         private TickerInfo m_Info;
013:         private DataRecord m_CurRecord;
014:         private ManualResetEvent m_Event;
015:         private TimeFrame m_TimeFrame;
016:         private FakeGen m_Gen;
017:
018:         internal FakeDataStream(FakeDataSource dataSource, TickerID tickerID, TickerInfo tickerInfo, FakeGen gen)
019:         {
020:             m_DataSource = dataSource;
021:             m_TickerID = tickerID;
022:             m_Info = tickerInfo;
023:             m_Gen = gen;
024:             m_Event = new ManualResetEvent(false);
025:
026:             m_TimeFrame = new TimeFrame(TimeFrameInterval.Minute, 1);
027:         }
028:
029:         public TickerID ID
030:         {
031:             get
032:             {
033:                 return m_TickerID;
034:             }
035:         }
036:         public TickerInfo Info
037:         {
038:             get
039:             {
040:                 return m_Info;
041:             }
042:         }
043:         public string FileName
044:         {
045:             get
046:             {
047:                 return "fake:
048:             }
049:         }
050:         public DataRecord CurRecord
051:         {
052:             get
053:             {
054:                 return m_CurRecord;
055:             }
056:         }
057:         public IDataSource DataSource
058:         {
059:             get
060:             {
061:                 return m_DataSource;
062:             }
063:         }
064:
065:         public DateTime FirstDateTime
066:         {
067:             get
068:             {
069:                 return new DateTime(2000, 1, 1);
070:             }
071:         }
072:         public DateTime LastDateTime
073:         {
074:             get
075:             {
076:                 return DateTime.Now;
077:             }
078:         }
079:         public TimeFrame TimeFrame
080:         {
081:             get
082:             {
083:                 return m_TimeFrame;
084:             }
085:         }
086:         public bool EOF
087:         {
088:             get
089:             {
090:                 return m_CurRecord.DT >= DateTime.Now;
091:             }
092:         }
093:         public ManualResetEvent WaitEvent
094:         {
095:             get
096:             {
097:                 return m_Event;
098:             }
099:         }
100:
101:         public bool Init()
102:         {
103:             return true;
104:         }
105:
106:         public bool GoTo(DateTime DateTime)
107:         {
108:             if (m_CurRecord == null)
109:             {
110:                 m_CurRecord = new DataRecord();
111:             }
112:             else
113:             {
114:                 if (m_CurRecord.DT >= LastDateTime) return false;
115:                 if (m_CurRecord.DT <= FirstDateTime) return false;
116:             }
117:
118:             m_CurRecord.DT = DateTime;
119:             GenNext();
120:
121:             return true;
122:         }
123:         public bool MoveFirst()
124:         {
125:             m_CurRecord.DT = FirstDateTime;
126:             GenNext();
127:             return true;
128:         }
129:         public bool MoveLast()
130:         {
131:             m_CurRecord.DT = LastDateTime;
132:             GenNext();
133:             return true;
134:         }
135:         public bool MoveNext()
136:         {
137:             if (m_CurRecord.DT >= LastDateTime) return false;
138:
139:             m_CurRecord.DT += m_TimeFrame.ToTimeSpan();
140:             GenNext();
141:             return true;
142:         }
143:         public bool MovePrev()
144:         {
145:             if (m_CurRecord.DT <= FirstDateTime) return false;
146:
147:             m_CurRecord.DT += m_TimeFrame.ToTimeSpan();
148:             GenNext();
149:
150:             return true;
151:         }
152:         public void Dispose()
153:         {
154:         }
155:
156:         private void GenNext()
157:         {
158:             double op, hi, lo, cl;
159:
160:             m_Gen.Next(out op, out hi, out lo, out cl);
161:
162:             m_CurRecord.op = op;
163:             m_CurRecord.hi = hi;
164:             m_CurRecord.lo = lo;
165:             m_CurRecord.cl = cl;
166:         }
167:         internal void NextRealtime()
168:         {
169:             GenNext();
170:             m_CurRecord.DT = DateTime.Now;
171:             m_Event.Set();
172:         }
173:     }
174: }

About Us   Contact Us   Download   ©2007-2009 Mike Kramarenko in association with SmartProj.com