Forex
Google
New signals service!

Go Back   Forex Trading > Discussion Areas > Setup Questions


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack (2) Thread Tools Display Modes
  #21 (permalink)  
Old 06-29-2006, 08:58 PM
Aaragorn's Avatar
Senior Member
 
Join Date: Jun 2006
Location: USA
Posts: 801
Aaragorn is on a distinguished road
PHP Code:
//+------------------------------------------------------------------+
//|                                                  EMA_CROSS_2.mq4 |
//|                                                      Coders Guru |
//|                                         http://www.forex-tsd.com |
//+------------------------------------------------------------------+
// ultima versiune cu micro lots! H1 si D1

#property copyright "Coders Guru"
#property link      "http://www.forex-tsd.com"

//---- Trades limits
extern double
                  TakeProfit        
10,
                  
TrailingStop      20,
                  
StopLoss          20;
extern bool      
                  UseStopLoss       
false;

//---- EMAs paris
extern int 
                  ShortEma          
1,
                  
LongEma           5;

//---- Crossing options
extern bool 
                  immediate_trade   
true//Open trades immediately or wait for cross.
                  
reversal          false//Use the originally reversal crossing method or not
                  
ConfirmedOnEntry  false;

//---- Money Management
extern double 
                  Lots              
1,
                  
HedgePercent      1;  // Used to calcualte the what percent of the lots the user wants to be 
                                          // used in the hedged position
                         
extern bool 
                  MM                
true//Use Money Management or not
                  
AccountIsMicro    true//Use Micro-Account or not
extern int
                  StartHour         
0,   //Indicates when the user wants the EA to start trading
                  
StopHour          23;  //Indicates when the user wants the EA to stop trading
extern int
                  Risk              
10//10%

extern int
                  MAGICMA           
20060301;
extern bool 
                  Show_Settings     
true;

//---- Global varaibles
static int
                  TimeFrame         
0;
datetime
                  CheckValueTime
;
                  
//---- Trend bands
double            upper[], middle1[], middle2lower[];
double            Xup[], Xdown[];

extern int        period 34;                  

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
   if(
Show_SettingsPrint_Details();
   else 
Comment("");
   return(
0);
  }

//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
   
TimeFrame=Period(); //Prevent counting the cross while the user changing the timeframe
   
return(0);
}
  
bool isNewSumbol(string current_symbol)
{
   
//loop through all the opened order and compare the symbols
   
int total  OrdersTotal();
   for(
int cnt cnt total cnt++)
   {
      
OrderSelect(cntSELECT_BY_POSMODE_TRADES);
      
string selected_symbol OrderSymbol();
      if (
current_symbol == selected_symbol)
      return (
False);
    }
    return (
True);
}

