1 #property copyright "Zarko Asenov"
\r
2 #property link "jaltoh.6x.to"
\r
5 extern int Slippage = 3;
\r
6 extern double LotSize= 0.1;
\r
7 extern double PipStep= 20.0;
\r
8 extern bool InitialBuyOrder = true;
\r
9 extern int MagicNumber = 1771611;
\r
10 extern double Close_All_Profit = 10.0;
\r
11 extern bool Debug_Print = false;
\r
12 extern int Bands_Days = 20;
\r
13 extern int Bands_Deviations = 3;
\r
14 extern double Min_Bands = 0.01;
\r
15 extern int Num_Bands = 2;
\r
16 extern double Order_Lifetime_Days = 0.2;
\r
17 extern bool Add_Spread_to_Profit = false;
\r
18 extern bool Add_Spread_to_PipStep = true;
\r
19 extern bool Add_MinStop_to_PipStep = true;
\r
20 extern bool Check_ADX = false;
\r
21 extern int ADX_positions_to_check = 2;
\r
22 extern int ADX_period_in_days = 4;
\r
23 extern double ADX_min = 37.0;
\r
24 extern double Hours_Bring_Even = 0;
\r
27 int longOrderCount = 0;
\r
28 int shortOrderCount = 0;
\r
29 double startPrice = 0;
\r
31 bool initOrdersDone = false;
\r
32 double spread = 0.0;
\r
33 double pipstep_adjusted = 0.0;
\r
34 double adx_diff = 0.0;
\r
36 double get_minstop()
\r
38 return( MarketInfo(Symbol(), MODE_STOPLEVEL) + 1.0 );
\r
46 adx_diff = iADX(NULL, PERIOD_D1, ADX_period_in_days, PRICE_CLOSE, MODE_MAIN, index);
\r
48 ADX1 = iADX(NULL, PERIOD_D1, ADX_period_in_days, PRICE_CLOSE, MODE_PLUSDI, index);
\r
49 ADX2 = iADX(NULL, PERIOD_D1, ADX_period_in_days, PRICE_CLOSE, MODE_MINUSDI, index);
\r
51 if(ADX1 < ADX2) adx_diff *= -1.0;
\r
57 adx_is_sell(int num_pos)
\r
59 if (Check_ADX == false) return (true);
\r
61 double neg_adx_min = -1.0 * ADX_min;
\r
62 for (int i=0; i<num_pos; i++)
\r
63 if (get_adx(i) > neg_adx_min) return (false);
\r
69 adx_is_buy(int num_pos)
\r
71 if (Check_ADX == false) return (true);
\r
73 for (int i=0; i<num_pos; i++)
\r
74 if (get_adx(i) < ADX_min) return (false);
\r
79 //+------------------------------------------------------------------+}
\r
83 digits=MarketInfo(Symbol(),MODE_DIGITS);
\r
84 Print("digits ...... "+digits);
\r
93 double result = 0.0;
\r
95 for (int i = 0;i < OrdersTotal(); i++) {
\r
96 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
97 if(OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
\r
99 if (OrderType() == OP_BUY || OP_SELL)
\r
100 result += OrderProfit();
\r
106 double bandwidth = 0.0;
\r
109 getBandwidth(int pos)
\r
111 double upper_band = iBands(NULL, PERIOD_M15, Bands_Days, Bands_Deviations, 0, PRICE_CLOSE, MODE_UPPER, pos);
\r
112 double lower_band = iBands(NULL, PERIOD_M15, Bands_Days, Bands_Deviations, 0, PRICE_CLOSE, MODE_LOWER, pos);
\r
114 return (upper_band - lower_band);
\r
120 for (int i = 0; i < Num_Bands; i++) {
\r
121 bandwidth = getBandwidth(i);
\r
122 if (bandwidth < Min_Bands) return (false);
\r
129 moveHitBuyOrdersToBE()
\r
131 for(int j = 0; j < OrdersTotal(); j++)
\r
133 OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
\r
135 if ((OrderMagicNumber() == MagicNumber) && (OrderType() == OP_BUY) &&
\r
136 ((TimeCurrent() - OrderOpenTime()) > (3600 * Hours_Bring_Even)) )
\r
138 OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), 0, 0, Blue);
\r
144 moveHitSellOrdersToBE()
\r
146 for(int j = 0; j < OrdersTotal(); j++)
\r
148 OrderSelect(j, SELECT_BY_POS, MODE_TRADES);
\r
150 if ((OrderMagicNumber() == MagicNumber) && (OrderType() == OP_SELL) &&
\r
151 ((TimeCurrent() - OrderOpenTime()) > (3600 * Hours_Bring_Even)) )
\r
153 OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), 0, 0, Red);
\r
160 if(startPrice ==0) {
\r
161 startPrice=iOpen( NULL, PERIOD_M15, 0); //PERIOD_M15
\r
162 if (Debug_Print) Print("startPrice ...... "+startPrice);
\r
165 pipstep_adjusted = PipStep;
\r
167 if (Add_Spread_to_PipStep)
\r
168 pipstep_adjusted += spread / MarketInfo(Symbol(), MODE_POINT);
\r
170 if (Add_MinStop_to_PipStep)
\r
171 pipstep_adjusted += get_minstop();
\r
174 if(!initOrdersDone){
\r
178 if(InitialBuyOrder) {
\r
179 placeInitBuyOrder();
\r
181 placeInitSellOrder();
\r
184 initOrdersDone=true;
\r
187 if(iClose( NULL, PERIOD_M15, 0) > (startPrice + pipstep_adjusted*Point)){
\r
188 startPrice=iClose( NULL, PERIOD_M15, 0);
\r
189 if (Debug_Print) Print("new startPrice ...... "+startPrice);
\r
190 // need to set the sl to BE of last order
\r
191 moveSellOrderToBE();
\r
192 moveHitBuyOrdersToBE();
\r
193 // place buy limit order
\r
194 if (inBands() && adx_is_buy(ADX_positions_to_check)) placeBuyOrder();
\r
195 } else if (iClose( NULL, PERIOD_M15, 0) < (startPrice - pipstep_adjusted*Point)) {
\r
196 startPrice=iClose( NULL, PERIOD_M15, 0);
\r
197 if (Debug_Print) Print("new startPrice ...... "+startPrice);
\r
198 // need to set the sl to BE of last order
\r
199 moveBuyOrderToBE();
\r
200 moveHitSellOrdersToBE();
\r
201 // place buy limit order
\r
202 if (inBands() && adx_is_sell(ADX_positions_to_check)) placeSellOrder();
\r
205 double current_profit = Calc_Profit();
\r
206 double bid = MarketInfo(Symbol(), MODE_BID);
\r
207 double ask = MarketInfo(Symbol(), MODE_ASK);
\r
208 spread = ask - bid;
\r
212 "SpeedTrain Forte\nDrZ 2011\n"+
\r
213 "Symbol: "+Symbol()+"\n"+
\r
214 "Spread: "+spread+"\n"+
\r
215 "Bandwidth: "+bandwidth+"\n"+
\r
216 "Pipstep: "+pipstep_adjusted+"\n"+
\r
217 "Current Price: "+startPrice+"\n"+
\r
218 "Close Price: "+iClose( NULL, PERIOD_M15, 0)+"\n"+
\r
219 "ADX diff: "+adx_diff+"\n"+
\r
220 "Profit: "+current_profit+"\n"
\r
224 if (current_profit > Close_All_Profit) {
\r
227 Print("Closing orders on profit " + AccountProfit() + " Spread: " + (ask - bid) + " pips.");
\r
231 int buys_count = 0;
\r
232 int sells_count = 0;
\r
234 for (int i = 0;i < OrdersTotal(); i++) {
\r
236 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
238 if(OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
\r
240 if (OrderType() == OP_BUY) {
\r
241 buy_ids[buys_count] = OrderTicket();
\r
243 OrderClose( OrderTicket(), LotSize, OrderClosePrice(), Red); //bid
\r
244 } else if (OrderType() == OP_SELL) {
\r
245 sell_ids[sells_count] = OrderTicket();
\r
247 OrderClose( OrderTicket(), LotSize, OrderClosePrice(), Red); //ask
\r
248 } else if (OrderType() == OP_SELLSTOP || OP_BUYSTOP) {
\r
249 OrderDelete( OrderTicket() );
\r
253 if (buys_count != 0 && sells_count != 0) {
\r
254 Print("Lets try save.");
\r
259 while (buyix < buys_count && sellix < sells_count) {
\r
260 bool res = OrderCloseBy(buy_ids[buyix], sell_ids[sellix], Red);
\r
261 if (!res) Print("Closing of orders "+buy_ids[buyix]+" and "+sell_ids[sellix]+" failed.");
\r
268 bid = MarketInfo(Symbol(), MODE_BID);
\r
269 ask = MarketInfo(Symbol(), MODE_ASK);
\r
271 Print("Closing orders the plain ole way.");
\r
272 for (i = 0;i < buys_count; i++)
\r
273 OrderClose(buy_ids[i], LotSize, bid, Red);
\r
274 for (i = 0;i < sells_count; i++)
\r
275 OrderClose(sell_ids[i], LotSize, ask, Red);
\r
294 void placeSellOrder(){
\r
296 double bid = MarketInfo(Symbol(), MODE_BID) ;
\r
298 double sellPrice = bid - (pipstep_adjusted*Point);
\r
299 double sellSL = bid;
\r
302 Print("Placing sell stop order at " + sellPrice + " with SL at " + sellSL);
\r
304 int ticketshort = OrderSend(Symbol(), OP_SELLSTOP, LotSize, NormalizeDouble(sellPrice, digits),
\r
305 Slippage, NormalizeDouble(sellSL, digits), 0, "Short Speed Train",
\r
306 MagicNumber, MathRound(Order_Lifetime_Days * 86400) + TimeCurrent(), Red);
\r
308 if (ticketshort < 0) {
\r
309 Print("Short OrderSend failed with error #",GetLastError());
\r
313 Print("Sell stop order placed with ticket " + ticketshort + " at " + sellPrice);
\r
322 void placeBuyOrder(){
\r
324 double ask = MarketInfo(Symbol(), MODE_ASK) ;
\r
326 double buyPrice = ask + (pipstep_adjusted*Point);
\r
327 double buySL = ask;
\r
330 if (Debug_Print) Print("Placing buy stop order at "+buyPrice+" with SL at "+buySL);
\r
332 int ticketlong = OrderSend(Symbol(), OP_BUYSTOP, LotSize, NormalizeDouble(buyPrice, digits),
\r
333 Slippage, NormalizeDouble(buySL, digits), 0, "Long Speed Train",
\r
334 MagicNumber, MathRound(Order_Lifetime_Days * 86400) + TimeCurrent(),Blue);
\r
336 if (ticketlong < 0) {
\r
337 Print("Long OrderSend failed with error #",GetLastError());
\r
341 if (Debug_Print) Print("Buy stop order placed with ticket " + ticketlong + " at " + buyPrice);
\r
347 void placeInitBuyOrder(){
\r
349 double ask = MarketInfo(Symbol(), MODE_ASK) ;
\r
351 double buySL = ask - (pipstep_adjusted*Point);
\r
355 if (Debug_Print) Print("Placing buy stop order at "+ask+" with SL at "+buySL);
\r
357 int ticketlong = OrderSend(Symbol(), OP_BUY, LotSize, NormalizeDouble(ask, digits),
\r
358 Slippage, NormalizeDouble(buySL, digits), 0, "Long Speed Train",
\r
359 MagicNumber, 0, Blue);
\r
361 if (ticketlong < 0) {
\r
362 if (Debug_Print) Print("Long OrderSend failed with error #",GetLastError());
\r
366 if (Debug_Print) Print("Buy stop order placed with ticket "+ticketlong+" at "+ask);
\r
373 void placeInitSellOrder(){
\r
375 double bid = MarketInfo(Symbol(), MODE_BID) ;
\r
377 double sellSL = bid + (pipstep_adjusted*Point);
\r
379 if (Debug_Print) Print("Placing sell stop order at "+bid+" with SL at "+sellSL);
\r
381 int ticketshort = OrderSend(Symbol(),OP_SELL,LotSize,NormalizeDouble(bid, digits),
\r
382 Slippage,NormalizeDouble(sellSL, digits),0,"Short Speed Train",MagicNumber,0,Red);
\r
384 if (ticketshort < 0) {
\r
385 if (Debug_Print) Print("Short OrderSend failed with error #",GetLastError());
\r
389 if (Debug_Print) Print("Sell stop order placed with ticket "+ticketshort+" at "+bid);
\r
391 shortOrderCount++;
\r
396 void moveBuyOrderToBE(){
\r
399 for(int j = 0; j < OrdersTotal(); j++)
\r
401 OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
\r
402 // Print("order type "+OrderType());
\r
404 if (OrderMagicNumber() == MagicNumber && OrderType() == OP_BUYSTOP || OP_BUY)
\r
406 int ticketId = OrderTicket() ;
\r
407 //Print("ticketId "+ticketId);
\r
408 if(OrderType() == OP_BUYSTOP && ticketId> lastTicket) lastTicket=ticketId;
\r
410 //Print("lastTicket "+lastTicket);
\r
411 if (TimeCurrent() - OrderOpenTime() > 3600 * Hours_Bring_Even)
\r
412 OrderModify(ticketId,OrderOpenPrice(),OrderOpenPrice(),0,0,Blue);
\r
417 if(lastTicket >0) {
\r
418 if (Debug_Print) Print(" ----------------------- Moving long order number "+lastTicket+ " to BE at "+OrderOpenPrice());
\r
420 int ticketlong = OrderModify(lastTicket,OrderOpenPrice(),OrderOpenPrice(),0,0,Blue);
\r
423 if (ticketlong < 0) {
\r
424 if (Debug_Print) Print("Long SL2BE order failed with error #",GetLastError());
\r
440 void moveSellOrderToBE(){
\r
443 for(int j = 0; j < OrdersTotal(); j++)
\r
445 OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
\r
447 // Print("order type "+OrderType());
\r
448 if (OrderMagicNumber() == MagicNumber && OrderType() == OP_SELLSTOP || OP_SELL)
\r
450 int ticketId = OrderTicket() ;
\r
451 // Print(" ticketId "+ticketId);
\r
452 if( ticketId> lastTicket && OrderType() == OP_SELLSTOP) lastTicket=ticketId;
\r
453 // Print(" lastTicket "+lastTicket);
\r
454 if (TimeCurrent() - OrderOpenTime() > 3600 * Hours_Bring_Even)
\r
455 OrderModify(ticketId,OrderOpenPrice(),OrderOpenPrice(),0,0,Red);
\r
466 if (Debug_Print) Print("------------ Moving short order number "+lastTicket+ " to BE at "+OrderOpenPrice());
\r
467 int ticketshort = OrderModify(lastTicket,OrderOpenPrice(),OrderOpenPrice(),0,0,Red);
\r
469 if (ticketshort < 0) {
\r
470 if (Debug_Print) Print("Short SL2BE order failed with error #",GetLastError());
\r