小幫手你好:
如圖及程式碼, 在回測時(股票:0050), 在2022/10/12 時, 應該買進"2"張就好,
但, 為何回測會出現賣出3張, 再買進3張這種不必要的交易!?
另外, 我在日K下回測10年, 但在2019之前回都沒有資料?
謝謝幫忙!
input: InitCapital(100, "初始資金(萬)");
input: AddCurrency(20, "每年加碼(萬)");
input: AddTime(1, "加碼月份");
input: Ratio(50, "投入百分比");
input: ControlUp(60, "動態調整上限");
input: ControlLow(40, "動態調整下限");
var: intraBarPersist _capital(0); //總資產
var: intraBarPersist _stock(0); //股票資產
var: intraBarPersist _stocks(0); //股票數量
var: intraBarPersist _currency(0); //現金
var: _stockRatio(0); //股票比例
var: intraBarPersist _extra(false); //入金指標
var: qty(0); //目前股票數量
if currentBar=1 then _currency = InitCapital;
_stock = _stocks*close/10; //股票資產(萬)
_capital = _currency + _stock; //淨資產(萬)
_stockRatio = _stock/_capital*100; //股票比例(%)
if month(date)=AddTime and _extra=false then begin
_currency += AddCurrency;
_extra = true;
end;
if month(date)<>AddTime then _extra=false;
if _stockRatio[1] > ControlUp or _stockRatio[1] < ControlLow then begin
qty = intPortion(_capital*Ratio/100/close[1]*10);
end;
if currentBar>1 and position<>qty then begin
setposition(qty);
_stocks = qty;
_currency = _capital - _stocks*close/10;
end;




7 評論