TrendStuffer-expert-ULTIMATE-LongTermH1





#property  copyright "Copyright c 2007, TrendStuffer.com"
#property  link "http://www.trendstuffer.com/"

#include   <stdlib.mqh>

extern   string   introone = "**TrendStuffer.com LongTerm H1 EA**";
extern   string   introtwo = "**Built for GBP/USD H1 chart**";
extern   string   texta = "-=[Expert's Settings Area]=-";
extern   int      TrendStufferMagic = 20070601;
extern   string   EAComment = "TrendStuffer EA GBPUSD H1";
extern   int      MaxSlippage = 2;
extern   int      OrderTriesNumber = 5;
extern   int      StartHour = 0;
extern   int      EndHour = 24;
extern   int      ExpirationHour = 24;
extern   string   textb = "-=[How much to trade with?]=-";
extern   bool     FixedLot = true;
extern   double   Lots = 0.1;
extern   double   MaximumRisk = 5;
extern   string   textc = "-=[Position's settings?]=-";
extern   double   TakeProfit = 200;
extern   double   StopLoss = 90;
extern   int      CurrentBarTrailingStart = 30;
extern   int      CurrentBarTrailingStop = 80;
extern   int      ClosedBarTrailingStop = 60;
extern   int      ClosedBarTrailingStart = 50;
extern   string   textd = "-=[Use the 3-Positions method?]=-";
extern   bool     UseThreeP = false;
extern   int      ThreePMagic = 320070601;
extern   double   Lot1 = 0.5;
extern   double   Lot2 = 0.3;
extern   double   Lot3 = 0.2;
extern   int      TakeProfit1 = 20;
extern   int      TakeProfit2 = 50;
extern   int      TakeProfit3 = 80;
extern   int      InitialSL1 = 50;
extern   int      InitialSL2 = 70;
extern   int      InitialSL3 = 100;
int      var_264 = 1;
int      var_268 = 1;
int      var_272 = 14;
double   var_276 = 2.824;
bool     var_284;
bool     var_288;
bool     var_292;
bool     var_296;
int      var_300;
int      Attempt;
int      var_308;
double   var_312;
double   var_320;
double   var_328;
double   var_336;

//+------------------------------------------------------------------+

double LotsRisk(int risk)
{
double lots = Lots;

if (!FixedLot)
   lots = NormalizeDouble(AccountFreeMargin() * MaximumRisk / 1000.0 / risk,1);
      else
   lots = Lots;

if (lots < 0.01) lots = 0.01;
return(lots);
}

//+------------------------------------------------------------------+

int CalculateCurrentOrders()
{
if (UseThreeP == false)
   {
   int buy_cnt = 0;
   int sell_cnt = 0;
   for (int i = 0; i < OrdersTotal(); i++)
      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;
      if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == TrendStufferMagic))
         {
         if (OrderType() == OP_BUY) buy_cnt++;
         if (OrderType() == OP_SELL) sell_cnt++;
         }
      }
   if (buy_cnt > 0) return(buy_cnt); else return(-sell_cnt);
   }

if (UseThreeP == true)
   {
   buy_cnt = 0;
   sell_cnt = 0;
   for (i = 0; i < OrdersTotal(); i++)
      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;
      if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == ThreePMagic))
         {
         if (OrderType() == OP_BUY) buy_cnt++;
         if (OrderType() == OP_SELL) sell_cnt++;
         }
      }
   if (buy_cnt > 0) return(buy_cnt); else return(-sell_cnt);
   }
}

//+------------------------------------------------------------------+

