-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathDataMaker_code.cs
More file actions
101 lines (90 loc) · 3.46 KB
/
Copy pathDataMaker_code.cs
File metadata and controls
101 lines (90 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Drawing;
using OpenQuant.API;
using OpenQuant.API.Indicators;
namespace QuantBox.OQ.Demo.Data
{
/// <summary>
/// 通过策略运行的方式来生成价差序列
/// </summary>
public class DataMaker_code : Strategy
{
static Instrument Instrument1;
static Instrument Instrument2;
Instrument Instrument3 = InstrumentManager.Instruments["IF1305 - IF1306"];
long barSize;
public override void OnStrategyStart()
{
if (Instrument1 == null)
{
Instrument1 = Instrument;
}
else if (Instrument2 == null)
{
Instrument2 = Instrument;
}
barSize = long.MaxValue;
foreach (BarRequest barRequest in DataRequests.BarRequests)
{
barSize = Math.Min(barSize, barRequest.BarSize);
}
Console.WriteLine("barSize = {0}", barSize);
}
public override void OnStrategyStop()
{
Instrument1 = null;
Instrument2 = null;
}
public override void OnTrade(Trade trade)
{
// 只用第二个合约的生成,数量约为第二个合约的Trade数
// 假如第一个是IF1309,第二个是399300.SZ,这下生成的就6秒一次了
// 按自己需求调
if (Instrument == Instrument2)
{
if (Instrument1.Trade != null
&& Instrument2.Trade != null)
{
double Price = Instrument1.Trade.Price - Instrument2.Trade.Price;
int Size = Math.Min(Instrument1.Trade.Size, Instrument2.Trade.Size);
Trade t = new Trade(Clock.Now, Price, Size);
DataManager.Add(Instrument3, t);
}
}
}
public override void OnQuote(Quote quote)
{
// 只要有报价就会生成,数量约为两个合约Quote之和
//if(Instrument == Instrument2)
{
if (Instrument1.Quote != null
&& Instrument2.Quote != null)
{
double Ask = Instrument1.Quote.Ask - Instrument2.Quote.Bid;
int AskSize = Math.Min(Instrument1.Quote.AskSize, Instrument2.Quote.BidSize);
double Bid = Instrument1.Quote.Bid - Instrument2.Quote.Ask;
int BidSize = Math.Min(Instrument1.Quote.BidSize, Instrument2.Quote.AskSize);
Quote q = new Quote(Clock.Now, Bid, BidSize, Ask, AskSize);
DataManager.Add(Instrument3, q);
}
}
}
public override void OnBarSlice(long size)
{
// 为了保证采样间隔一样,用户按自己的需求改
if (size != barSize)
{
return;
}
// 如果添加了两个合约就会触发两次,只选后面一次保存
if (Instrument == Instrument2)
{
// 本想保存成Bar,细想没必要,保存了Trade,用户自己手工压缩成Bar就成
double Price = Instrument1.Bar.Close - Instrument2.Bar.Close;
Trade t = new Trade(Clock.Now, Price, 0);
// 注释了。在前面的OnTrade可以生成更细致的Trade
//DataManager.Add(Instrument3,t);
}
}
}
}