【解説】 Moving Average :
【シグナル】
売りシグナル:移動平均がロウソク足を上から下を抜いたら。
買いシグナル:移動平均がロウソク足を下から上を抜いたら。
//+------------------------------------------------------------------+ //| Moving Average.mq4 | //| Copyright 2005, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #define MAGICMA 20050610 extern double Lots = 0.1; extern double MaximumRisk = 0.02; extern double DecreaseFactor = 3; extern double MovingPeriod = 12; extern double MovingShift = 6; //+------------------------------------------------------------------+ //| Calculate open positions | //+------------------------------------------------------------------+ int CalculateCurrentOrders(string symbol) { int buys=0,sells=0; //---- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA) { if(OrderType()==OP_BUY) buys++; if(OrderType()==OP_SELL) sells++; } } //---- return orders volume if(buys>0) return(buys); else return(-sells); } //+------------------------------------------------------------------+ //| Calculate optimal lot size | //+------------------------------------------------------------------+ double LotsOptimized() { double lot=Lots; int orders=HistoryTotal(); // history orders total int losses=0; // number of losses orders without a break //---- select lot size lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1); //---- calcuulate number of losses orders without a break if(DecreaseFactor>0) { for(int i=orders-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; } if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue; //---- if(OrderProfit()>0) break; if(OrderProfit()<0) losses++; } if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1); } //---- return lot size if(lot<0.1) lot=0.1; return(lot); } //+------------------------------------------------------------------+ //| Check for open order conditions | //+------------------------------------------------------------------+ void CheckForOpen() { double ma; int res; //---- go trading only for first tiks of new bar if(Volume[0]>1) return; //---- get Moving Average ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); //---- sell conditions ★(売りの条件)★ if(Open[1]>ma && Close[1]<ma) { res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red); return; } //---- buy conditions ★(買いの条件)★ if(Open[1]<ma && Close[1]>ma) { res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue); return; } //---- } //+------------------------------------------------------------------+ //| Check for close order conditions | //+------------------------------------------------------------------+ void CheckForClose() { double ma; //---- go trading only for first tiks of new bar if(Volume[0]>1) return; //---- get Moving Average ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0); //---- for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue; //---- check order type if(OrderType()==OP_BUY) { if(Open[1]>ma && Close[1]<ma) OrderClose(OrderTicket(),OrderLots(),Bid,3,White); break; } if(OrderType()==OP_SELL) { if(Open[1]<ma && Close[1]>ma) OrderClose(OrderTicket(),OrderLots(),Ask,3,White); break; } } //---- } //+------------------------------------------------------------------+ //| Start function | //+------------------------------------------------------------------+ void start() { //---- check for history and trading if(Bars<100 || IsTradeAllowed()==false) return; //---- calculate open orders by current symbol if(CalculateCurrentOrders(Symbol())==0) CheckForOpen(); else CheckForClose(); //---- } //+------------------------------------------------------------------+
【バックテスト】
Strategy Tester: Moving Average Strategy Tester ReportMoving AverageFOREX.comJapan-Demo(I) (Build 229)
通貨ペア EURUSDfxf. (Euro vs US Dollar) 期間 5分足(M5) 2011.03.23 21:45 - 2011.04.01 20:55 (2011.01.01 - 2011.04.02) モデル Every tick (the most precise method based on all available least timeframes) パラメーター Lots=0.1; MaximumRisk=0.02; DecreaseFactor=3; MovingPeriod=12; MovingShift=6; Bars in test 2106 Ticks modelled 103973 Modelling quality 38.45% Mismatched charts errors 11 Initial deposit 10000.00 Total net profit -359.40 Gross profit 635.30 Gross loss -994.70 Profit factor 0.64 Expected payoff -4.18 Absolute drawdown 368.40 Maximal drawdown 506.00 (4.99%) Relative drawdown 4.99% (506.00) Total trades 86 Short positions (won %) 33 (9.09%) Long positions (won %) 53 (26.42%) Profit trades (% of total) 17 (19.77%) Loss trades (% of total) 69 (80.23%) Largest profit trade 122.00 loss trade -83.80 Average profit trade 37.37 loss trade -14.42 Maximum consecutive wins (profit in money) 2 (131.00) consecutive losses (loss in money) 15 (-186.00) Maximal consecutive profit (count of wins) 131.00 (2) consecutive loss (count of losses) -186.00 (15) Average consecutive wins 1 consecutive losses 5
# 時間 取引種別 注文番号 数量 Price S/L:決済逆指値 T/P:決済指値 損益 Balance 1 2011.03.23 23:20 buy 1 0.20 1.4103 0.0000 0.0000 2 2011.03.24 06:30 close 1 0.20 1.4085 0.0000 0.0000 -35.40 9964.60 3 2011.03.24 06:35 buy 2 0.20 1.4089 0.0000 0.0000 4 2011.03.24 06:50 close 2 0.20 1.4082 0.0000 0.0000 -14.00 9950.60 5 2011.03.24 08:00 buy 3 0.10 1.4075 0.0000 0.0000 6 2011.03.24 11:10 close 3 0.10 1.4120 0.0000 0.0000 45.00 9995.60 7 2011.03.24 11:25 buy 4 0.20 1.4132 0.0000 0.0000 8 2011.03.24 14:15 close 4 0.20 1.4125 0.0000 0.0000 -14.00 9981.60 9 2011.03.24 14:20 buy 5 0.20 1.4134 0.0000 0.0000 10 2011.03.24 16:05 close 5 0.20 1.4192 0.0000 0.0000 116.00 10097.60 11 2011.03.24 16:35 buy 6 0.20 1.4204 0.0000 0.0000 12 2011.03.24 16:55 close 6 0.20 1.4180 0.0000 0.0000 -48.00 10049.60 13 2011.03.24 18:25 buy 7 0.20 1.4185 0.0000 0.0000 14 2011.03.24 18:30 close 7 0.20 1.4177 0.0000 0.0000 -16.00 10033.60 15 2011.03.24 18:40 buy 8 0.10 1.4187 0.0000 0.0000 16 2011.03.24 19:05 close 8 0.10 1.4180 0.0000 0.0000 -7.00 10026.60 17 2011.03.24 19:25 buy 9 0.10 1.4184 0.0000 0.0000 18 2011.03.24 19:40 close 9 0.10 1.4180 0.0000 0.0000 -4.00 10022.60 19 2011.03.24 21:50 sell 10 0.10 1.4169 0.0000 0.0000 20 2011.03.24 23:50 close 10 0.10 1.4180 0.0000 0.0000 -11.00 10011.60 21 2011.03.25 00:00 sell 11 0.10 1.4169 0.0000 0.0000 22 2011.03.25 00:10 close 11 0.10 1.4174 0.0000 0.0000 -5.00 10006.60 23 2011.03.25 00:15 sell 12 0.10 1.4168 0.0000 0.0000 24 2011.03.25 00:30 close 12 0.10 1.4177 0.0000 0.0000 -9.00 9997.60 25 2011.03.25 01:50 buy 13 0.10 1.4181 0.0000 0.0000 26 2011.03.25 02:15 close 13 0.10 1.4172 0.0000 0.0000 -9.00 9988.60 27 2011.03.25 04:15 buy 14 0.10 1.4172 0.0000 0.0000 28 2011.03.25 05:05 close 14 0.10 1.4167 0.0000 0.0000 -5.00 9983.60 29 2011.03.25 05:50 buy 15 0.10 1.4176 0.0000 0.0000 30 2011.03.25 09:40 close 15 0.10 1.4156 0.0000 0.0000 -20.00 9963.60 31 2011.03.25 09:55 sell 16 0.10 1.4151 0.0000 0.0000 32 2011.03.25 10:00 close 16 0.10 1.4161 0.0000 0.0000 -10.00 9953.60 33 2011.03.25 10:15 buy 17 0.10 1.4168 0.0000 0.0000 34 2011.03.25 10:55 close 17 0.10 1.4157 0.0000 0.0000 -11.00 9942.60 35 2011.03.25 11:10 buy 18 0.10 1.4168 0.0000 0.0000 36 2011.03.25 11:55 close 18 0.10 1.4161 0.0000 0.0000 -7.00 9935.60 37 2011.03.25 13:35 buy 19 0.10 1.4139 0.0000 0.0000 38 2011.03.25 13:45 close 19 0.10 1.4123 0.0000 0.0000 -16.00 9919.60 39 2011.03.25 14:00 sell 20 0.10 1.4125 0.0000 0.0000 40 2011.03.25 14:30 close 20 0.10 1.4133 0.0000 0.0000 -8.00 9911.60 41 2011.03.25 16:05 sell 21 0.10 1.4134 0.0000 0.0000 42 2011.03.25 18:35 close 21 0.10 1.4079 0.0000 0.0000 55.00 9966.60 43 2011.03.25 19:00 sell 22 0.20 1.4071 0.0000 0.0000 44 2011.03.25 20:00 close 22 0.20 1.4077 0.0000 0.0000 -12.00 9954.60 45 2011.03.27 23:55 buy 23 0.20 1.4033 0.0000 0.0000 46 2011.03.28 03:35 close 23 0.20 1.4057 0.0000 0.0000 48.20 10002.80 47 2011.03.28 05:00 buy 24 0.20 1.4060 0.0000 0.0000 48 2011.03.28 05:15 close 24 0.20 1.4053 0.0000 0.0000 -14.00 9988.80 49 2011.03.28 05:45 buy 25 0.20 1.4058 0.0000 0.0000 50 2011.03.28 05:50 close 25 0.20 1.4049 0.0000 0.0000 -18.00 9970.80 51 2011.03.28 06:00 buy 26 0.10 1.4059 0.0000 0.0000 52 2011.03.28 06:50 close 26 0.10 1.4049 0.0000 0.0000 -10.00 9960.80 53 2011.03.28 09:15 buy 27 0.10 1.4077 0.0000 0.0000 54 2011.03.28 09:35 close 27 0.10 1.4060 0.0000 0.0000 -17.00 9943.80 55 2011.03.28 11:00 buy 28 0.10 1.4058 0.0000 0.0000 56 2011.03.28 11:05 close 28 0.10 1.4047 0.0000 0.0000 -11.00 9932.80 57 2011.03.28 11:15 buy 29 0.10 1.4055 0.0000 0.0000 58 2011.03.28 11:30 close 29 0.10 1.4044 0.0000 0.0000 -11.00 9921.80 59 2011.03.28 11:35 buy 30 0.10 1.4054 0.0000 0.0000 60 2011.03.28 15:40 close 30 0.10 1.4102 0.0000 0.0000 48.00 9969.80 61 2011.03.28 16:15 buy 31 0.20 1.4108 0.0000 0.0000 62 2011.03.28 16:25 close 31 0.20 1.4097 0.0000 0.0000 -22.00 9947.80 63 2011.03.28 18:30 buy 32 0.20 1.4099 0.0000 0.0000 64 2011.03.28 19:45 close 32 0.20 1.4093 0.0000 0.0000 -12.00 9935.80 65 2011.03.28 21:00 sell 33 0.10 1.4086 0.0000 0.0000 66 2011.03.28 22:15 close 33 0.10 1.4090 0.0000 0.0000 -4.00 9931.80 67 2011.03.28 22:40 sell 34 0.10 1.4081 0.0000 0.0000 68 2011.03.29 00:15 close 34 0.10 1.4081 0.0000 0.0000 -1.50 9930.30 69 2011.03.29 00:25 buy 35 0.10 1.4082 0.0000 0.0000 70 2011.03.29 01:15 close 35 0.10 1.4064 0.0000 0.0000 -18.00 9912.30 71 2011.03.29 02:25 sell 36 0.10 1.4073 0.0000 0.0000 72 2011.03.29 02:35 close 36 0.10 1.4080 0.0000 0.0000 -7.00 9905.30 73 2011.03.29 03:00 buy 37 0.10 1.4079 0.0000 0.0000 74 2011.03.29 03:05 close 37 0.10 1.4073 0.0000 0.0000 -6.00 9899.30 75 2011.03.29 03:10 buy 38 0.10 1.4080 0.0000 0.0000 76 2011.03.29 09:05 close 38 0.10 1.4127 0.0000 0.0000 47.00 9946.30 77 2011.03.29 12:30 sell 39 0.20 1.4073 0.0000 0.0000 78 2011.03.29 12:45 close 39 0.20 1.4078 0.0000 0.0000 -10.00 9936.30 79 2011.03.29 12:55 sell 40 0.20 1.4069 0.0000 0.0000 80 2011.03.29 14:50 close 40 0.20 1.4084 0.0000 0.0000 -30.00 9906.30 81 2011.03.29 14:55 sell 41 0.10 1.4069 0.0000 0.0000 82 2011.03.29 15:25 close 41 0.10 1.4088 0.0000 0.0000 -19.00 9887.30 83 2011.03.29 16:30 sell 42 0.10 1.4080 0.0000 0.0000 84 2011.03.29 16:40 close 42 0.10 1.4086 0.0000 0.0000 -6.00 9881.30 85 2011.03.29 17:05 buy 43 0.10 1.4088 0.0000 0.0000 86 2011.03.29 17:20 close 43 0.10 1.4082 0.0000 0.0000 -6.00 9875.30 87 2011.03.29 17:30 sell 44 0.10 1.4081 0.0000 0.0000 88 2011.03.29 17:35 close 44 0.10 1.4087 0.0000 0.0000 -6.00 9869.30 89 2011.03.29 17:50 sell 45 0.10 1.4079 0.0000 0.0000 90 2011.03.29 18:00 close 45 0.10 1.4091 0.0000 0.0000 -12.00 9857.30 91 2011.03.29 18:30 sell 46 0.10 1.4084 0.0000 0.0000 92 2011.03.29 18:55 close 46 0.10 1.4091 0.0000 0.0000 -7.00 9850.30 93 2011.03.29 19:05 sell 47 0.10 1.4085 0.0000 0.0000 94 2011.03.29 22:10 close 47 0.10 1.4112 0.0000 0.0000 -27.00 9823.30 95 2011.03.29 22:30 buy 48 0.10 1.4113 0.0000 0.0000 96 2011.03.30 00:30 close 48 0.10 1.4117 0.0000 0.0000 4.10 9827.40 97 2011.03.30 03:05 sell 49 0.20 1.4081 0.0000 0.0000 98 2011.03.30 03:15 close 49 0.20 1.4088 0.0000 0.0000 -14.00 9813.40 99 2011.03.30 04:35 buy 50 0.20 1.4091 0.0000 0.0000 100 2011.03.30 04:40 close 50 0.20 1.4084 0.0000 0.0000 -14.00 9799.40 101 2011.03.30 05:30 buy 51 0.10 1.4091 0.0000 0.0000 102 2011.03.30 06:15 close 51 0.10 1.4081 0.0000 0.0000 -10.00 9789.40 103 2011.03.30 06:20 buy 52 0.10 1.4089 0.0000 0.0000 104 2011.03.30 06:25 close 52 0.10 1.4082 0.0000 0.0000 -7.00 9782.40 105 2011.03.30 07:20 buy 53 0.10 1.4083 0.0000 0.0000 106 2011.03.30 09:05 close 53 0.10 1.4094 0.0000 0.0000 11.00 9793.40 107 2011.03.30 10:45 buy 54 0.20 1.4085 0.0000 0.0000 108 2011.03.30 12:35 close 54 0.20 1.4089 0.0000 0.0000 8.00 9801.40 109 2011.03.30 13:05 buy 55 0.20 1.4097 0.0000 0.0000 110 2011.03.30 13:10 close 55 0.20 1.4087 0.0000 0.0000 -20.00 9781.40 111 2011.03.30 14:45 buy 56 0.20 1.4083 0.0000 0.0000 112 2011.03.30 17:35 close 56 0.20 1.4122 0.0000 0.0000 78.00 9859.40 113 2011.03.30 17:40 buy 57 0.20 1.4130 0.0000 0.0000 114 2011.03.30 18:05 close 57 0.20 1.4124 0.0000 0.0000 -12.00 9847.40 115 2011.03.30 19:15 buy 58 0.20 1.4125 0.0000 0.0000 116 2011.03.30 20:05 close 58 0.20 1.4121 0.0000 0.0000 -8.00 9839.40 117 2011.03.30 20:25 buy 59 0.10 1.4129 0.0000 0.0000 118 2011.03.30 21:20 close 59 0.10 1.4122 0.0000 0.0000 -7.00 9832.40 119 2011.03.30 21:25 buy 60 0.10 1.4130 0.0000 0.0000 120 2011.03.30 21:40 close 60 0.10 1.4122 0.0000 0.0000 -8.00 9824.40 121 2011.03.30 21:45 buy 61 0.10 1.4132 0.0000 0.0000 122 2011.03.30 22:55 close 61 0.10 1.4122 0.0000 0.0000 -10.00 9814.40 123 2011.03.31 00:00 buy 62 0.10 1.4130 0.0000 0.0000 124 2011.03.31 00:10 close 62 0.10 1.4119 0.0000 0.0000 -11.00 9803.40 125 2011.03.31 00:15 buy 63 0.10 1.4126 0.0000 0.0000 126 2011.03.31 05:05 close 63 0.10 1.4135 0.0000 0.0000 9.00 9812.40 127 2011.03.31 05:20 buy 64 0.20 1.4152 0.0000 0.0000 128 2011.03.31 10:05 close 64 0.20 1.4213 0.0000 0.0000 122.00 9934.40 129 2011.03.31 11:20 sell 65 0.20 1.4208 0.0000 0.0000 130 2011.03.31 11:25 close 65 0.20 1.4219 0.0000 0.0000 -22.00 9912.40 131 2011.03.31 12:15 sell 66 0.20 1.4206 0.0000 0.0000 132 2011.03.31 15:00 close 66 0.20 1.4193 0.0000 0.0000 26.00 9938.40 133 2011.03.31 15:05 sell 67 0.20 1.4179 0.0000 0.0000 134 2011.03.31 15:20 close 67 0.20 1.4193 0.0000 0.0000 -28.00 9910.40 135 2011.03.31 15:30 sell 68 0.20 1.4180 0.0000 0.0000 136 2011.03.31 15:45 close 68 0.20 1.4196 0.0000 0.0000 -32.00 9878.40 137 2011.03.31 16:00 sell 69 0.10 1.4182 0.0000 0.0000 138 2011.03.31 16:10 close 69 0.10 1.4188 0.0000 0.0000 -6.00 9872.40 139 2011.03.31 16:15 buy 70 0.10 1.4191 0.0000 0.0000 140 2011.03.31 18:25 close 70 0.10 1.4192 0.0000 0.0000 1.00 9873.40 141 2011.03.31 18:35 buy 71 0.20 1.4200 0.0000 0.0000 142 2011.03.31 18:40 close 71 0.20 1.4195 0.0000 0.0000 -10.00 9863.40 143 2011.03.31 19:40 buy 72 0.20 1.4200 0.0000 0.0000 144 2011.04.01 00:40 close 72 0.20 1.4158 0.0000 0.0000 -83.80 9779.60 145 2011.04.01 00:45 buy 73 0.10 1.4166 0.0000 0.0000 146 2011.04.01 01:25 close 73 0.10 1.4161 0.0000 0.0000 -5.00 9774.60 147 2011.04.01 02:15 buy 74 0.10 1.4165 0.0000 0.0000 148 2011.04.01 02:40 close 74 0.10 1.4157 0.0000 0.0000 -8.00 9766.60 149 2011.04.01 03:15 buy 75 0.10 1.4163 0.0000 0.0000 150 2011.04.01 05:30 close 75 0.10 1.4170 0.0000 0.0000 7.00 9773.60 151 2011.04.01 06:00 buy 76 0.20 1.4178 0.0000 0.0000 152 2011.04.01 06:10 close 76 0.20 1.4171 0.0000 0.0000 -14.00 9759.60 153 2011.04.01 06:25 sell 77 0.20 1.4166 0.0000 0.0000 154 2011.04.01 07:10 close 77 0.20 1.4180 0.0000 0.0000 -28.00 9731.60 155 2011.04.01 07:20 sell 78 0.10 1.4167 0.0000 0.0000 156 2011.04.01 08:45 close 78 0.10 1.4160 0.0000 0.0000 7.00 9738.60 157 2011.04.01 09:15 sell 79 0.20 1.4144 0.0000 0.0000 158 2011.04.01 09:30 close 79 0.20 1.4162 0.0000 0.0000 -36.00 9702.60 159 2011.04.01 09:35 sell 80 0.20 1.4156 0.0000 0.0000 160 2011.04.01 09:50 close 80 0.20 1.4163 0.0000 0.0000 -14.00 9688.60 161 2011.04.01 09:55 sell 81 0.10 1.4156 0.0000 0.0000 162 2011.04.01 11:10 close 81 0.10 1.4162 0.0000 0.0000 -6.00 9682.60 163 2011.04.01 11:55 sell 82 0.10 1.4150 0.0000 0.0000 164 2011.04.01 12:00 close 82 0.10 1.4162 0.0000 0.0000 -12.00 9670.60 165 2011.04.01 14:15 sell 83 0.10 1.4096 0.0000 0.0000 166 2011.04.01 14:25 close 83 0.10 1.4116 0.0000 0.0000 -20.00 9650.60 167 2011.04.01 19:15 sell 84 0.10 1.4223 0.0000 0.0000 168 2011.04.01 20:25 close 84 0.10 1.4231 0.0000 0.0000 -8.00 9642.60 169 2011.04.01 20:30 sell 85 0.10 1.4223 0.0000 0.0000 170 2011.04.01 20:40 close 85 0.10 1.4228 0.0000 0.0000 -5.00 9637.60 171 2011.04.01 20:45 buy 86 0.10 1.4230 0.0000 0.0000 172 2011.04.01 20:59 close at stop 86 0.10 1.4233 0.0000 0.0000 3.00 9640.60
Back to Meta Trader