
等级: 新手上路
- 注册:
- 2023-12-11
- 曾用名:
|
麻烦老师帮我编译下列代码能到决策系统里正常运行 麻烦了
// 定义参数
Input: FastLength(5); // 快速均线周期
Input: SlowLength(20); // 慢速均线周期
Input: ATRLength(14); // ATR周期
Input: ATRMultiplier(2); // ATR倍数(用于止损和止盈)
Input: TradeStart(0900); // 交易开始时间(9:00)
Input: TradeEnd(1500); // 交易结束时间(15:00)
Vars: FastMA(0), SlowMA(0), ATRValue(0);
Vars: CurrentTime(0), EntryPrice(0);
Vars: StopLossPrice(0), TakeProfitPrice(0);
// 计算均线和ATR
FastMA = Average(Close, FastLength);
SlowMA = Average(Close, SlowLength);
ATRValue = ATR(ATRLength);
// 获取当前时间
CurrentTime = Time;
// 开仓条件
If CurrentTime >= TradeStart And CurrentTime <= TradeEnd Then Begin
// 开多仓条件:快速均线上穿慢速均线,且波动性足够
If FastMA > SlowMA And ATRValue > Average(ATRValue, 10) And MarketPosition = 0 Then Begin
Buy 1 Contract;
EntryPrice = Close;
StopLossPrice = EntryPrice - ATRMultiplier * ATRValue; // 动态止损
TakeProfitPrice = EntryPrice + ATRMultiplier * ATRValue; // 动态止盈
End;
// 开空仓条件:快速均线下穿慢速均线,且波动性足够
If FastMA < SlowMA And ATRValue > Average(ATRValue, 10) And MarketPosition = 0 Then Begin
SellShort 1 Contract;
EntryPrice = Close;
StopLossPrice = EntryPrice + ATRMultiplier * ATRValue; // 动态止损
TakeProfitPrice = EntryPrice - ATRMultiplier * ATRValue; // 动态止盈
End;
End;
// 止损条件
If MarketPosition > 0 And Close <= StopLossPrice Then Begin // 多仓止损
Sell All Contracts;
End;
If MarketPosition < 0 And Close >= StopLossPrice Then Begin // 空仓止损
BuyToCover All Contracts;
End;
// 止盈条件
If MarketPosition > 0 And Close >= TakeProfitPrice Then Begin // 多仓止盈
Sell All Contracts;
End;
If MarketPosition < 0 And Close <= TakeProfitPrice Then Begin // 空仓止盈
BuyToCover All Contracts;
End;
// 收盘前平仓
If CurrentTime >= TradeEnd And MarketPosition <> 0 Then Begin
If MarketPosition > 0 Then Sell All Contracts; // 平多仓
If MarketPosition < 0 Then BuyToCover All Contracts; // 平空仓
End;
|
|