- 精华
- 0
- 在线时间
- 49 小时
- UID
- 216337
- 积分
- 19
- 帖子
- 9
- 阅读权限
- 10
- 注册时间
- 2015-8-7
- 最后登录
- 2016-11-5
- 精华
- 0
- UID
- 216337
- 积分
- 19
- 帖子
- 9
- 主题
- 3
- 阅读权限
- 10
- 注册时间
- 2015-8-7
- 最后登录
- 2016-11-5
|
Begin
MinPoint = MinMove*PriceScale;
MyBarsSinceToday = BarsSinceToday;
CloseD_1 = CloseD(1);
if(BarsSinceToday==0)
{
DayCount = DayCount + 1; //记录交易日数;
DayHigh = High;
DayLow = Low;
//从第二个交易日开始记录;
if(DayCount>=2)
{
// 前一日真实波幅;
Atr = max(max(Abs(DayHigh[1]-CloseD_1),Abs(DayLow[1]-CloseD_1)),DayHigh[1]-DayLow[1]);
// 存储前一天的Atr到全局变量环0-19;
AtrIndex = (DayCount-2)%LengthAtr;
SetGlobalVar(AtrIndex,Atr[1]);
}
// 从第LengthAtr+1个交易日开始计算;
if(DayCount>=LengthAtr+1)
{
// 计算日线级别长度为LengthAtr的平均真实波幅;
AtrAve = 0 ;
for i=0 to LengthAtr-1
{
AtrAve = AtrAve + getGlobalVar(i);
}
AtrAve = AtrAve/LengthAtr;
N = IntPart(AtrAve);
DonchianFastUpper = Highest(DayHigh[1],(LengthAtr-0.1)*(MyBarsSinceToday[1]+1));
DonchianFastLower = Lowest(DayLow[1],(LengthAtr-0.1)*(MyBarsSinceToday[1]+1));
DonchianSlowUpper = Highest(DayHigh[1],(LengthSlow-0.1)*(MyBarsSinceToday[1]+1));
DonchianSlowLower = Lowest(DayLow[1],(LengthSlow-0.1)*(MyBarsSinceToday[1]+1));
DonchianExitUpper = Highest(DayHigh[1],(LengthExit-0.1)*(MyBarsSinceToday[1]));
DonchianExitLower = Lowest(DayLow[1],(LengthExit-0.1)*(MyBarsSinceToday[1]));
}
//账户最新资产 = 按当前Bar开盘价计算的可用资金 + 持仓保证金 ;
TotalEquity = Portfolio_CurrentCapital() + Portfolio_UsedMargin();
//标准单位头寸 = (本金*风险比例)/价值波动率
TurtleUnits = (TotalEquity*RiskRatio/100) /(N * ContractUnit()*BigPointValue());
TurtleUnits = IntPart(TurtleUnits); // 对小数取整
// PlotString("Units","Units="+Text(TurtleUnits),High+(High-Low),white);
// PlotString("N","N="+Text(N),Low-(High-Low)/2,white);
}
Else
{
DayHigh = Max(DayHigh,High);
DayLow = Min(DayLow,Low);
}
If(MarketPosition==0 and TurtleUnits>=1)
{
// 首次入市1:带过滤条件的,如果过滤条件为真,则需要前一笔交易是否是突破失败;
if(LastProfitableTradeFilter==false Or PreBreakoutFailure )
{
//开多1:突破短周期唐奇安通道上轨;
If(High>DonchianFastUpper)
{
MyPrice = Max(Open,DonchianFastUpper)+MinPoint;
Buy(TurtleUnits,MyPrice);
MyExitPrice = MyPrice - 2*N;
SendOrderThisBar = true;
}
//开空1:突破短周期唐奇安通道下轨;
if(Low<DonchianFastLower)
{
MyPrice = Min(Open,DonchianFastLower)-MinPoint;
SellShort(TurtleUnits,MyPrice);
MyExitPrice = MyPrice + 2*N;
SendOrderThisBar = true;
}
}
// 首次入市2:过滤条件之外的,长周期唐奇安通道突破开仓;
//开多2:突破长周期唐奇安通道上轨;
else
{
if(CrossOver(High,DonchianSlowUpper))
{
MyPrice = Max(Open,DonchianSlowUpper)+MinPoint;
Buy(TurtleUnits,MyPrice);
MyExitPrice = MyPrice - 2*N;
SendOrderThisBar = true;
}
//开空2:突破长周期唐奇安通道下轨;
if(CrossUnder(Low,DonchianSlowLower))
{
MyPrice = Min(Open,DonchianSlowLower)+MinPoint;
SellShort(TurtleUnits,MyPrice);
MyExitPrice = MyPrice + 2*N;
SendOrderThisBar = true;
}
}
}
// 当持有多单;
if(MarketPosition==1)
{
//离市:价格反向穿越离市的唐奇安通道;
if(CrossUnder(Low,DonchianExitLower))
{
MyPrice = Min(Open,DonchianExitLower)-MinPoint;
Sell(0,MyPrice);
PreBreakoutFailure = False; //假突破值为假;
// PlotBool("突破",PreBreakoutFailure,High,white);
}
Else
{
//加仓:与最近一次开仓价间隔0.5*N开仓;
if(High>LastEntryPrice+0.5*N and TurtleUnits>=1)
{
MyPrice = Max(Open,LastEntryPrice+0.5*N)-MinPoint;
Buy(TurtleUnits,MyPrice);
SendOrderThisBar = true;
}
//止损:价格反向穿越最近一次开仓价格-2N;
if(Low<LastEntryPrice-2*N)
{
MyPrice = Min(Open,LastEntryPrice-2*N)-MinPoint;
Sell(0,MyPrice);
PreBreakoutFailure = True; //假突破值为真;
// PlotBool("突破",PreBreakoutFailure,High,white);
}
}
}
// 当持有空单;
else if(MarketPosition==-1)
{
// 离市:价格反向穿越离市的唐奇安通道;
if(CrossOver(High,DonchianExitUpper))
{
MyPrice = Max(Open,DonchianExitUpper)+MinPoint;
BuyToCover(0,MyPrice);
PreBreakoutFailure = False; //假突破值为假;
// PlotBool("突破",PreBreakoutFailure,High,white);
}
Else
{
// 加仓:与最近一次开仓价格间隔0.5*N开仓;
if(Low<LastEntryPrice-0.5*N and TurtleUnits>=1)
{
MyPrice = Min(Open,LastEntryPrice-0.5*N)-MinPoint;
SellShort(TurtleUnits,MyPrice);
SendOrderThisBar = true;
}
// 止损:价格反向穿越最近一次开仓价格+2N;
if(High>LastEntryPrice+2*N)
{
MyPrice = Max(Open,LastEntryPrice+2*N)+MinPoint;
BuyToCover(0,MyPrice);
PreBreakoutFailure = True; //假突破值为真;
// PlotBool("突破",PreBreakoutFailure,High,white);
}
}
}
PlotNumeric("DonchianFastUpper",DonchianFastUpper);
PlotNumeric("DonchianFastLower",DonchianFastLower);
PlotNumeric("DonchianSlowUpper",DonchianSlowUpper);
PlotNumeric("DonchianSlowLower",DonchianSlowLower);
PlotNumeric("DonchianExitUpper",DonchianExitUpper);
PlotNumeric("DonchianExitLower",DonchianExitLower);
// Commentary("点值="+Text(ContractUnit()*BigPointValue()));
Commentary("TotalEquity="+Text(TotalEquity));
Commentary("N="+Text(N));
Commentary("Units="+Text(TurtleUnits));
// Commentary("Atr="+Text(Atr));
// Commentary("AtrAve="+Text(AtrAve));
// PlotNumeric("AtrAve",AtrAve);
Commentary("DayCount="+Text(DayCount));
Commentary("MyBarsSinceToday="+Text(MyBarsSinceToday[1]+1));
if(PreBreakoutFailure == True)
{
Commentary("PreBreakoutFailure = True");
}Else
{
Commentary("PreBreakoutFailure = false");
}
if(LastProfitableTradeFilter==true)
{
Commentary("LastProfitableTradeFilter = True");
}Else
{
Commentary("LastProfitableTradeFilter = False");
}
End |
|