請教小幫手大大及版上先進:
我的交易邏輯如下。
1.5MA、10MA 黃金交叉買進固定金額100萬的部位
2.以第一次進場(底倉)的成本價為基準,上漲+10%加碼100萬的部位
3.加碼後以第二次進場(加碼單)的進場價為基準,上漲+10%再加碼100萬的部位。
共計最多進場3次(1次底倉2次加碼)。
5.成交價跌破20MA全數出場
問題: 以回測股票代號2498為例。
現在的狀況是乎是出現在entry_count的計數上。它總是會跳回到1,因此變成不斷循環在第1次的加碼處,直至總資金耗盡。
我試圖以變數entry_count來控制加碼的次數,但試了幾種不同寫法,出來的結果似乎都不是我想要的。
我的寫法是否有問題?請問類似像這種固額金額加碼或是限制加碼次數的交易邏輯該怎麼寫呢?還請先進不吝指教。謝謝!!
原始碼如下
{
交易邏輯:
1.5MA、10MA 黃金交叉買進固定金額100萬的部位
2.以第一次進場(底倉)的成本價為基準,上漲+10%加碼100萬的部位
3.加碼後以第二次進場(加碼單)的進場價為基準,上漲+10%再加碼100萬的部位。
共計最多進場3次(一次底倉2次加碼)。
4.成交價跌破20MA全數出場
問題: 以回測股票代號2498為例。
現在的狀況是乎是出現在entry_count的計數上。它總是會跳回到1,因此變成不斷循環在第1次的加碼處,直至總資金耗盡。
}
input : ordersize(100, "每筆交易金額(萬)");
var:long_condition(false);
var:exit_long_condition(false);
var:entry_count(0); //進場次數
long_condition = crossOver(average(close,5),average(close,10)); //進場條件。5MA、10MA 黃金交叉
exit_long_condition = crossUnder(close,average(close,20)); //出場條件。成交價跌破20MA
// 底倉進場 //
if filled = 0 and long_condition then
begin
var: order_price(0);{ 預期委託價格 }
var: order_qty(0);{ 換算後數量 }
order_price = close[1]*1.1; //預期成交價格
order_qty = ((ordersize*10000) / (order_price * 1000)); //換算後下單量
SetPosition(order_qty);
entry_count = 1; //計數進場次數
print(file("C:\SysJust\XQLite\XS\Print\test.log"),
"Date=", NumToStr(Date, 0),"symbol=",symbol,"Close=", NumToStr(Close, 2),"trade_count=", NumToStr(filledRecordCount, 2),
"possition=",NumToStr(position, 2),"filled=",NumToStr(filled, 2),
"entry_count=", NumToStr(entry_count, 2));
end;
// 第一次加碼 //
if entry_count = 1 and filled <> 0 then
begin
var:first_order_cost(0); //母單成本價
var:first_plratio(0); //母單進場後漲跌幅
first_order_cost = filledRecordPrice(filledRecordCount);
if first_order_cost<>0 then first_plratio = ((close-first_order_cost)/first_order_cost)*100; //停損幅度
if first_plratio > 10 then
begin
order_price = close[1]*1.1; //預期成交價格
order_qty = ((ordersize*10000) / (order_price * 1000)); //換算後下單量
SetPosition(position + order_qty); //加碼
//SetPosition(position + 2); //加碼
entry_count = 2; //計數進場次數
end;
print(file("C:\SysJust\XQLite\XS\Print\test1.log"),
"Date=", NumToStr(Date, 0),"symbol=",symbol,"Close=", NumToStr(Close, 2),"trade_count=", NumToStr(filledRecordCount, 2),
"possition=",NumToStr(position, 2),"filled=",NumToStr(filled, 2),
"entry_count=", NumToStr(entry_count, 2));
end;
// 第二次加碼 //
if entry_count = 2 and Filled <> 0 then
begin
var:second_order_cost(0); //加碼單成本價
var:second_plratio(0);//加碼單漲跌幅
second_order_cost = filledRecordPrice(filledRecordCount);
if second_order_cost<>0 then second_plratio = ((close-second_order_cost)/second_order_cost)*100; //停損幅度
if second_plratio > 10 then
begin
order_price = close[1]*1.1; //預期成交價格
order_qty = ((ordersize*10000) / (order_price * 1000)); //換算後下單量
SetPosition(position + order_qty); //加碼
//SetPosition(position + 3); //加碼
entry_count = 3; //計數進場次數
end;
print(file("C:\SysJust\XQLite\XS\Print\test2.log"),
"Date=", NumToStr(Date, 0),"symbol=",symbol, //"Close=", NumToStr(Close, 2),"trade_count=", NumToStr(filledRecordCount, 2),
"possition=",NumToStr(position, 2),"filled=",NumToStr(filled, 2),
"entry_count=", NumToStr(entry_count, 2));
end;
// 出場 //
if Position <> 0 and exit_long_condition then
begin
SetPosition(0);
entry_count = 0; //進場次數歸零
print(file("C:\SysJust\XQLite\XS\Print\test3.log"),
"Date=", NumToStr(Date, 0),"symbol=",symbol,"Close=", NumToStr(Close, 2),"trade_count=", NumToStr(filledRecordCount, 2),
"possition=",NumToStr(position, 2),"filled=",NumToStr(filled, 2),
"entry_count=", NumToStr(entry_count, 2));
end;
print(file("C:\SysJust\XQLite\XS\Print\test4.log"),
"Date=", NumToStr(Date, 0),"symbol=",symbol,"Close=", NumToStr(Close, 2),"trade_count=", NumToStr(filledRecordCount, 2),
"possition=",NumToStr(position, 2),"filled=",NumToStr(filled, 2),
"entry_count=", NumToStr(entry_count, 2));
2 評論