#Graal-CROSSmuvingi-tral





//+------------------------------------------------------------------+
//|                                            Graal-FxProg_team.mq4 |
//|               //+-                                              Rosh |
//|               http://www.investo.ru/forum/viewtopic.php?t=124777 |
//+------------------------------------------------------------------+
#property copyright "Rosh"

#define MAGIC  256
extern int       FastPeriod=13;
extern int       SlowPeriod=34;
extern int       MomPeriod=14;
extern double    MomFilter=0.1;
extern double    PercentCapital=10.0;
extern double    Lots=0.1;
extern int       Slippage=3;
extern int       StopLoss=20;
extern int       TakeProfit=200;
int myBars;
extern double Patr=9;
extern double Prange=5;
extern double Kstop=1.5;
extern double kts=2;
extern double Vts=2;
extern double MaximumRisk        = 0.05;
extern double DecreaseFactor     = 10;
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//----
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//---- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }
//+------------------------------------------------------------------+
//| Calculate optimal lot size                                       |
//+------------------------------------------------------------------+
double LotsOptimized()
  {
   double lot=Lots;
   int    orders=HistoryTotal();     // history orders total
   int    losses=0;                  // number of losses orders without a break
//---- select lot size
   lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
   if(DecreaseFactor>0)
     {
      for(int i=orders-1;i>=0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); break; }
         if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
         //----
         if(OrderProfit()>0) break;
         if(OrderProfit()<0) losses++;
        }
      if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
     }
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
  }
//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForOpen()
  {
  int cnt;
  double curFastMA=iMA(NULL,0,FastPeriod,0,MODE_EMA,PRICE_CLOSE,1);
  double curSlowMA=iMA(NULL,0,SlowPeriod,0,MODE_EMA,PRICE_OPEN,1);
  double prevFastMA=iMA(NULL,0,FastPeriod,0,MODE_EMA,PRICE_CLOSE,2);
  double prevSlowMA=iMA(NULL,0,SlowPeriod,0,MODE_EMA,PRICE_OPEN,2);
  double curMom=iMomentum(NULL,0,MomPeriod,PRICE_CLOSE,1)-100.0;
  double prevMom=iMomentum(NULL,0,MomPeriod,PRICE_CLOSE,2)-100.0;
//----
   if (Bars!=myBars)
      {
      myBars=Bars;
      if (curFastMA>curSlowMA && prevFastMA<prevSlowMA && curMom>MomFilter && curMom>prevMom)
         {
         
         OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,Slippage,0,Ask+TakeProfit*Point,"buy",MAGIC,0,Blue);
         }
      if (curFastMA<curSlowMA && prevFastMA>prevSlowMA && curMom<-MomFilter && curMom<prevMom)
         {
         
         OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,Slippage,0,Bid-TakeProfit*Point,"sell",MAGIC,0,Red);
         }
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose()
  {
  int i,mode,ticket,total;
  double cnt=0,ValATR=0,hi=0,lo=0,SL=0,TS=0,prevBars=0;
  if (prevBars!=Bars) 
  {
    ValATR=0;
    for(i=1; i<=Patr; i++) { if(i<=Patr) { ValATR+=High[i]-Low[i]; } }
    ValATR=ValATR/Patr;
     
    hi=High[Highest(NULL,0,MODE_HIGH,Prange,Prange)]; 
    lo=Low[Lowest(NULL,0,MODE_LOW,Prange,Prange)]; 
 
    if (Vts==1)  {TS=kts*ValATR; SL=Kstop*ValATR;}
    if (Vts==2)  {TS=(hi-lo);    SL=Kstop*(hi-lo);}
    prevBars = Bars;
  }

  if (Vts<1 || Vts>2)  return(0);

//-------------------------------------------------------

  for (cnt=0; cnt<=OrdersTotal(); cnt++)
  {
    OrderSelect(cnt,SELECT_BY_POS);
    mode=OrderType();
    if(OrderSymbol()==Symbol())
    {
//First Stop---------------------------------
      if (mode==OP_BUY && OrderStopLoss()== 0)
      {
        OrderModify(OrderTicket(),OrderOpenPrice(),Low[0]-SL,OrderTakeProfit(),0,CLR_NONE);
        PlaySound("expert.wav");
        return(0);
      }

      if (mode==OP_SELL && OrderStopLoss()==0)
      {
        OrderModify(OrderTicket(),OrderOpenPrice(),High[0]+SL,OrderTakeProfit(),0,CLR_NONE);
        PlaySound("expert.wav");
        return(0);
      }
//Main Trailing-------------------------------
      if ((mode==OP_BUY && High[0]-OrderOpenPrice()>TS && OrderStopLoss()<High[0]-TS) || OrderStopLoss()==0)
      {
        OrderModify(OrderTicket(),OrderOpenPrice(),High[0]-TS,OrderTakeProfit(),0,CLR_NONE);
        PlaySound("expert.wav");
        return(0);
      }

      if ((mode==OP_SELL && OrderOpenPrice()-Low[0]>TS && OrderStopLoss()>Low[0]+TS) || OrderStopLoss()==0)
      {
        OrderModify(OrderTicket(),OrderOpenPrice(),Low[0]+TS,OrderTakeProfit(),0,CLR_NONE);
        PlaySound("expert.wav");
        return(0);
      }
    }
  }

  Comment("Versia: ",Vts,"\n",
          "Per_ATR: ",Patr,"\n",
          "Per_Range: ",Prange,"\n",
          "Range: ",(High[0]-Low[0]),"\n",
          "ATR: ",ValATR,"\n",
          "SL: ",SL,"\n",
          "TS: ",TS);

  return(0);
}

//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
   if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
   else                                    CheckForClose();
//----
  }
//+------------------------------------------------------------------+



Sample





Analysis



Market Information Used:

Series array that contains the highest prices of each bar
Series array that contains the lowest prices of each bar


Indicator Curves created:


Indicators Used:

Moving average indicator
Momentum indicator


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

It automatically opens orders when conditions are reached
It can change open orders parameters, due to possible stepping strategy

Other Features:

It plays sound alerts

BackTest : EURUSD on H1

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

BackTest : EURUSD on H1

From 2009-12-01 to 2010-01-17 Profit Factor:2.15 Total Net Profit:534.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.74 Total Net Profit:-473.30

BackTest : USDCAD on H1

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

BackTest : USDCHF on H1

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

BackTest : USDJPY on H1

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

Request Backtest for #Graal-CROSSmuvingi-tral


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

Pair: Period: