Kloss_





//+------------------------------------------------------------------+
//|                                                       Kloss_.mq4 |
//|                                       Copyright © 2007, Tinytjan |
//|                                                 tinytjan@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Tinytjan"
#property link      "tinytjan@mail.ru"

#define STUPID 0x60BE57

extern string     Lots_Desc         =  "if 0 dynamic lot is applied";
extern double     Lots              =  0;

extern string     RiskPercentage_Desc =  "actual only of Lots == 0, percentage of the risk for opening order";
extern int        RiskPercentage    =  0;

extern int        Slippage          =  2;

extern string     Target_Desc       =  "Take Profit -- none if 0";
extern int        Target            =  152;

extern string     Loss_Desc         =  "Stop Loss -- none if 0";
extern int        Loss              =  48;

extern int        MA                =  1;

extern int        CCI               =  10;

extern int        StochasticK       =  5;

extern int        StochasticD       =  3;

extern int        StochasticS       =  3;

extern string     CCIDiffer_Desc    =  "CCIDiffer and -CCIDiffer are signal levels";
extern int        CCIDiffer         =  120;

extern string     StochDiffer_Desc  =  "50 + StochDiffer and 50 - StochDiffer are signal levels";
extern int        StochDiffer       =  20;

extern string     MaxOrders_Desc    =  "no limit if 0";
extern int        MaxOrders         =  3;

double LotsToBid;
string symbol;

void CloseBuys(int MagicNumber, int Slippage)
{
   for(int i = 0; i < OrdersTotal(); i++)
   {
      // already closed
      if(OrderSelect(i, SELECT_BY_POS) == false) continue;
      // not current symbol
      if(OrderSymbol() != Symbol()) continue;
      // order was opened in another way
      if(OrderMagicNumber() != MagicNumber) continue;
      
      if(OrderType() == OP_BUY)
      {
         if(OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue))
         {
            i--;
         }
         RefreshRates();
      }
   }
}

void CloseSells(int MagicNumber, int Slippage)
{
   for(int i = 0; i < OrdersTotal(); i++)
   {
      // already closed
      if(OrderSelect(i, SELECT_BY_POS) == false) continue;
      // not current symbol
      if(OrderSymbol() != Symbol()) continue;
      // order was opened in another way
      if(OrderMagicNumber() != MagicNumber) continue;
      
      if(OrderType() == OP_SELL)
      {
         if (OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red))
         {
            i--;
         }
         RefreshRates();
      }
   }
}

int GetOrdersCount(int MagicNumber, int Type)
{
   int count = 0;
   
   for(int i = 0; i < OrdersTotal(); i++)
   {
      // already closed
      if(OrderSelect(i, SELECT_BY_POS) == false) continue;
      // not current symbol
      if(OrderSymbol() != Symbol()) continue;
      // order was opened in another way
      if(OrderMagicNumber() != MagicNumber) continue;
      
      if(OrderType() == Type)
      {
         count++;
      }
   }
   
   return (count);
}

double GetLotsToBid(int RiskPercentage)
{
   double margin = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
   double minLot = MarketInfo(Symbol(), MODE_MINLOT);
   double maxLot = MarketInfo(Symbol(), MODE_MAXLOT);
   double account = AccountFreeMargin();
   
   double percentage = account*RiskPercentage/100;
   
   double lots = MathRound(10*percentage/margin)/10;
   
   if(lots < minLot)
   {
      lots = minLot;
   }
   
   if(lots > maxLot)
   {
      lots = maxLot;
   }

   return (lots);
}

void OpenBuy()
{
   double TP = 0;
   if (Target > 0)
   {
      TP = Bid + Target*Point;
   }

   double SL = 0;
   if (Loss > 0)
   {
      SL = Bid - Loss*Point;
   }
   
   if (Lots == 0) LotsToBid = GetLotsToBid(RiskPercentage);
   
   OrderSend(Symbol(), OP_BUY, LotsToBid, Ask, Slippage, SL, TP, NULL, STUPID, 0, Blue);
}

void OpenSell()
{
   double TP = 0;
   if (Target > 0)
   {
      TP = Ask - Target*Point;
   }

   double SL = 0;
   if (Loss > 0)
   {
      SL = Ask + Loss*Point;
   }
   
   if (Lots == 0) LotsToBid = GetLotsToBid(RiskPercentage);
   
   OrderSend(Symbol(), OP_SELL, LotsToBid, Bid, Slippage, SL, TP, NULL, STUPID, 0, Red);
}

void Check()
{
   double ma = iMA(symbol, 0, MA, 0, MODE_LWMA, PRICE_TYPICAL, 5);
   double cci = iCCI(symbol, 0, CCI, PRICE_WEIGHTED, 0);
   double stoch = iStochastic(symbol, 0, StochasticK, StochasticD, StochasticS, MODE_SMA, 0, MODE_MAIN, 0);
   
   if (cci < -CCIDiffer && stoch < 50 - StochDiffer && Open[1] > ma)
   {
      if (GetOrdersCount(STUPID, OP_SELL) > 0)
      {
         CloseSells(STUPID, Slippage);
         return;
      }
      if (GetOrdersCount(STUPID, OP_BUY) < MaxOrders || MaxOrders == 0) 
      {
         OpenBuy();
      }
   }

   if (cci > CCIDiffer && stoch > 50 + StochDiffer && Close[1] < ma)
   {
      if (GetOrdersCount(STUPID, OP_BUY) > 0)
      {
         CloseBuys(STUPID, Slippage);
         return;
      }
      if (GetOrdersCount(STUPID, OP_SELL) < MaxOrders || MaxOrders == 0) 
      {
         OpenSell();
      }
   }
}

int init()
{
   LotsToBid = Lots;
   symbol = Symbol();
}

datetime LastTime = 0;

int start()
{
   if (LastTime == 0)
   {
      LastTime = TimeCurrent();
      return (0);
   }
   
   // Trading only when tick is opening
   //if (TimeCurrent() - LastTime > 20)
   {
      // Check for open new orders and close current ones
      Check();
      LastTime = TimeCurrent();
   }

   return(0);
}



Sample





Analysis



Market Information Used:

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


Indicator Curves created:


Indicators Used:

Moving average indicator
Commodity channel index
Stochastic oscillator


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

It Closes Orders by itself
It automatically opens orders when conditions are reached

Other Features:

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

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

BackTest : GBPUSD on H1

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

BackTest : GBPUSD on H1

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

BackTest : USDCAD on H1

From 2009-01-01 to 2010-01-01 Profit Factor:1.48 Total Net Profit:243.56

BackTest : USDCAD on H1

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

BackTest : USDJPY on H1

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

Request Backtest for Kloss_


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

Pair: Period: