C#扩展
来自tradeStar帮助系统
嵌入C#脚本和嵌入 VBScript 脚本类似.
嵌入 VBScript 使用<% %>
嵌入 C#多加一个# 就是 <%# %>
C#脚本有三种运行模式
模式1
#MAINCHART
涨停价:C;
<%#
float[] ztj = (float[])GetVarData("涨停价");
for (int i = 0; i < ztj.Length; i++)
ztj[i] = ztj[i] * (float)1.1;
SetVarData("涨停价", ztj);
%>
这是极简模式,通过GetVarData和SetVarData传入和传出数据。
模式2:
#MAINCHART
涨停价:C;
<%#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fox.api;
namespace N{
// 从 fox.api.Formula 继承 这样才能用 GetVarData, SetVarData
// 完整模式,需要定义命名空间,类,还必须定义个 Execute 函数作为入口点
public class Class1 : Formula{
public void Execute(){
float[] ztj = (float[])GetVarData("涨停价");
for (int i = 0; i < ztj.Length; i++)
ztj[i] = CalcZTJ(ztj[i]) ;
SetVarData("涨停价", ztj);
}
public float CalcZTJ(float fPrice){
return fPrice*(float)1.1;
}
}
}
%>
模式2必须定义命名空间N、类Class1
其中,类Class1必须继承fox.api.Formula。
Formula类预定义了入口接口Execute。
用户实现Excute,公式在运行时会自动调用Execute。
这种模式的好处是可以实现较为复杂的功能,如上例所示,可以在脚本内部定义子函数alcZTJ为Execute所使用。
模式3:
在脚本里定义函数,然后在公式里显式调用,就像调用dll里的函数一样
// 新dll接口调用规范只能在逐根模式下运行,因此要RUN_BY_BAR
// 此公式适用于日线
#MainChart
#RUN_BY_BAR
#NoDefaultOutPut
// 这个函数声明表示 嵌入脚本里面 命名空间N中类名为Class1的成员函数my_ma
extern void N.Class1.my_ma(NumericSeries resultArray, NumericSeries array1, int n, int barpos);
//C#脚本实现函数my_ma
<%#
namespace N
{
public class Class1
{
public void my_ma(ref float[] resultArray, float[] array, int n, int barpos)
{
int nK = barpos - 1;
if (barpos >= n)
{
float sum = 0;
for (int i = 0; i < n; i++)
sum += array[nK - i];
resultArray[nK] = sum / n;
}
else
resultArray[nK] = float.NaN;
}
}
}
%>
ma1:0;
my_ma(ma1, close, 5, barpos);//注意调用my_ma必须放在脚本后面调用
如上例所示,在脚本中定义了函数my_ma,然后在脚本之外调用