void CheckForSignals()
{
double var_CheckForSignals_0;
double ordprice;
int    ordtype;
int    i;

if (var_308 != Time[0])
   {
   var_308 = Time[0];
   var_312 = iCustom(NULL,0,"TrendStuffer-base",0,40,var_272,var_276,0,var_264);
   var_320 = iCustom(NULL,0,"TrendStuffer-base",0,40,var_272,var_276,0,var_264 + 1);
   var_312 = NormalizeDouble(var_312,Digits);
   var_320 = NormalizeDouble(var_320,Digits);
   var_328 = iCustom(NULL,0,"TrendStuffer-base",1,ClosedBarTrailingStop,var_272,var_276,0,var_268);
   var_336 = iCustom(NULL,0,"TrendStuffer-base",1,ClosedBarTrailingStop,var_272,var_276,0,var_268 + 1);
   var_328 = NormalizeDouble(var_328,Digits);
   var_336 = NormalizeDouble(var_336,Digits);
   }

var_288 = false;
var_284 = false;
if ((NormalizeDouble(Close[var_264],Digits) - NormalizeDouble(var_312,Digits) >= Point) && (NormalizeDouble(var_320,Digits) - NormalizeDouble(Close[var_264 + 1],Digits) >= 0)) var_284 = true;
if ((NormalizeDouble(var_312,Digits) - NormalizeDouble(Close[var_264],Digits) >= Point) && (NormalizeDouble(Close[var_264 + 1],Digits) - NormalizeDouble(var_320,Digits) >= 0)) var_288 = true;
var_296 = var_284;
var_292 = var_288;

if (var_296 || var_292) return;
if (CalculateCurrentOrders() == 0) return;

for (i = 0; i < OrdersTotal(); i++)
   {
   if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;
   if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == TrendStufferMagic))
      {
      ordprice = OrderOpenPrice();
      ordtype = OrderType();
      }
   }

var_296 = false;
var_292 = false;
var_CheckForSignals_0 = NormalizeDouble(ordprice + ClosedBarTrailingStart * Point,Digits);
if ((ordtype == OP_BUY) && (Close[var_268] >= var_CheckForSignals_0) && (Close[var_268] < var_328) && (Close[var_268 + 1] >= var_336)) var_292 = true;
var_CheckForSignals_0 = NormalizeDouble(ordprice - ClosedBarTrailingStart * Point,Digits);
if ((ordtype == OP_SELL) && (Close[var_268] <= var_CheckForSignals_0) && (Close[var_268] > var_328) && (Close[var_268 + 1] <= var_336)) var_296 = true;
}

//+------------------------------------------------------------------+

void CheckForOpen()
{
int    ticket;
int    hour;
int    ticket1;
int    ticket2;
int    ticket3;
string comment;

if (UseThreeP == false)
   {
   hour = TimeHour(TimeCurrent());
   if ((StartHour < hour) && (EndHour > hour))
      {
      if (var_288 && (var_300 != -1))
         {
         ticket = 0;
         Attempt = 0;
         while ((ticket <= 0) && (Attempt < OrderTriesNumber))
            {
            while (!IsTradeAllowed()) Sleep(5000);
            RefreshRates();
            ticket = OrderSend(Symbol(),OP_SELL,LotsRisk(StopLoss),Bid,MaxSlippage,Bid + StopLoss * Point,Bid - TakeProfit * Point,EAComment,TrendStufferMagic,0,Red);
            if (ticket < 0) Print("Error opening SELL order : ",ErrorDescription(GetLastError()));
            Attempt++;
            }
         var_300 = -1;
         return;
         }
      if (var_284 && (var_300 != 1))
         {
         ticket = 0;
         Attempt = 0;
         while ((ticket <= 0) && (Attempt < OrderTriesNumber))
            {
            while (!IsTradeAllowed()) Sleep(5000);
            RefreshRates();
            ticket = OrderSend(Symbol(),OP_BUY,LotsRisk(StopLoss),Ask,MaxSlippage,Ask - StopLoss * Point,Ask + TakeProfit * Point,EAComment,TrendStufferMagic,0,Blue);
            Attempt++;
            }
         var_300 = 1;
         return;
         }
      }
   }

if (UseThreeP == true)
   {
   hour = TimeHour(TimeCurrent());
   if ((StartHour < hour) && (EndHour > hour))
      {
      comment = "TrendStuffer ThreeP " + Period() + " " + Symbol();
      if (var_288 && (var_300 != -1))
         {
         ticket1 = OrderSend(Symbol(),OP_SELL,Lot1,Bid,MaxSlippage,Bid + InitialSL1 * Point,Bid - TakeProfit1 * Point,comment,ThreePMagic,0,Red);
         ticket2 = OrderSend(Symbol(),OP_SELL,Lot2,Bid,MaxSlippage,Bid + InitialSL2 * Point,Bid - TakeProfit2 * Point,comment,ThreePMagic,0,Red);
         ticket3 = OrderSend(Symbol(),OP_SELL,Lot3,Bid,MaxSlippage,Bid + InitialSL3 * Point,Bid - TakeProfit3 * Point,comment,ThreePMagic,0,Red);
         var_300 = -1;
         }
            else
         {
         if (var_284 && (var_300 != 1))
            {
            ticket1 = OrderSend(Symbol(),OP_BUY,Lot1,Ask,MaxSlippage,Ask - InitialSL1 * Point,Ask + TakeProfit1 * Point,comment,ThreePMagic,0,Blue);
            ticket2 = OrderSend(Symbol(),OP_BUY,Lot2,Ask,MaxSlippage,Ask - InitialSL2 * Point,Ask + TakeProfit2 * Point,comment,ThreePMagic,0,Blue);
            ticket3 = OrderSend(Symbol(),OP_BUY,Lot3,Ask,MaxSlippage,Ask - InitialSL3 * Point,Ask + TakeProfit3 * Point,comment,ThreePMagic,0,Blue);
            var_300 = 1;
            }
         }
      }
   }
}

