放在附加檔案,我有看到,你沒看到嗎? 我直接COPY&PASTE給你。
Input: ATR_Length(14, "ATR 計算天數");
Input: RSI_Length (14, "RSI 計算天數");
Input: MA_Length (50, "移動平均線天數");
Input: BB_Length (20, "布林通道天數");
Input: BB_Deviation (2, "布林通道標準差");
Input: stoploss_point (100, "停損點(點)");
Input: takeprofit_point (200, "停利點(點)");
Var: grid_gap (0);
Var: grid_maxcount(2);
Var: grid_started (false);
Var: grid_base (0);
Var: grid_current_base (0);
Var:grid_current_ord(0);
Var:grid_buycount(0);
Var:grid_sellcount(0);
Var: LastTradePrice (0);
Var: TotalProfit (0);
Var: MA_Value (0);
Var: BB_Upper (0), BB_Lower (0);
// 計算指標
Grid_gap = ATR (ATR_Length);
MA_Value = Average (Close, MA_Length);
BB_Upper = BollingerBand (Close, BB_Length, BB_Deviation, 1); // 布林通道上軌
BB_Lower = BollingerBand (Close, BB_Length, BB_Deviation, -1); //布林通道下軌
// 啟動網格交易邏輯
If not grid_base <> 0 then
begin
//檢查總盈虧,達到停利或停損點時停止交易
If Total Profit >= takeprofit_point then
begin
SetPosition (0, label:= "網格: 達到獲利目標,停止交易");
Grid_base = 0; // 停止網格交易
End
else
begin //未達停利點則繼續執行網格交易
// 根據布林通道與ATR動態調整網格區間
Grid_gap = (BB_Upper- BB_Lower) / 4 + ATR (ATR_Length);
// 檢查價格突破網格判斷,計算穿越的網格數量
If Close >= grid_current_base + grid_gap then
begin
Value2 = grid_current_ord + IntPortion ((Close – grid_current_base) / grid_gap);
If value2 >= grid_maxcount then value2 = grid_maxcount;
Value2 = value2 – grid_current_ord; // 往上移動的網格數
// 只在價格高於MA時允許做空
If value2 > 0 and Close > MA_Value then
begin
Grid_current_base = grid_current_base + value2 * grid_gap;
Grid_current_ord = grid_current_ord + value2;
Grid_sellcount = grid_sellcount + value2;
SetPosition(Position – value2, label : = "網格: 上漲賣出");
LastTradePrice = Close;
// 計算盈虧
TotalProfit = TotalProfit + (Close – grid_base) * value2;
end;
end
else
if Close <= grid_current_base – grid_gap then
begin
Value2 = grid_current_ord – IntPortion ( (grid_current_base – Close) / grid_gap);
If value2 <= -1 * grid_maxcount then value2 = -1 * grid_maxcount;
Value2 = grid_current_ord – value2; //往下移動的網格數
// 只在價格低於MA 時允許做多
If value2 > 0 and Close < MA_Value then
begin
grid_current_base = grid_current_base – value2 * grid_gap;
grid_current_ord = grid_current_ord – value2;
grid_buycount = grid_buycount + value2;
SetPosition (Position + value2, label: = "網格: 下跌買進");
LastTradePrice = Close;
//計算盈虧
TotalProfit = TotalProfit + (grid_base – Close) * value2;
end;
end;
end;
end;
5 評論