TrendStuffer-expert-ULTIMATE-StochasticFiltered





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

#include   <stdlib.mqh>

extern   string   introone = "**TrendStuffer.com StochasticFiltered EA**";
extern   string   introtwo = "**Built for GBP/USD M15 chart**";
extern   string   texta = "-=[Expert's Settings Area]=-";
extern   int      TrendStufferMagic = 20070523;
extern   string   EAComment = "TrendStuffer StochFilt EA GBPUSD H1";
extern   int      MaxSlippage = 2;
extern   int      OrderTriesNumber = 5;
extern   int      StochBuy = 80;
extern   int      StochSell = 65;
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_260 = 1;
int      var_264 = 1;
int      var_268 = 14;
double   var_272 = 2.824;
bool     var_280;
bool     var_284;
bool     var_288;
bool     var_292;
int      var_296;
int      Attempt;
int      var_304;
double   var_308;
double   var_316;
double   var_324;
double   var_332;

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

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 oprice;
int    ordtype;
int    i;

if (var_304 != Time[0])
   {
   var_304 = Time[0];
   var_308 = iCustom(NULL,0,"TrendStuffer-base",0,40,var_268,var_272,0,var_260);
   var_316 = iCustom(NULL,0,"TrendStuffer-base",0,40,var_268,var_272,0,var_260 + 1);
   var_308 = NormalizeDouble(var_308,Digits);
   var_316 = NormalizeDouble(var_316,Digits);
   var_324 = iCustom(NULL,0,"TrendStuffer-base",1,ClosedBarTrailingStop,var_268,var_272,0,var_264);
   var_332 = iCustom(NULL,0,"TrendStuffer-base",1,ClosedBarTrailingStop,var_268,var_272,0,var_264 + 1);
   var_324 = NormalizeDouble(var_324,Digits);
   var_332 = NormalizeDouble(var_332,Digits);
   }

var_284 = false;
var_280 = false;
if ((NormalizeDouble(Close[var_260],Digits) - NormalizeDouble(var_308,Digits) >= Point) && (NormalizeDouble(var_316,Digits) - NormalizeDouble(Close[var_260 + 1],Digits) >= 0) && (iStochastic(NULL,0,14,5,5,MODE_SMA,0,MODE_MAIN,1) < StochBuy)) var_280 = true;
if ((NormalizeDouble(var_308,Digits) - NormalizeDouble(Close[var_260],Digits) >= Point) && (NormalizeDouble(Close[var_260 + 1],Digits) - NormalizeDouble(var_316,Digits) >= 0) && (iStochastic(NULL,0,14,5,5,MODE_SMA,0,MODE_MAIN,1) > StochSell)) var_284 = true;
var_292 = var_280;
var_288 = var_284;

if (var_292 || var_288) 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))
      {
      oprice = OrderOpenPrice();
      ordtype = OrderType();
      }
   }

var_292 = false;
var_288 = false;

var_CheckForSignals_0 = NormalizeDouble(oprice + ClosedBarTrailingStart * Point,Digits);
if ((ordtype == OP_BUY) && (Close[var_264] >= var_CheckForSignals_0) && (Close[var_264] < var_324) && (Close[var_264 + 1] >= var_332)) var_288 = true;

var_CheckForSignals_0 = NormalizeDouble(oprice - ClosedBarTrailingStart * Point,Digits);
if ((ordtype == OP_SELL) && (Close[var_264] <= var_CheckForSignals_0) && (Close[var_264] > var_324) && (Close[var_264 + 1] <= var_332)) var_292 = true;
}

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

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

if (UseThreeP == false)
   {
   if (var_284 && (var_296 != -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_296 = -1;
      return;
      }
   if (var_280 && (var_296 != 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_296 = 1;
      return;
      }
   }

if (UseThreeP == true)
   {
   comment = "TrendStuffer ThreeP " + Period() + " " + Symbol();
   if (var_284 && (var_296 != -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_296 = -1;
      }
         else
      {
      if (var_280 && (var_296 != 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_296 = 1;
         }
      }
   }
}

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

void CheckForClose()
{
int 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_288)
            {
            result = 0;
            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_292)
            {
            result = 0;
            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() + 1 * Point))
            {
            result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() + 1 * Point,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_288)
            {
            result = OrderClose(OrderTicket(),OrderLots(),Bid,0,Gold);
            }
         }
      if (OrderType() == OP_SELL)
         {
         if ((Ask < OrderOpenPrice() - TakeProfit1 * Point) && (Ask > OrderOpenPrice() - TakeProfit2 * Point) && (OrderStopLoss() != OrderOpenPrice() - 1 * Point))
            {
            result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() - 1 * Point,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_292)
            {
            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)
         {
         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:


Stochastic oscillator


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


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

Pair: Period: