Author: Copyright � 2005, Nick Bilak
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
Indicators Used
Bill Williams Awesome oscillator
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites

Profitability Reports

AUD/USD Oct 2024 - Jan 2025
2.00 %
Total Trades 136
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -1.12
Gross Profit 2.50
Gross Loss -154.60
Total Net Profit -152.10
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
16.00 %
Total Trades 130
Won Trades 15
Lost trades 115
Win Rate 0.12 %
Expected payoff -1.39
Gross Profit 34.00
Gross Loss -214.10
Total Net Profit -180.10
-100%
-50%
0%
50%
100%
ross-exp
/*-----------------------------+
|			       |
| Shared by www.Aptrafx.com    |
|			       |
+------------------------------*/

//+------------------------------------------------------------------+
//|                                                     ross-exp.mq4 |
//|                Copyright © 2005, Nick Bilak, beluck[AT]gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Nick Bilak"
#property link      "http://metatrader.50webs.com/"
#include <stdlib.mqh>

extern int myMagic = 20051203;

extern double AllProfit = 100;
extern double AllLoss = 50;  
extern double TakeProfit = 100;
extern double StopLoss = 40;  
extern double TrailingStop = 15;

extern int    slippage=2;   	//slippage for market order processing
extern int    shift=0;			//shift to current bar, 

extern double Lots = 0.1;

extern int       AOLongLevel=60;
extern int       AOShortLevel=-30;

extern int       OrderTriesNumber=5;

bool buysig1,sellsig1,buysig2,sellsig2,buysig3,sellsig3,closeall,closeloss,closebuy,closesell; 
int lastsig,last,tries,a1,a2,a3,a4;
double openPrice1,openPrice2,openPrice3;
double openProfit,openLoss;

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders()
  {
   int orders;
   openProfit=0;
   openLoss=0;
   for(int i=0;i<OrdersTotal();i++) {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==myMagic) {
         orders++;
         if (OrderProfit()>0) openProfit+=OrderProfit(); else openLoss+=-OrderProfit();
      }
   }
   return(orders);
}

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForSignals() {
      double ao1=iAO(NULL,0,shift);
      double ao2=iAO(NULL,0,shift+1);
      double ao3=iAO(NULL,0,shift+2);
      double ao4=iAO(NULL,0,shift+3);
      double ao5=iAO(NULL,0,shift+4);

      buysig1=false;
      buysig2=false;
      buysig3=false;
      sellsig1=false;
      sellsig2=false;
      sellsig3=false;
      if ((ao1>AOLongLevel*Point && ao2<=AOLongLevel*Point) || (ao1>AOShortLevel*Point && ao2<=AOShortLevel*Point)) {
         if (a1!=Time[0]) {
            Alert(Symbol()+Period()+" 0.AO - level cross up");
            a1=Time[0];
         }
      }
      if ((ao2>AOLongLevel*Point && ao3<=AOLongLevel*Point) || (ao2>AOShortLevel*Point && ao3<=AOShortLevel*Point) && ao1>ao2) {
         if (a2!=Time[0]) {
            Alert(Symbol()+Period()+" 1.AO-enter long 1 lot");
            a2=Time[0];
         }
         buysig1=true;
      }
      if ((ao3>AOLongLevel*Point && ao4<=AOLongLevel*Point) || (ao3>AOShortLevel*Point && ao4<=AOShortLevel*Point) && ao2>ao3 && ao1>ao2 && Bid-openPrice1>0.0) {
         if (a3!=Time[0]) {
            Alert(Symbol()+Period()+" 2.AO-enter long 2 lots");
            a3=Time[0];
         }
         buysig2=true;
      }
      if ((ao4>AOLongLevel*Point && ao5<=AOLongLevel*Point) || (ao4>AOShortLevel*Point && ao5<=AOShortLevel*Point) && ao3>ao4 && ao2>ao3 && ao1>ao2 && Bid-openPrice2>0.0) {
         if (a4!=Time[0]) {
            Alert(Symbol()+Period()+" 3.AO-enter long 1 lot");
            a4=Time[0];
         }
         buysig3=true;
      }

      if ((ao1<AOLongLevel*Point && ao2>=AOLongLevel*Point) || (ao1<AOShortLevel*Point && ao2>=AOShortLevel*Point)) {
         if (a1!=Time[0]) {
            Alert(Symbol()+Period()+" 0.AO - level cross down");
            a1=Time[0];
         }
      }
      if ((ao2<AOLongLevel*Point && ao3>=AOLongLevel*Point) || (ao2<AOShortLevel*Point && ao3>=AOShortLevel*Point) && ao1<ao2) {
         if (a2!=Time[0]) {
            Alert(Symbol()+Period()+" 1.AO-enter short 1 lot");
            a2=Time[0];
         }
         sellsig1=true;
      }
      if ((ao3<AOLongLevel*Point && ao4>=AOLongLevel*Point) || (ao3<AOShortLevel*Point && ao4>=AOShortLevel*Point) && ao2<ao3 && ao1<ao2 && openPrice1-Ask>0.0) {
         if (a3!=Time[0]) {
            Alert(Symbol()+Period()+" 2.AO-enter short 2 lots");
            a3=Time[0];
         }
         sellsig2=true;
      }
      if ((ao4<AOLongLevel*Point && ao5>=AOLongLevel*Point) || (ao4<AOShortLevel*Point && ao5>=AOShortLevel*Point) && ao3<ao4 && ao2<ao3 && ao1<ao2 && openPrice2-Ask>0.0) {
         if (a4!=Time[0]) {
            Alert(Symbol()+Period()+" 3.AO-enter short 1 lot");
            a4=Time[0];
         }
         sellsig3=true;
      }
      closeall=false;
      closeloss=false;
      if (openProfit>AllProfit) closeall=true;
      if (openLoss>AllLoss) closeloss=true;
}

