reversing EA is unwantingly switching positions at stops

 

greetings everyone, 

im trying to modify this basic EA that a lot of people use as a foundation


for simplicity sake i will use the original EA as an example

it buys under certain rsi condition,closes and reverses direction to short on the opposite condition....switching direction back and forth

it also has takeprofit and  stoploss which should exit positions completely

with backtesting, hitting the stoploss closes my open position and does not open a reversal<<<<this is what i want


however, when i test this live on my forex.com account, the stoplosses are closing my buys and opening sells thus unwantingly reversing position, why is this happening...i do not want this

the brackets are only there for risk management, not stratgey reversal


is this written in the code to do this? im trying to remove some functions but i cant figure how to stop this...




//+------------------------------------------------------------------+
//|                                                       RSI EA.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, dXerof"

#property version   "1.00" #property strict input bool   OpenBUY=True; input bool   OpenSELL=True; input bool   CloseBySignal=True; input double StopLoss=0; input double TakeProfit=0; input double TrailingStop=0; input int    RSIperiod=14; input double BuyLevel=30; input double SellLevel=70; input bool   AutoLot=True; input double Risk=10; input double ManualLots=0.1; input int    MagicNumber=123; input string Koment="RSIea"; input int    Slippage=10; //--- int OrderBuy,OrderSell; int ticket; int LotDigits; double Trail,iTrailingStop; //+------------------------------------------------------------------+ //| Expert initialization function                                   | //+------------------------------------------------------------------+ int init()   {    return(0);   } //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ int deinit()   {    return(0);   } //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ int start()   {    double stoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL);    OrderBuy=0;    OrderSell=0;    for(int cnt=0; cnt<OrdersTotal(); cnt++)      {       if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))          if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderComment()==Koment)            {             if(OrderType()==OP_BUY) OrderBuy++;             if(OrderType()==OP_SELL) OrderSell++;             if(TrailingStop>0)               {                iTrailingStop=TrailingStop;                if(TrailingStop<stoplevel) iTrailingStop=stoplevel;                Trail=iTrailingStop*Point;                double tsbuy=NormalizeDouble(Bid-Trail,Digits);                double tssell=NormalizeDouble(Ask+Trail,Digits);                if(OrderType()==OP_BUY && Bid-OrderOpenPrice()>Trail && Bid-OrderStopLoss()>Trail)                  {                   ticket=OrderModify(OrderTicket(),OrderOpenPrice(),tsbuy,OrderTakeProfit(),0,Blue);                  }                if(OrderType()==OP_SELL && OrderOpenPrice()-Ask>Trail && (OrderStopLoss()-Ask>Trail || OrderStopLoss()==0))                  {                   ticket=OrderModify(OrderTicket(),OrderOpenPrice(),tssell,OrderTakeProfit(),0,Blue);                  }               }            }      }    double rsi=iRSI(Symbol(),0,RSIperiod,PRICE_CLOSE,0);    double rsi1=iRSI(Symbol(),0,RSIperiod,PRICE_CLOSE,1); // double HTb=iCustom(Symbol(),0,"HalfTrend-1.02",0,0); //buy // double HTs=iCustom(Symbol(),0,"HalfTrend-1.02",1,0); //buy //--- open position    if(OpenSELL && OrderSell<1 && rsi<SellLevel && rsi1>SellLevel) OPSELL();    if(OpenBUY && OrderBuy<1 && rsi>BuyLevel && rsi1<BuyLevel) OPBUY(); //--- close position by signal    if(CloseBySignal)      {       if(OrderBuy>0 && rsi<SellLevel && rsi1>SellLevel) CloseBuy();       if(OrderSell>0 && rsi>BuyLevel && rsi1<BuyLevel) CloseSell();      } //---    return(0);   } //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ void OPBUY()   {    double StopLossLevel;    double TakeProfitLevel;    if(StopLoss>0) StopLossLevel=Bid-StopLoss*Point; else StopLossLevel=0.0;    if(TakeProfit>0) TakeProfitLevel=Ask+TakeProfit*Point; else TakeProfitLevel=0.0;    ticket=OrderSend(Symbol(),OP_BUY,LOT(),Ask,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DodgerBlue);   } //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ void OPSELL()   {    double StopLossLevel;    double TakeProfitLevel;    if(StopLoss>0) StopLossLevel=Ask+StopLoss*Point; else StopLossLevel=0.0;    if(TakeProfit>0) TakeProfitLevel=Bid-TakeProfit*Point; else TakeProfitLevel=0.0; //---    ticket=OrderSend(Symbol(),OP_SELL,LOT(),Bid,Slippage,StopLossLevel,TakeProfitLevel,Koment,MagicNumber,0,DeepPink);   } //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ void CloseSell()   {    int  total=OrdersTotal();    for(int y=OrdersTotal()-1; y>=0; y--)      {       if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))          if(OrderSymbol()==Symbol() && OrderType()==OP_SELL && OrderMagicNumber()==MagicNumber)            {             ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);            }      }   } //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ void CloseBuy()   {    int  total=OrdersTotal();    for(int y=OrdersTotal()-1; y>=0; y--)      {       if(OrderSelect(y,SELECT_BY_POS,MODE_TRADES))          if(OrderSymbol()==Symbol() && OrderType()==OP_BUY && OrderMagicNumber()==MagicNumber)            {             ticket=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Black);            }      }   } //+------------------------------------------------------------------+ //|                                                                  | //+------------------------------------------------------------------+ double LOT()   {    double lotsi;    double ilot_max =MarketInfo(Symbol(),MODE_MAXLOT);    double ilot_min =MarketInfo(Symbol(),MODE_MINLOT);    double tick=MarketInfo(Symbol(),MODE_TICKVALUE); //---    double  myAccount=AccountBalance(); //---    if(ilot_min==0.01) LotDigits=2;    if(ilot_min==0.1) LotDigits=1;    if(ilot_min==1) LotDigits=0; //---    if(AutoLot)      {       lotsi=NormalizeDouble((myAccount*Risk)/10000,LotDigits);         } else { lotsi=ManualLots;      } //---    if(lotsi>=ilot_max) { lotsi=ilot_max; } //---    return(lotsi);   } //+------------------------------------------------------------------+
 


   double rsi=iRSI(Symbol(),0,RSIperiod,PRICE_CLOSE,0);
   double rsi1=iRSI(Symbol(),0,RSIperiod,PRICE_CLOSE,1);

