Code:
//+------------------------------------------------------------------+
//| pippa_D1_mover_GuruEx01.mq4 |
//| Copyright © 2009, Marketing Dreams Ltd. All Rights Reserved. |
//| http://trading-gurus.com |
//| |
//| GuruTrader™ example 1 |
//| Version 1.0 |
//| |
//| About as simple a "robot" as it is possible to produce |
//| Despite its simplicity it still manages to "predict the future" |
//| with 100% accuracy.... |
//| ....in backtesting EUR/USD on one of my demo accounts |
//| between January 5th 2009 and April 11th 2009 |
//| |
//| Wealth Warning! This expert is for educational purposes only. |
//| It should NEVER be used on a live account. Past performance is |
//| in no way indicative of future results! |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Marketing Dreams Ltd."
#property link "http://trading-gurus.com"
#define SLEEP_OK 250
#define SLEEP_ERR 250
//---- input parameters
extern int Magic = 20110820;
extern int Slippage=30;
extern int RiskPercent = 11; //added pippa
extern double BarTriggerSize=1.15; //added pippa
extern double ATRFactor=1.80; //added pippa
extern int ProfitTarget=200;
extern int StopLoss=190;
extern double Lots=0.1;//not in use
//---- static variables
static int Dig;
static double Points;
static bool Initialized = FALSE;
static bool Running = FALSE;
static int OrderNumber;
static double LastBid;
static double LastAsk;
static color clBuy = DodgerBlue;
static color clSell = Crimson;
//+------------------------------------------------------------------+
//| Utility functions |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
#include <stderror.mqh>
#include <WinUser32.mqh>
//+------------------------------------------------------------------+
//| Expert helper functions |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Places an order |
//+------------------------------------------------------------------+
int Order(string symbol, int Type, double Entry, double Quantity,
double TargetPrice, double StopPrice, string comment="")
{
string TypeStr;
color TypeCol;
int ErrorCode, Ticket;
double Price, FillPrice;
Price = NormalizeDouble(Entry, Dig);
switch (Type) {
case OP_BUY:
TypeStr = "BUY";
TypeCol = clBuy;
break;
case OP_SELL:
TypeStr = "SELL";
TypeCol = clSell;
break;
default:
Print("Unknown order type ", Type);
break;
}
double MoneyAvailable=MathRound(AccountEquity());
Quantity = 0.1 + 0.1 * MathFloor((MoneyAvailable / StopLoss * RiskPercent * 0.01 - 0.1)/ 0.1);
Ticket = OrderSend(symbol, Type, Quantity, Price, Slippage,
StopPrice, TargetPrice, comment, Magic, 0, TypeCol);
if (Ticket >= 0) {
Sleep(SLEEP_OK);
if (OrderSelect(Ticket, SELECT_BY_TICKET) == TRUE) {
FillPrice = OrderOpenPrice();
if (Entry != FillPrice) {
RefreshRates();
Print("Slippage on order ", Ticket, " - Requested = ",
Entry, ", Fill = ", FillPrice, ", Current Bid = ",
Bid, ", Current Ask = ", Ask);
}
}
else {
ErrorCode = GetLastError();
Print("Error selecting new order ", Ticket, ": ",
ErrorDescription(ErrorCode), " (", ErrorCode, ")");
}
return (Ticket);
}
ErrorCode = GetLastError();
RefreshRates();
Print("Error opening ", TypeStr, " order: ", ErrorDescription(ErrorCode),
" (", ErrorCode, ")", ", Entry = ", Price, ", Target = ",
TargetPrice, ", Stop = ", StopPrice, ", Current Bid = ", Bid,
", Current Ask = ", Ask);
Sleep(SLEEP_ERR);
return (-1);
}
//+------------------------------------------------------------------+
//| Performs system initialisation |
//+------------------------------------------------------------------+
void InitSystem()
{
Running = FALSE;
RefreshRates();
LastBid = Bid;
LastAsk = Ask;
Initialized = TRUE;
}
//+------------------------------------------------------------------+
//| Checks for entry to a trade |
//+------------------------------------------------------------------+
int CheckEntry(double Size)
{
if (iVolume(0,0,0) < 11 && iClose(0,0,1) < iOpen(0,0,1) && (iOpen(0,0,1) - iClose(0,0,1)) * BarTriggerSize >= (iHigh(0,0,1) - iLow(0,0,1)) && (iATR(NULL,0,1,1) > iATR(NULL,0,10,2) * ATRFactor)){ // Large move down yesterday, BUY today
OrderNumber = Order(Symbol(), OP_BUY, Ask, Size,
Ask + (Points * ProfitTarget), Bid - (Points * StopLoss));
if (OrderNumber > 0)
return(1);
}
else if (iVolume(0,0,0) < 11 && iClose(0,0,1) > iOpen(0,0,1)&& (iClose(0,0,1) - iOpen(0,0,1)) * BarTriggerSize >= (iHigh(0,0,1) - iLow(0,0,1)) && (iATR(NULL,0,1,1) > iATR(NULL,0,10,2) * ATRFactor)){ // Large move up yesterday, SELL today
OrderNumber = Order(Symbol(), OP_SELL, Bid, Size,
Bid - (Points * ProfitTarget), Ask + (Points * StopLoss));
if (OrderNumber > 0)
return(1);
}
return(0);
}
//+------------------------------------------------------------------+
//| Checks for exit from a trade |
//+------------------------------------------------------------------+
int CheckExit()
{
int ErrorCode;
if (OrderSelect(OrderNumber, SELECT_BY_TICKET) != TRUE) {
ErrorCode = GetLastError();
Print("Error selecting order ", OrderNumber, ": ",
ErrorDescription(ErrorCode), " (", ErrorCode, ")");
return(-1);
}
else if (OrderCloseTime() > 0) {
Print("Order ", OrderNumber, " closed: ", OrderClosePrice(),
", at ", TimeToStr(OrderCloseTime()));
return(1);
}
else {
return(0);
}
}
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int init()
{
Dig = MarketInfo(Symbol(), MODE_DIGITS);
Points = MarketInfo(Symbol(), MODE_POINT);
Print("Digits = ", Dig, ", Points = ", DoubleToStr(Points, 5));
if (!IsDemo() && !IsTesting()) {
MessageBox("Wealth Warning! This expert is for educational purposes only." +
" It should NEVER be used on a live account." +
" Past performance is in no way indicative of future results!");
Print("Initialization Failure");
return(-1);
}
InitSystem();
Print("Initialized OK");
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
Print("DeInitialized OK");
return(0);
}
//+------------------------------------------------------------------+
//| Expert start function |
//| Executed on every tick |
//+------------------------------------------------------------------+
int start()
{
if (!Initialized) {
return(-1);
}
else if (Running) { // Are we in a trade at the moment?
if (CheckExit() > 0) { // Yes - Last trade complete?
Initialized = FALSE; // Yes - Indicate we need to reinitialise
InitSystem(); // and start all over again!
}
}
else {
if (CheckEntry(Lots) > 0) { // Entered a trade?
Running = TRUE; // Yes - Indicate that we're in a trade
}
}
return(0);
}