1 #property copyright "Zarko Asenov"
\r
2 #property link "jaltoh.6x.to"
\r
9 /* General functionality settings */
\r
10 extern string _ = " General Settings ";
\r
11 extern double Lot_Size = 0.12;
\r
12 extern double Pip_Step = 0; // On variable spread brokers keep it over average spread, 0 automatic step
\r
13 extern bool Add_Spread_to_Pip_Step = false; // For fixed spread brokers - be sure about this!
\r
14 extern bool Add_Min_Stop_to_Pip_Step = false;
\r
15 extern double Order_Lifetime_Hours = 1; // Pending order life time
\r
16 extern double Max_spread = 22; // dont send orders with prices above this spread
\r
17 extern double Hours_Bring_Even = 0; // wait for N hours before an active order stop loss is pushed up
\r
18 extern bool Debug_Print = true;
\r
19 extern double Panic_Profit = -200.0;
\r
20 extern int Midprice_Time_Frame = PERIOD_H1;
\r
21 extern double Calmdown_Hours = 2;
\r
22 extern double Go_After = 0.4; // periods
\r
23 extern int Magic_Number = 775225426;
\r
25 /* Grid Bandwidth */
\r
26 extern string __ = " Market noise bandwidth ";
\r
27 extern int BW_Back_Period = 39;
\r
28 extern int BW_Time_Frame = 0;
\r
31 extern string ___ = " Sentiment";
\r
32 extern double Sentiment_Threshold = 0.15;
\r
33 extern int Sentiment_MA_Bars = 12;
\r
34 extern double Sentiment_Time_Frame = 0;
\r
35 extern int Sentiment_Bars_Check = 1;
\r
36 extern double Sentiment_Time_Factor = 0.01;
\r
38 /* Average True Range */
\r
39 extern string ____ = " ATR";
\r
40 extern double ATR_Threshold = 1.5;
\r
41 extern int ATR_Time_Frame = PERIOD_H1;
\r
42 extern int ATR_Period = 6;
\r
43 extern int ATR_Period_Long = 22;
\r
44 extern int ATR_MA_Bars = 6;
\r
45 extern int ATR_Bars_Check = 2;
\r
48 /* Global variables */
\r
49 double current_atr = 0.0;
\r
51 double start_price = 0.0;
\r
52 bool init_done = false;
\r
55 double pipstep_adjusted;
\r
59 double time_bring_even;
\r
62 double tick_value_pt;
\r
67 double total_spread_loss;
\r
69 double inv_sentiment_threshold;
\r
70 double calmdown_period;
\r
74 datetime prev_bar_time;
\r
83 if (RefreshRates() == false)
\r
84 Print("Refresh rates failed!");
\r
87 order_lifetime = MathRound(Order_Lifetime_Hours * 3600);
\r
88 time_bring_even = 3600 * Hours_Bring_Even;
\r
90 min_stop = MarketInfo(Symbol(), MODE_STOPLEVEL);
\r
91 min_stop_pt = Point * MarketInfo(Symbol(), MODE_STOPLEVEL);
\r
93 tick_value = MarketInfo(Symbol(), MODE_TICKVALUE);
\r
94 tick_value_pt = Point * MarketInfo(Symbol(), MODE_TICKVALUE);
\r
96 pipstep_pt = Pip_Step * Point;
\r
98 inv_sentiment_threshold = -1.0 * Sentiment_Threshold;
\r
99 calmdown_period = Calmdown_Hours * 3600;
\r
101 num_orders = count_orders();
\r
106 "Min stop: " + min_stop +
\r
107 ", Tick Value: " + tick_value +
\r
108 ", Number of our orders: " + num_orders +
\r
109 ", Grid height: " + pipstep_pt);
\r
115 bool end_is_neigh(int cur_period)
\r
119 if ( (TimeCurrent() - Time[0]) < (cur_period * 60. * Go_After) ) return (false);
\r
120 else return (true);
\r
126 double presult = 0.0;
\r
127 total_spread_loss = 0.0;
\r
128 tick_value = MarketInfo(Symbol(), MODE_TICKVALUE);
\r
130 for (int i = 0; i < OrdersTotal(); i++) {
\r
131 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
\r
133 if ((OrderSymbol() != Symbol() || OrderMagicNumber() != Magic_Number) &&
\r
134 OrderType() != OP_BUY && OrderType() != OP_SELL) continue;
\r
136 double spread_loss = tick_value * OrderLots() * spread;
\r
137 presult += (OrderProfit() - spread_loss);
\r
138 total_spread_loss += spread_loss;
\r
144 datetime last_go_time = 0;
\r
149 close_all_orders_type(OP_BUY);
\r
150 close_all_orders_type(OP_SELL);
\r
154 close_all_orders_type(int order_type)
\r
156 for (int i = 0; i < OrdersTotal(); i++) {
\r
158 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
\r
160 if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic_Number) continue;
\r
162 if (OrderType() != order_type) continue;
\r
164 switch ( OrderType() ) {
\r
167 OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), SLIPPAGE, Blue);
\r
171 OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), SLIPPAGE, Red);
\r
176 OrderDelete( OrderTicket() );
\r
180 if (Debug_Print) Print("Uknown order type " + OrderType() + " of order #" + OrderTicket() );
\r
189 for (int i = 0; i < OrdersTotal(); i++) {
\r
191 OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
\r
193 if ( (OrderSymbol() != Symbol()) || (OrderMagicNumber() != Magic_Number) ) continue;
\r
203 atr_is_go(int num_pos)
\r
205 for (int i = 0; i < num_pos; i++) {
\r
206 current_atr = iCustom(Symbol(), ATR_Time_Frame, "ATR ratio", ATR_Period, ATR_Period_Long, 0.0, 1.0, ATR_MA_Bars, false, 1, i);
\r
207 double ma_atr = iCustom(Symbol(), ATR_Time_Frame, "ATR ratio", ATR_Period, ATR_Period_Long, 0.0, 1.0, ATR_MA_Bars, false, 0, i);
\r
209 if (current_atr < ma_atr || current_atr < ATR_Threshold) return (false);
\r
218 return ( atr_is_go(ATR_Bars_Check) );
\r
226 int sentiment = NEUTRAL;
\r
233 Symbol(), BW_Time_Frame, "Bandwidth",
\r
234 0, BW_Back_Period, 1, "", false, PRICE_WEIGHTED,
\r
240 get_sentiment(int index, int bar)
\r
244 Symbol(), Sentiment_Time_Frame, "jmarket_sentiment",
\r
245 Sentiment_MA_Bars, Sentiment_MA_Bars, 0, Sentiment_MA_Bars, Sentiment_Time_Factor, 0.9, 0, "", "", "", NEUTRAL, "", "", "",
\r
253 double threshold_adjusted = Sentiment_Threshold;
\r
255 for (int ix = 0; ix < Sentiment_Bars_Check; ix++) {
\r
256 if (get_sentiment(0, ix) < threshold_adjusted) return (false);
\r
259 sentiment = BULLISH;
\r
266 double inv_threshold_adjusted = inv_sentiment_threshold;
\r
268 for (int ix = 0; ix < Sentiment_Bars_Check; ix++) {
\r
269 if (get_sentiment(0, ix) > inv_threshold_adjusted) return (false);
\r
272 sentiment = BEARISH;
\r
280 if (auto_step == 0) auto_step = get_grid();
\r
282 // return previous unchaged step if already trading
\r
283 if (num_orders > 0) return (auto_step);
\r
285 // same bar same step
\r
286 if (Time[0] == prev_bar_time) return (auto_step);
\r
291 // get recommended grid height
\r
292 double _step = get_grid();
\r
294 // if errored out stick with previous one
\r
296 Print("Fatal, got zero auto step. Returning old step " + auto_step);
\r
297 return (auto_step);
\r
302 if (Debug_Print == true) Print("New step set to: " + auto_step);
\r
304 prev_bar_time = Time[0];
\r
305 return (auto_step);
\r
311 num_orders = count_orders();
\r
312 if (num_orders < 1) {
\r
313 init_done = false; // reinit!
\r
319 spread = Point * MarketInfo(Symbol(), MODE_SPREAD);
\r
322 (last_go_time != 0) &&
\r
323 ((TimeCurrent() - last_go_time) > calmdown_period)
\r
326 close_all_orders();
\r
330 if (pipstep_pt != 0) pipstep_adjusted = pipstep_pt;
\r
331 else pipstep_adjusted = get_auto_step();
\r
333 /* nothing to do with step 0 */
\r
334 if (pipstep_adjusted == 0) {
\r
335 Print("Fatal, pipstep is zero! Skipping tick.");
\r
339 if (Add_Spread_to_Pip_Step) pipstep_adjusted += spread;
\r
340 if (Add_Min_Stop_to_Pip_Step) pipstep_adjusted += min_stop_pt;
\r
343 if ( init_done == false ) {
\r
344 /* fire up new grid */
\r
348 start_bin = MathFloor(start_price / pipstep_adjusted);
\r
349 if (Debug_Print) Print("Init go long, start price: " + start_price + " bin: " + start_bin);
\r
351 place_pending_buy(start_price, order_lifetime + TimeCurrent() );
\r
352 //place_pending_sell(start_price, order_lifetime + TimeCurrent() );
\r
353 place_buy(start_price);
\r
355 last_go_time = TimeCurrent();
\r
357 } else if ( go_sell() ) {
\r
359 start_bin = MathFloor(start_price / pipstep_adjusted);
\r
360 if (Debug_Print) Print("Init go short, start price: " + start_price + " bin: " + start_bin);
\r
362 //place_pending_buy(start_price, order_lifetime + TimeCurrent() );
\r
363 place_sell(start_price);
\r
364 place_pending_sell(start_price, order_lifetime + TimeCurrent() );
\r
366 last_go_time = TimeCurrent();
\r
370 } else if (start_price != 0) {
\r
373 if ( Ask > (start_price + pipstep_adjusted) ) {
\r
376 int bin = MathFloor(start_price / pipstep_adjusted);
\r
378 if (Debug_Print) Print("Go buy new start_price:" + start_price + " bin: " + bin);
\r
380 /* things to do once per bin */
\r
381 if (start_bin != bin) {
\r
388 close_all_orders_type(OP_SELL);
\r
389 place_pending_buy(start_price, order_lifetime + TimeCurrent());
\r
390 place_pending_sell(start_price, order_lifetime + TimeCurrent());
\r
391 last_go_time = TimeCurrent();
\r
392 } else if ( go_sell() ) {
\r
393 close_all_orders_type(OP_BUY);
\r
394 place_pending_sell(start_price, order_lifetime + TimeCurrent());
\r
395 place_pending_buy(start_price, order_lifetime + TimeCurrent());
\r
396 last_go_time = TimeCurrent();
\r
398 } else if ( Bid < (start_price - pipstep_adjusted) ) {
\r
401 bin = MathFloor(start_price / pipstep_adjusted);
\r
403 if (Debug_Print) Print("Go sell new start_price: " + start_price + " bin: " + bin);
\r
405 /* things to do once per bin */
\r
406 if (start_bin != bin) {
\r
407 move_down_sells_sl();
\r
411 // place sell stops
\r
413 close_all_orders_type(OP_BUY);
\r
414 place_pending_sell(start_price, order_lifetime + TimeCurrent());
\r
415 place_pending_buy(start_price, order_lifetime + TimeCurrent());
\r
416 last_go_time = TimeCurrent();
\r
417 } else if ( go_buy() ) {
\r
418 close_all_orders_type(OP_SELL);
\r
419 place_pending_buy(start_price, order_lifetime + TimeCurrent());
\r
420 place_pending_sell(start_price, order_lifetime + TimeCurrent());
\r
421 last_go_time = TimeCurrent();
\r
430 "SpeedTrain Forte\nby DrZed 2011\n" +
\r
431 "Symbol: " + Symbol() + "\n" +
\r
432 "Spread: " + spread + "\n" +
\r
433 "ATR value: " + current_atr + "\n" +
\r
434 "Pipstep adjusted: " + pipstep_adjusted + "\n" +
\r
435 "Last orders price: " + start_price + "\n" +
\r
436 "Current close M15 price: " + iClose(NULL, PERIOD_M15, 0) + "\n" +
\r
437 "Total spread loss: " + total_spread_loss + " USD\n"
\r
443 place_pending_buy(double base_price, datetime lifetime)
\r
445 double buy_price = base_price + pipstep_adjusted;
\r
446 double stop_loss = base_price;
\r
447 double take_profit = 0;
\r
449 if (Debug_Print) Print("Placing buy stop order at " + buy_price + " with SL at " + stop_loss);
\r
453 while (ticket < 0 && retrct < RETRIES) {
\r
455 ticket = OrderSend(
\r
459 NormalizeDouble(buy_price, Digits),
\r
461 NormalizeDouble(stop_loss, Digits),
\r
462 NormalizeDouble(take_profit, Digits),
\r
471 Print("Long OrderSend failed with error #", GetLastError());
\r
475 if (Debug_Print) Print("Buy stop order placed with ticket " + ticket + " at " + buy_price);
\r
479 place_pending_sell(double base_price, datetime lifetime)
\r
481 double sell_price = base_price - pipstep_adjusted;
\r
482 double stop_loss = base_price;
\r
483 double take_profit = 0.0;
\r
485 if (Debug_Print) Print("Placing sell stop order at " + sell_price + " with SL at " + stop_loss);
\r
489 while (ticket < 0 && retrct < RETRIES) {
\r
491 ticket = OrderSend(
\r
495 NormalizeDouble(sell_price, Digits),
\r
497 NormalizeDouble(stop_loss, Digits),
\r
498 NormalizeDouble(take_profit, Digits),
\r
507 Print("Short OrderSend failed with error #", GetLastError());
\r
511 if (Debug_Print) Print(
\r
512 "Sell stop order placed with ticket " + ticket + " at " + sell_price);
\r
516 place_buy(double base_price)
\r
518 double stop_loss = base_price - pipstep_adjusted;
\r
520 if (Debug_Print) Print("Placing long order at " + base_price + " with SL at " + stop_loss);
\r
524 while (ticket < 0 && retrct < RETRIES) {
\r
525 ticket = OrderSend(
\r
529 NormalizeDouble(base_price, Digits),
\r
531 NormalizeDouble(stop_loss, Digits),
\r
540 if (Debug_Print == false) return;
\r
542 if (ticket < 0) Print("Long OrderSend failed with error #", GetLastError());
\r
544 Print("Buy stop order placed with ticket " + ticket + " at " + Ask);
\r
549 place_sell(double base_price)
\r
551 double stop_loss = base_price + pipstep_adjusted;
\r
553 if (Debug_Print) Print("Placing short order at " + base_price + " with SL at " + stop_loss);
\r
557 while (ticket < 0 && retrct < RETRIES) {
\r
559 ticket = OrderSend(
\r
563 NormalizeDouble(base_price, Digits),
\r
565 NormalizeDouble(stop_loss, Digits),
\r
576 if (Debug_Print) Print("Short OrderSend failed with error #", GetLastError());
\r
580 if (Debug_Print) Print("Sell stop order placed with ticket " + ticket + " at " + base_price);
\r
586 for(int j = 0; j < OrdersTotal(); j++)
\r
588 OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
\r
591 (OrderMagicNumber() == Magic_Number) &&
\r
592 (OrderType() == OP_BUY) &&
\r
593 (TimeCurrent() - OrderOpenTime()) > time_bring_even) {
\r
595 double stop_loss = OrderStopLoss();
\r
598 int new_stop_loss_bin = MathRound( (Ask - min_stop_pt - stop_loss) / pipstep_adjusted );
\r
599 new_stop_loss_bin > 0;
\r
600 new_stop_loss_bin--) {
\r
602 double new_stop_loss = NormalizeDouble(stop_loss + (new_stop_loss * pipstep_adjusted), Digits);
\r
604 if (Debug_Print) Print(
\r
605 "Moving long ticket " + OrderTicket() + " stop loss " + stop_loss +
\r
606 " to " + new_stop_loss + ", current ask price " + Ask);
\r
610 while (res == false && retrct < RETRIES) {
\r
615 OrderTakeProfit(),
\r
616 OrderExpiration(),
\r
621 // Order modify failed.
\r
622 if (res == false) Print("Failed moving order " + OrderTicket() + " stop loss to " + new_stop_loss);
\r
626 if (OrderProfit() < Panic_Profit) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), SLIPPAGE, Blue);
\r
633 move_down_sells_sl()
\r
635 for(int j = 0; j < OrdersTotal(); j++)
\r
637 OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
\r
640 (OrderMagicNumber() == Magic_Number) &&
\r
641 (OrderType() == OP_SELL) &&
\r
642 ((TimeCurrent() - OrderOpenTime()) > time_bring_even)) {
\r
644 double stop_loss = OrderStopLoss();
\r
647 int new_stop_loss_bin = MathRound( (stop_loss - min_stop_pt - Bid) / pipstep_adjusted );
\r
648 new_stop_loss_bin > 0;
\r
649 new_stop_loss_bin--) {
\r
651 double new_stop_loss = NormalizeDouble(stop_loss - (new_stop_loss_bin * pipstep_adjusted), Digits);
\r
652 if (Debug_Print) Print(
\r
653 "Moving short SL @ " + OrderOpenPrice() + " SL " + stop_loss +
\r
654 " to " + new_stop_loss + " current BID " + Bid);
\r
658 while (res == false && retrct < RETRIES) {
\r
665 OrderExpiration(),
\r
670 // Order modify failed.
\r
671 if (res == false) Print("Failed moving order " + OrderTicket() + " stop loss to " + new_stop_loss);
\r
676 if (OrderProfit() < Panic_Profit) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), SLIPPAGE, Red);
\r