void CheckForOpen() {
   int    res,tr;
//---- buy conditions
   if(CalculateCurrentOrders()==0 && buysig1 && last!=Time[0])  {
	   res=0;
	   tries=0;
		while (res<=0 && tries<OrderTriesNumber) {
	       tr=0;
          while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
		    RefreshRates();
          res=OrderSend(Symbol(),OP_BUY,Lots,Ask,slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"ao1b",myMagic,0,Blue);
		    if (res<0) Print("Error opening BUY order : ",ErrorDescription(GetLastError()));
          tries++;
		}
		if (res>0) { last=Time[0]; openPrice1=Ask; }
      return;
   }
   if(CalculateCurrentOrders()==1 && buysig2 && last!=Time[0])  {
	   res=0;
	   tries=0;
		while (res<=0 && tries<OrderTriesNumber) {
	       tr=0;
          while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
		    RefreshRates();
          res=OrderSend(Symbol(),OP_BUY,Lots*2,Ask,slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"ao2b",myMagic,0,Blue);
		    if (res<0) Print("Error opening BUY order : ",ErrorDescription(GetLastError()));
          tries++;
		}
		if (res>0) { last=Time[0]; openPrice2=Ask; }
      return;
   }
   if(CalculateCurrentOrders()==2 && buysig3 && last!=Time[0])  {
	   res=0;
	   tries=0;
		while (res<=0 && tries<OrderTriesNumber) {
	       tr=0;
          while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
		    RefreshRates();
          res=OrderSend(Symbol(),OP_BUY,Lots*2,Ask,slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"ao3b",myMagic,0,Blue);
		    if (res<0) Print("Error opening BUY order : ",ErrorDescription(GetLastError()));
          tries++;
		}
		if (res>0) { last=Time[0]; openPrice3=Ask; }
      return;
   }
//---- sell conditions
   if(CalculateCurrentOrders()==0 && sellsig1 && last!=Time[0])  {
	   res=0;
	   tries=0;
		while (res<=0 && tries<OrderTriesNumber) {
	       tr=0;
          while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
		    RefreshRates();
          res=OrderSend(Symbol(),OP_SELL,Lots,Bid,slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"ao1s",myMagic,0,Red);
		    if (res<0) Print("Error opening SELL order : ",ErrorDescription(GetLastError()));
		    tries++;
		}
		if (res>0) { last=Time[0]; openPrice1=Bid; }
      return;
   }
   if(CalculateCurrentOrders()==1 && sellsig2 && last!=Time[0])  {
	   res=0;
	   tries=0;
		while (res<=0 && tries<OrderTriesNumber) {
	       tr=0;
          while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
		    RefreshRates();
          res=OrderSend(Symbol(),OP_SELL,Lots*2,Bid,slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"ao2s",myMagic,0,Red);
		    if (res<0) Print("Error opening SELL order : ",ErrorDescription(GetLastError()));
		    tries++;
		}
		if (res>0) { last=Time[0]; openPrice2=Bid; }
      return;
   }
   if(CalculateCurrentOrders()==2 && sellsig3 && last!=Time[0])  {
	   res=0;
	   tries=0;
		while (res<=0 && tries<OrderTriesNumber) {
	       tr=0;
          while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
		    RefreshRates();
          res=OrderSend(Symbol(),OP_SELL,Lots,Bid,slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"ao3s",myMagic,0,Red);
		    if (res<0) Print("Error opening SELL order : ",ErrorDescription(GetLastError()));
		    tries++;
		}
		if (res>0) { last=Time[0]; openPrice3=Bid; }
      return;
   }
}
  
  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()  {
   bool bres; int tr;
   for(int i=0;i<OrdersTotal();i++)  {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)  break;
      if(OrderMagicNumber()!=myMagic || OrderSymbol()!=Symbol()) continue;
      if (closeall) {
         bres=false;
         tries=0;
         while (!bres && tries<OrderTriesNumber) {
           tr=0;
           while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
            RefreshRates();
            bres=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,White);
            Sleep(3000);
	         if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError()));
	         tries++;
	      }
      }
      if (closeloss && OrderProfit()<0) {
         bres=false;
         tries=0;
	      while (!bres && tries<OrderTriesNumber) {
           tr=0;
           while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
            RefreshRates();
            bres=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),slippage,White);
            Sleep(3000);
		      if (!bres) Print("Error closing order : ",ErrorDescription(GetLastError()));
		      tries++;
		   }
	   }
	}
}


