Stochastics & RSI Values not seen by code

 

I am trying to make this EA only enter Trades when Stoch & RSI are above 80 and 70 for Short. Less than 20 and 30 for Long.

This is just a bool ad-on to the rest of the signal code.

I have submitted the entire code and a screen shot of a recent Long Signal.

You will see that the Stoch and RSI are both far above the Oversold zone.

For the EURUSD trading, a 4hr. timeframe works best.

Thanks,

JimTrader1

 


//+------------------------------------------------------------------+
//| DAveFU_Auto3ADXCrossover.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#define MAGICMA 20050610
extern double TakeProfit = 36;
extern double Lots = 0.1;
extern double MaximumRisk = 0.02;
extern double DecreaseFactor = 3;
extern int MaximumLots = 2;
extern int MaximumNumberofTrades = 2;
extern bool OpenNewTrades = False;
int OpenTrades = 0;
extern double MovingPeriod = 14;
extern double MovingShift = 5;
extern double MovingPeriodS = 21;
extern double MovingShiftS = 3;
extern double F = 13;
extern double F2 = 21;
extern double F3 = 50;
extern bool Condition1 = False;
extern bool Condition2 = False;
extern bool Condition3 = False;
extern bool Condition4 = False;


//+------------------------------------------------------------------+
//| 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()==MAGICMA)
{
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()
{
double osma;
double osmaf;
double ma;
double maa;
double xa;
double xb;
double xc;
double xsp;
double xxa;
double xxb;
double xxc;
double xxsp;
double atr;
double atr2;
double cci;
double cci1;
double sto;
double rsi;
double valx;
double valx1;
double val;
double val2;
double val3;
double val1;
double val21;
double val31;
bool is_siesta=false;
int res;
//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get Moving Average
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_WEIGHTED,5);
maa=iMA(NULL,0,MovingPeriodS,MovingShiftS,MODE_SMA,PRICE_CLOSE,5);
double x=iADX(NULL,0,7,PRICE_CLOSE,MODE_MAIN,0);
double x2=iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,0);
double x3=iADX(NULL,0,21,PRICE_CLOSE,MODE_MAIN,0);
double x1=iADX(NULL,0,7,PRICE_CLOSE,MODE_MAIN,1);
double x21=iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1);
double x31=iADX(NULL,0,21,PRICE_CLOSE,MODE_MAIN,1);
double x11=iADX(NULL,0,7,PRICE_CLOSE,MODE_MAIN,2);
double x22=iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,2);
double x33=iADX(NULL,0,21,PRICE_CLOSE,MODE_MAIN,2);
double xx=iADX(NULL,PERIOD_D1,7,PRICE_CLOSE,MODE_MAIN,0);
double xx2=iADX(NULL,PERIOD_D1,14,PRICE_CLOSE,MODE_MAIN,0);
double xx3=iADX(NULL,PERIOD_D1,21,PRICE_CLOSE,MODE_MAIN,0);
double xx1=iADX(NULL,PERIOD_D1,7,PRICE_CLOSE,MODE_MAIN,1);
double xx21=iADX(NULL,PERIOD_D1,14,PRICE_CLOSE,MODE_MAIN,1);
double xx31=iADX(NULL,PERIOD_D1,21,PRICE_CLOSE,MODE_MAIN,1);
double xx11=iADX(NULL,PERIOD_D1,7,PRICE_CLOSE,MODE_MAIN,2);
double xx22=iADX(NULL,PERIOD_D1,14,PRICE_CLOSE,MODE_MAIN,2);
double xx33=iADX(NULL,PERIOD_D1,21,PRICE_CLOSE,MODE_MAIN,2);
sto=iStochastic(NULL,0,5,3,3,MODE_SMA,0,MODE_MAIN,0);
rsi=iRSI(NULL,0,4,PRICE_CLOSE,0);
cci=iCCI(NULL,0,14,PRICE_TYPICAL,0);
cci1=iCCI(NULL,0,21,PRICE_TYPICAL,1);
osma=iOsMA(NULL,0,12,26,9,PRICE_OPEN,1);
osmaf=iOsMA(NULL,0,10,21,7,PRICE_WEIGHTED,0);
val=iForce(NULL,0,F,MODE_SMA,PRICE_CLOSE,0);
val2=iForce(NULL,0,F2,MODE_SMA,PRICE_CLOSE,0);
val3=iForce(NULL,0,F3,MODE_SMA,PRICE_CLOSE,0);
val1=iForce(NULL,0,F,MODE_SMA,PRICE_CLOSE,1);
val21=iForce(NULL,0,F2,MODE_SMA,PRICE_CLOSE,1);
val31=iForce(NULL,0,F3,MODE_SMA,PRICE_CLOSE,1);
xa=(x+x2+x3);
xb=(x1+x21+x31);
xc=(x11+x22+x33);
xsp=(xb+xc)/2;
xxa=(xx+xx2+xx3);
xxb=(xx1+xx21+xx31);
xxc=(xx11+xx22+xx33);
xxsp=(xxb+xxc)/2;
atr=iATR(NULL,0,5,0);
atr2=iATR(NULL,0,14,1);
//---- sell conditions
if(sto>80)Condition1=True;
if(rsi>70)Condition2=True;
if(MaximumNumberofTrades>1)OpenNewTrades=False;
if((xxa>xxsp && xa>xsp && xxa>=-75) && (val2<val31) && (cci<cci1 && cci1>0) && (Condition1==True && Condition2==True))
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
return;
}
//---- buy conditions
if(sto<20)Condition3=True;
if(rsi<30)Condition4=True;
if((xxa>xxsp && xa>xsp && xxa>=+100) && (val2>val31) && (osmaf>osma) && (ma>maa) && (cci>cci1 && cci1<0) && (Condition3==True && Condition4==True))
if(Hour()>=12 || Hour()<17) is_siesta=false;
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
double valx;
double valx1;
double val;
double val2;
double val3;
double val1;
double val21;
double val31;
//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//---- get Moving Average
val=iForce(NULL,0,F,MODE_SMA,PRICE_CLOSE,0);
val2=iForce(NULL,0,F2,MODE_SMA,PRICE_CLOSE,0);
val3=iForce(NULL,0,F3,MODE_SMA,PRICE_CLOSE,0);
val1=iForce(NULL,0,F,MODE_SMA,PRICE_CLOSE,1);
val21=iForce(NULL,0,F2,MODE_SMA,PRICE_CLOSE,1);
val31=iForce(NULL,0,F3,MODE_SMA,PRICE_CLOSE,1);
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY)
{
if(val2<val31) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
break;
}
if(OrderType()==OP_SELL)
{
if(val2>val31) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
break;
}
}
//----
}
//+------------------------------------------------------------------+
//| 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();
//----
}
//+------------------------------------------------------------------+
 
Hi JimTrader1

That's a fairly complicated EA with lots of indicators...

With all the different indicator values in the Buy/Sell logic, it's Hard to figure out what's causing the Buy above the Stoch 80 line...

One problem may be that the bool's set as True don't get reset to False for the next cycle...so the condition always stays True...

If you want only Stoch and RSI trades, you need to blank out all the rest of the indicators - use // to blank the lines and replace the logic with the Stoch/RSI values.

You can also eliminate the bool's entirely so you know they are not the problems - just combine the Stoch/RSI values into the same statement:

Then just use the Stoch and RSI values you want for the Buy and Sell trades...

// Sell
if(sto>80 && rsi>70)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,3,0,0,"",MAGICMA,0,Red);
return;
}

// Buy
if(sto<20 && rsi<30)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,3,0,0,"",MAGICMA,0,Blue);
return;
}

Also FWIW...the TakeProfit function does not seem to work in that EA.

Hope this helps. Have fun!

Robert

 
Reason: