請問我要如何修正以下的高頻自動交易?我希望在上行的BBI線趨勢中,若股價在BBI線之上,每靠近BBI線十點以內,買入一口做多,若在下行的BBI線趨勢中,若股價在BBI線之下,也是每靠近BBI線十點以內,賣出一口做空,然後不管做多做空,都獲利十點之後停利。若發生虧損十點之後停損,然後買賣完成後再依照相同邏輯再進場。
input:a1(3,"第一根均線天期"),a2(6,"第二根均線天期"),a3(12,"第三根均線天期"),a4(24,"第四根均線天期"),n(2,"格局期數");
input:_angle(5,"BBI均線角度"),m(20,"ATR(5)閾值"),_priceThreshold(10,"價格閾值"),_stopProfit(15,"停利點數"),_stopLoss(10,"停損點數");
variable:BBI(0),EBBI(0);
BBI=(average(close,a1)+average(close,a2)+average(close,a3)+average(close,a4))/4;
EBBI=(Xaverage(close,a1)+Xaverage(close,a2)+Xaverage(close,a3)+Xaverage(close,a4))/4;
value1=EBBI-BBI;//趨勢強弱
condition1=LinearRegAngle(BBI,3)>_angle AND trueAll(BBI>BBI[1],n);//多方格局
condition2=LinearRegAngle(BBI,3)<-_angle AND trueAll(BBI<BBI[1],n);//空方格局
condition3=value1>0;//多方趨勢
condition4=value1<0;//空方趨勢
condition5=time >= 084500 and time < 133500;//日盤
condition6=time >= 150000 or time < 045000;//夜盤
condition7=time >= 133500 and time <= 134500;//日盤出清
condition8=time >= 045000 and time <= 050000;//夜盤出清
if barfreq <> "Min" or barinterval <> 5 then RaiseRuntimeError("請設定頻率為5分鐘");
if condition5 or condition6 then begin
// 進場條件
if position = 0 and filled = 0 then begin
if condition1 and condition3 and ATR(5)>m and close > BBI and close <= BBI + _priceThreshold then begin
SetPosition(1,BBI + _priceThreshold);// 買進 1 張做
end;
if condition2 and condition4 and ATR(5)>m and close < BBI and close >= BBI - _priceThreshold then begin
SetPosition(-1,BBI - _priceThreshold); // 賣出 1 張做空
end;
end;
// 出場條件
if position = 1 and filled = 1 then begin
if close >= filledAvgPrice+ _stopProfit then SetPosition(0,filledAvgPrice+ _stopProfit );// 多頭停利
if close <= filledAvgPrice - _stopLoss then SetPosition(0,filledAvgPrice - _stopLoss);// 多頭停損
end;
if position = -1 and filled = -1 then begin
if close <= filledAvgPrice - _stopProfit then SetPosition(0,filledAvgPrice - _stopProfit);// 空頭停利
if close >= filledAvgPrice + _stopLoss then SetPosition(0,filledAvgPrice + _stopLoss);// 空頭停損
end;
end;
if (condition7 or condition8) and filled <> 0 then setposition(0);
對了,我是要盤中一達到條件就買賣交易,不是等K棒收完再判斷,所以是要用逐筆洗價的,請看如何改才對?謝謝
2 評論