void TickTrailing() {
   bool bres; int tr;
   double StopLoss;
      for (int i = 0; i < OrdersTotal(); i++) {
         if ( OrderSelect (i, SELECT_BY_POS) == false )  continue;
         if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic )  continue;
         if ( OrderType() == OP_BUY ) {
            StopLoss = OrderStopLoss()+Point;
            tr=0;
            while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
            bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
            Sleep(3000);
			   if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError()));
         }
   
         if ( OrderType() == OP_SELL ) {
            StopLoss = OrderStopLoss()-Point;
            tr=0;
            while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
            bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold);
            Sleep(3000);
			   if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError()));
         }
      }
}

void TrailStop() {
   bool bres; int tr;
   double StopLoss;
   if ( TrailingStop > 2 ) {
      for (int i = 0; i < OrdersTotal(); i++) {
         if ( OrderSelect (i, SELECT_BY_POS) == false )  continue;
         if ( OrderSymbol() != Symbol() || OrderMagicNumber() != myMagic )  continue;
         if ( OrderType() == OP_BUY ) {
            if ( Bid < OrderOpenPrice()+TrailingStop*Point )  continue;
            StopLoss = Bid-TrailingStop*Point;
            if ( StopLoss > OrderStopLoss() ) {
                  tr=0;
                  while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
                  Sleep(3000);
					   if (!bres) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError()));
            }
         }
   
         if ( OrderType() == OP_SELL ) {
            if ( Ask > OrderOpenPrice()-TrailingStop*Point )  continue;
            StopLoss = Ask+TrailingStop*Point;
            if ( StopLoss < OrderStopLoss() ) {
                  tr=0;
                  while(tr<7 && !IsTradeAllowed()) { tr++; Sleep(5000); }
                  bres=OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold);
                  Sleep(3000);
					   if (!bres) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError()));
            }
         }
      }
   }
   return;
}

void start()  {

   //---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;

   //---- check for signals
   CheckForSignals();

   //---- calculate open orders by current symbol
   if (CalculateCurrentOrders()<3) 
      CheckForOpen();
      
   if (CalculateCurrentOrders()>0) 
      CheckForClose();

   TickTrailing();     

   TrailStop();
}
//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---