int Crossed (double line1 double line2)
{
      static 
int last_direction 0;
      static 
int current_direction 0;
      
      if(
TimeFrame!=Period())
      {
         
TimeFrame=Period();
         return (
0);
      }
      
      if(
line1>line2)current_direction 1//up
      
if(line1<line2)current_direction 2//down
      
      
if(immediate_trade==false)
      {
         if(
last_direction == 0//first use
         
{
               
last_direction current_direction;
               return(
0);
         }
      }

      if(
current_direction != last_direction//changed 
      
{
            
last_direction current_direction;
            return (
last_direction);
      }
      else
      {
            return (
0); //not changed
      
}
}

//--- Bassed on Alex idea! More ideas are coming
double LotSize()
{
     
double lotMM MathCeil(AccountFreeMargin() *  Risk 1000) / 100;
      
      if(
AccountIsMicro==false//normal account
      
{
         if (
lotMM 0.1lotMM Lots;
         if ((
lotMM 0.5) && (lotMM 1)) lotMM=0.5;
         if (
lotMM 1.0lotMM MathCeil(lotMM);
         if  (
lotMM 100lotMM 100;
      }
      else 
//micro account
      
{
         if (
lotMM 0.01lotMM Lots;
         if (
lotMM 1.0lotMM MathCeil(lotMM);
         if  (
lotMM 100lotMM 100;
      }
      
      return (
lotMM);
}

string BoolToStr bool value)
{
   if(
value) return ("True");
   else return (
"False");
}
void Print_Details()
{
   
string sComment "";
   
string sp "----------------------------------------\n";
   
string NL "\n";

   
sComment sp;
   
sComment sComment "TakeProfit=" DoubleToStr(TakeProfit,0) + " | ";
   
sComment sComment "TrailingStop=" DoubleToStr(TrailingStop,0) + " | ";
   
sComment sComment "StopLoss=" DoubleToStr(StopLoss,0) + " | "
   
sComment sComment "UseStopLoss=" BoolToStr(UseStopLoss) + NL;
   
sComment sComment sp;
   
sComment sComment "immediate_trade=" BoolToStr(immediate_trade) + " | ";
   
sComment sComment "reversal=" BoolToStr(reversal) + NL;
   
sComment sComment sp;
   
sComment sComment "Lots=" DoubleToStr(Lots,0) + " | ";
   
sComment sComment "MM=" BoolToStr(MM) + " | ";
   
sComment sComment "Risk=" DoubleToStr(Risk,0) + "%" NL;
   
sComment sComment sp;
  
   
Comment(sComment);
}

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   
int limit;
   
int counted_bars=IndicatorCounted();
   if(
counted_bars<0) return(-1);
   if(
counted_bars>0counted_bars--;
   
limit=Bars-counted_bars;
   
   
double avg;
   
   for(
int x=0x<limitx++) {
      
Xdown[x] = 0Xup[x] = 0;
      
middle1[x] = iMA(NULL0period0MODE_EMAPRICE_TYPICALx);// drawn line
      
middle2iMA(NULL0period0MODE_SMAPRICE_TYPICALx);// only used to calculate outer bands

      
avg  findAvg(periodx);
      
upper[x] = middle2 + (3.5*avg);
      
lower[x] = middle2 - (3.5*avg);
      
      if (
MathAbs(upper[x] - High[x]) < 2*Point)
      {
         
Xdown[x] = upper[x];
         if (
NewBar() && == 0)
            
Alert(Symbol()," ",Period()," reach upper edge");
      }   
      if (
MathAbs(lower[x] - Low[x]) < 2*Point)
      {
         
Xup[x] = lower[x];
         if (
NewBar() && == 0)
            
Alert(Symbol()," ",Period()," reach lower edge");
      }
   }
   return(
0);
  }
//+------------------------------------------------------------------+


   
double findAvg(int periodint shift) {
      
double sum=0;
      for (
int x=shift;x<(shift+period);x++) {     
         
sum += High[x]-Low[x];
      }
      
sum sum/period;
      return (
sum);
   }
   
bool NewBar()
{
   static 
datetime dt 0;
   if (
dt != Time[0])
   {
      
dt Time[0];
      return(
true);
   }
   return(
false);

//end of trend bands indicator 



{
 if(
Hour() >= StartHour && Hour() <= StopHour){  //Nothing will happen unless the time is within the trading time
   
int cnttickettotal;
   
double SEmaLEmaSEmaLASTLEmaLAST;
   
   
string comment "";
   if(
reversal==true)  comment "EMA_CROSS_Counter-Trend";
   if(
reversal==falsecomment "EMA_CROSS_Trend-Following";

   if(
Bars<100)
     {
      Print(
"bars less than 100");
      return(
0);  
     }
   if(
TakeProfit<1)
     {
      Print(
"TakeProfit less than 1");
      return(
0);  // check TakeProfit
     
}
     
   static 
int isCrossed 0;

   if(
ConfirmedOnEntry)
   {
      if(
CheckValueTime==iTime(NULL,TimeFrame,0)) return(0); else CheckValueTime iTime(NULL,TimeFrame,0);

      
SEma     iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,1);
      
LEma     iMA(NULL,0,LongEma ,0,MODE_EMA,PRICE_CLOSE,1);
      
SEmaLAST iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,2);
      
LEmaLAST iMA(NULL,0,LongEma ,0,MODE_EMA,PRICE_CLOSE,2);
      
      if(
SEmaLAST<LEmaLAST && SEma>LEmaisCrossed 1;
      if(
SEmaLAST>LEmaLAST && SEma<LEmaisCrossed 2;
   }
   else
   {
      
SEma     iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,0);
      
LEma     iMA(NULL,0,LongEma ,0,MODE_EMA,PRICE_CLOSE,0);

      
isCrossed Crossed (LEma,SEma);
   }
   
   if(
reversal==false)
   {
           if(
isCrossed==1isCrossed 2;
      else if(
isCrossed==2isCrossed 1;
   }
   
   if(
MM==trueLots LotSize(); //Adjust the lot size
  
   
total  OrdersTotal();

// TRAILING STOP 
no errors so far from the compiler..

Last edited by Aaragorn; 06-29-2006 at 09:04 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #22 (permalink)  
Old 06-29-2006, 09:35 PM
Aaragorn's Avatar
Senior Member
 
Join Date: Jun 2006
Location: USA
Posts: 801
Aaragorn is on a distinguished road
ok somewhere down here I think I have to make the logic conditions that use the indicator to filter...

PHP Code:
// ENTRY
  
    
if(total || isNewSumbol(Symbol())) //I have modified the if condition too: it was total<1 (orBanAway aka cucurucu)
     
{
       
double HedgeLots = (HedgePercent/100)*Lots//calculates the Lots for the hedged position
       
if(isCrossed == 1)
         {
            if(
UseStopLoss)
               
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,comment,MAGICMA,0,Green);
            else
               
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,comment,MAGICMA,0,Green);
            
            if(
ticket>0)
              {
               if(
OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
              }
            else Print(
"Error opening BUY order : ",GetLastError()); 
//######################################################################   the added code starts here         
            
if(UseStopLoss)
               
ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,comment,MAGICMA,0,Red);
            else
               
ticket=OrderSend(Symbol(),OP_SELL,HedgeLots,Bid,3,0,Bid-TakeProfit*Point,comment,MAGICMA,0,Red);
            
            if(
ticket>0)
              {
               if(
OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
              }
            else Print(
"Error opening SELL order : ",GetLastError());             
//######################################################################   ends here          
            
return(0);
         }
         if(
isCrossed == 2)
         {
            if(
UseStopLoss)
               
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,comment,MAGICMA,0,Red);
            else
               
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,comment,MAGICMA,0,Red);
            
            if(
ticket>0)
              {
               if(
OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
              }
            else Print(
"Error opening SELL order : ",GetLastError()); 
//###################################################################### the added code starts here  
            
if(UseStopLoss)
               
ticket=OrderSend(Symbol(),OP_BUY,HedgeLots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,comment,MAGICMA,0,Green);
            else
               
ticket=OrderSend(Symbol(),OP_BUY,HedgeLots,Ask,3,0,Ask+TakeProfit*Point,comment,MAGICMA,0,Green);
            
            if(
ticket>0)
              {
               if(
OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
              }
            else Print(
"Error opening BUY order : ",GetLastError()); 
//######################################################################  ends here   
            
return(0);
         }
         return(
0);
      }
     return(
0);
     }
     
   return(
0);
  }
  }
//+------------------------------------------------------------------+ 
I'm thinking to make this...
if(isCrossed == 1 && point < **the top line variable from the custom indicator**- 25)
if(isCrossed == 2 && point > **the bottom line variable from the custom indicator** +20)

would that work, would that make the indicator filter the orders?

no that's not right that doesn't separate the buys from the sells because it triggers both a sell and a buy on a buy signal and on a sell signal ...oy

but which variable from the indicator? I'm still not certain which to use?

if(isCrossed == 1 && Point < topindicatorlinevalue - longrange)
if(isCrossed == 2 && Point < bottomindicatorlinevalue + shortrange)

I have trouble getting the variable out of the indicator. I tried making a global variable to do it but that didn't work for me, I don't know why.

I put this in the top part...

//---- Filter Parameters
extern double longrange = 25;
extern double shortrange = 20;

the compiler was ok with that....

I put this in the top part too..

double t[];
double b[];


then i tried to put the t and b where the indicator would give them it's values....right with the others...

avg = findAvg(period, x);
upper[x] = middle2 + (3.5*avg);
lower[x] = middle2 - (3.5*avg);
t[x] = middle2 + (3.5*avg);
b[x] = middle2 - (3.5*avg);

the compiler was still ok with all this...

then i tried to do this where it orders buys...

if(isCrossed == 1 && Point < t - longrange)

it didn't work and I got this from the compiler...

Compiling 'EMA_CROSSmodv4.mq4'...
'-' - left square parenthesis expected for array C:\Program Files\Interbank FX Trader 4-live mini\experts\EMA_CROSSmodv4.mq4 (350, 38)
'-' - unexpected token C:\Program Files\Interbank FX Trader 4-live mini\experts\EMA_CROSSmodv4.mq4 (350, 38)
')' - assignment expected C:\Program Files\Interbank FX Trader 4-live mini\experts\EMA_CROSSmodv4.mq4 (350, 48)
3 error(s), 0 warning(s)

Last edited by Aaragorn; 06-30-2006 at 01:00 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #23 (permalink)  
Old 06-30-2006, 08:54 AM
Administrator
 
Join Date: Sep 2005
Posts: 15,874
Blog Entries: 54
newdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud of
Quote:
Originally Posted by Aaragorn
http://www.forex-tsd.com/35141-post14.html

I like the trend bands...but which variable do I use to embed it in the EA?
If you are writing icustom in EA for trend bands indicator so you will have the following lines (see image).

So in icustom you may have the following:

Code:
double dtb_high=iCustom(NULL,0,"Trend Bands v2",period,0,1);
where:

- NULL - EA will work for any currency (otherwise place simbol instead of NULL);

- 0 - EA will work with the chart for any timeframe you attached to (otherwise place PERIOD_M30 for example or any);

- "Trend Bands v2" - exact name of custom indicator;

- period - period in indicator. This indicator is having some settings. In our case it is one only - period. It is 34 by default. You should place the folowing in the beginning of the EA:
Code:
extern int period = 34;
Or place 34 in icustom instead of period and you do not need to place this additional line in this case (extern int period = 34;);

- 0 - it is line 0 (see image).

- 1 - is first bar. Previous bar starting from current one.

If I am wrong so somebody may correct me.
Attached Images
File Type: gif trendbands.gif (26.9 KB, 66 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #24 (permalink)  
Old 06-30-2006, 09:23 AM
Administrator
 
Join Date: Sep 2005
Posts: 15,874
Blog Entries: 54
newdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud ofnewdigital has much to be proud of
As I saw Kalenzo did it in different way.
I mean this one:

Quote:
- 1 - is first bar. Previous bar starting from current one.
He did as following:

Code:
{
   int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) counted_bars=0;
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
And after that:

Code:
double dtb_high1=iCustom(NULL,0,"Trend Bands v2",period,0,i);
double dtb_high2=iCustom(NULL,0,"Trend Bands v2",period,0,i+1);
It is for line 0.

For line 2 (blue one) it may be the following:

Code:
double dtb_low1=iCustom(NULL,0,"Trend Bands v2",period,2,i);
double dtb_low2=iCustom(NULL,0,"Trend Bands v2",period,2,i+1);
So the price should be above or below some lines (double dtb_low1, dtb_low2, double dtb_high1 and double dtb_high2). And there is middle line as well (the same but with 1). It may be double dtb_middle1 and double dtb_middle2 for example.
Digg this Post!Add Post to del.icio.us