2013年11月12日 星期二

程式碼自動產生器的開發 Part 3

EasyTrader ArtNo 048
上篇中產生的隨機變數要先作是否交易條件成立的判定,部份程式碼範例如下

{Function: _MagicQS168_fRule Evaluate the price pattern. }
Inputs:
iRule(NumericSimple), { 符合必要的條件組數 }
PriceL[max1] (NumericArrayRef), { 運算符號左邊的價格 O/H/L/C}
PriceR[max2] (NumericArrayRef), { 運算符號右邊的價格 O/H/L/C}
CompareSign[max3](NumericArrayRef), { 運算符號選擇 >= , <= }
BarLeft[max4](NumericArrayRef), { 運算符號左邊的引用Bar位置 }
BarRight[max5](NumericArrayRef); { 運算符號右邊的引用Bar位置 }

Var: RuleVal(FALSE); { value of rule/pattern }
{這裡開始計算 }

If PriceL[iRule] = 0 then Begin
If PriceR[iRule] = 0 then Begin
If CompareSign[iRule] = 0 then
{判斷是否成立 ,如果RuleVal = true ,就將此參數傳回主程式作下單 }
RuleVal = Open[BarLeft[iRule]] >= Open[BarRight[iRule]]
else if CompareSign[iRule] = 1 then

RuleVal = Open[BarLeft[iRule]] <= Open[BarRight[iRule]];
end

else if PriceR[iRule] = 1 then Begin
If CompareSign[iRule] = 0 then
RuleVal = Open[BarLeft[iRule]] >= High[BarRight[iRule]]
else if CompareSign[iRule] = 1 then
RuleVal = Open[BarLeft[iRule]] <= High[BarRight[iRule]];
end
else if PriceR[iRule] = 2 then Begin
If CompareSign[iRule] = 0 then
RuleVal = Open[BarLeft[iRule]] >= Low[BarRight[iRule]]

....
....
_MagicQS168_fRule = RuleVal;


那麼主程式是什麼樣子呢 ?部份程式碼如下
{ System _MagicQS168 }

Inputs:
SystemNo (0), { 作優化的參數}
LongORshort (1), { 多單或空單 }
setNetProf(150000), { 最低淨利}
setMaxDD(500000), {最大回撤 MDD }
setMinTrade(20), {最低交易次數 }
setWinRate(20), {最低勝率}
setPFvalue(0.0), {最低獲利因子 }
setMaxTrade(1200), {最大交易次數}
setIntraday(0); {日內或留倉交易 }

Var:
NRules(0),
NBarExit(0),
ExitType(0), { 1 for SetStopLoss , 2 for SetPercentTarget ,3 for Highest/Lowest ,4 for SetProfitTarge}
TradeStopLoss(0), { stop loss }
Barlength(0) , { for Highest/Lowest Length }
TradeProfit(0) , { Target Profit}
PullBack(0), { pull back ratio}

LoopNo(0), { Loop counter }
ProfFac(0), { profit factor }
ReturnValue(false),
EntryCheck(false);

....
....

{取變數的亂數值}

If BarNumber = 1 then
ReturnValue = _MagicQS168_fVars(PriceL, PriceR, CompareSign, BarLeft, BarRight, NRules,
NBarExit,ExitType,TradeProfit,PullBack,BarLength,TradeStopLoss);

{判定是否下單條件成立}
EntryCheck = true;

For LoopNo = 0 to NRules - 1 Begin
EntryCheck = EntryCheck and _MagicQS168_fRule(LoopNo, PriceL, PriceR, CompareSign, BarLeft, BarRight);
End;

{下單}
{ Place the trade, either long or short }
If EntryCheck then Begin
If LongORshort > 0 then
Buy next bar at market
else if LongORshort < 0 then
Sell next bar at market ;
end;

{ 內定四種出場方式選擇}
{ Exit the open tradeSetStopLoss after entry }
if ExitType = 1 then Begin
SetStopLoss(TradeProfit * BigPointValue) ;
End;

{ Exit the open trade SetPercentTrailing after entry }
if ExitType = 2 then Begin
SetPercentTrailing(TradeProfit * BigPointValue , PullBack) ;
End;


{ Exit the open trade High/Low range after entry }
if ExitType = 3 then Begin
If MarketPosition > 0 then
ExitLong next bar at Lowest(Low,BarLength) stop
else if MarketPosition < 0 then
ExitShort next bar at Highest(High,BarLength) stop ;
End;

if ExitType = 4 then Begin
SetProfitTarget(TradeProfit * BigPointValue) ;
End; 


....
....

{ 符合參數條件的交易策略程式碼輸出}

If AbsValue(GrossLoss) > 0 then
ProfFac = GrossProfit/AbsValue(GrossLoss)
else
ProfFac = 100;

If LastBarOnChart and NetProfit >= setNetProf and
AbsValue(MaxIDDrawDown) <= AbsValue(setMaxDD) and
TotalTrades >= setMinTrade and TotalTrades < setMaxTrade and
PercentProfit >= setWinRate and
ProfFac >= setPFvalue then

ReturnValue = _MagicQS168_fCode(SystemNo, LongORshort, PriceL, PriceR, CompareSign, BarLeft,

BarRight, NRules, NBarExit,ExitType,TradeProfit,PullBack,BarLength,TradestopLoss,SetIntraDay, "R:\_MS168_CodeRpt.txt");

{---------- 待續 -----------}

沒有留言:

張貼留言