Fractal Break Out-Stuck

 

Hey guys,


I'm trying to make a fractal breakout EA, that is filtered with time of day and a moving average.


Can anyone point me in the right direction/overview any defects in the code???


1: It is only taking buys after I input the iFractal()

2: It does not enter where the Fractals are

//+------------------------------------------------------------------+
//|                                                    Simple EA.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//Literally copying Ferrus Format to a tee

extern string Label1="===General Trade Settings===";
extern int   TakeProfit=25;
extern int    StopLoss=10;
extern int    TrailingStop=0;
extern int    Slippage=2;
extern double  Lots=0.1;
//---------Time Filter--------//

//-----------Money Managerment---------//
extern bool Money.Management=true ;
extern double Risk=1;


//----------------------Moving Average-----//
extern string Label5="===Moving Average Settings===";
extern int    MA_Period=200;
extern int    MA_Shift=0;
extern int    MA_Type=1;
extern int    MA_Price=0;
//----------


//-------Initialize EA Orders Accounting-----//


int start()
{

int mypoint;

if (Digits==3||Digits==5){ mypoint=10;}
else {mypoint=1;}


//------------Orders Accounting---------//

 int total = OrdersTotal();
if(total<1)
{
//------------Money Management----------//

//Money Management sequence
 if (Money.Management)
   {
      if (Risk<1 || Risk>1000)
      {
         Comment("Invalid Risk Value.");
         return(0);
      }
      else
      {
         Lots=MathFloor((AccountFreeMargin()*AccountLeverage()*Risk*Point*mypoint*100)/(Ask*MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo(Symbol(),MODE_MINLOT)))*MarketInfo(Symbol(),MODE_MINLOT);
      }
   }

//-------------EMA SETTINGS-----------------//
//
//------------------------------------------//
double EMA=iMA(NULL,0,MA_Period,MA_Shift,MA_Type,MA_Price,0);
double BarCloseB;
double BarCloseS;
BarCloseB=Bid;
BarCloseS=Ask;
//----Processing a buy---------//
//


double fractalU=iFractals(NULL,0,1,0);
double fractalD=iFractals(NULL,0,2,0);



//-----------------------------//
if((BarCloseB>EMA) && (BarCloseB>=fractalU))
{
double SLB=Bid-StopLoss*Point*mypoint;
double TPB=Bid+TakeProfit*Point*mypoint;
int buy= OrderSend(Symbol(),0,Lots,Ask,Slippage,0,0);
}

if(buy>0) 
{
OrderSelect(buy,SELECT_BY_TICKET,MODE_TRADES);
OrderModify(buy,OrderOpenPrice(),SLB,TPB,0,Green);
}
//---------Processing a sell---------//
//
//-----------------------------------//
if((BarCloseS<EMA)&&(BarCloseS<=fractalD))
{
double SLS=Ask+StopLoss*Point*mypoint;
double TPS=Ask-TakeProfit*Point*mypoint;

int sell= OrderSend(Symbol(),1,Lots,Bid,Slippage,0,0);
}

if (sell>0)
{
OrderSelect(sell,SELECT_BY_TICKET,MODE_TRADES);
OrderModify(sell,OrderOpenPrice(),SLS,TPB,0,Green);
}

Print(GetLastError());
return(0);
}
}
 
fractal  

 Break Out  ??

double fractalU=iFractals(NULL,0,1,0);     //What if you had chosen for bar 1  ??
double fractalD=iFractals(NULL,0,2,0);             //then i think you will get sell trades also

 count your trades before open new one....

and more errors but try out this first 


 
deVries:
fractal  

 Break Out  ??

 count your trades before open new one....

and more errors but try out this first 



Hey deVries,


I am having a hard time understanding that statement. My two takeaways were:


1)You want me to choose the first bar?

2) Re-do my OrdersAccounting


Thank you again for your support.

 
ZacharyRC:

Hey deVries,


I am having a hard time understanding that statement. My two takeaways were:


1)You want me to choose the first bar?

2) Re-do my OrdersAccounting


Thank you again for your support.


double fractalU=iFractals(NULL,0,1,0);

returns see my next post  .....

double fractalU=iFractals(NULL,0,1,1);

returns see my next post...... 

 

 your ordercounting like

 int total = OrdersTotal();
if(total<1)
{
 

 will give situations no order get open if you have another trade not from this EA  open or pending

with backtesting you won't see that but running the EA on an account you will notice 

 

Did a little test on your frctal code 

like this

   double fractalU;
   for(int y=0;fractalU < Point;y++)
     {
      fractalU=iFractals(NULL,0,1,y);
      Alert("fractalUp  y =  "+y+ " "+fractalU);
     }

 Do the same and you will see what bar you have to choose for getting right fractalbar

 
deVries:

Did a little test on your frctal code 

like this

 Do the same and you will see what bar you have to choose for getting right fractalbar


Hi deVries!



You sure are a helpful guy!

 
deVries:

Did a little test on your frctal code 

like this

 Do the same and you will see what bar you have to choose for getting right fractalbar


Hmmm...


deVries, the check worked perfectly and alerted the correct price for each "down" fractal and "up" fractal.


I am still investigating the code for errors, because it is "ignoring" the fractals when processing the order.


You are correct, I need to change the OrdersAccounting sections, because as soon as I put the EA on the chart, it takes a trade.

 
deVries:

Did a little test on your frctal code 

like this

 Do the same and you will see what bar you have to choose for getting right fractalbar


SILLY ME!


After researching the code, I was using => instead of ==, which was causing the buffer to cause issues.


Thank you deVries!!!!

 
ZacharyRC:

SILLY ME!


After researching the code, I was using => instead of ==, which was causing the buffer to cause issues.


Thank you deVries!!!!

You mean......
if((BarCloseB>EMA) && (BarCloseB>=fractalU))
changed to
if((BarCloseB>EMA) && (BarCloseB==fractalU))

this way now ????  that will also be not correct

Something like this  

if((BarCloseB>EMA) && (BarCloseB>fractalU)&& fractalU>Point)

 might work for you  and get fractalU  the way you see its done in the test...

 
deVries:
You mean......
changed to

this way now ????  that will also be not correct

Something like this  

 might work for you  and get fractalU  the way you see its done in the test...


I have not implemented your input because it is trading correctly at this time. I will try it later tonight with your input.



Thank you deVries!

 

Hey deVries:


I am running into issues in my final code, with trying to put a modify stop loss to breakeven after so many pips.


Do you see any glaring error???

extern int    StopLoss=10;

//-------------------------------------------------//
extern bool Move.BE=true;
extern int  MoveStopTo=1;



//----------------------------------------------//
//-----------------EXITING ORDERS---------------//
if(OrdersTotal()>1)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==(OP_BUY)&&(Move.BE))
{
if(Bid - OrderOpenPrice() >= Point * StopLoss)
 {
    if(OrderStopLoss() < OrderOpenPrice() + Point * MoveStopTo)
     {
     OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + Point * MoveStopTo, OrderTakeProfit(), 0, Red);
}
}
}
}

if(OrdersTotal()>1)
{
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
{
 if(OrderType()==(OP_SELL)&&(Move.BE))
 {     
          
if(OrderOpenPrice() - Ask >= Point * StopLoss) 
 {
    if(OrderStopLoss() > OrderOpenPrice() - Point * MoveStopTo) 
     {
      OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() - Point * MoveStopTo, OrderTakeProfit(), 0, Red);
}
}
}
}
//--------CHECKING FOR ERRORS-------------------//
Print(GetLastError());
return(0);
}
}