可以把传统行情软件的指标移过来吗?
可以的,并且搬到这边来也是一件很容易的事情,我们的股票自动交易软件已经有提供了一些基础的函数。
到目前为止我们股票自动交易软件提供的函数有
- MA平均
- EMA滑动平均
- WMA加权平均
- SMA移动平均
- HHV某周期内最高价
- LLV某周期最低价
- CROSS穿越。
我们软件实现的EMA是这样的
public static List<float> EMA(this List<float> input,int n)
{
float[] list = new float[input.Count];
list[0] = input[0];
float x;
float y;
float yLast;
for (int i = 1;i < input.Count;i++)
{
yLast = list[i - 1];
x = input[i];
y = (x * 2 + (n - 1) * yLast) / (n + 1);
list[i] = y;
}
return list.ToList();
}
我们直接根据原来的值返回一个有同样位数的返回值。这样软件同样可以非常方便的去判断。最后期的一个值是不是满足条件了。
现在来看一下如何写一个MACD的函数
DIFF : EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);
DEA : EMA(DIFF,M);
MACD : 2*(DIFF-DEA), COLORSTICK;
MACD : 2*(DIFF-DEA), COLORSTICK;
上面的是Macd在大智慧中的函数。
先看一下我们软件中的代码 我自己先定义了一个MacdInfo的类
public class MacdInfo
{
public List<float> Diff
{ get; set; }
public class MacdInfo
{
public List<float> Diff
{ get; set; }
public List<float> DEA
{ get; set; }
{ get; set; }
public List<float> MACD
{ get; set; }
}
Diff DEA MACD上面的三个我们都有定义了。再看一下计算类是什么实现的
public class MACD
{
/// <summary>
/// DIFF : EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);
/// DEA : EMA(DIFF,M);
/// MACD : 2*(DIFF-DEA), COLORSTICK;
/// </summary>
/// <param name="list"></param>
/// <param name="?"></param>
/// <returns></returns>
public MacdInfo Calculate(List<StockLog> list,int s,int l,int m)
{
var closeList = list.Select(it => it.Close).ToList();//先得到所有的Close的值
var shortList = closeList.EMA(s);//算出短期的滑动平均
var longList = closeList.EMA(l);//算出长期的滑动平均
{ get; set; }
}
Diff DEA MACD上面的三个我们都有定义了。再看一下计算类是什么实现的
public class MACD
{
/// <summary>
/// DIFF : EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);
/// DEA : EMA(DIFF,M);
/// MACD : 2*(DIFF-DEA), COLORSTICK;
/// </summary>
/// <param name="list"></param>
/// <param name="?"></param>
/// <returns></returns>
public MacdInfo Calculate(List<StockLog> list,int s,int l,int m)
{
var closeList = list.Select(it => it.Close).ToList();//先得到所有的Close的值
var shortList = closeList.EMA(s);//算出短期的滑动平均
var longList = closeList.EMA(l);//算出长期的滑动平均
List<float> diff = new List<float>();
for (int i = 0;i < shortList.Count;i++)//使用循环的方法算出每一个周期的diff
{
diff.Add(shortList[i] - longList[i]);
}
for (int i = 0;i < shortList.Count;i++)//使用循环的方法算出每一个周期的diff
{
diff.Add(shortList[i] - longList[i]);
}
var dea = diff.EMA(m);//跟据diff算出dea
List<float> macd = new List<float>();
for (int i = 0;i < diff.Count;i++)//算出macd
{
macd.Add(2 * (diff[i] - dea[i]));
}
MacdInfo info = new MacdInfo();
info.DEA = dea;
info.Diff = diff;
info.MACD = macd;
List<float> macd = new List<float>();
for (int i = 0;i < diff.Count;i++)//算出macd
{
macd.Add(2 * (diff[i] - dea[i]));
}
MacdInfo info = new MacdInfo();
info.DEA = dea;
info.Diff = diff;
info.MACD = macd;
return info;
}
}
}
}
///StockLog是我们的股票K线记录类
public class StockLog
{
public string StockCode
{ get; set; }
public class StockLog
{
public string StockCode
{ get; set; }
public DateTime Date
{ get; set; }
{ get; set; }
public float Open
{ get; set; }
{ get; set; }
public float Low
{ get; set; }
{ get; set; }
public float Close
{ get; set; }
{ get; set; }
public float High
{ get; set; }
{ get; set; }
///新浪不支持啊
//public decimal Amount
//{ get; set; }
//public decimal Amount
//{ get; set; }
public float Volume
{ get; set; }
{ get; set; }
public StockLog Clone()
{
return this.MemberwiseClone() as StockLog;
}
public string ToChartTip()
{
return string.Format("高:{0}\r\n低:{1}\r\n开:{2}\r\n收:{3}\r\n日期:{4}", High.ToString("f3"), Low.ToString("f3"), Open.ToString("f3"), Close.ToString("f3"),Date.ToShortDateString());
}
}
{
return string.Format("高:{0}\r\n低:{1}\r\n开:{2}\r\n收:{3}\r\n日期:{4}", High.ToString("f3"), Low.ToString("f3"), Open.ToString("f3"), Close.ToString("f3"),Date.ToShortDateString());
}
}
这样我们就完成了一个macd的指标了,如果把这个指标变成可交易策略。在后续的帮助里会继续介绍到。