Fibonacci levels

 

I have an EA that I want to expand with some trading rules. Right now it enters a trade whenever an indicator tells it to do so. However I want it not to trade when it is near a fibonacci resistance or support level. For instance, it could enter a trade long just when it is at a resistance or short just a few pips of a support, that would then likely be a losing trade..

Is there some who has tried this before?


Also, I like to use the fibo levels as stop loss levels.

I tried to code something but it didn't work.


Thanks,


Ilovepippin.

 
Ilovepippin:

I have an EA that I want to expand with some trading rules. Right now it enters a trade whenever an indicator tells it to do so. However I want it not to trade when it is near a fibonacci resistance or support level. For instance, it could enter a trade long just when it is at a resistance or short just a few pips of a support, that would then likely be a losing trade..

Is there some who has tried this before?


Also, I like to use the fibo levels as stop loss levels.

I tried to code something but it didn't work.


Thanks,


Ilovepippin.


> Also, I like to use the fibo levels as stop loss levels.

> I tried to code something but it didn't work.


Post the code that dont work & we'll try & fix it :)


-BB-

 
BarrowBoy:
Ilovepippin:

I have an EA that I want to expand with some trading rules. Right now it enters a trade whenever an indicator tells it to do so. However I want it not to trade when it is near a fibonacci resistance or support level. For instance, it could enter a trade long just when it is at a resistance or short just a few pips of a support, that would then likely be a losing trade..

Is there some who has tried this before?


Also, I like to use the fibo levels as stop loss levels.

I tried to code something but it didn't work.


Thanks,


Ilovepippin.


> Also, I like to use the fibo levels as stop loss levels.

> I tried to code something but it didn't work.


Post the code that dont work & we'll try & fix it :)


-BB-

Okay, be carefull what you wish for :-)


I assembled this from several EA's posted here.

The following code is a learning project and not (by any means) a holy grail..

Now, when I run it, it doesn't open a trade. I must assume it has to do with the fibo adjustments I made.

Also I started some code to add Money Management.

I have a feeling that some "return" values are not returned, hopefully you can tell me exactly what it is.


Thanks.

<---------------------------------------------------------------------->


//----
extern double Lots=0.1;
//extern int StopLoss=0;
extern int TrailingStop=0;
extern int TakeProfit=80;
//extern double MaximumRisk=0.02;
extern int mafastperiod=3;
extern int mafastshift=0;
extern int mafastmethod =MODE_SMA;
extern int mafastprice=PRICE_CLOSE;
extern int maslowperiod=5;
extern int maslowshift=0;
extern int maslowmethod =MODE_SMA;
extern int maslowprice=PRICE_OPEN;
//----
datetime TimePrev=0;
//+------------------------------------------------------------------+
//| Set fibonacci resistance levels |
//+------------------------------------------------------------------+

int init()
{
return(0);
}
int deinit()
{
ObjectDelete("S1");
ObjectDelete("R1");
ObjectDelete("Pivot");
ObjectDelete("Support 1");
ObjectDelete("Pivot level");
ObjectDelete("Resistance 1");
Comment(" ");
return(0);
}
int start()
{
double rates[1][6],yesterday_close,yesterday_high,yesterday_low;
ArrayCopyRates(rates, Symbol(), PERIOD_D1);
if(DayOfWeek() == 1)
{ if(TimeDayOfWeek(iTime(Symbol(),PERIOD_D1,1)) == 5)
{ yesterday_close = rates[1][4];
yesterday_high = rates[1][3];
yesterday_low = rates[1][2];
}
else
{ for(int d = 5;d>=0;d--)
{ if(TimeDayOfWeek(iTime(Symbol(),PERIOD_D1,d)) == 5)
{yesterday_close = rates[d][4];
yesterday_high = rates[d][3];
yesterday_low = rates[d][2];}
}
}
}
else
{
yesterday_close = rates[1][4];
yesterday_high = rates[1][3];
yesterday_low = rates[1][2];
}

//+------------------------------------------------------------------+
//| 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);
// }


{
//Trailing Stop
TrailingAlls(TrailingStop);
//close/Open
if (TimePrev==Time[0]) return(0);
TimePrev=Time[0];
//Calculate indicators
double fast1=iMA(NULL,0,mafastperiod,mafastshift,mafastmethod,mafastprice,1);
double fast2=iMA(NULL,0,mafastperiod,mafastshift,mafastmethod,mafastprice,2);
double slow1=iMA(NULL,0,maslowperiod,maslowshift,maslowmethod,maslowprice,1);
double slow2=iMA(NULL,0,maslowperiod,maslowshift,maslowmethod,maslowprice,2);
double SARP1= iSAR(NULL,0,0.02,0.2,1);
double SARP2 =iSAR(NULL,0,0.02,0.2,2);
double R = yesterday_high - yesterday_low;//range
double p = (yesterday_high + yesterday_low + yesterday_close)/3;// Standard Pivot
double r1 = p + (R * 0.236);
double s1 = p - (R * 0.236);
double sll = ((Ask - s1 * Point )+3);
double sls = ((r1 - Ask * Point )+3);

//Going LONG
//Note calculate stoploss by current support s1 - price + 3 pips
// double SL=Ask-s1*point;
if ((fast1>slow1&&fast2<slow2)&& (SARP1 > SARP2))
{
closeshrts();
OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Stoplong(Ask,sll),Takelong(Ask,TakeProfit),NULL,0,0,Blue);
}

//Going short
//Note calculate stoploss by resistance r1 - price + 3 pips
if ((fast1<slow1&&fast2>slow2)&& (SARP1 < SARP2))
{
closelongs();
OrderSend(Symbol(),OP_SELL,Lots,Bid,0,Stopshrt(Bid,sls),Takeshrt(Bid,TakeProfit),NULL,0,0,Red);
}

return(0);
}
}

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Stoplong(double price,int stop)
{
if(stop==0)
return(0.0);
return(price-(stop*Point));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Stopshrt(double price,int stop)
{
if (stop==0)
return(0.0);
return(price+(stop*Point));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Takelong(double price,int Take)
{
if (Take==0)
return(0.0);
return(price+(Take*Point));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Takeshrt(double price,int Take)
{
if (Take==0)
return(0.0);
return(price-(Take*Point));
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void closelongs()
{
int trade;
int trades=OrdersTotal();
for(trade=0;trade<trades;trade++)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol())
continue;
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,0,Blue);
}//for
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void closeshrts()
{
int trade;
int trades=OrdersTotal();
for(trade=0;trade<trades;trade++)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol())
continue;
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,0,Red);
}//for
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void TrailingAlls(int trail)
{
if(trail==0)
return;
//----
double stopcrnt;
double stopcal;
int trade;
int trades=OrdersTotal();
for(trade=0;trade<trades;trade++)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol())
//continue;
//LONG
if(OrderType()==OP_BUY)
{
stopcrnt=OrderStopLoss();
stopcal=Bid-(trail*Point);
if (stopcrnt==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
}
else
if(stopcal>stopcrnt)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
}
}
}//LONG
//Shrt
if(OrderType()==OP_SELL)
{
stopcrnt=OrderStopLoss();
stopcal=Ask+(trail*Point);
if (stopcrnt==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
}
else
if(stopcal<stopcrnt)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
}
}
}//Shrt
//----
return(0);
//+------------------------------------------------------------------+

Reason: