50ETF期权期货正向套利
Data
datac:"50ETF2012-C-2.700"; //看涨期权
dataf:"IH2012"; //标的期货
datax50etf:"510050"; //50ETF现价
Vars
Numeric New_F; //标的期货合约的最新价
Numeric New_X50ETF; //标的现货合约的最新价
Numeric N_C; //看涨期权的下单手数
Numeric N_F(1); //期货合约的下单手数
Numeric SellRemainPosition_C; //看涨期权空头可用持仓
Numeric BuyRemainPosition_F; //标的期货多头可用持仓
Global_Numeric FLGA; //交易开关
Begin
New_F = dataf.Price("New");//标的期货的最新价
New_X50ETF = datax50etf.Price("New");//标的现货的最新价
N_C = IntPart(((New_F*300) / New_X50ETF) / 10000);//根据标的期货1手价值计算对应期权等价值下单手数
SellRemainPosition_C = datac.F_SellRemainPosition();//看涨期权空头可用持仓
BuyRemainPosition_F = dataf.F_BuyRemainPosition();//标的期货多头可用持仓
If(New_X50ETF * 1000 > New_F && FLGA == 0)//当标的现货价格大于标的期货价格时
{
dataf.A_SendOrder(Enum_Buy,Enum_Entry,N_F,dataf.Price("Ask1"));//买入开仓1手期货合约
datac.A_SendOrder(Enum_Sell,Enum_Entry,N_C,dataC.Price("Bid1"));//卖出开仓等价值手数的虚值期权合约
FLGA = 1;//设置交易开关为1
}
If(New_X50ETF * 1000 < New_F && FLGA == 1 && SellRemainPosition_C > 0 && BuyRemainPosition_F > 0)//当标的现货价格小于标的期货价格,且看涨期权多头可用持仓大于0、标的期货多头可用持仓大于0时
{
dataf.A_SendOrder(Enum_Sell,Enum_Exit,N_F,dataf.Price("Bid1"));//卖出平仓全部期货持仓合约
datac.A_SendOrder(Enum_Buy,Enum_Exit,N_C,dataC.Price("Ask1"));//买入平仓全部期权持仓合约
FLGA = 0;//设置交易开关为0
}
End
欧式期权定价模型套利
Data
datac:"m2101-C-2850"; //看涨期权
datap:"m2101-P-2850"; //看跌期权
dataf:"m2101"; //标的期货
Params
Numeric R(1); //无风险收益率
Numeric N(5);
Vars
Numeric HistoryVolatility_C; //看涨期权的历史波动率
Numeric HistoryVolatility_P; //看跌期权的历史波动率
Numeric HistoryVolatility_F; //标的期货的价格波动率
Numeric New_C; //看涨期权的最新价
Numeric New_P; //看跌期权的最新价
Numeric New_F; //期货合约的最新价
Numeric ExercisePrice; //行权价
Numeric TheoreticalPrice_C; //看涨期权的理论价格
Numeric TheoreticalPrice_P; //看跌期权的理论价格
Numeric RTS; //距行权日剩余的天数
Numeric D1; //中间值
Numeric D2; //中间值
Numeric BuyRemainPosition_C; //看涨期权多头可用持仓
Numeric BuyRemainPosition_P; //看跌期权多头可用持仓
Numeric BuyRemainPosition_F; //标的期货多头可用持仓
Numeric SellRemainPosition_F; //标的期货空头可用持仓
Begin
HistoryVolatility_C = datac.Price("HistoricalVolatility");//取看涨期权的历史波动率
HistoryVolatility_P = datap.Price("HistoricalVolatility");//取看跌期权的历史波动率
New_F = dataf.Price("New");//取IF1701期货合约的最新价
ExercisePrice = datac.Price("StrikePrice");//取行权价
RTS = DateDiff(Date,datac.Price("ExpirationDate"));//距行权日到期剩余天数
HistoryVolatility_F = Std(Ln(Close/Close[1]), N) * Sqrt(252) ;//计算标的期货的价格波动率
D1 = (Ln(New_F / ExercisePrice) + (R/10 + 0.5 * Power(HistoryVolatility_F,2)) * RTS) / (HistoryVolatility_F * Power(RTS,0.5));//求期权理论价格的中间值
D2 = D1 - HistoryVolatility_F * Power(RTS,0.5);//求期权理论价格的中间值
TheoreticalPrice_C = New_F * NormDist(D1) - ExercisePrice * Exp(-1 * (R / 10) * RTS) * NormDist(D2);//根据B-S-M公式计算看涨期权理论价格
TheoreticalPrice_P = ExercisePrice * Exp(-1 * (R / 10) * RTS) * (1 - NormDist(D2)) - New_F * (1 - NormDist(D1));//根据B-S-M公式计算看跌期权理论价格
New_C = datac.Price("New");//看涨期权的最新价
New_P = datap.Price("New");//看跌期权的最新价
BuyRemainPosition_C = datac.F_BuyRemainPosition();//看涨期权多头可用持仓
BuyRemainPosition_P = datap.F_BuyRemainPosition();//看跌期权多头可用持仓
BuyRemainPosition_F = dataf.F_BuyRemainPosition();//标的期货多头可用持仓
SellRemainPosition_F = dataf.F_SellRemainPosition();//标的期货空头可用持仓
If((TimeDiff(Date,datac.Price("ExpirationDate")) <= 5 || TimeDiff(Date,datap.Price("ExpirationDate")) <= 5) && (GetGlobalVar(0) == 1 || GetGlobalVar(1) == 1))
//看涨期权或看跌期权行权日前五天
{
If(BuyRemainPosition_C > 0)//看涨期权有多头可用持仓
{
datac.A_SendOrder(Enum_sell,Enum_Exit,BuyRemainPosition_C,datac.Price("Bid1"));//对价卖平看涨期权可用多仓
}
If(BuyRemainPosition_P > 0)//看跌期权有多头可用持仓
{
datap.A_SendOrder(Enum_Sell,Enum_Exit,BuyRemainPosition_P,datap.Price("Bid1"));//对价卖平看跌期权可用多仓
}
If( SellRemainPosition_F > 0)//标的期货有空头可用持仓
{
dataf.A_SendOrder(Enum_Buy,Enum_Exit,BuyRemainPosition_F,dataf.Price("Ask1"));//对价买平标的期货可用空仓
}
If(BuyRemainPosition_F > 0)//标的期货有多头可用持仓
{
dataf.A_SendOrder(Enum_Sell,Enum_Exit,SellRemainPosition_F,dataf.Price("Bid1"));//对价卖平标的期货可用多仓
}
SetGlobalVar(0,0);
SetGlobalVar(1,0);
}
Else
{
If(New_C < TheoreticalPrice_C * 0.5 && datac.Price("Stdderiation") < HistoryVolatility_C && BuyRemainPosition_C == 0 && SellRemainPosition_F == 0 && GetGlobalVar(0) == 0)
//看涨期权的最新价低于看涨期权理论价格的50%,并且看涨期权的隐含波动率低于历史波动率,并且看涨期权没有多头持仓,对应标的期货合约没有空头持仓
{
datac.A_SendOrder(Enum_Buy,Enum_Entry,1,datac.Price("Ask1"));//对价买开看涨期权1手
dataf.A_SendOrder(Enum_Sell,Enum_Entry,1,dataf.Price("Bid1"));//对价卖开标的期货合约1手
SetGlobalVar(0,1);
}
If(TheoreticalPrice_P > New_P * 0.5 && datap.Price("Stdderiation") > HistoryVolatility_P && BuyRemainPosition_P == 0 && BuyRemainPosition_F == 0 && GetGlobalVar(1) == 0)
//看跌期权理论价格高于看跌期权最新价的50%,并且看跌期权隐含波动率高于历史波动率,并且看跌期权没有多头持仓,对应标的期货合约没有多头持仓
{
datap.A_SendOrder(Enum_Buy,Enum_Entry,1,datap.Price("Ask1"));//对价买开看跌期权1手
dataf.A_SendOrder(Enum_buy,Enum_Entry,1,dataf.Price("Ask1"));//对价买开标的期货合约1手
SetGlobalVar(1,1);
}
}
End
日历价差套利
Data
data1:"m2101-C-3000"; //定义近月合约
data2:"m2103-C-3000"; //定义远月合约
Params
Numeric Lots(2); //定义参数,下单手数,默认值为2
Numeric Length(5); //定义参数,周期,默认值为5
Vars
Numeric HistoryVolatility; //定义变量,取历史波动率
Numeric Stdderiation1; //定义变量,取近月合约的隐含波动率
Numeric Stdderiation2; //定义变量,取远月合约的隐含波动率
Numeric Cond; //定义变量,判断入场条件
Numeric SellRemainPosition; //定义变量,取空头可用持仓
Numeric BuyRemainPosition; //定义变量,取多头可用持仓
Begin
HistoryVolatility = Std(Ln(Close / Close[1]), Length) * Sqrt(252) ;//计算标的物历史波动率
Stdderiation1 = data1.Price("Stdderiation");//取近月合约的隐含波动率
Stdderiation2 = data2.Price("Stdderiation");//取远月合约的隐含波动率
Cond = Stdderiation1 - HistoryVolatility + Stdderiation1 - Stdderiation2;//计算隐含波动率的综合价差
If(Cond > 0 && GetGlobalVar(0) == 0)//当价差大于0并且没有持仓时建立日历价差组合
{
data1.A_SendOrder(Enum_Sell,Enum_Entry,Lots,data1.Price("Ask1"));//卖出近月期权合约
data2.A_SendOrder(Enum_Buy,Enum_Entry,Lots,data2.Price("Bid1")); //买入远月期权合约
SetGlobalVar(0,1);
}
If(GetGlobalVar(0) == 1 && DateDiff(Date,data1.Price("ExpirationDate")) <= 5)//当距行权日期小于等于5天时平仓
{
BuyRemainPosition = data2.F_BuyRemainPosition();//取远月合约的多头可用持仓
If(BuyRemainPosition > 0)//如果远月合约的多头可用持仓大于0
{
data2.A_SendOrder(Enum_Sell,Enum_Exit,BuyRemainPosition,data1.Price("Bid1")); //平多头可用持仓
}
SellRemainPosition= data1.F_SellRemainPosition();//取近月合约的空头可用持仓
If(SellRemainPosition > 0)//如果近月合约的空头可用持仓大于0
{
data1.A_SendOrder(Enum_Buy,Enum_Exit,SellRemainPosition,data1.Price("Ask1")); //平空头可用持仓
}
SetGlobalVar(0,0);
}
End
买卖权平价套利
Data
datac:"m2101-C-2850"; //定义看涨期权
datap:"m2101-P-2850"; //定义看跌期权
dataf:"m2101"; //定义期权标的物
Vars
Numeric New_C; //定义看涨期权最新价
Numeric New_P; //定义看跌期权最新价
Numeric New_F; //定义标的物最新价
Numeric OptionStrikePrice; //定义行权价
Numeric BuyRemainPosition_C; //定义看涨期权多头持仓
Numeric SellRemainPosition_C; //定义看涨期权空头持仓
Numeric BuyRemainPosition_P; //定义看跌期权多头持仓
Numeric SellRemainPosition_P; //定义看跌期权空头持仓
Global_NumericArray SKID_C[5000]; //卖看涨期权索引数组
Global_NumericArray BKID_C[5000]; //买看涨期权索引数组
Global_NumericArray SKID_P[5000]; //卖看跌期权索引数组
Global_NumericArray BKID_P[5000]; //买看跌期权索引数组
Global_NumericArray Time1[5000]; //卖看涨期权,买看跌期权时间数组
Global_NumericArray Time2[5000]; //买看涨期权,卖看跌期权时间数组
Global_Numeric i; //卖看涨期权,买看跌期权次数
Global_Numeric j; //买看涨期权,卖看跌期权次数
Global_Numeric Index; //定义全局变量用于撤单
Global_Numeric Coin; //定义全局变量控制卖看涨期权,买看跌期权
Global_Numeric Coin1; //定义全局变量控制买看涨期权,卖看跌期权
Numeric NumOrder; // 是否下单标志 0未下单 1下单
Begin
If(i >= 5000 || j >= 5000)//卖看涨期权,买看跌期权次数或者买看涨期权,卖看跌期权次数>=5000
{
Commentary("记录次数耗尽,请重新加载此模型");//输出记录次数耗尽,请重新加载此模型
}
Else
{
New_C = datac.Price("New");//看涨期权最新价
New_P = datap.Price("New");//看跌期权最新价
New_F = dataf.Price("New");//标的物最新价
OptionStrikePrice = datac.Price("StrikePrice");//行权价
BuyRemainPosition_C = datac.F_BuyRemainPosition();//看涨期权多头持仓
SellRemainPosition_C = datac.F_SellRemainPosition();//看涨期权空头持仓
BuyRemainPosition_P = datap.F_BuyRemainPosition();//看跌期权多头持仓
SellRemainPosition_P = datap.F_SellRemainPosition();//看跌期权空头持仓
If(DateDiff(CurrentDate,datac.Price("ExpirationDate")) <= 5 || DateDiff(CurrentDate,datap.Price("ExpirationDate")) <= 5)//看涨或者看跌期权距离行权日5日内
{
If(BuyRemainPosition_C > 0)//看涨期权多头持仓>0
{
datac.A_SendOrder(Enum_Sell,Enum_Exit,BuyRemainPosition_C,datac.Price( "Bid1"));//以买1价卖平看涨期权
}
If(BuyRemainPosition_P > 0)//看涨期权空头持仓>0
{
datac.A_SendOrder(Enum_Buy,Enum_Exit,BuyRemainPosition_P,datac.Price("Ask1"));//以卖一价买平看涨期权空头持仓
}
If(BuyRemainPosition_P > 0)//看跌期权多头持仓>0
{
datap.A_SendOrder(Enum_Sell,Enum_Exit,BuyRemainPosition_P,datap.Price( "Bid1"));//以买一价卖平看跌期权多头持仓
}
If(SellRemainPosition_P > 0)//看跌期权空头持仓>0
{
datap.A_SendOrder(Enum_Buy,Enum_Exit,SellRemainPosition_P,datap.Price( "Ask1"));//以卖一价买平看跌期权
}
}
Else If(New_C - New_P > New_F - OptionStrikePrice)//看涨期权最新价-看跌期权最新价>标的物最新价-行权价
{
If(BuyRemainPosition_C > 0)//看涨期权多头持仓>0
{
datac.A_SendOrder(Enum_sell,Enum_Exit,BuyRemainPosition_C,datac.Price( "Bid1"));//以看涨期权买一价卖平看涨期权多头持仓
Coin = 0;//Coin为0
}
If(SellRemainPosition_P > 0) //看跌期权空头持仓>0
{
datap.A_SendOrder(Enum_Buy,Enum_Exit,SellRemainPosition_P,datap.Price( "Ask1"));//以看跌期权卖一价买平看跌期权空头持仓
Coin = 0;//Coin为0
}
If(BuyRemainPosition_C == 0 && SellRemainPosition_C == 0 && Coin == 0) //如果看涨期权多头持仓0并且看跌期权空头持仓0并且Coin为0
{
SKID_C[i] = datac.A_SendOrder( Enum_Sell,Enum_Entry,1,datac.Price("Bid1"));//以看涨期权买一价卖开看涨期权1手的卖开索引
BKID_P[i] = datap.A_SendOrder( Enum_Buy,Enum_Entry,1,datap.Price( "Ask1"));//以看跌期权卖一价买开看跌期权1手的买开索引
Time1[i] = CurrentTime;//第i个索引的时间
Index = 0;//index为0
Coin = 1;//coin为1
NumOrder = 1;//下单后1
}
While(Index < i)
{
If(F_OrderStatus(SKID_C[Index]) == Enum_Declared && TimeDiff(Time1[Index],CurrentTime) >= 15)//15秒没有成交
{
datac.A_DeleteOrder(F_OrderContractNo(SKID_C[Index]));//撤单
datac.A_SendOrder( Enum_Sell,Enum_Entry,1,datac.Price("FallLimit"));//以跌停价卖开1手看涨期权
}
If(F_OrderStatus(BKID_P[Index]) == Enum_Declared && TimeDiff(Time1[Index],CurrentTime) >= 15)//15秒没有成交
{
datap.A_DeleteOrder(F_OrderContractNo(BKID_P[Index]));//撤单
datap.A_SendOrder( Enum_Buy,Enum_Entry,1,datap.Price("RiseLimit"));//以涨停价买开1手看跌期权
}
Index = Index + 1;
}
If (NumOrder == 1)//每次下单后
{
i = i + 1;//次数加1
NumOrder = 0;//清0
}
}
Else If(New_C - New_P < New_F - OptionStrikePrice)//看涨期权最新价-看跌期权最新价<标的物最新价-行权价
{
If(SellRemainPosition_C > 0)//看涨期权空头持仓>0
{
datac.A_SendOrder( Enum_buy,Enum_Exit,SellRemainPosition_C,datac.Price( "Ask1")); //以卖一价买平看涨期权空头持仓
Coin1=0;//Coin1为0
}
If(BuyRemainPosition_P > 0) //看跌期权多头持仓>0
{
datap.A_SendOrder(Enum_Sell,Enum_Exit,BuyRemainPosition_P,datap.Price("Bid1"));//以买一价卖平看跌期权多头持仓
Coin1 = 0;//Coin1为0
}
If(SellRemainPosition_C == 0 && BuyRemainPosition_P == 0 && Coin1 == 0) //看涨期权空头持仓为0并且看跌期权多头持仓为0且Coin1为0
{
BKID_C[j] = datac.A_SendOrder( Enum_Buy, Enum_entry, 1, datac.Price("Ask1"));//以卖一价买开看涨期权1手的索引
SKID_P[j] = datap.A_SendOrder( Enum_Sell, Enum_Entry, 1, datap.Price( "Bid1"));//以买一价卖开看跌期权1手的索引
Time2[j] = CurrentTime;//第i个索引的时间
Index = 0;//Index为0
Coin1 = 1;//Coin1为1
NumOrder = 1;//下单后1
}
While(Index < j)
{
If(F_OrderStatus(BKID_C[Index]) == Enum_Declared && TimeDiff(Time2[index],CurrentTime) >= 15)// 15秒没有成交
{
datac.A_DeleteOrder(F_OrderContractNo(BKID_C[Index]));//撤单
datac.A_SendOrder(Enum_Buy,Enum_Entry,1,datac.price("RiseLimit"));//以涨停价买开1手看涨期权
}
If(F_OrderStatus(SKID_P[Index]) == Enum_Declared && TimeDiff(Time2[j],CurrentTime) >= 15)// 15秒没有成交
{
datap.A_DeleteOrder(F_OrderContractNo(SKID_P[Index]));//撤单
datap.A_SendOrder( Enum_Sell,Enum_Entry,1,datap.Price("FallLimit"));//以跌停价卖开1手看跌期权
}
Index = Index + 1;
}
If (NumOrder == 1)//每次下单后
{
j = j + 1;//下单次数+1
NumOrder = 0;//清0
}
}
}
End
牛市看涨期权垂直套利
Data
data1:"m2101-C-2850"; //定义第一个看涨期权合约
data2:"m2103-C-3000"; //定义第二个看涨期权合约
Params
Numeric Lots(2); //定义手数
Numeric Length1(5); //定义周期数
Numeric Length2(10); //定义周期数
Vars
Numeric MA1; //均线
Numeric MA2; //均线
Numeric OptionStrikePrice1; //行权价
Numeric OptionStrikePrice2; //行权价
Numeric BKfun; //BK条件
Numeric SPfun; //SP条件
Begin
MA1 = Ma(Close,Length1); //5周期均线
MA2 = Ma(Close,Length2); //10周期均线
BKfun = CrossUp(MA1,MA2);//5周期均线上穿10周期均线
SPfun = CrossDown(MA1,MA2);//5周期均线下穿10周期均线
OptionStrikePrice1 = data1.Price("StrikePrice");//取data1合约的行权价
OptionStrikePrice2 = data2.Price("StrikePrice");//取data2合约的行权价
If(BKfun && OptionStrikePrice2 > OptionStrikePrice1 && GetGlobalVar(0) == 0)//满足BK信号条件并且合约2的行权价>合约1的行权价
{
data1.A_SendOrder(Enum_Buy,Enum_Entry,Lots,data1.Price("Ask1"));//对合约1以卖价1 的价格发出买2手开仓委托
data2.A_SendOrder(Enum_Sell,Enum_Entry,Lots,data2.Price("Bid1"));//对合约2以买价1 的价格发出卖2手开仓委托
SetGlobalVar(0,1);//全局变量为1
}
If(SPfun && data1.F_BuyRemainPosition() > 0 && data2.F_SellRemainPosition() > 0&&GetGlobalVar(0) == 1)//满足平仓信号并且合约1有多头持仓并且合约2有空头持仓
{
data1.A_SendOrder(Enum_Sell,Enum_Exit,data1.F_BuyRemainPosition(),data1.Price("Bid1"));//对合约1以买价1 的价格发出全卖平的委托
data2.A_SendOrder(Enum_Buy,Enum_Exit,data2.F_SellRemainPosition(),data2.Price("Ask1"));//对合约2以卖价1 的价格发出全买平的委托
SetGlobalVar(0,0);//全局变量为0
}
End
牛市看跌期权垂直套利
Data
data1:"m2101-P-2850"; //定义第一个看跌期权合约
data2:"m2103-P-3000"; //定义第二个看跌期权合约
Params
Numeric Lots(2); //定义手数
Numeric Length1(5); //定义周期数
Numeric Length2(10); //定义周期数
Vars
Numeric MA1; //均线
Numeric MA2; //均线
Numeric OptionStrikePrice1; //行权价
Numeric OptionStrikePrice2; //行权价
Numeric BKfun; //BK条件
Numeric SPfun; //SP条件
Begin
MA1 = Ma(Close,Length1); //5周期均线
MA2 = Ma(Close,Length2); //10周期均线
BKfun = CrossUp(MA1,MA2);//5周期均线上穿10周期均线
SPfun = CrossDown(MA1,MA2);//5周期均线下穿10周期均线
OptionStrikePrice1 = data1.Price("StrikePrice");//取data1合约的行权价
OptionStrikePrice2 = data2.Price("StrikePrice");//取data2合约的行权价
If(BKfun && OptionStrikePrice2 > OptionStrikePrice1 && GetGlobalVar(0) == 0)//满足BK信号条件并且合约2的行权价>合约1的行权价
{
data1.A_SendOrder(Enum_Buy,Enum_Entry,Lots,data1.Price("Ask1"));//对合约1以卖价1 的价格发出买2手开仓委托
data2.A_SendOrder(Enum_Sell,Enum_Entry,Lots,data2.Price("Bid1"));//对合约2以买价1 的价格发出卖2手开仓委托
SetGlobalVar(0,1);//全局变量为1
}
If(SPfun && data1.F_BuyRemainPosition() > 0 && data2.F_SellRemainPosition() > 0&&GetGlobalVar(0) == 1)//满足SP信号并且合约1有多头持仓并且合约2有空头持仓
{
data1.A_SendOrder(Enum_Sell,Enum_Exit,data1.F_BuyRemainPosition(),data1.Price("Bid1"));//对合约1以买价1 的价格发出全卖平的委托
data2.A_SendOrder(Enum_Buy,Enum_Exit,data2.F_SellRemainPosition(),data2.Price("Ask1"));//对合约2以卖价1 的价格发出全买平的委托
SetGlobalVar(0,0);//全局变量为0
}
End
网友评论