bleh
[mqlkit.git] / SpeedTrainNeuNeu.mq4
blob1e6a6f5d0dcd8d3bc0e7344c7b5dc033a826c854
1 #property copyright "Zarko Asenov"\r
2 #property link      "jaltoh.6x.to"\r
3 \r
4 /* General functionality settings */\r
5 extern string       _ = " General Settings ";\r
6 \r
7 extern int          Slippage = 3;\r
8 extern double       Lot_Size = 0.12;\r
9 extern double       Pip_Step = 16.0; // -For variable spread brokers keep it at the average guaranteed spread\r
10 extern bool         Auto_Pip_Step = false; // Pip step is deduced from average bar height\r
11 extern bool         InitialBuyOrder = true;\r
12 extern int          MagicNumber =  775225426;\r
13 extern double       Close_All_Profit = 100.0;\r
14 extern bool         Clear_Stops = true;\r
15 extern double       Order_Lifetime_Days = 0.2;\r
16 extern bool         Add_Spread_to_PipStep = false; // For fixed spread brokers - be sure about this!\r
17 extern bool         Add_MinStop_to_PipStep = true;\r
18 extern double       Max_spread = 22.0; // dont send orders above this spread\r
19 extern double       Hours_Bring_Even = 0.0; // wait for hours before an active order stop loss is pushed up\r
20 extern double       Multi_Coef = 1.0; // multiplies LotSize PipStep Close_All_Profit Min_Bands\r
21 extern bool         Debug_Print = false;\r
22 extern int          Calmdown_Period = 240;\r
23 extern bool         Persistent_Stop = true;\r
24 extern double       Panic_Profit = -200.0;\r
27 /* Bollinger bands */\r
28 extern string       __ = " Bandwidth ";\r
29 extern int          Back_Period = 10;\r
30 extern double       Bands_Deviations = 4.0;\r
31 extern double       Min_Bandwidth = 0.0005;\r
32 extern int          BW_Bars_Checked = 2;\r
34 /* Stochastic */\r
35 extern string       ___ = " Stochastic";\r
36 extern bool         Check_Stochastic = true;\r
37 extern int          Stochastic_K = 5;\r
38 extern int          Stochastic_D = 3;\r
39 extern int          Stochastic_Slowing = 3;\r
40 extern int          Stochastic_Positions = 2;\r
42 /* Average True Range */\r
43 extern string       ____ = " ATR";\r
44 extern double       ATR_Floor = 1.0;\r
45 extern int          ATR_Period = 6;\r
46 extern int          ATR_Period_Long = 31;\r
47 extern int          ATR_positions_to_check = 2;\r
50 /* Global variables */\r
51 double current_atr = 0.0;\r
53 double start_price = 0.0;\r
54 bool init_done = false;\r
55 double spread;\r
56 double pipstep_adjusted;\r
57 int order_lifetime;\r
58 double time_bring_even;\r
59 double tick_value;\r
60 double min_stop;\r
61 double total_spread_loss;\r
62 int start_bin;\r
65 #define NUM_RETR 5\r
69 int init()\r
70 {\r
71     order_lifetime = MathRound(Order_Lifetime_Days * 86400);\r
72     time_bring_even = 3600 * Hours_Bring_Even;\r
73     min_stop = MarketInfo(Symbol(), MODE_STOPLEVEL);\r
74     tick_value = MarketInfo(Symbol(), MODE_TICKVALUE); \r
75     \r
76     Print("Min stop: " + min_stop + ", Tick Value: " + tick_value);\r
77    \r
78     return(0);\r
79 }\r
81 extern double Period_Start_Trading = 0.7;\r
83 bool end_is_neigh(int cur_period)\r
84 {\r
85   return (true);\r
87   if (TimeCurrent() - Time[0] < cur_period * 60.0 * Period_Start_Trading) return (false);\r
88   else return (true);\r
89 }  \r
91 double\r
92 calc_profit()\r
93 {\r
94     double presult = 0.0;\r
95     total_spread_loss = 0.0;\r
96     tick_value = MarketInfo(Symbol(), MODE_TICKVALUE);\r
97     \r
98     for (int i = 0; i < OrdersTotal(); i++) {\r
99         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);\r
100         \r
101         if ((OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) && \r
102           OrderType() != OP_BUY && OrderType() != OP_SELL) continue;\r
104         double spread_loss = tick_value * OrderLots() * spread;\r
105         presult += (OrderProfit() - spread_loss);\r
106         total_spread_loss += spread_loss;\r
107     }\r
109     return (presult);   \r
112 datetime Last_Go_Buy = 0;\r
113 datetime Last_Go_Sell = 0;\r
115 void\r
116 close_all_orders()\r
118      close_all_orders_type(OP_BUY);\r
119      close_all_orders_type(OP_SELL);\r
122 void\r
123 close_all_orders_type(int order_type)\r
125      for (int i = 0; i < OrdersTotal(); i++) {\r
126         \r
127           OrderSelect(i, SELECT_BY_POS, MODE_TRADES);\r
128             \r
129           if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;\r
130          \r
131           if (OrderType() != order_type) continue;\r
132          \r
133           switch ( OrderType() ) {\r
134             \r
135           case OP_BUY:\r
136                OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Blue);\r
137                break;\r
138                     \r
139           case OP_SELL:\r
140                OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Red);\r
141                break;\r
142                     \r
143           case OP_SELLSTOP: \r
144           case OP_BUYSTOP:\r
145                OrderDelete( OrderTicket() );\r
146                break;\r
147                     \r
148           default:\r
149                if (Debug_Print) Print("Uknown order type " + OrderType() + " of order #" + OrderTicket() );\r
150           }\r
151      }\r
154 int\r
155 count_orders()\r
157      int count = 0;\r
158      for (int i = 0; i < OrdersTotal(); i++) {\r
159         \r
160           OrderSelect(i, SELECT_BY_POS, MODE_TRADES);\r
161             \r
162           if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;\r
164           count++;         \r
165      }\r
166      \r
167      return (count);\r
172 double Stoch_border = 40.0;\r
173 double neg_stoch_border = 60.0;\r
174 double current_stoch_diff = 0.0;\r
175 double current_stoch_delta = 0.0;\r
177 double\r
178 get_stoch(int index, int bar)\r
180      double res = iCustom(Symbol(), PERIOD_M15, "StochDelta", Stochastic_K, Stochastic_D, Stochastic_Slowing, 1, 0, true, false, 0.1, index, bar);\r
181      return (res);\r
184 bool\r
185 stoch_is_buy(int num_pos)     \r
187      if (Check_Stochastic == false) return (true);\r
188      \r
189      for (int i = 0; i < num_pos; i++) {\r
190      \r
191           current_stoch_diff = get_stoch(0, i);\r
192           current_stoch_delta = get_stoch(1, i);\r
193           \r
194           if (current_stoch_diff <= 0.0 || current_stoch_delta <= 0.0) \r
195                return (false);\r
196                \r
197      }\r
198      \r
199      return (true);\r
202 bool\r
203 stoch_is_sell(int num_pos)\r
205      if (Check_Stochastic == false) return (true);\r
206      \r
207      for (int i = 0; i < num_pos; i++) {\r
208      \r
209           current_stoch_diff = get_stoch(0, i);\r
210           current_stoch_delta = get_stoch(1, i);\r
211           \r
212           if (current_stoch_diff >= 0.0 || current_stoch_delta >= 0.0) return (false);\r
213      }\r
214      \r
215      return (true);\r
218 bool\r
219 atr_is_go(int num_pos)\r
221     for (int i = 0; i < num_pos; i++) {\r
222         current_atr = iCustom(Symbol(), PERIOD_M15, "ATR ratio", ATR_Period, ATR_Period_Long, 0.0, 1.0, 13, false, 1, i);\r
223         double ma_atr = iCustom(Symbol(), PERIOD_M15, "ATR ratio", ATR_Period, ATR_Period_Long, 0.0, 1.0, 13, false, 0, i);\r
224         \r
225         if (current_atr < ma_atr || current_atr < ATR_Floor) return (false);\r
226     }\r
227         \r
228    return (true);\r
231 bool\r
232 in_trend()\r
234      return ( true );\r
237 bool\r
238 go_buy()\r
240      return ( true );\r
243 bool\r
244 go_sell()\r
246      return ( true );\r
249 int\r
250 get_best_pipstep()\r
254 int \r
255 start()\r
257     if(start_price == 0.0) {\r
258         start_price = Ask;\r
259         if (Debug_Print) \r
260           Print("Start Price: " + start_price);\r
261     }\r
263     if (Auto_Pip_Step == true && count_orders() < 1) {\r
264           pipstep_adjusted = get_best_pipstep();\r
265     } else {\r
266           pipstep_adjusted = Pip_Step * Point;\r
267     }\r
268       \r
269     if (Add_Spread_to_PipStep) pipstep_adjusted += spread;\r
271     if (Add_MinStop_to_PipStep) pipstep_adjusted += (min_stop * Point);\r
272     \r
273     \r
274     if ( !init_done && in_trend() ) {\r
275            \r
276         if (go_buy()) {\r
277             start_price = Ask;\r
278             place_pending_buy();\r
279             place_init_buy();\r
280             Last_Go_Buy = TimeCurrent();\r
281             \r
282         } else if (go_sell()) {\r
283             start_price = Bid;\r
284             place_pending_sell();\r
285             place_init_sell();\r
286             Last_Go_Sell = TimeCurrent();\r
287             \r
288         }\r
289         \r
290         init_done = true;\r
291     }\r
293     int ask_bin = MathFloor(Ask / pipstep_adjusted);\r
294     int bid_bin = MathFloor(Bid / pipstep_adjusted);\r
296     if (Ask > (start_price + pipstep_adjusted) && (start_bin != ask_bin)) {\r
297     \r
298         start_price = Ask;\r
299         start_bin = ask_bin;\r
300         \r
301         if (Debug_Print) Print("Go buy ,new start_price:" + start_price);\r
302           \r
303         // need to set the sl to BE of last order\r
304         //moveLastSellStopToBE();\r
305         \r
306         move_up_buys_sl();\r
307         \r
308         // place buy stops\r
309         if ( in_trend() && go_buy() ) {\r
310           place_pending_buy();\r
311           close_all_orders_type(OP_SELL);\r
312           close_all_orders_type(OP_SELLSTOP);\r
313           Last_Go_Buy = TimeCurrent();\r
314         }\r
315         \r
316     } else if ((Bid < start_price - pipstep_adjusted) && (start_bin != bid_bin)) {\r
317     \r
318         start_price = Bid;\r
319         start_bin = bid_bin;\r
320         \r
321         if (Debug_Print) Print("Go sell, new start_price: " + start_price);\r
322           \r
323         // need to set the sl to BE of last order\r
324         //moveLastBuyStopToBE();  \r
326         move_down_sells_sl();\r
327         \r
328         // place sell stops\r
329         if ( in_trend() && go_sell() ) {\r
330           place_pending_sell();\r
331           close_all_orders_type(OP_BUY);\r
332           close_all_orders_type(OP_BUYSTOP);\r
333           Last_Go_Sell = TimeCurrent();\r
334         }  \r
335           \r
336     }\r
337     \r
338     if (Last_Go_Buy != 0 && TimeCurrent() - Last_Go_Buy > Calmdown_Period * 60) {\r
339 //          close_all_orders_type(OP_BUY);\r
340           Last_Go_Buy = 0;\r
341     } else if (Last_Go_Sell != 0 && TimeCurrent() - Last_Go_Sell > Calmdown_Period * 60) {\r
342 //          close_all_orders_type(OP_SELL);\r
343           Last_Go_Sell = 0;\r
344     }\r
346     RefreshRates();       \r
347     spread = Point * MarketInfo(Symbol(), MODE_SPREAD);\r
348     double current_profit = 0;//calc_profit();\r
350     if (Debug_Print) {\r
351         Comment(\r
352             "SpeedTrain Forte\nby DrZed 2011\n" +\r
353             "Symbol: " + Symbol() + "\n" +\r
354             "Spread: " + spread + "\n" +\r
355             "ATR value: " + current_atr + "\n" +\r
356             "Pipstep adjusted: " + pipstep_adjusted + "\n" +\r
357             "Last orders price: " + start_price + "\n" +\r
358             "Current close M15 price: " + iClose(NULL, PERIOD_M15, 0) + "\n" +\r
359             "Profit: " + current_profit + " USD\n" +\r
360             "Total spread loss: " + total_spread_loss + " USD\n"\r
361             );\r
362     }\r
363    \r
364     if (current_profit > Close_All_Profit) {\r
365         if (Debug_Print) \r
366             Print(\r
367                "Closing orders on profit " + current_profit + \r
368                " total spread loss: " + total_spread_loss + \r
369                " spread: " + spread + " pips.");\r
370           close_all_orders();    \r
371     }\r
372       \r
375 void \r
376 place_pending_buy()\r
378     double buy_price = Ask + pipstep_adjusted;\r
379     double stop_loss = Ask;\r
380     double take_profit = 0; //Ask + 3 * pipstep_adjusted * Point;\r
381           \r
382     if (Debug_Print) \r
383         Print("Placing buy stop order at " + buy_price + " with SL at " + stop_loss);\r
384          \r
385     int ticket_long = OrderSend(\r
386                                Symbol(), \r
387                                OP_BUYSTOP, \r
388                                Lot_Size, \r
389                                NormalizeDouble(buy_price, Digits),\r
390                                Slippage, \r
391                                NormalizeDouble(stop_loss, Digits), \r
392                                NormalizeDouble(take_profit, Digits), \r
393                                "",\r
394                                MagicNumber, \r
395                                order_lifetime + TimeCurrent(), \r
396                                Blue);\r
397                                       \r
398     if (ticket_long < 0) {\r
399         Print("Long OrderSend failed with error #", GetLastError()); \r
400         return;\r
401     }\r
402           \r
403     if (Debug_Print) \r
404        Print("Buy stop order placed with ticket " + ticket_long + " at " + buy_price);\r
406     \r
407 void \r
408 place_pending_sell()\r
410     double sell_price = Bid - pipstep_adjusted;\r
411     double stop_loss = Bid;\r
412     double take_profit = 0.0;\r
413           \r
414     if (Debug_Print) \r
415          Print("Placing sell stop order at " + sell_price + " with SL at " + stop_loss);\r
416          \r
417     int ticket_short = OrderSend(\r
418                                  Symbol(), \r
419                                  OP_SELLSTOP, \r
420                                  Lot_Size, \r
421                                  NormalizeDouble(sell_price, Digits),\r
422                                  Slippage, \r
423                                  NormalizeDouble(stop_loss, Digits), \r
424                                  NormalizeDouble(take_profit, Digits), \r
425                                  "", \r
426                                  MagicNumber, \r
427                                  order_lifetime + TimeCurrent(), \r
428                                  Red);\r
429                                       \r
430     if (ticket_short < 0) {\r
431         Print("Short OrderSend failed with error #", GetLastError()); \r
432         return;\r
433     }\r
434           \r
435     if (Debug_Print) Print(\r
436                          "Sell stop order placed with ticket " + \r
437                          ticket_short + " at " + sell_price);\r
439       \r
440 void \r
441 place_init_buy()\r
443     double stop_loss = Ask - pipstep_adjusted;\r
445     if (Debug_Print) \r
446          Print("Placing buy stop order at " + Ask + " with SL at " + stop_loss);\r
447          \r
448     int  ticketlong = OrderSend(\r
449                                 Symbol(),\r
450                                 OP_BUY, \r
451                                 Lot_Size, \r
452                                 NormalizeDouble(Ask, Digits),\r
453                                 Slippage, \r
454                                 NormalizeDouble(stop_loss, Digits), \r
455                                 0, \r
456                                 "", \r
457                                 MagicNumber, \r
458                                 0, \r
459                                 Blue);\r
460                                       \r
461     if (ticketlong < 0) {\r
462         if (Debug_Print) \r
463            Print("Long OrderSend failed with error #", GetLastError()); \r
464         return;\r
465     }\r
466           \r
467     if (Debug_Print) Print("Buy stop order placed with ticket " + ticketlong + "  at " + Ask);\r
469       \r
470       \r
471 void \r
472 place_init_sell()\r
474      \r
475     double stop_loss = Bid + pipstep_adjusted * Point;\r
476           \r
477     if (Debug_Print) Print("Placing sell stop order at "+Bid+" with SL at "+stop_loss);\r
478          \r
479     int  ticketshort = OrderSend(\r
480                                  Symbol(), \r
481                                  OP_SELL, \r
482                                  Lot_Size, \r
483                                  NormalizeDouble(Bid, Digits), \r
484                                  Slippage, \r
485                                  NormalizeDouble(stop_loss, Digits), \r
486                                  0, \r
487                                  "", \r
488                                  MagicNumber, \r
489                                  0, \r
490                                  Red);\r
491                                       \r
492     if (ticketshort < 0) {\r
493         if (Debug_Print) \r
494           Print("Short OrderSend failed with error #", GetLastError());\r
495           \r
496         return;\r
497     }\r
498           \r
499     if (Debug_Print) Print("Sell stop order placed with ticket " + ticketshort + "  at " + Bid);\r
502 void \r
503 move_up_buys_sl()\r
505     for(int j = 0; j < OrdersTotal(); j++)\r
506     {\r
507         OrderSelect(j, SELECT_BY_POS, MODE_TRADES);\r
508             \r
509         if (\r
510           OrderMagicNumber() == MagicNumber && \r
511           OrderType() == OP_BUY &&\r
512           (TimeCurrent() - OrderOpenTime()) > time_bring_even) {\r
513           \r
514                 double stop_loss = OrderStopLoss();\r
515                 double new_stop_loss = NormalizeDouble(stop_loss + pipstep_adjusted, Digits);\r
516                 \r
517                 //if (new_stop_loss < Ask + min_stop * Point) continue;\r
518             \r
519                 if (Debug_Print) Print(\r
520                                    "Moving long SL @ " + OrderOpenPrice() + " SL " + stop_loss + \r
521                                    " to " + new_stop_loss + " current ASK " + Ask);\r
522             \r
523                 bool res = OrderModify(\r
524                          OrderTicket(),                                \r
525                          OrderOpenPrice(), \r
526                          new_stop_loss, \r
527                          OrderTakeProfit(), \r
528                          OrderExpiration(), \r
529                          Blue);\r
531                 if (Persistent_Stop == true && res == false) {\r
532                 \r
533                     new_stop_loss -= (Point * min_stop);\r
534                     \r
535                     res = OrderModify(\r
536                          OrderTicket(),                                \r
537                          OrderOpenPrice(), \r
538                          new_stop_loss - Point * min_stop, \r
539                          OrderTakeProfit(), \r
540                          OrderExpiration(), \r
541                          Blue);\r
542                          \r
543                 }\r
544                 \r
545                  if (OrderProfit() < Panic_Profit) \r
546                     OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Blue);\r
547          }\r
549     }\r
551       \r
552 void \r
553 move_down_sells_sl()\r
555     for(int j = 0; j < OrdersTotal(); j++)\r
556     {\r
557         OrderSelect(j, SELECT_BY_POS, MODE_TRADES);\r
558             \r
559         if (OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL &&\r
560             TimeCurrent() - OrderOpenTime() > time_bring_even) {\r
561                 double stop_loss = OrderStopLoss();\r
562                 double new_stop_loss = NormalizeDouble(stop_loss - pipstep_adjusted, Digits);\r
564                 //if (new_stop_loss > Bid - min_stop * Point) continue;\r
565             \r
566                 if (Debug_Print) Print(\r
567                                    "Moving short SL @ " + OrderOpenPrice() + " SL " + stop_loss + \r
568                                    " to " + new_stop_loss + " current BID " + Bid);\r
570                 bool res = OrderModify(\r
571                          OrderTicket(),                                \r
572                          OrderOpenPrice(), \r
573                          new_stop_loss, \r
574                          OrderTakeProfit(), \r
575                          OrderExpiration(), \r
576                          Red);   \r
578                 if (Persistent_Stop == true && res == false) {\r
579                     new_stop_loss += (Point * min_stop);\r
580                     res = OrderModify(\r
581                          OrderTicket(),                                \r
582                          OrderOpenPrice(), \r
583                          new_stop_loss, \r
584                          OrderTakeProfit(), \r
585                          OrderExpiration(), \r
586                          Red);\r
587                 }\r
588                          \r
589                 if (OrderProfit() < Panic_Profit) \r
590                          OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Red);\r
591         }\r
593     }\r
596 void \r
597 moveLastBuyStopToBE()\r
599         int lastTicket = 0;\r
600           \r
601         for(int j = 0; j < OrdersTotal(); j++)\r
602         {\r
603             OrderSelect(j, SELECT_BY_POS, MODE_TRADES);\r
604             if (OrderMagicNumber() != MagicNumber || OrderType() != OP_BUYSTOP) continue;\r
605             \r
606                int ticketId = OrderTicket();\r
607                if (ticketId > lastTicket) lastTicket = ticketId;\r
608             \r
609        }\r
610        \r
611        if(lastTicket > 0) {\r
612             OrderSelect(lastTicket, SELECT_BY_TICKET, MODE_TRADES);\r
613             \r
614             if (Debug_Print) \r
615                Print("Moving long order number " + lastTicket + " to BE at " + OrderOpenPrice() );\r
617                bool res = OrderModify(\r
618                                 lastTicket, \r
619                                 OrderOpenPrice(), \r
620                                 OrderOpenPrice(), \r
621                                 OrderTakeProfit(), \r
622                                 OrderExpiration(), \r
623                                 Blue);\r
624           \r
625                                       \r
626             if (!res && Debug_Print) \r
627                 Print("Long SL2BE order failed with error #", GetLastError());\r
628           \r
629             if (false && res == false)\r
630                      res = OrderModify(\r
631                                         lastTicket, \r
632                                         OrderOpenPrice(), \r
633                                         OrderOpenPrice() - (min_stop * Point), \r
634                                         OrderTakeProfit(), \r
635                                         OrderExpiration(), \r
636                                         Blue);\r
637                                         \r
638        }           \r
640       \r
641 void moveLastSellStopToBE()\r
643      int lastTicket=0;\r
644       \r
645      for(int j = 0; j < OrdersTotal(); j++) {\r
646           OrderSelect(j, SELECT_BY_POS, MODE_TRADES);\r
647           if (OrderMagicNumber() != MagicNumber || OrderType() != OP_SELLSTOP) continue;\r
648           \r
649                int ticketId = OrderTicket();\r
650                if(ticketId > lastTicket) lastTicket = ticketId;\r
651           \r
652      }\r
653        \r
654      if(lastTicket > 0) {\r
655           OrderSelect(lastTicket, SELECT_BY_TICKET, MODE_TRADES);\r
656        \r
657           if (Debug_Print) \r
658               Print("Moving short order number " + lastTicket+ " to BE at " + OrderOpenPrice());\r
659                \r
660           bool res = OrderModify(\r
661                                 lastTicket, \r
662                                 OrderOpenPrice(), \r
663                                 OrderOpenPrice(), \r
664                                 OrderTakeProfit(), \r
665                                 OrderExpiration(), \r
666                                 Red);\r
667                                       \r
668           if (false && res == false) {\r
669                  if (Debug_Print) Print("Short SL2BE order failed with error #",GetLastError()); \r
670                                     res = OrderModify(\r
671                                         lastTicket, \r
672                                         OrderOpenPrice(), \r
673                                         OrderOpenPrice() + Point * min_stop, \r
674                                         OrderTakeProfit(), \r
675                                         OrderExpiration(), \r
676                                         Red);\r
677               }\r
678                          \r
679      }\r
680      \r