Generic EA V1_0

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategy
Indicators Used
Moving average indicator
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites

Profitability Reports

AUD/USD Oct 2024 - Jan 2025
0.00 %
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
0.00 %
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%
NZD/USD Oct 2024 - Jan 2025
0.00 %
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%
Generic EA V1_0
//+------------------------------------------------------------------+
#define g_strEAname "Generic EA" 
#define g_nSystemID    20070428 // chanege to current date
//|                                               Paul Hampton-Smith |
//+------------------------------------------------------------------+

// MarketInfo variables
int g_nDigits = 0;
double g_dblBid = 0.0;
double g_dblAsk = 0.0;
double g_dblPoint = 0.0;
double g_dblSwapLong = 0.0;		
double g_dblSwapShort = 0.0;

/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
// CODE SPECIFIC TO THIS EA BELOW THIS
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////


// external optimisable variables
extern int e_nStopLoss = 50;
extern int e_nTakeProfit = 50;
extern bool e_bTrailingStop = false;

// custom variables for this EA
extern int e_nPeriod = PERIOD_D1; // run the EA on this timeframe chart
extern int e_nMAperiod = 200;

// money management
extern double e_dblLots = 0.1;
extern bool e_bMoneyManagement = false;
extern int e_nStartCapital = 5000;
extern int e_nPercentOfMargin = 2;

// Indicator variables
double g_MA1 = 0.0;
double g_Close1 = 0.0;


bool LoadIndicatorValues()
{
	g_Close1 = Close[1];
	g_MA1 = iMA(Symbol(),0,e_nMAperiod,0,MODE_SMA,PRICE_CLOSE,1);

	// sanity check of values	
	if (g_Close1 == 0 || g_MA1 == 0)
	{
		Print("Error in LoadIndicatorValues() ",CommentLine());
		return(false);
	}
	else
	{
		return(true);
	}
}

bool BuyEntrySignal()
{
	return(
		g_Close1 > g_MA1 &&
		OpenOrders() == 0
		);
} 

bool SellEntrySignal()
{
	return(
		g_Close1 < g_MA1 &&
		OpenOrders() == 0
		);
} 

bool BuyExitSignal()
{
	return(
		g_Close1 < g_MA1
		);
} 
      
bool SellExitSignal()
{
	return(
		g_Close1 > g_MA1
		);
} 

string CommentLine()
{
	return("");
}


/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
// GENERIC CODE BELOW THIS
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////

#include <Stdlib.mqh>

int start()
{
	if (e_nPeriod != 0 && e_nPeriod != Period())
	{
		Alert("This EA should only run on a ",PeriodToStr(e_nPeriod)," chart");
		return(0);
	}

	if (!LoadMarketInfo()) return(0); // once we know g_strSymbol, load the current MarketInfo values
	if (!LoadIndicatorValues()) return(0); // create all the indicator values needed for entry and exit
	if (e_bTrailingStop) AdjustTrailingStops();
	ConditionalExit(); // exit if exit rules are met
	MoneyManagement(); // adjust lot size according to account balance and risk
	ConditionalEntry(); // enter if entry rules are met
	Comment(CommentLine()); // display info

   return(0);  
}

int OpenOrders()
{
	// counts total buy and sell open orders
   int nCount = 0;
   for ( int nPosition=0 ; nPosition<OrdersTotal() ; nPosition++ )
   {
      OrderSelect(nPosition, SELECT_BY_POS, MODE_TRADES);
      if ( OrderSymbol()==Symbol() &&
         ( OrderType() == OP_BUY || OrderType() == OP_SELL ) &&
         OrderMagicNumber() == g_nSystemID )
      {
         nCount++;
      }
   }
   return(nCount);
}

void ConditionalExit()
{
   for ( int nPosition=0 ; nPosition<OrdersTotal() ; nPosition++ )
   {
      OrderSelect(nPosition, SELECT_BY_POS, MODE_TRADES);
      if( OrderSymbol()==Symbol() && OrderMagicNumber()==g_nSystemID)
      {
      	switch (OrderType())
      	{
      	case OP_BUY:
	         if (BuyExitSignal()) 
            {
           		OrderClose(OrderTicket(),OrderLots(),g_dblBid,3,Violet);
					Print(ErrorDescription(GetLastError())); 
            }
            break;
      	case OP_SELL:
         	if (SellExitSignal())
           	{
					OrderClose(OrderTicket(),OrderLots(),g_dblAsk,3,Violet);
					Print(ErrorDescription(GetLastError())); 
            }
         }
      }
   }
}