//--- open position
   if(OpenSELL && OrderSell<1 && rsi<SellLevel && rsi1>SellLevel) OPSELL();
   if(OpenBUY && OrderBuy<1 && rsi>BuyLevel && rsi1<BuyLevel) OPBUY();


The functions OPSELL and OPBUY are only called after checking the RSI conditions. They are not called anywhere else as far as I can see. So there is no way that an order being closed by stoploss can initiate a new order.

I note that you are using the RSI value for the current bar, by the time that the bar closes the condition may no longer be true. This means that when you look back over the chart it may appear that an order was opened when conditions were not met.

 

this is why im confused as it doesnt appear it should be reversing at stoploss


my modified EA does not rely on rsi nor does it use the live candle...all of that was removed and replaced in my EA


my EA relies on a function of price action between one candle and two candles back


  x = 100*(1-(Close[2] / Close[1]));
  

once x is of a large enough magnitude, it opens an order 

clearly there is more to my EA but these "closes" are the only inputs


somehow my stoplosses are reversing my position and i think its a glitch, not my program

here is the output of what is happening in my journal

notice these are all within seconds, this was the cross of a stoploss:

0       22:51:04.012    '99025322': login datacenter on Forex.com-Live 22 through New York 15 (ping: 1.78 ms)
0       22:51:04.231    '99025322': previous successful authorization performed from 73.80.73.163
0       23:39:35.472    '99025322': order buy market 0.10 EURUSDpro sl: 1.05647 tp: 1.09660
1       23:39:35.550    '99025322': order buy 0.10 EURUSDpro opening at market sl: 1.05647 tp: 1.09660 failed [Hedge is prohibited]
0       23:39:35.550    '99025322': close order #24812457 sell 0.10 EURUSDpro at 1.07642 sl: 1.09657 tp: 1.05642 at price 0.00000
0       23:39:35.691    '99025322': order #24812457 sell 0.10 EURUSDpro at 1.07642 sl: 1.09657 tp: 1.05642 closed at price 1.07660
0       23:39:36.503    '99025322': order sell market 0.10 EURUSDpro sl: 1.09660 tp: 1.05646
0       23:39:36.660    '99025322': order was opened : #24813043 sell 0.10 EURUSDpro at 1.07646 sl: 1.09660 tp: 1.05646




