1 #property copyright "Zarko Asenov"
\r
2 #property link "jaltoh.6x.to"
\r
4 /* General functionality settings */
\r
5 extern string _ = " General Settings ";
\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
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
56 double pipstep_adjusted;
\r
58 double time_bring_even;
\r
61 double total_spread_loss;
\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
76 Print("Min stop: " + min_stop + ", Tick Value: " + tick_value);
\r
81 extern double Period_Start_Trading = 0.7;
\r
83 bool end_is_neigh(int cur_period)
\r
87 if (TimeCurrent() - Time[0] < cur_period * 60.0 * Period_Start_Trading) return (false);
\r
94 double presult = 0.0;
\r
95 total_spread_loss = 0.0;
\r
96 tick_value = MarketInfo(Symbol(), MODE_TICKVALUE);
\r
98 for (int i = 0; i < OrdersTotal(); i++) {
\r
99 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
\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
112 datetime Last_Go_Buy = 0;
\r
113 datetime Last_Go_Sell = 0;
\r
118 close_all_orders_type(OP_BUY);
\r
119 close_all_orders_type(OP_SELL);
\r
123 close_all_orders_type(int order_type)
\r
125 for (int i = 0; i < OrdersTotal(); i++) {
\r
127 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
\r
129 if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
\r
131 if (OrderType() != order_type) continue;
\r
133 switch ( OrderType() ) {
\r
136 OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Blue);
\r
140 OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Red);
\r
145 OrderDelete( OrderTicket() );
\r
149 if (Debug_Print) Print("Uknown order type " + OrderType() + " of order #" + OrderTicket() );
\r
158 for (int i = 0; i < OrdersTotal(); i++) {
\r
160 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
\r
162 if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
\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
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
185 stoch_is_buy(int num_pos)
\r
187 if (Check_Stochastic == false) return (true);
\r
189 for (int i = 0; i < num_pos; i++) {
\r
191 current_stoch_diff = get_stoch(0, i);
\r
192 current_stoch_delta = get_stoch(1, i);
\r
194 if (current_stoch_diff <= 0.0 || current_stoch_delta <= 0.0)
\r
203 stoch_is_sell(int num_pos)
\r
205 if (Check_Stochastic == false) return (true);
\r
207 for (int i = 0; i < num_pos; i++) {
\r
209 current_stoch_diff = get_stoch(0, i);
\r
210 current_stoch_delta = get_stoch(1, i);
\r
212 if (current_stoch_diff >= 0.0 || current_stoch_delta >= 0.0) return (false);
\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
225 if (current_atr < ma_atr || current_atr < ATR_Floor) return (false);
\r
257 if(start_price == 0.0) {
\r
260 Print("Start Price: " + start_price);
\r
263 if (Auto_Pip_Step == true && count_orders() < 1) {
\r
264 pipstep_adjusted = get_best_pipstep();
\r
266 pipstep_adjusted = Pip_Step * Point;
\r
269 if (Add_Spread_to_PipStep) pipstep_adjusted += spread;
\r
271 if (Add_MinStop_to_PipStep) pipstep_adjusted += (min_stop * Point);
\r
274 if ( !init_done && in_trend() ) {
\r
278 place_pending_buy();
\r
280 Last_Go_Buy = TimeCurrent();
\r
282 } else if (go_sell()) {
\r
284 place_pending_sell();
\r
286 Last_Go_Sell = TimeCurrent();
\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
299 start_bin = ask_bin;
\r
301 if (Debug_Print) Print("Go buy ,new start_price:" + start_price);
\r
303 // need to set the sl to BE of last order
\r
304 //moveLastSellStopToBE();
\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
316 } else if ((Bid < start_price - pipstep_adjusted) && (start_bin != bid_bin)) {
\r
319 start_bin = bid_bin;
\r
321 if (Debug_Print) Print("Go sell, new start_price: " + start_price);
\r
323 // need to set the sl to BE of last order
\r
324 //moveLastBuyStopToBE();
\r
326 move_down_sells_sl();
\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
338 if (Last_Go_Buy != 0 && TimeCurrent() - Last_Go_Buy > Calmdown_Period * 60) {
\r
339 // close_all_orders_type(OP_BUY);
\r
341 } else if (Last_Go_Sell != 0 && TimeCurrent() - Last_Go_Sell > Calmdown_Period * 60) {
\r
342 // close_all_orders_type(OP_SELL);
\r
347 spread = Point * MarketInfo(Symbol(), MODE_SPREAD);
\r
348 double current_profit = 0;//calc_profit();
\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
364 if (current_profit > Close_All_Profit) {
\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
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
383 Print("Placing buy stop order at " + buy_price + " with SL at " + stop_loss);
\r
385 int ticket_long = OrderSend(
\r
389 NormalizeDouble(buy_price, Digits),
\r
391 NormalizeDouble(stop_loss, Digits),
\r
392 NormalizeDouble(take_profit, Digits),
\r
395 order_lifetime + TimeCurrent(),
\r
398 if (ticket_long < 0) {
\r
399 Print("Long OrderSend failed with error #", GetLastError());
\r
404 Print("Buy stop order placed with ticket " + ticket_long + " at " + buy_price);
\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
415 Print("Placing sell stop order at " + sell_price + " with SL at " + stop_loss);
\r
417 int ticket_short = OrderSend(
\r
421 NormalizeDouble(sell_price, Digits),
\r
423 NormalizeDouble(stop_loss, Digits),
\r
424 NormalizeDouble(take_profit, Digits),
\r
427 order_lifetime + TimeCurrent(),
\r
430 if (ticket_short < 0) {
\r
431 Print("Short OrderSend failed with error #", GetLastError());
\r
435 if (Debug_Print) Print(
\r
436 "Sell stop order placed with ticket " +
\r
437 ticket_short + " at " + sell_price);
\r
443 double stop_loss = Ask - pipstep_adjusted;
\r
446 Print("Placing buy stop order at " + Ask + " with SL at " + stop_loss);
\r
448 int ticketlong = OrderSend(
\r
452 NormalizeDouble(Ask, Digits),
\r
454 NormalizeDouble(stop_loss, Digits),
\r
461 if (ticketlong < 0) {
\r
463 Print("Long OrderSend failed with error #", GetLastError());
\r
467 if (Debug_Print) Print("Buy stop order placed with ticket " + ticketlong + " at " + Ask);
\r
475 double stop_loss = Bid + pipstep_adjusted * Point;
\r
477 if (Debug_Print) Print("Placing sell stop order at "+Bid+" with SL at "+stop_loss);
\r
479 int ticketshort = OrderSend(
\r
483 NormalizeDouble(Bid, Digits),
\r
485 NormalizeDouble(stop_loss, Digits),
\r
492 if (ticketshort < 0) {
\r
494 Print("Short OrderSend failed with error #", GetLastError());
\r
499 if (Debug_Print) Print("Sell stop order placed with ticket " + ticketshort + " at " + Bid);
\r
505 for(int j = 0; j < OrdersTotal(); j++)
\r
507 OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
\r
510 OrderMagicNumber() == MagicNumber &&
\r
511 OrderType() == OP_BUY &&
\r
512 (TimeCurrent() - OrderOpenTime()) > time_bring_even) {
\r
514 double stop_loss = OrderStopLoss();
\r
515 double new_stop_loss = NormalizeDouble(stop_loss + pipstep_adjusted, Digits);
\r
517 //if (new_stop_loss < Ask + min_stop * Point) continue;
\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
523 bool res = OrderModify(
\r
527 OrderTakeProfit(),
\r
528 OrderExpiration(),
\r
531 if (Persistent_Stop == true && res == false) {
\r
533 new_stop_loss -= (Point * min_stop);
\r
538 new_stop_loss - Point * min_stop,
\r
539 OrderTakeProfit(),
\r
540 OrderExpiration(),
\r
545 if (OrderProfit() < Panic_Profit)
\r
546 OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Blue);
\r
553 move_down_sells_sl()
\r
555 for(int j = 0; j < OrdersTotal(); j++)
\r
557 OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
\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
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
574 OrderTakeProfit(),
\r
575 OrderExpiration(),
\r
578 if (Persistent_Stop == true && res == false) {
\r
579 new_stop_loss += (Point * min_stop);
\r
584 OrderTakeProfit(),
\r
585 OrderExpiration(),
\r
589 if (OrderProfit() < Panic_Profit)
\r
590 OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Red);
\r
597 moveLastBuyStopToBE()
\r
599 int lastTicket = 0;
\r
601 for(int j = 0; j < OrdersTotal(); j++)
\r
603 OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
\r
604 if (OrderMagicNumber() != MagicNumber || OrderType() != OP_BUYSTOP) continue;
\r
606 int ticketId = OrderTicket();
\r
607 if (ticketId > lastTicket) lastTicket = ticketId;
\r
611 if(lastTicket > 0) {
\r
612 OrderSelect(lastTicket, SELECT_BY_TICKET, MODE_TRADES);
\r
615 Print("Moving long order number " + lastTicket + " to BE at " + OrderOpenPrice() );
\r
617 bool res = OrderModify(
\r
621 OrderTakeProfit(),
\r
622 OrderExpiration(),
\r
626 if (!res && Debug_Print)
\r
627 Print("Long SL2BE order failed with error #", GetLastError());
\r
629 if (false && res == false)
\r
633 OrderOpenPrice() - (min_stop * Point),
\r
634 OrderTakeProfit(),
\r
635 OrderExpiration(),
\r
641 void moveLastSellStopToBE()
\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
649 int ticketId = OrderTicket();
\r
650 if(ticketId > lastTicket) lastTicket = ticketId;
\r
654 if(lastTicket > 0) {
\r
655 OrderSelect(lastTicket, SELECT_BY_TICKET, MODE_TRADES);
\r
658 Print("Moving short order number " + lastTicket+ " to BE at " + OrderOpenPrice());
\r
660 bool res = OrderModify(
\r
664 OrderTakeProfit(),
\r
665 OrderExpiration(),
\r
668 if (false && res == false) {
\r
669 if (Debug_Print) Print("Short SL2BE order failed with error #",GetLastError());
\r
673 OrderOpenPrice() + Point * min_stop,
\r
674 OrderTakeProfit(),
\r
675 OrderExpiration(),
\r