int ConditionalEntry()
{
	// defaults
	int nSlippage = 3;
	color clrBuy = Green;
	color clrSell = Red;

	double dblStopLoss = 0.0, dblTakeProfit = 0.0;
	int nTicket;

	if (BuyEntrySignal())
	{
		if (e_nStopLoss > 0) dblStopLoss = NormalizeDouble(g_dblAsk-e_nStopLoss*g_dblPoint,g_nDigits);
		if (e_nTakeProfit > 0) dblTakeProfit = NormalizeDouble(g_dblBid+e_nTakeProfit*g_dblPoint,g_nDigits);
		nTicket = OrderSend(Symbol(),OP_BUY,e_dblLots,g_dblAsk,nSlippage,dblStopLoss,dblTakeProfit,g_strEAname,g_nSystemID,0,clrBuy);
		Print(ErrorDescription(GetLastError())); 
		return(nTicket);
	}
	
	if (SellEntrySignal())
	{
		if (e_nStopLoss > 0) dblStopLoss = NormalizeDouble(g_dblBid+e_nStopLoss*g_dblPoint,g_nDigits);
		if (e_nTakeProfit > 0) dblTakeProfit = NormalizeDouble(g_dblAsk-e_nTakeProfit*g_dblPoint,g_nDigits);
		nTicket = OrderSend(Symbol(),OP_SELL,e_dblLots,g_dblBid,nSlippage,dblStopLoss,dblTakeProfit,g_strEAname,g_nSystemID,0,clrSell);
		Print(ErrorDescription(GetLastError())); 
		return(nTicket);
	}
	return(-1); // no entry signal
}


bool LoadMarketInfo()
{
	g_nDigits = Digits;		
	g_dblBid = NormalizeDouble(Bid,g_nDigits);		
	g_dblAsk = NormalizeDouble(Ask,g_nDigits);		
	g_dblPoint = NormalizeDouble(Point,g_nDigits);		
	g_dblSwapLong = MarketInfo(Symbol(),MODE_SWAPLONG);		
	g_dblSwapShort = MarketInfo(Symbol(),MODE_SWAPSHORT);
	
	// sanity check
	if (g_nDigits == 0 || g_dblBid == 0 || g_dblAsk == 0 || g_dblPoint == 0)
	{
		Print("Error in LoadMarketInfo()");
		return(false);
	}
	else
	{
		return(true);
	}
}


void MoneyManagement()
{
	if (e_bMoneyManagement)
	{
  		e_dblLots = NormalizeDouble((e_nPercentOfMargin/100.0)*(AccountFreeMargin()-e_nStartCapital)*AccountLeverage()/MarketInfo(Symbol(),MODE_LOTSIZE),1);
		// if using 100% (!!!!), reduce slightly to ensure under limit
   	if (e_nPercentOfMargin == 100) e_dblLots -= 0.1;
	}
	// if !e_bMoneyManagement, no change to e_dblLots

	// minimum lots is 0.1
   e_dblLots = MathMax(e_dblLots,0.1);
}

string PeriodToStr(int nPeriod)
{
	switch(nPeriod)
	{
	case PERIOD_M1: return("1 Minute");
	case PERIOD_M5: return("5 Minute");
	case PERIOD_M15: return("15 Minute");
	case PERIOD_M30: return("30 Minute");
	case PERIOD_H1: return("1 Hour");
	case PERIOD_H4: return("4 Hour");
	case PERIOD_D1: return("Daily");
	case PERIOD_W1: return("Weekly");
	case PERIOD_MN1: return("Monthly");
	default: return("Unknown period");
	}
}	

void AdjustTrailingStops()
{
   for ( int nPosition=0 ; nPosition<OrdersTotal() ; nPosition++ )
   {
      OrderSelect(nPosition, SELECT_BY_POS, MODE_TRADES);
      if ( OrderSymbol()==Symbol() && OrderMagicNumber() == g_nSystemID)
      {
      	double dblNewStop = 0.0;
      	switch(OrderType())
      	{
      	case OP_BUY:
      		dblNewStop = NormalizeDouble(g_dblBid-e_nStopLoss*g_dblPoint,g_nDigits);
      		if (dblNewStop > OrderStopLoss())
         	{
            	OrderModify(OrderTicket(),OrderOpenPrice(),dblNewStop,OrderTakeProfit(),0,Green);
         	}
         	break;
         case OP_SELL:
      		dblNewStop = NormalizeDouble(g_dblAsk+e_nStopLoss*g_dblPoint,g_nDigits);
      		if (dblNewStop < OrderStopLoss())
	         {
   	         OrderModify(OrderTicket(),OrderOpenPrice(),dblNewStop,OrderTakeProfit(),0,Red);
      	   }
      	}
      } 
   }
}

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 ---