//------------------------------------------------------------------------
// 简称: TestShuangjunxian
// 名称:
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
Numeric Length1(10);
Numeric Length2(20);
Numeric Lots(1);
Numeric TrailingStop(1);
Numeric BarsReEntry(5);
Vars
Series<Numeric> MA1;
Series<Numeric> MA2;
Series<Bool> condBuy(False);//是否出场过
Series<Bool> condSell(False);//是否出场过
Numeric MinPoint;
Numeric MyPrice;
Numeric MyExitPrice;
Series<Numeric> HighestAfterEntry;
Series<Numeric> LowestAfterEntry;
Numeric StopLine(0);
Series<Bool> BLong(False);//是否出场过
Series<Bool> BShort(False);//是否出场过
Numeric BarsAfterLongExit(0);
Numeric BarsAfterShortExit(0);
Events
//此处实现事件函数
//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
OnBar(ArrayRef<Integer> indexs)
{
Commentary(\"BLong = \"+IIFString(Blong,\"True\",\"False\"));
Commentary(\"BShort = \"+IIFString(BShort,\"True\",\"False\"));
If(BarsSinceEntry==1)
{
HighestAfterEntry=AvgEntryPrice;
LowestAfterEntry=AvgEntryPrice;
}Else If(BarsSinceEntry>1)
{
HighestAfterEntry=Max(HighestAfterEntry[1],High[1]);
LowestAfterEntry=Min(LowestAfterEntry[1],Low[1]);
}Else
{
HighestAfterEntry=HighestAfterEntry[1];
LowestAfterEntry=LowestAfterEntry[1];
}
MA1=AverageFC(Close,Length1);
MA2=AverageFC(Close,Length2);
condBuy=CrossOver(MA1,MA2);
condSell=CrossUnder(MA1,MA2);
IF(condBuy==False AND condSell ==False)
{
condBuy=condBuy[1];
condSell=condSell[1];
}
If(MarketPosition<>1 And condBuy[1]==true And BLong==False)//完全初始状态,做多做空都没出场过
{
Buy(Lots,Open);
HighestAfterEntry=Open;
BLong=False;
BShort=false;
}
If(MarketPosition<>-1 And condSell[1]==true And BShort==False)//完全初始状态,做多做空都没出场过
{
SellShort(Lots,Open);
LowestAfterEntry=Open;
BLong=False;
BShort=false;
}
MinPoint=MinMove*pricescale;
//BarsAfterLongExit=NthCon(!BLong,1);
//If(BLong AND MarketPosition==0 AND condBuy[1]==TRUE AND BarsAfterLongExit>=BarsReEntry)
//{
// Buy(Lots,Open);
// HighestAfterEntry=Open;
// BLong=false;
// Return;
//}
If(BLong AND MarketPosition==0 AND High >=HighestAfterEntry)
{
MyPrice=HighestAfterEntry+MinPoint;
If(Open > MyPrice)
{
MyPrice = Open; // 如果该 Bar 开盘价即跳空触发,则用开盘价代替
}
Buy(Lots,MyPrice);
BLong==False;//
//Return;
}
If(BShort AND MarketPosition==0 AND Low <=LowestAfterEntry)
{
MyPrice=LowestAfterEntry-MinPoint;
If(Open < MyPrice)
{
MyPrice = Open; // 如果该 Bar 开盘价即跳空触发,则用开盘价代替
}
SellShort(Lots,MyPrice);
BShort==False;//
//Return;
}
//BarsAfterShortExit=NthCon(BShort,1);
//If(BShort AND MarketPosition==0 AND condSell[1]==TRUE AND BarsAfterShortExit>=BarsReEntry)
//{
// SellShort(Lots,Open);
// LowestAfterEntry=Open;
// BShort=false;
// Return;
//}
//MinPoint=MinMove*pricescale;
If(BarssinceENtry>0 And MarketPosition == 1 ) // 有多仓的情况
//If(MarketPosition == 1 )
{
StopLine=HighestAfterEntry*(1-TrailingStop*0.01);
If(Low <= StopLine)//AvgTR已经是历史信息得到的
{
MyExitPrice = StopLine - MinPoint;
If(Open < MyExitPrice)
{
MyExitPrice = Open; // 如果该 Bar 开盘价即跳空触发,则用开盘价代替
}
Sell(0,MyExitPrice);
BLong==True;//有过多入场,出场时把做多出场标记为真,做空出场标记为假
//BShort==False;
}
}
If(BarssinceENtry>0 And MarketPosition == -1) // 有空仓的情况
//If(And MarketPosition == -1) // 有空仓的情况
{
StopLine=LowestAfterEntry*(1+TrailingStop*0.01);
If(High >= StopLine)//AvgTR已经是历史信息得到的
{
MyExitPrice = StopLine + MinPoint;
If(Open > MyExitPrice)
{
MyExitPrice = Open; // 如果该 Bar 开盘价即跳空触发,则用开盘价代替
}
BuyToCover(0,MyExitPrice);
//BLong==False;//有过多入场,出场时把做多出场标记为真,做空出场标记为假
BShort==True;
}
}
Commentary(\"HighestAfterEntry = \"+Text(HighestAfterEntry));
Commentary(\"LowestAfterEntry = \"+Text(LowestAfterEntry));
}
//------------------------------------------------------------------------
// 编译版本 2024/08/16 144031
// 版权所有 Grateful
// 更改声明 TradeBlazer Software保留对TradeBlazer平台
// 每一版本的TradeBlazer公式修改和重写的权利
//------------------------------------------------------------------------
赋值操作符是 \"=\",不是\"==\"。
这是138和155,有什么问题?
If(BarssinceENtry>0 And MarketPosition == 1 ) // 有多仓的情况
{
StopLine=HighestAfterEntry*(1-TrailingStop*0.01);
If(Low <= StopLine)//AvgTR已经是历史信息得到的
{
MyExitPrice = StopLine - MinPoint;
If(Open < MyExitPrice)
{
MyExitPrice = Open; // 如果该 Bar 开盘价即跳空触发,则用开盘价代替
}
Sell(0,MyExitPrice);
BLong==True;//这句不执行
}
}
If(BarssinceENtry>0 And MarketPosition == -1) // 有空仓的情况
{
StopLine=LowestAfterEntry*(1+TrailingStop*0.01);
If(High >= StopLine)//AvgTR已经是历史信息得到的
{
MyExitPrice = StopLine + MinPoint;
If(Open > MyExitPrice)
{
MyExitPrice = Open; // 如果该 Bar 开盘价即跳空触发,则用开盘价代替
}
BuyToCover(0,MyExitPrice);
BShort==True;//这句不执行
}
}