抄代码 BarFactory
using System;
using System.Drawing;
using System.ComponentModel;
using SmartQuant;
using SmartQuant.FIX;
using SmartQuant.Data;
using SmartQuant.FIXData;
using SmartQuant.Trading;
using SmartQuant.Series;
using SmartQuant.Optimization;
using SmartQuant.Indicators;
using SmartQuant.Instruments;
using SmartQuant.Execution;
using Fohjin.DataFilter;
[StrategyComponent("{7b92e958-576b-485e-b10f-edf111308b01}", ComponentType.ATSComponent , Name="Fohjin_Real_Time_Data_Filtering", Description="Fohjin Real Time Data Filtering")]
public class Fohjin_Real_Time_Data_Filtering : ATSComponent
{
RealTimeDataFilter RTDF;
SMA sma_1;
SMA sma_2;
public override void Init()
{
RTDF = new Fohjin.DataFilter.RealTimeDataFilter(Instrument);
RTDF.SaveCreatedBars = true;
RTDF.BarFactoryItems.Add(BarType.Time, 5, true);
RTDF.BarFactoryItems.Add(BarType.Time, 60, true);
RTDF.NewBar += new Fohjin.DataFilter.ATSBarEventHandler(OnBar);
RTDF.BarSeriesList = Bars;
BarSeries series1 = Bars[Instrument, BarType.Time, 60];
BarSeries series2 = Bars[Instrument, BarType.Time, 5];
sma_1 = new SMA(series1, 14, Color.Blue);
Draw(sma_1, 0);
sma_2 = new SMA(series2, 13, Color.Red);
Draw(sma_2, 0);
}
public override void OnBar(Bar bar)
{
//Console.WriteLine("Bar "+ bar.Size +" "+ Instrument.ToString());
}
public override void OnTrade(Trade trade)
{
if (RTDF.OnNewTrade(trade)) // Use the trade or not
{
}
}
}
sing System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using SmartQuant.FIX;
using SmartQuant.Data;
using SmartQuant.Series;
using SmartQuant.Trading;
using SmartQuant.Charting;
using SmartQuant.Providers;
using SmartQuant.Instruments;
using Fohjin.DataFilter;
namespace Fohjin.DataFilter
{
#region Delegates
public delegate void ATSBarEventHandler(Bar bar);
#endregion
public class RealTimeDataFilter
{
#region Public Events
public event ATSBarEventHandler NewBar;
public event ATSBarEventHandler NewBarOpen;
#endregion
#region Private Variables
BarSeriesList _BarSeriesList;
BarFactory _BarFactory;
bool _SaveCreatedBars;
IFIXInstrument _Instrument;
Filter _Filter;
#endregion
#region Properties
public BarSeriesList BarSeriesList
{
get
{
return (_BarSeriesList);
}
set
{
_BarSeriesList = value;
}
}
public BarFactoryItemList BarFactoryItems
{
get
{
return (_BarFactory.Items);
}
}
public BarFactoryInput BarFactoryInput
{
get
{
return (_BarFactory.Input);
}
set
{
_BarFactory.Input = value;
}
}
public bool BarFactoryEnabled
{
get
{
return (_BarFactory.Enabled);
}
set
{
_BarFactory.Enabled = value;
}
}
public bool SaveCreatedBars
{
get
{
return (_SaveCreatedBars);
}
set
{
_SaveCreatedBars = value;
}
}
#endregion
#region Public Functions
public RealTimeDataFilter(IFIXInstrument instrument)
{
_Instrument = instrument;
_BarFactory = new BarFactory();
_BarFactory.Enabled = true;
_BarFactory.Input = BarFactoryInput.Trade;
_BarFactory.Items.Add(BarType.Time, 60, true);
_BarFactory.NewBar += new BarEventHandler(_BarFactory_NewBar);
_BarFactory.NewBarOpen += new BarEventHandler(_BarFactory_NewBarOpen);
_BarFactory.NewBarSlice += new BarSliceEventHandler(_BarFactory_NewBarSlice);
_SaveCreatedBars = true;
_Filter = new Filter();
_Filter.UsePreMarketData = false;
_Filter.UseAfterMarketdata = false;
_Filter.BeginTradeDayTime = "01:30:00 AM";
_Filter.EndTradeDayTime = "08:00:00 AM";
}
public bool OnNewTrade(Trade trade)
{
bool UseNewTrade = _Filter.ValidateTrade(trade);
if (UseNewTrade)
{
_BarFactory.OnNewTrade(_Instrument, trade);
}
return (UseNewTrade);
}
public bool OnNewQuote(Quote quote)
{
bool UseNewQuote = true;
// DoTo Actual filtering
if (UseNewQuote)
{
_BarFactory.OnNewQuote(_Instrument, quote);
}
return (UseNewQuote);
}
#endregion
#region Private BarFactory Event Handlers
private void _BarFactory_NewBar(object sender, BarEventArgs args)
{
string series = args.Instrument.ToString() + ".Bar." + args.Bar.BarType + "." + args.Bar.Size;
IDataSeries DataSeries;
DataSeries = DataManager.Server.GetDataSeries(series);
if (DataSeries == null)
{
DataSeries = DataManager.Server.AddDataSeries(series);
}
if (DataSeries.Count <= 0 || DataSeries.LastDateTime.Ticks < args.Bar.DateTime.Ticks)
{
if (_SaveCreatedBars)
{
DataSeries.Add(args.Bar.DateTime, args.Bar);
}
BarSeries TmpBarSeries = _BarSeriesList[(Instrument)args.Instrument, args.Bar.BarType, args.Bar.Size];
if (TmpBarSeries.Count <= 0 || TmpBarSeries.LastDateTime.Ticks < args.Bar.DateTime.Ticks)
{
TmpBarSeries.Add(args.Bar);
}
if (NewBar != null)
{
NewBar(args.Bar);
}
}
}
private void _BarFactory_NewBarOpen(object sender, BarEventArgs args)
{
if (NewBarOpen != null)
{
NewBarOpen(args.Bar);
}
}
private void _BarFactory_NewBarSlice(object sender, BarSliceEventArgs args)
{
}
#endregion
}
}
using System.Drawing;
using System.ComponentModel;
using SmartQuant;
using SmartQuant.FIX;
using SmartQuant.Data;
using SmartQuant.FIXData;
using SmartQuant.Trading;
using SmartQuant.Series;
using SmartQuant.Optimization;
using SmartQuant.Indicators;
using SmartQuant.Instruments;
using SmartQuant.Execution;
using Fohjin.DataFilter;
[StrategyComponent("{7b92e958-576b-485e-b10f-edf111308b01}", ComponentType.ATSComponent , Name="Fohjin_Real_Time_Data_Filtering", Description="Fohjin Real Time Data Filtering")]
public class Fohjin_Real_Time_Data_Filtering : ATSComponent
{
RealTimeDataFilter RTDF;
SMA sma_1;
SMA sma_2;
public override void Init()
{
RTDF = new Fohjin.DataFilter.RealTimeDataFilter(Instrument);
RTDF.SaveCreatedBars = true;
RTDF.BarFactoryItems.Add(BarType.Time, 5, true);
RTDF.BarFactoryItems.Add(BarType.Time, 60, true);
RTDF.NewBar += new Fohjin.DataFilter.ATSBarEventHandler(OnBar);
RTDF.BarSeriesList = Bars;
BarSeries series1 = Bars[Instrument, BarType.Time, 60];
BarSeries series2 = Bars[Instrument, BarType.Time, 5];
sma_1 = new SMA(series1, 14, Color.Blue);
Draw(sma_1, 0);
sma_2 = new SMA(series2, 13, Color.Red);
Draw(sma_2, 0);
}
public override void OnBar(Bar bar)
{
//Console.WriteLine("Bar "+ bar.Size +" "+ Instrument.ToString());
}
public override void OnTrade(Trade trade)
{
if (RTDF.OnNewTrade(trade)) // Use the trade or not
{
}
}
}
sing System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using SmartQuant.FIX;
using SmartQuant.Data;
using SmartQuant.Series;
using SmartQuant.Trading;
using SmartQuant.Charting;
using SmartQuant.Providers;
using SmartQuant.Instruments;
using Fohjin.DataFilter;
namespace Fohjin.DataFilter
{
#region Delegates
public delegate void ATSBarEventHandler(Bar bar);
#endregion
public class RealTimeDataFilter
{
#region Public Events
public event ATSBarEventHandler NewBar;
public event ATSBarEventHandler NewBarOpen;
#endregion
#region Private Variables
BarSeriesList _BarSeriesList;
BarFactory _BarFactory;
bool _SaveCreatedBars;
IFIXInstrument _Instrument;
Filter _Filter;
#endregion
#region Properties
public BarSeriesList BarSeriesList
{
get
{
return (_BarSeriesList);
}
set
{
_BarSeriesList = value;
}
}
public BarFactoryItemList BarFactoryItems
{
get
{
return (_BarFactory.Items);
}
}
public BarFactoryInput BarFactoryInput
{
get
{
return (_BarFactory.Input);
}
set
{
_BarFactory.Input = value;
}
}
public bool BarFactoryEnabled
{
get
{
return (_BarFactory.Enabled);
}
set
{
_BarFactory.Enabled = value;
}
}
public bool SaveCreatedBars
{
get
{
return (_SaveCreatedBars);
}
set
{
_SaveCreatedBars = value;
}
}
#endregion
#region Public Functions
public RealTimeDataFilter(IFIXInstrument instrument)
{
_Instrument = instrument;
_BarFactory = new BarFactory();
_BarFactory.Enabled = true;
_BarFactory.Input = BarFactoryInput.Trade;
_BarFactory.Items.Add(BarType.Time, 60, true);
_BarFactory.NewBar += new BarEventHandler(_BarFactory_NewBar);
_BarFactory.NewBarOpen += new BarEventHandler(_BarFactory_NewBarOpen);
_BarFactory.NewBarSlice += new BarSliceEventHandler(_BarFactory_NewBarSlice);
_SaveCreatedBars = true;
_Filter = new Filter();
_Filter.UsePreMarketData = false;
_Filter.UseAfterMarketdata = false;
_Filter.BeginTradeDayTime = "01:30:00 AM";
_Filter.EndTradeDayTime = "08:00:00 AM";
}
public bool OnNewTrade(Trade trade)
{
bool UseNewTrade = _Filter.ValidateTrade(trade);
if (UseNewTrade)
{
_BarFactory.OnNewTrade(_Instrument, trade);
}
return (UseNewTrade);
}
public bool OnNewQuote(Quote quote)
{
bool UseNewQuote = true;
// DoTo Actual filtering
if (UseNewQuote)
{
_BarFactory.OnNewQuote(_Instrument, quote);
}
return (UseNewQuote);
}
#endregion
#region Private BarFactory Event Handlers
private void _BarFactory_NewBar(object sender, BarEventArgs args)
{
string series = args.Instrument.ToString() + ".Bar." + args.Bar.BarType + "." + args.Bar.Size;
IDataSeries DataSeries;
DataSeries = DataManager.Server.GetDataSeries(series);
if (DataSeries == null)
{
DataSeries = DataManager.Server.AddDataSeries(series);
}
if (DataSeries.Count <= 0 || DataSeries.LastDateTime.Ticks < args.Bar.DateTime.Ticks)
{
if (_SaveCreatedBars)
{
DataSeries.Add(args.Bar.DateTime, args.Bar);
}
BarSeries TmpBarSeries = _BarSeriesList[(Instrument)args.Instrument, args.Bar.BarType, args.Bar.Size];
if (TmpBarSeries.Count <= 0 || TmpBarSeries.LastDateTime.Ticks < args.Bar.DateTime.Ticks)
{
TmpBarSeries.Add(args.Bar);
}
if (NewBar != null)
{
NewBar(args.Bar);
}
}
}
private void _BarFactory_NewBarOpen(object sender, BarEventArgs args)
{
if (NewBarOpen != null)
{
NewBarOpen(args.Bar);
}
}
private void _BarFactory_NewBarSlice(object sender, BarSliceEventArgs args)
{
}
#endregion
}
}