Params
Numeric SLPercent(3); // 止损百分比
Numeric TPPercent(6); // 目标利润百分比
Numeric ReservePercent(80); // 预留百分比
Numeric MA_Length(20); // 均线长度
Numeric CCI_Period(14); // CCI指标周期
Numeric TrailSLBars(3); // 止损跟踪Bar数
Vars
Series <Numeric> AvgClose;
Series <Numeric> CCI_Value;
Numeric StopLossPrice;
Numeric TargetPrice;
Numeric EntryPrice;
Events
onBar(ArrayRef<Integer> indexs)
{
// 计算指标值
AvgClose = AverageFC(Close, MA_Length);
CCI_Value = CCI(CCI_Period);
// 多单开仓
if (MarketPosition == 0 and AvgClose[1] > AvgClose[2] and CCI_Value[1] > 100 and CCI_Value[2] <= 100) {
EntryPrice = Open[1];
StopLossPrice = EntryPrice * (1 - SLPercent/100);
TargetPrice = EntryPrice * (1 + TPPercent/100);
buy(1, Open);
}
// 空单开仓
if (MarketPosition == 0 and AvgClose[1] < AvgClose[2] and CCI_Value[1] < -100 and CCI_Value[2] >= -100) {
EntryPrice = Open[1];
StopLossPrice = EntryPrice * (1 + SLPercent/100);
TargetPrice = EntryPrice * (1 - TPPercent/100);
sellShort(1, Open);
}
// 止损跟踪
if (MarketPosition == 1 and Close[1] < StopLossPrice) {
StopLossPrice = Close[1];
}
else if (MarketPosition == -1 and Close[1] > StopLossPrice) {
StopLossPrice = Close[1];
}
// 平仓
if (MarketPosition == 1 and Low[1] <= StopLossPrice) {
sell(1, StopLossPrice);
}
else if (MarketPosition == 1 and High[1] >= TargetPrice) {
sell(1, TargetPrice);
}
else if (MarketPosition == -1 and High[1] >= StopLossPrice) {
buyToCover(1, StopLossPrice);
}
else if (MarketPosition == -1 and Low[1] <= TargetPrice) {
buyToCover(1, TargetPrice);
}
}