20260322 XQ多空網格程式碼
-
20260322 XQ多空網格程式碼
最近可能有人會想要用空方網格,寫了一個可以多空切換的網格,而且改良之前的做法,只要價格落在設定區間內,就會自動買賣操作,之前程式碼是賣光就停了,價格回到區間也不會再啟動,大概功能說明
- 可選擇要做多還是做空
- 不須設定進場價,只要價格在區間就會自動交易
- 會有基本倉買入,如開啟時,現在價格距上方還有3格,就會買三張/口,如果不想要一開始就買多張,上界價格要跟現價接近
- 增加停損機制,可以設定觸碰到某個價位,就全部出場,避免擴大損失
自動交易設定
- 設定網距時,要能整除上下界區間
- 停損位置,多方網格要在下界以下,空方網格要在上界以上,才不會一直停損
- 用分K逐筆
- 部位依庫存或延續之前,如果你還會去調整庫存,請用延續之前
- 安控一定要加,尤其是最大購買口數
PS: 程式碼只做了回測,未實單驗證實際執行情況,請小心使用,交易安控一定要設置,我不負責程式碼問題造成的損失
PS: VIP社團會有進階版,上下界可以隨著價格自動上調,想要的就請加入A+策略網的VIP會員吧
程式碼
// A+策略網 20260321
// 網格連續交易版 v1
// 邏輯說明: 僅在設定區間內交易
Input: _tradeDir(1, "網格方向", inputkind:=Dict(["做多網格", 1], ["做空網格", -1]), quickedit:=true);
Input: _gridTop(31400, "網格上界");
Input: _gridBottom(30000, "網格下界");
Input: _gridgap(200, "網距");
Input: _positionmulti(1, "每次口數");
Input: _exitall(29500, "停損點(多單跌破/空單突破)");
Vars: intraBarPersist _upline(0), intraBarPersist _dnline(0);
Vars: intraBarPersist _lastFilled(0);
Vars: intraBarPersist _init_done(false);
Vars: intraBarPersist _dynGridTop(0), intraBarPersist _dynGridBottom(0);
Vars: _m4(0), _targetQty(0), _targetPos(0);
condition1 = (CurrentTime >= 134400 and CurrentTime <= 134500) or (CurrentTime >= 045900 and CurrentTime <= 050000);
Once begin
_m4 = _gridgap / 4;
end;
// 階段一: 啟動時的庫存偵測與網格定位
if _init_done = false then begin
_dynGridTop = _gridTop;
_dynGridBottom = _gridBottom;
if _tradeDir = 1 then begin
if filled > 0 then begin
value1 = filled / _positionmulti;
_upline = _dynGridTop - (value1 - 1) * _gridgap;
_dnline = _upline - (_gridgap * 2);
_lastFilled = filled;
_init_done = true;
end else if filled = 0 and close >= _dynGridBottom and close <= _dynGridTop then begin
_targetQty = IntPortion((_dynGridTop - close) / _gridgap) * _positionmulti;
if _targetQty > 0 then SetPosition(_targetQty, market);
end;
end else begin
if filled < 0 then begin
value1 = AbsValue(filled) / _positionmulti;
_dnline = _dynGridBottom + (value1 - 1) * _gridgap;
_upline = _dnline + (_gridgap * 2);
_lastFilled = filled;
_init_done = true;
end else if filled = 0 and close >= _dynGridBottom and close <= _dynGridTop then begin
_targetQty = IntPortion((close - _dynGridBottom) / _gridgap) * _positionmulti * -1;
if _targetQty < 0 then SetPosition(_targetQty, market);
end;
end;
end;
// 階段二: 庫存變動驅動網格平移
if _init_done = true and filled <> _lastFilled then begin
if _tradeDir = 1 then begin
if filled > _lastFilled then begin
_upline = _dnline + _gridgap;
_dnline = _dnline - _gridgap;
end else if filled < _lastFilled then begin
_dnline = _upline - _gridgap;
_upline = _upline + _gridgap;
end;
end else begin
if filled < _lastFilled then begin
_dnline = _upline - _gridgap;
_upline = _upline + _gridgap;
end else if filled > _lastFilled then begin
_upline = _dnline + _gridgap;
_dnline = _dnline - _gridgap;
end;
end;
_lastFilled = filled;
if filled > 0 and _tradeDir = 1 then begin
value1 = filled / _positionmulti;
_dynGridTop = _upline + (value1 - 1) * _gridgap;
_dynGridBottom = _dynGridTop - (_gridTop - _gridBottom);
end else if filled < 0 and _tradeDir = -1 then begin
value1 = AbsValue(filled) / _positionmulti;
_dynGridBottom = _dnline - (value1 - 1) * _gridgap;
_dynGridTop = _dynGridBottom + (_gridTop - _gridBottom);
end;
end;
// 階段三: 進行預掛委託 (區間封閉模式)
if _init_done = true and condition1 = False then begin
if _tradeDir = 1 then begin
if filled >= 1 * _positionmulti then begin
if close >= (_upline - _m4) then begin
_targetPos = filled - _positionmulti;
if position <> _targetPos then SetPosition(_targetPos, _upline);
end;
end;
if _dnline >= _dynGridBottom then begin
if close <= (_dnline + _m4) then begin
_targetPos = filled + _positionmulti;
if position <> _targetPos then SetPosition(_targetPos, _dnline);
end;
end;
end else begin
if filled <= -1 * _positionmulti then begin
if close <= (_dnline + _m4) then begin
_targetPos = filled + _positionmulti;
if position <> _targetPos then SetPosition(_targetPos, _dnline);
end;
end;
if _upline <= _dynGridTop then begin
if close >= (_upline - _m4) then begin
_targetPos = filled - _positionmulti;
if position <> _targetPos then SetPosition(_targetPos, _upline);
end;
end;
end;
end;
// 例外與出場管理
if position <> filled and condition1 then begin
SetPosition(filled, market);
end;
if _tradeDir = 1 and close <= _exitall and filled > 0 then begin
SetPosition(0, market);
_init_done = false;
end;
if _tradeDir = -1 and close >= _exitall and filled < 0 then begin
SetPosition(0, market);
_init_done = false;
end;
Log in to reply.