1 //+------------------------------------------------------------------+
\r
5 //+------------------------------------------------------------------+
\r
6 #property copyright "roundrock"
\r
9 #include <stderror.mqh>
\r
10 #include <stdlib.mqh>
\r
14 extern int Slippage = 3;
\r
19 extern double LotSize= 0.1;
\r
21 extern string PipStep_Remarks="------ Pip Distance. 30 pips is 30 pips for all brokers -------";
\r
22 extern double PipStep= 50;
\r
24 extern string MoveSL2BE_Remarks="------ Move Prev order SL to BE ------";
\r
25 extern bool MoveSL2BE = true;
\r
27 extern string InitialBuyOrder_Remarks="------ First Order on start, if true its LONG ------";
\r
28 extern bool InitialBuyOrder = true;
\r
30 extern string UseTradingHours_Remarks="------ Place orders only during these hours. however closing can happen any time ------";
\r
31 extern bool UseTradingHours = false;
\r
32 extern int StartHour = 7;
\r
33 extern int FinishHour = 19;
\r
35 extern string ProfitPct_Remarks="------ Profit Percentage ------";
\r
36 extern double ProfitPct= 1.0;
\r
39 extern int MagicNumber = 1771611;
\r
41 int longOrderCount=0;
\r
42 int shortOrderCount=0;
\r
43 double DollarProfitTarget;
\r
44 double valLastFlatEq;
\r
45 double valTargetFlatEq;
\r
47 bool longBias=false;
\r
49 //double currentPL=0;
\r
51 double startPrice=0.0;
\r
53 double avgEntryPrice=0.0;
\r
58 bool initOrdersDone=false;
\r
62 digits=MarketInfo(Symbol(),MODE_DIGITS);
\r
76 //---- TODO: add your code here
\r
85 for(int j = 0; j <= 10000; j++){
\r
86 ObjectDelete("Comment" + j);
\r
95 //DollarProfitTarget = StrToDouble(DoubleToStr(AccountEquity()*ProfitPct/100.0,2));
\r
96 if(startPrice ==0.0) {
\r
97 startPrice=iOpen( NULL, PERIOD_M15, 0);
\r
98 Print("----- startPrice ...... "+startPrice);
\r
101 if(!initOrdersDone && inTradingHours()){
\r
104 placeBuyStopOrder();
\r
105 placeSellStopOrder();
\r
106 if(InitialBuyOrder) {
\r
108 placeInitBuyOrder();
\r
110 placeInitSellOrder();
\r
114 initOrdersDone=true;
\r
117 double currentPL = getEAPL();
\r
120 // if (!CloseAll && currentPL > DollarProfitTarget)
\r
122 if (!CloseAll && currentPL+AccountBalance() > valTargetFlatEq)
\r
124 Print("================ closing ================");
\r
125 Print("currentPL "+currentPL+" AccountBalance "+AccountBalance()+" valTargetFlatEq "+valTargetFlatEq);
\r
131 if(inTradingHours()) {
\r
133 if(iClose( NULL, PERIOD_M15, 0) > (startPrice + PipStep*pip)){
\r
135 startPrice=iClose( NULL, PERIOD_M15, 0);
\r
136 Print("----- moved to startPrice ...... "+startPrice);
\r
137 // need to set the sl to BE of last order
\r
138 if(MoveSL2BE) moveSellOrderToBE();
\r
139 CloseAllPendingOrders();
\r
140 // place buy limit order
\r
141 placeBuyStopOrder();
\r
142 placeSellStopOrder();
\r
145 }else if(iClose( NULL, PERIOD_M15, 0) < (startPrice - PipStep*pip)){
\r
147 startPrice=iClose( NULL, PERIOD_M15, 0);
\r
148 Print("------ moved to startPrice ...... "+startPrice);
\r
149 // need to set the sl to BE of last order
\r
150 if(MoveSL2BE) moveBuyOrderToBE();
\r
151 CloseAllPendingOrders();
\r
152 // place buy limit order
\r
153 placeSellStopOrder();
\r
154 placeBuyStopOrder();
\r
160 printComment(currentPL);
\r
166 bool inTradingHours(){
\r
168 bool returnVal=true;
\r
170 if (UseTradingHours)
\r
172 if (TimeHour(iTime( NULL, 0, 0) ) < StartHour &&
\r
173 ((TimeHour(iTime( NULL, 0, 0)) > FinishHour && FinishHour != 0) ||
\r
174 (TimeHour(iTime( NULL, 0, 0)) < 24 && FinishHour == 0)))
\r
185 void printComment(double currentPL){
\r
188 double curPL = -valLastFlatEq + AccountBalance()+currentPL;
\r
189 double drawdown = (curPL/valLastFlatEq)*100;
\r
191 string commentSt = "\nEquity "+DoubleToStr(valLastFlatEq,0) + " Dollar Profit "+DoubleToStr(DollarProfitTarget,2)+" PL " +DoubleToStr(curPL,2)
\r
192 +" DD "+DoubleToStr(drawdown,2);
\r
194 ObjectCreate("CommentStr",OBJ_LABEL,0,0,0);
\r
195 ObjectSet("CommentStr",OBJPROP_XDISTANCE,5);
\r
196 ObjectSet("CommentStr",OBJPROP_YDISTANCE,40);
\r
197 ObjectSet("CommentStr",OBJPROP_WIDTH,5);
\r
198 ObjectSetText("CommentStr",commentSt,12,"Arial",DodgerBlue);
\r
200 string bias="Short";
\r
201 double targetPrice=0;
\r
204 if(totalOrders>0) {
\r
206 // Print("profit "+DollarProfitTarget+" totalOrders "+totalOrders+" LotSize "+LotSize+" qty "+MarketInfo(Symbol(), MODE_LOTSIZE) +" avgEntryPrice "+avgEntryPrice+
\r
207 // " bottom "+totalOrders*LotSize*MarketInfo(Symbol(), MODE_LOTSIZE) +" top "+(DollarProfitTarget*pip));
\r
209 targetPrice = (DollarProfitTarget/(totalOrders*LotSize*MarketInfo(Symbol(), MODE_LOTSIZE)))+avgEntryPrice;
\r
212 if(totalOrders>0) {
\r
213 // Print("totalOrders "+totalOrders+" LotSize "+LotSize+" qty "+MarketInfo(Symbol(), MODE_LOTSIZE) +" avgEntryPrice "+avgEntryPrice);
\r
215 targetPrice = -(DollarProfitTarget/(totalOrders*LotSize*MarketInfo(Symbol(), MODE_LOTSIZE)))+avgEntryPrice;
\r
219 string commentSt1 = "\nBias "+bias + ", Orders "+totalOrders+", Target " +DoubleToStr(targetPrice,4) ;
\r
221 ObjectCreate("CommentStr1",OBJ_LABEL,0,0,0);
\r
222 ObjectSet("CommentStr1",OBJPROP_XDISTANCE,5);
\r
223 ObjectSet("CommentStr1",OBJPROP_YDISTANCE,60);
\r
224 ObjectSet("CommentStr1",OBJPROP_WIDTH,5);
\r
225 ObjectSetText("CommentStr1",commentSt1,12,"Arial",MediumSpringGreen);
\r
230 void resetAccountCalc(){
\r
231 DollarProfitTarget = StrToDouble(DoubleToStr(AccountBalance()*ProfitPct/100.0,2));
\r
232 valLastFlatEq = AccountBalance();
\r
233 valTargetFlatEq = valLastFlatEq + DollarProfitTarget;
\r
234 Print("\nDollarProfitTarget ...... "+DollarProfitTarget+" valLastFlatEq "+valLastFlatEq);
\r
238 //+------------------------------------------------------------------+
\r
239 //| Close all of the open trades. |
\r
240 //+------------------------------------------------------------------+
\r
242 void CloseAllTrades()
\r
245 for(int i = 0; i < OrdersTotal(); i++)
\r
247 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
249 if (OrderMagicNumber() == MagicNumber)
\r
251 if (OrderType() == OP_BUY)
\r
253 Comment("In full closure mode. Closing a ", OrderSymbol(), " buy trade...");
\r
254 Print("======================================================== In full closure mode. Closing a ", OrderSymbol(), " buy trade...");
\r
255 bool closeStatus = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_BID),Slippage,Blue);
\r
256 if (!closeStatus) {
\r
257 Print("Close Buy failed with error #",GetLastError());
\r
258 int check=GetLastError();
\r
259 if(check!=0) Print("Close Buy failed with error: ",ErrorDescription(check));
\r
264 if (OrderType() == OP_SELL)
\r
266 Comment("In full closure mode. Closing a ", OrderSymbol(), " sell trade...");
\r
267 Print("============================================================In full closure mode. Closing a ", OrderSymbol(), " sell trade...");
\r
268 closeStatus = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_ASK),Slippage,Red);
\r
269 if (!closeStatus) {
\r
270 Print("Close Sell failed with error #",GetLastError());
\r
271 check=GetLastError();
\r
272 if(check!=0) Print("Close Sell failed with error: ",ErrorDescription(check));
\r
275 }else if (OrderType() == OP_BUYSTOP)
\r
277 Comment("In full closure mode. Closing pending order ", OrderSymbol(), " buy trade...");
\r
278 Print("======================================================== In full closure mode. Closing a ", OrderSymbol(), " buy trade...");
\r
279 closeStatus = OrderDelete(OrderTicket(),Blue);
\r
280 if (!closeStatus) {
\r
281 Print("Close Buy Stop failed with error #",GetLastError());
\r
282 check=GetLastError();
\r
283 if(check!=0) Print("Close Buy Stop failed with error: ",ErrorDescription(check));
\r
288 if (OrderType() == OP_SELLSTOP)
\r
290 Comment("In full closure mode. Closing pending order ", OrderSymbol(), " sell trade...");
\r
291 Print("============================================================In full closure mode. Closing a ", OrderSymbol(), " sell trade...");
\r
292 closeStatus=OrderDelete(OrderTicket(),Red);
\r
293 if (!closeStatus) {
\r
294 Print("Close Sell Stop with error #",GetLastError());
\r
295 check=GetLastError();
\r
296 if(check!=0) Print("Close Sell Stop with error: ",ErrorDescription(check));
\r
303 resetAccountCalc();
\r
304 startPrice=iOpen( NULL, PERIOD_M15, 0);
\r
305 Print("resetting startPrice ...... "+startPrice);
\r
306 initOrdersDone=false;
\r
311 void CloseAllPendingOrders()
\r
314 for(int i = 0; i < OrdersTotal(); i++)
\r
316 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
318 if (OrderMagicNumber() == MagicNumber)
\r
320 // Print(" order type "+OrderType());
\r
321 if (OrderType() == OP_BUYSTOP)
\r
323 Comment("..... Closing pending order ", OrderSymbol(), " buy trade...");
\r
324 Print("======================================================== Closing a ", OrderSymbol(), " buy stop order...");
\r
325 bool closeStatus = OrderDelete(OrderTicket(),Blue);
\r
326 if (!closeStatus) {
\r
327 Print("Close Buy Stop failed with error #",GetLastError());
\r
328 int check=GetLastError();
\r
329 if(check!=0) Print("Close Buy Stop failed with error: ",ErrorDescription(check));
\r
334 if (OrderType() == OP_SELLSTOP)
\r
336 Comment("..... Closing pending order ", OrderSymbol(), " sell trade...");
\r
337 Print("=========================================================== Closing a ", OrderSymbol(), " sell stop order...");
\r
338 closeStatus=OrderDelete(OrderTicket(),Red);
\r
339 if (!closeStatus) {
\r
340 Print("Close Sell Stop with error #",GetLastError());
\r
341 check=GetLastError();
\r
342 if(check!=0) Print("Close Sell Stop with error: ",ErrorDescription(check));
\r
357 double totalEntryPrice=0.0;
\r
360 for(int i = 0; i < OrdersTotal(); i++)
\r
362 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
\r
364 if (OrderMagicNumber() == MagicNumber && ( (OrderType() == OP_BUY)||(OrderType() == OP_SELL)))
\r
367 double curPL = OrderProfit() + OrderSwap() + OrderCommission();
\r
368 double openPrice = OrderOpenPrice();
\r
369 // Print(" openPrice "+openPrice);
\r
370 totalEntryPrice = totalEntryPrice + openPrice;
\r
371 // Print(" totalEntryPrice "+totalEntryPrice);
\r
372 // Print("curPL "+curPL);
\r
373 overallPL = overallPL + curPL;
\r
379 if(totalOrders>0) {
\r
380 avgEntryPrice = totalEntryPrice/totalOrders;
\r
381 //Print(" avgEntryPrice "+avgEntryPrice+" totalOrders "+totalOrders+" totalEntryPrice "+totalEntryPrice);
\r
389 void placeSellStopOrder(){
\r
391 double bid = MarketInfo(Symbol(), MODE_BID) ;
\r
393 double sellPrice = bid - (PipStep*pip);
\r
394 double sellSL = bid;
\r
397 Print("Placing sell stop order at "+sellPrice+" with SL at "+sellSL);
\r
399 int ticketshort = OrderSend(Symbol(),OP_SELLSTOP,LotSize,NormalizeDouble(sellPrice, digits),
\r
400 Slippage,NormalizeDouble(sellSL, digits),0,"Short Speed Train",MagicNumber,0,Red);
\r
402 if (ticketshort < 0) {
\r
403 // Print("Short OrderSend OP_SELLSTOP failed with error #",GetLastError());
\r
404 int check=GetLastError();
\r
405 if(check!=0) Print("Short OrderSend OP_SELLSTOP failed with error: ",ErrorDescription(check));
\r
409 Print("Sell stop order placed with ticket "+ticketshort+" at "+sellPrice);
\r
418 void placeBuyStopOrder(){
\r
420 double ask = MarketInfo(Symbol(), MODE_ASK) ;
\r
422 double buyPrice = ask + (PipStep*pip);
\r
423 double buySL = ask;
\r
426 Print("Placing buy stop order at "+buyPrice+" with SL at "+buySL);
\r
428 int ticketlong = OrderSend(Symbol(),OP_BUYSTOP,LotSize,NormalizeDouble(buyPrice, digits),
\r
429 Slippage,NormalizeDouble(buySL, digits),0,"Long Speed Train",MagicNumber,0,Blue);
\r
431 if (ticketlong < 0) {
\r
432 //Print("Long OrderSend OP_BUYSTOP failed with error #",GetLastError());
\r
434 int check=GetLastError();
\r
435 if(check!=0) Print("Long OrderSend OP_BUYSTOP failed with error: ",ErrorDescription(check));
\r
441 Print("Buy stop order placed with ticket "+ticketlong+" at "+buyPrice);
\r
451 void placeInitBuyOrder(){
\r
453 double ask = MarketInfo(Symbol(), MODE_ASK) ;
\r
455 double buySL = ask - (PipStep*pip);
\r
459 Print("Placing buy order at "+ask+" with SL at "+buySL);
\r
461 int ticketlong = OrderSend(Symbol(),OP_BUY,LotSize,Ask,
\r
462 Slippage,0,0,"Long Speed Train",MagicNumber,0,Blue);
\r
464 if (ticketlong < 0) {
\r
465 // Print("Long OrderSend OP_BUY failed with error #",GetLastError());
\r
466 int check=GetLastError();
\r
467 if(check!=0) Print("Long OrderSend OP_BUY failed with error: ",ErrorDescription(check));
\r
471 Print("Buy stop order placed with ticket "+ticketlong+" at "+ask);
\r
473 int ticketlong1 = OrderModify(ticketlong,OrderOpenPrice(),NormalizeDouble(buySL, digits),0,0,Blue);
\r
476 // Print("ticketlong1 "+ticketlong1);
\r
478 if (ticketlong1 < 1) {
\r
479 // Print("Long OrderModify failed with error #",GetLastError());
\r
480 check=GetLastError();
\r
481 // Print("check "+check);
\r
482 if(check!=0) Print("Long OrderModify failed with error: ",ErrorDescription(check));
\r
491 void placeInitSellOrder(){
\r
493 double bid = MarketInfo(Symbol(), MODE_BID) ;
\r
495 double sellSL = bid + (PipStep*pip);
\r
497 Print("Placing sell order at "+bid+" with SL at "+sellSL);
\r
499 int ticketshort = OrderSend(Symbol(),OP_SELL,LotSize,NormalizeDouble(bid, digits),
\r
500 Slippage,0,0,"Short Speed Train",MagicNumber,0,Red);
\r
502 if (ticketshort < 0) {
\r
503 //Print("Short OrderSend OP_SELL failed with error #",GetLastError());
\r
504 int check=GetLastError();
\r
505 if(check!=0) Print("Short OrderSend OP_SELL failed with error: ",ErrorDescription(check));
\r
509 Print("Sell stop order placed with ticket "+ticketshort+" at "+bid);
\r
511 int ticketshort1 = OrderModify(ticketshort,OrderOpenPrice(),NormalizeDouble(sellSL, digits),0,0,Red);
\r
514 if (ticketshort1 < 1) {
\r
515 // Print("Short OrderModify failed with error #",GetLastError());
\r
516 check=GetLastError();
\r
517 if(check!=0) Print("Short OrderModify failed with error: ",ErrorDescription(check));
\r
520 shortOrderCount++;
\r
525 void moveBuyOrderToBE(){
\r
528 for(int j = 0; j < OrdersTotal(); j++)
\r
530 OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
\r
531 // Print("order type "+OrderType());
\r
533 if (OrderMagicNumber() == MagicNumber && OrderType() == OP_BUYSTOP)
\r
535 int ticketId = OrderTicket() ;
\r
536 //Print("ticketId "+ticketId);
\r
537 if( ticketId> lastTicket) lastTicket=ticketId;
\r
539 //Print("lastTicket "+lastTicket);
\r
543 if(lastTicket >0) {
\r
544 Print(" ----------------------- Moving long order number "+lastTicket+ " to BE at "+OrderOpenPrice());
\r
546 int ticketlong = OrderModify(lastTicket,OrderOpenPrice(),OrderOpenPrice(),0,0,Blue);
\r
549 if (ticketlong < 0) {
\r
550 // Print("Long SL2BE order failed with error #",GetLastError());
\r
551 int check=GetLastError();
\r
552 if(check!=0) Print("Long SL2BE order failed with error: ",ErrorDescription(check));
\r
568 void moveSellOrderToBE(){
\r
571 for(int j = 0; j < OrdersTotal(); j++)
\r
573 OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
\r
575 // Print("order type "+OrderType());
\r
576 if (OrderMagicNumber() == MagicNumber && OrderType() == OP_SELLSTOP)
\r
578 int ticketId = OrderTicket() ;
\r
579 // Print(" ticketId "+ticketId);
\r
580 if( ticketId> lastTicket) lastTicket=ticketId;
\r
581 // Print(" lastTicket "+lastTicket);
\r
591 Print("------------ Moving short order number "+lastTicket+ " to BE at "+OrderOpenPrice());
\r
592 int ticketshort = OrderModify(lastTicket,OrderOpenPrice(),OrderOpenPrice(),0,0,Red);
\r
594 if (ticketshort < 0) {
\r
595 //Print("Short SL2BE order failed with error #",GetLastError());
\r
596 int check=GetLastError();
\r
597 if(check!=0) Print("Short SL2BE order failed with error: ",ErrorDescription(check));
\r