以下是做多策略的主部分,其中My_State为全局的数组,在初始化中将其全设为0
在没有触发平仓的情况下,不应该再发出多单,交易记录如下:中间那个原油的平仓是手动设置
If(Choice_direction == 1 And BarStatus == 2)
{
If(My_State[i] == 0)//如多头持仓合约数为0
{
If(Condiction_Buy)//如果符合开仓条件
{
//Buy(Buy_Lots, Close); //开仓
A_Buy(RelativeSymbol,Buy_Lots, Close,orders);
My_longEntryPrice = longEntryPrice;
My_State[i] = 1;
ret = FileAppend("C:\\Users\\14760\\Desktop\\量化交易\\交易日志\\daili",Text(My_State[i])+RelativeSymbol+Text(My_longEntryPrice)+"开多仓"+",Close:"+Text(Close)+",Low:"+Text(Low)
+",MA_Daily_Amplitude:"+Text(MA_Daily_Amplitude)+",运算:"+Text(Buy_Calculate));
Print("FileAppend:" + IIFString(ret, "True", "False"));
}
}
Else //如多头持仓合约数不为0
{
//平仓条件:持仓天数
Condiction_Exit = BarsSinceEntry >=2;
//止损条件: 多单:当多单买入价>当天某现价(当时最低价) 即平多单
Condiction_Sell_StopLoss = My_longEntryPrice > Close;
//止盈: 当天收盘后 如果有未平仓多单,第二天以前一天的最低价作为止盈条件 即 第二天现价<第一天最低价 平仓 以此类推
Condiction_Sell_StopProfit = Close < Yesterday_Low;
If(Condiction_Sell_StopLoss) //如果符合止损条件
{
//Sell(Buy_Lots,Close);
A_Sell(RelativeSymbol,Buy_Lots, Close,orders);
My_State[i] = 0;
ret = FileAppend("C:\\Users\\14760\\Desktop\\量化交易\\交易日志\\daili",RelativeSymbol+"多仓止损"+",BarsSinceEntry:"+Text(BarsSinceEntry)+",My_longEntryPrice:"+Text(longEntryPrice)
+",Close:"+Text(Close));
Print("FileAppend:" + IIFString(ret, "True", "False"));
}
If(Not_EntryBar And Condiction_Sell_StopProfit) //当前K线上是持仓状态,但不是开仓K线,如果符合止盈条件
{
A_Sell(RelativeSymbol,Buy_Lots, Close,orders);
My_State[i] = 0;
ret = FileAppend("C:\\Users\\14760\\Desktop\\量化交易\\交易日志\\daili",RelativeSymbol+Text(longCurrentContracts)+"多仓止赢"+",BarsSinceEntry:"+Text(BarsSinceEntry)+",Close:"+Text(Close)
+",Yesterday_low:"+Text(Yesterday_low));
Print("FileAppend:" + IIFString(ret, "True", "False"));
}
}
}
以下为日志输出