程式有錯誤碼

  •   359 
  • 最後發表   Meli  2025 二月 20
Meli 發文於   2025/02/17

input: grid_gap(150, "每格點數");

input: grid_maxcount (2, "最多格數");

input: stoploss_point (300, "停損(點)");

input: MinTradeDifference (50,"最小交易價差");

{變量設置}

var: intrabarpersist grid_started(false);

var: intrabarpersist grid_base(0);

var: intrabarpersist grid_current_base(0);

var: intrabarpersist grid_current_ord(0);

var: intrabarpersist grid_buycount(0);

var: intrabarpersist grid_sellcount(0);

var: intrabarpersist LastTradePrice(0);

{啟動網格交易邏輯}

if not grid_started and GetInfo("TradeMode") = 1 then begin

grid_started =true;

grid_base = Close;

grid_current_base = close;

grid_current_ord = 0;

grid_buycount = 0;

grid_sellcount = 0;

LastTradePrice = close;

Print ("=> 啟動網格交易,中心點:", numtostr (grid_current_base, 0));

end;

{交易邏輯}

if grid_base <> 0 then begin

{停損機制: 價格超過網格中心 停損點 平倉}

if close >=grid_base + stoploss_point or close<= grid_base-stoploss_point then begin

SetPosition (0, label:= "網格: 停損出場");

grid_base = 0; //停止網格交易

end else begin

{價格突破網格判斷,計算穿越的網格數量}

if close>= grid_current_base + grid_gap then begin

value1=grid_current_ord + IntPortion (( close-grid_current_base) / grid_gap);

 

上面的程式在黑體字中有問題,跑compilation一直出現錯誤,不知是那裏出問題?

 

附加文件

排序方式: 標準 | 最新
XS小編 發文於   2025/02/19

 Hello Meli,

 

小編建議您在編寫腳本的時候適度的縮排,會比較容易確認 begin 是否有搭配的 end 來結束。

從交易邏輯開始的 begin 沒有對應的 end 作結。

以下提供一種解法 (可能和您所需的不同):

{交易邏輯}

if grid_base <> 0 then begin

{停損機制: 價格超過網格中心 停損點 平倉}

    if close >=grid_base + stoploss_point or close<= grid_base-stoploss_point then begin

        SetPosition (0, label:= "網格: 停損出場");

        grid_base = 0; //停止網格交易

        end; 

end else begin

{價格突破網格判斷,計算穿越的網格數量}

    if close>= grid_current_base + grid_gap then begin

        value1=grid_current_ord + IntPortion (( close-grid_current_base) / grid_gap);

        end;

    end;

Meli 發文於   2025/02/19

感謝你的回覆,

這裡想要請教一個問題,關於你提到的縮排,我理解,但是發現有好幾個loop時,我就confused。下面這個程式也跑不出來。應該也是縮牌沒有對到。可以幫我看一下嗎?感謝

 

 

 

附加文件

虎科大許教授 發文於   2025/02/19

當好幾個Loop或是很多的IF,縮排更是需要。沒看到你所謂的「下面這個程式」。

Meli 發文於   2025/02/20

放在附加檔案,我有看到,你沒看到嗎? 我直接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;

 

虎科大許教授 發文於   2025/02/20

//學著找出錯誤很重要。你的程式碼很多全形的符號,例如減號、冒號等等。
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 TotalProfit >= 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;

  • 按讚來自於
  • YEAH301000
發表回覆
Close