- 精华
- 0
- 在线时间
- 30 小时
- UID
- 62557
- 积分
- 124
- 帖子
- 27
- 阅读权限
- 40
- 注册时间
- 2011-8-31
- 最后登录
- 2017-1-10
- 精华
- 0
- UID
- 62557
- 积分
- 124
- 帖子
- 27
- 主题
- 8
- 阅读权限
- 40
- 注册时间
- 2011-8-31
- 最后登录
- 2017-1-10
|
Commentary("111");已经打印出来了说明已经进入了开仓的语句,问题是EntryPrice居然是3149元,那一日的最高价也没到3149元,自然是开仓不成功了。问题是我要求以收盘价开仓他怎么给了3149元的开仓价?
Params
Numeric P(1.20); //如果该日的最高价-最低价是前N日的P倍,则该日定义为宽幅震荡日。《交易策略说明》中的K,因与下面的K重复而改成P。
Numeric N(2); //如果该日的最高价-最低价是前N日的P倍,则该日定义为宽幅震荡日。
Numeric N1(2); //PTR区间取宽幅震荡日前N1日至后N2日的最大值和最小值。
Numeric N2(0); //PTR区间取宽幅震荡日前N1日至后N2日的最大值和最小值。
Numeric ATRLength(100); //ATR周期,用于计算开仓数量和止损位置。
Numeric OpenUnit(10); //开仓所用头寸单位。
Numeric K(1); //K值,表示愿意承受的风险度。具体含义参考《开仓数量的公式》。
Vars
Numeric NHigh; //前N日的最高价
Numeric NLow; //前N日的最低价
NumericSeries PTRH; //PTR区间的最高值
NumericSeries PTRL; //PTR区间的最低值
Numeric PTRH2; //临时记录N1+N2+1天内的最高值
Numeric PTRL2; //临时记录N1+N2+1天内的最低值
NumericSeries LastWideDay; //最近一个宽幅震荡日的BAR值。
NumericSeries PreMarketPosition; //用于止损后记录之前的头寸方向,0代表这次开仓还没有止损,1代表多头,-1代表空头。
Numeric TempMaretktosition; //操作后MarketPosition的值改变了,防止程序误走入MarketPosition == 1(-1)的语句。目的在震荡较大的日子,防止开仓后直接止损。
NumericSeries PreEntryPrice(0); //止损后,记录止损前的开仓价。
Numeric OpenShares;
NumericSeries StopLossPrice(0); //止损价。
Numeric ATR(0); //ATR 值
Begin
if(CurrentBar == (Max(N1+N2+1,ATRLength))-1) // 两条均线和ATR值都能计算出来,接下来是有效的数据,进行初始化操作。
{
PreEntryPrice = 0;
StopLossPrice = 0;
PTRH = 0;
PTRL = 0;
PreMarketPosition = 0;
}
ATR = Average(TrueRange,ATRLength);
NHigh = HighestFC(High[1],N); //前N天的最高价。
NLow = LowestFC(Low[1],N); //前N天的最低价。
if((High-Low) >= ((NHigh-NLow)*P)) //如果今天的最高价-最低价是前N日最高价-最低价的P倍,那么今天就是宽幅震荡日。
{
LastWideDay = CurrentBar;
}
PTRH2 = HighestFC(High,N1+N2+1);
PTRL2 = LowestFC(Low,N1+N2+1);
if((PTRH!=0) && (PTRL!=0))
{
TempMaretktosition = MarketPosition; //操作后MarketPosition的值改变了,防止程序误走入MarketPosition == 1(-1)的语句。目的在震荡较大的日子,防止开仓后直接止损。
if(TempMaretktosition == 0) // 目前是空仓,检查收盘价是否突破了PTR高点和低点,如果是开仓。另外检查是否止损后收盘价回到了之前的位置。
{
if((Close>PTRH) && (PreMarketPosition!=1))// PTRH和PTRL是相对稳定的,如果在多头市场中止损了,但是价格仍然在PTRH之上,第二天程序就会自动做多,止损也变得无意义了。
{
OpenShares = OpenShare(OpenUnit,ATRLength,K,ATR);
Buy(OpenShares,Close);
PreMarketPosition = 0;
PreEntryPrice = EntryPrice;
StopLossPrice = PreEntryPrice - ATR;
}
else if((Close<PTRL) && (PreMarketPosition!=-1)) // PTRH和PTRL是相对稳定的,如果在空头市场中止损了,但是价格仍然在PTRL之下,第二天程序就会自动做多,止损也变得无意义了。
{
OpenShares = OpenShare(OpenUnit,ATRLength,K,ATR);
SellShort(OpenShares,Close);
PreMarketPosition = 0;
PreEntryPrice = EntryPrice;
StopLossPrice = PreEntryPrice + ATR;
}
else if((PreMarketPosition==1) && (Close >= PreEntryPrice))
{
OpenShares = OpenShare(OpenUnit,ATRLength,K,ATR);
Buy(OpenShares,Close);
PreMarketPosition = 0;
PreEntryPrice = EntryPrice;
StopLossPrice = PreEntryPrice - ATR;
}
else if((PreMarketPosition==-1) && (Close <= PreEntryPrice))
{
OpenShares = OpenShare(OpenUnit,ATRLength,K,ATR);
SellShort(OpenShares,Close);
PreMarketPosition = 0;
PreEntryPrice = EntryPrice;
StopLossPrice = PreEntryPrice + ATR;
}
}
if(TempMaretktosition == 1) //目前持多单,检查是否跌破了最近的PTR下边界,并检查是否需要止损。
{
if(Close < PTRL)
{
OpenShares = OpenShare(OpenUnit,ATRLength,K,ATR);
SellShort(OpenShares,Close);
PreMarketPosition = 0;
PreEntryPrice = EntryPrice;
StopLossPrice = PreEntryPrice + ATR;
}
else
{
StopLoss(EntryPrice,StopLossPrice);
PreMarketPosition = 1;
}
}
else if(TempMaretktosition == -1) //目前持空单,检查是否突破了最近的PTR上边界,并检查是否需要止损。
{
if(Close > PTRH)
{
Commentary("111");
OpenShares = OpenShare(OpenUnit,ATRLength,K,ATR);
Buy(OpenShares,Close);
PreMarketPosition = 0;
PreEntryPrice = EntryPrice;
StopLossPrice = PreEntryPrice - ATR;
Commentary("OpenShares"+Text(OpenShares));
Commentary("EntryPrice"+Text(EntryPrice));
}
else
{
StopLoss(EntryPrice,StopLossPrice);
PreMarketPosition = -1;
}
}
}
if((LastWideDay+N2) == CurrentBar) //检查是否需要操作后,更新PTR。
{
PTRH = PTRH2;
PTRL = PTRL2;
}
PlotNumeric("PTRH",PTRH);
PlotNumeric("PTRL",PTRL);
Commentary("WideDay="+Text(IIF(LastWideDay==CurrentBar,1,0)));
Commentary("TotalEquity="+Text(Portfolio_CurrentCapital()+Portfolio_UsedMargin()));
Commentary("StopLossPrice="+Text(StopLossPrice));
End |
附件: 你需要登录才可以下载或查看附件。没有帐号?注册
|