this line in particular leaves me curious something is going wrong

1       23:39:35.550    '99025322': order buy 0.10 EURUSDpro opening at market sl: 1.05647 tp: 1.09660 failed [Hedge is prohibited]




any ideas??

thanks

 

this line in particular leaves me curious something is going wrong

1       23:39:35.550    '99025322': order buy 0.10 EURUSDpro opening at market sl: 1.05647 tp: 1.09660 failed [Hedge is prohibited]

Why? It is self explanatory.



0       23:39:35.691    '99025322': order #24812457 sell 0.10 EURUSDpro at 1.07642 sl: 1.09657 tp: 1.05642 closed at price 1.07660
0       23:39:36.503    '99025322': order sell market 0.10 EURUSDpro sl: 1.09660 tp: 1.05646
0       23:39:36.660    '99025322': order was opened : #24813043 sell 0.10 EURUSDpro at 1.07646 sl: 1.09660 tp: 1.05646

It is not opening a reversal, it is opening another sell.


x = 100*(1-(Close[2] / Close[1]));

When a new trade is opened, print the value of x to check whether the condition is satisfied or not.

 
Keith Watford:

Why? It is self explanatory.


It is not opening a reversal, it is opening another sell.


x = 100*(1-(Close[2] / Close[1]));

When a new trade is opened, print the value of x to check whether the condition is satisfied or not.


ok but what in the EA is causing a hedge condition?

i cant tell why its opening a new sell automatically once stoploss is hit


is there a function for printing to the journal


sorry im still pretty new with mt4

 
ok but what in the EA is causing a hedge condition?

Your EA is trying to open a buy when there is a sell open.


i cant tell why its opening a new sell automatically once stoploss is hit

It is not opening automatically, it is opening because your conditions for a sell are satisfied.


is there a function for printing to the journal

Yes, Print()

It prints to the journal in the tester and to the Experts log on a real-time chart.

 
Keith Watford:

Your EA is trying to open a buy when there is a sell open.


It is not opening automatically, it is opening because your conditions for a sell are satisfied.


Yes, Print()

It prints to the journal in the tester and to the Experts log on a real-time chart.


i think i may have figured something out, i was stuck in a trade so i hadn't had  chance to add the print function yet

but it occurred again last night so i reviewed the journal...

the stoploss did not activate the reversal, however a new trade was activated 20 or so minutes later, immediately on the 3:00:00 hour leading me to believe the EA is also running on the H1 not just the M5

is it even possible that i accidentally set the EA to run in two different time frames??? i did not know that was even possible

perhaps this also explains why its attempting "hedging"??
 
jazzpur: is it even possible that i accidentally set the EA to run in two different time frames??? i did not know that was even possible perhaps this also explains why its attempting "hedging"??
  1. Of course it is. Two charts two EAs. You only check symbol and magic number, so they conflict.
  2. Definitely (they conflict)
 
whroeder1:
  1. Of course it is. Two charts two EAs. You only check symbol and magic number, so they conflict.
  2. Definitely (they conflict)


yikes!


how do i know which aggregation chart ive activated the EA on? if every single aggregation shows this smiley face?

its kinda crappy that the charts dont visually show you unless im missing something

 

im not sure if im understanding the EAs workings fundamentally in mt4


i only have one chart open, trading eur/usd with EA

do you need 2 separate chart windows to run 2 instances of the same EA?


if i set the EA up to run on 5min but click into the 1hour to view a bigger picture...and then shut down my VPS connection...does that EA now trade H1 candles?


i dont understand if it stays "set in place" on 5m only, or does it view the currently showing aggregation?

 
jazzpur: if i set the EA up to run on 5min but click into the 1hour...does that EA now trade H1 candles?
It does unless every call is specifically M5