//+------------------------------------------------------------------+

void CheckForClose()
{
bool result;
int  i;

if (UseThreeP == false)
   {
   for (i = 0; i < OrdersTotal(); i++)
      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;
      if ((OrderMagicNumber() != TrendStufferMagic) || (OrderSymbol() != Symbol())) continue;
      if (OrderType() == OP_BUY)
         {
         if (var_292)
            {
            result = false;
            Attempt = 0;
            while (!result && (Attempt < OrderTriesNumber))
               {
               while (!IsTradeAllowed()) Sleep(5000);
               RefreshRates();
               result = OrderClose(OrderTicket(),OrderLots(),Bid,MaxSlippage,Gold);
               Sleep(3000);
               if (!result) Print("Error closing order : ",ErrorDescription(GetLastError()));
               Attempt++;
               }
            }
         break;
         }
      if (OrderType() == OP_SELL)
         {
         if (var_296)
            {
            result = false;
            Attempt = 0;
            while (!result && (Attempt < OrderTriesNumber))
               {
               while (!IsTradeAllowed()) Sleep(5000);
               RefreshRates();
               result = OrderClose(OrderTicket(),OrderLots(),Ask,MaxSlippage,Gold);
               Sleep(3000);
               if (!result) Print("Error closing order : ",ErrorDescription(GetLastError()));
               Attempt++;
               }
            }
         break;
         }
      }
   }

if (UseThreeP == true)
   {
   for (i = 0; i < OrdersTotal(); i++)
      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES) == false) break;
      if ((OrderMagicNumber() != ThreePMagic) || (OrderSymbol() != Symbol())) continue;
      if (OrderType() == OP_BUY)
         {
         if ((Bid > OrderOpenPrice() + TakeProfit1 * Point) && (Bid < OrderOpenPrice() + TakeProfit2 * Point) && (OrderStopLoss() != OrderOpenPrice() + Point * 1.0))
            {
            result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() + Point * 1.0,OrderTakeProfit(),0,Gold);
            }
         if ((Bid > OrderOpenPrice() + TakeProfit2 * Point) && (Bid < OrderOpenPrice() + TakeProfit3 * Point) && (OrderStopLoss() != OrderOpenPrice() + TakeProfit1 * Point))
            {
            result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() + TakeProfit1 * Point,OrderTakeProfit(),0,Gold);
            }
         if (var_292)
            {
            result = OrderClose(OrderTicket(),OrderLots(),Bid,0,Gold);
            }
         }
      if (OrderType() == OP_SELL)
         {
         if ((Ask < OrderOpenPrice() - TakeProfit1 * Point) && (Ask > OrderOpenPrice() - TakeProfit2 * Point) && (OrderStopLoss() != OrderOpenPrice() - Point * 1.0))
            {
            result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() - Point * 1.0,OrderTakeProfit(),0,Gold);
            }
         if ((Ask < OrderOpenPrice() - TakeProfit2 * Point) && (Ask > OrderOpenPrice() - TakeProfit3 * Point) && (OrderStopLoss() != OrderOpenPrice() - TakeProfit1 * Point))
            {
            result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() - TakeProfit1 * Point,OrderTakeProfit(),0,Gold);
            }
         if (var_296)
            {
            result = OrderClose(OrderTicket(),OrderLots(),Ask,0,Gold);
            }
         }
      }
   }
}

//+------------------------------------------------------------------+

void start()
{
int    var_start_0 = 2007;
int    var_start_4 = 8;
int    var_start_8 = 27;
string var_start_12 = var_start_0 + "." + var_start_4 + "." + var_start_8;
int    var_start_20 = StrToTime(var_start_12);
/*
if (TimeCurrent() >= var_start_20)
   {
   Alert("This version has expired! Please order a new copy from www.TrendStuffer.com!");
   return;
   }
*/
if ((Bars < 100) || (IsTradeAllowed() == false)) return;

CheckForSignals();
if (CalculateCurrentOrders() == 0) CheckForOpen(); else CheckForClose();

TrailStop();
}

//+------------------------------------------------------------------+

void TrailStop()
{
bool   result;
double sl;
int    i;

if (CurrentBarTrailingStop > 2)
   {
   for (i = 0; i < OrdersTotal(); i++)
      {
      if (OrderSelect(i,SELECT_BY_POS) == false) continue;
      if ((OrderSymbol() != Symbol()) || (OrderMagicNumber() != TrendStufferMagic)) continue;
      if (OrderType() == OP_BUY)
         {
         if (Bid < OrderOpenPrice() + CurrentBarTrailingStart * Point) continue;
         sl = Bid - CurrentBarTrailingStop * Point;
         if (sl > OrderStopLoss())
            {
            result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Gold);
            if (!result) Print("Error Modifying BUY order : ",ErrorDescription(GetLastError()));
            }
         }
      if (OrderType() == OP_SELL)
         {
         if (Ask > OrderOpenPrice() - CurrentBarTrailingStart * Point) continue;
         sl = Ask + CurrentBarTrailingStop * Point;
         if (sl < OrderStopLoss())
            {
            result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,Gold);
            if (!result) Print("Error Modifying SELL order : ",ErrorDescription(GetLastError()));
            }
         }
      }
   }
}



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar
Series array that contains close prices for each bar


Indicator Curves created:


Indicators Used:




Custom Indicators Used:
TrendStuffer-base

Order Management characteristics:
Checks for the total of open orders
It automatically opens orders when conditions are reached
It Closes Orders by itself
It can change open orders parameters, due to possible stepping strategy

Other Features:


It issuies visual alerts to the screen

BackTest : EURUSD on H1

From 2009-08-01 to 2009-10-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-03-01 to 2010-03-27 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-04-01 to 2010-04-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-05-01 to 2010-05-31 Profit Factor:0.00 Total Net Profit:0.00

BackTest : EURUSD on H1

From 2010-06-01 to 2010-06-30 Profit Factor:0.00 Total Net Profit:0.00

BackTest : GBPUSD on H1

From 2010-01-01 to 2010-02-27 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDCAD on H1

From 2009-12-01 to 2010-01-01 Profit Factor:0.00 Total Net Profit:0.00

BackTest : USDJPY on H1

From 2009-11-01 to 2009-11-30 Profit Factor:0.00 Total Net Profit:0.00

Request Backtest for TrendStuffer-expert-ULTIMATE-LongTermH1


From : (yyyy/mm/dd) To: (yyyy/mm/dd)

Pair: Period: