Trend Following Code สูตรโค้ดสำหรับโปรแกรม AmiBroker ซึ่งใน TQ AFL Template ในการลำดับโค้ดให้เป็นระเบียบและหมวดหมู่ เหมาะสำหรับมือใหม่ในการเขียน Strategy
ก่อนดูวีดีโอชุดนี้ ขอแนะนำว่าให้ดูวีดีโอ Trend Following Strategy Discussion ก่อนน่ะครับ
วีดีโอสำหรับ Trend Following Code in AmiBroker:
สูตรสำหรับ Trend Following Code in AmiBroker:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
{/*DESCRIPTION Author: ThaiQuants.com Strategy: Basic Trend Following Strategy, MA cross MA Trades: Long only Result: Acceptable */} {//OPTION SetOption("InitialEquity", 1000000); SetOption("MaxOpenPositions", 20); SetOption("MinShares", 100); RoundLotSize = 100; SetOption("CommissionMode", 1); SetOption("CommissionAmount", 0.16); SetTradeDelays(1, 1, 0, 0); BuyPrice = SellPrice = Open; } {//SPLIT C1 = Ref(C, 1); O1 = Ref(O, 1); O2 = Ref(O, 2); detectSplit = 0.35; buyAvoidSplit = !(C1/O1 < 1-detectSplit) AND !(C1/O1 > 1+detectSplit) AND !(O2/C1 < 1-detectSplit) AND !(O2/C1 > 1+detectSplit); sellAvoidSplit = (C1/O1 < 1-detectSplit) OR (C1/O1 > 1+detectSplit) OR (O2/C1 < 1-detectSplit) OR (O2/C1 > 1+detectSplit); } {//SIGNAL //Stock Conditions buyCon1 = BarsSince(Cross(MA(C, 10),MA(C, 20))) < 10 AND C > 1; buyCon2 = ADX() > 25 AND PDI() > MDI(); buyCon3 = MACD() > Signal(); buyCon4 = MA(C, 20) > MA(C, 60); buyCon5 = C*V > Ref(HHV(C*V, 20), -1) AND C*V > 1000000; sellCon1 = MACD() < Signal(); sellCon2 = MA(C, 20) < MA(C, 60); //Executing Signals Buy = buyCon1 AND buyCon2 AND buyCon3 AND buyCon4 AND buyCon5 AND buyAvoidSplit; Sell = (sellCon1 AND sellCon2) OR sellAvoidSplit; Buy = ExRem(Buy, Sell); Sell = ExRem(Sell, Buy); Short = Cover = 0; } {//POSITION SetPositionSize(5, spsPercentOfEquity); PositionScore = C*V/Ref(MA(C*V, 20), -1); } {//STOP ApplyStop(stopTypeLoss, stopModePercent, 10); ApplyStop(stopTypeProfit, stopModePercent, 25); ApplyStop(stopTypeTrailing, stopModePercent, 20); } {//MC } |