Need help on my EA - page 2

 
Quattrofan:

Hello Viffer,

Sorry, can you explain me your last code,

with an example. I have read in mq4, but my englisch not so good.

I have tested the code with your last statement, and now on a few trades the EA modifed the SL, but not on all.

Thanks


The broker will have a minimum allowable distance for stops.... MarketInfo MODE_STOPLEVEL is retrieving the minimum distance and we get the price level of the minimum threshold (for long) by subtracting that value from bid. After we have set where we want NewSL, we need to check it is not above (for long) the allowable stop threshold. If it is, the modify will fail and the stop won't change so instead, we set NewSL to the minimum allowable distance. Obviously if NewSL is outside the minimum distance, it will modify OK.

V

 

Thanks for your answer,

Now i`ve tested my EA on an Demo Account, and there is a Problem. See the picture.


The problem always arises, if a longer upward phase (downward phase) is broken.

 

Assuming your options are set to allow both long and short positions, check your log for errors. Use GetLastError() and Print(). I suspect you will need also to test if the stops are allowable at ordersend.

V

 

Sorry V, my englisch and the translationprogramms are not the best. What are you meen with" I suspect you will need also to test if the stops are allowable at ordersend."

Another Question: SL 1.2988 ->Close Current Candle 1.2992 -> follow Current Candle open 1.2993 falling to 1.2990; MarketInfo Mode_Stoplevel 5 PIPS.

In this Example, the SL is not changed. But the candle should have to close by Market at 1.2991. Is there a possibility in MQL4 to code it. Thanks.

I have tested my idea at papertrading an the result are cut through 30 pips a day.

I have check all your proposals, but the EA don´t open a sell order.

What is the result of 1*Point????

#include <stdlib.mqh>

int MagicNumber=291177;
extern double TakeProfit=0.0008;
extern double Lots=0.01;
extern double BreakOutLevel=0.0001;
extern bool UseTimeLimit=true;
extern int StartHour=0;
extern int StopHour=24;
extern int FridayCloseHour=20;
int TradesInThisSymbol;
bool YesStop;
double NewSL;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {int ticket;


if (UseTimeLimit)
   {   YesStop=True;
        if(DayOfWeek()==0 || DayOfWeek()==6) return(0);                 // kein Trading am Samstag und Sonntag
        if(DayOfWeek()==5  && Hour()>= FridayCloseHour) return(true);   // kein Trading Freitags nach 20:00 Uhr
        if(Hour()>=StartHour && Hour()<StopHour) YesStop=false;  // Trading von 0:00 bis 24:00 Uhr
        if (YesStop) return(0);
   }

HandleOpenPositions();


   // Only allow 1 trade per Symbol
   
TradesInThisSymbol=openPositions();
   if(TradesInThisSymbol == 0) 
     {
     if(Bid > High[1])
        ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Low[1]-0.0001,Ask + TakeProfit,"CashAndy",MagicNumber,0,Green);
   

     if(Bid < Low[1])
        ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,High[1]+0.0001,Bid - TakeProfit,"CashAndy",MagicNumber,0,Red);
  
         
  if (   NewSL>Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point)             
      {
        NewSL=Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;
     } }
    
  
    return(0); }


  
//+------------------------------------------------------------------------+
//| counts the number of open positions                                    |
//+------------------------------------------------------------------------+
int openPositions(  )
  {  int op =0;
   for(int i=OrdersTotal()-1;i>=0;i--)                                // scan all orders and positions...
     {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber()!=MagicNumber) continue;
      if(OrderSymbol()==Symbol() )
        {
         if(OrderType()==OP_BUY)op++;
         if(OrderType()==OP_SELL)op++;
        }
     }return(op);
}

//+------------------------------------------------------------------------+
//| Handle open positions                                    |
//+------------------------------------------------------------------------+

int HandleOpenPositions()
{int cnt; double NewSL; double SL;
 
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()!=Symbol() )continue;
      if (OrderMagicNumber()!=MagicNumber) continue;
         if(OrderType()==OP_BUY)
         {  SL=OrderStopLoss();
                   NewSL=Low[1]-0.0001;
                   if (NewSL-1*Point>SL+1*Point)
                     {OrderModify(OrderTicket(), OrderOpenPrice(), NewSL,OrderTakeProfit(),0,0);}}
         if(OrderType()==OP_SELL)          
          { SL=OrderStopLoss();                  
                            
                    NewSL=High[1]+0.0001;
                    if (NewSL+1*Point<SL-1*Point)
                     { OrderModify(OrderTicket(),OrderOpenPrice(),NewSL,OrderTakeProfit(),0,0);}}}  }
 
Quattrofan:

.... What are you meen with" I suspect you will need also to test if the stops are allowable at ordersend." ....

In the same way you check stops are allowable during the order modify, you should check your stops are allowable when you want to send the order. It will fail otherwise and I suspect you will get a lot of this on this strategy.



Quattrofan:#

...But the candle should have to close by Market at 1.2991. Is there a possibility in MQL4 to code it....

Select the order, test your criteria and close it.

https://book.mql4.com/trading/orderclose


Quattrofan:What is the result of 1*Point????

https://docs.mql4.com/predefined/variables/Point

V
Reason: