Calculation of SL

To add comments, please log in or register
thierrybl
87
thierrybl  

Hello,


I have a problem coding my SL based on fractals.

I took an EA existing on the net which works perfectly by putting SL on the opposite fractals when that way of calculate SL is set.


I just invert Buy and Sell orders, but in this case it's always the maximum stop command which is used. The choice "stop using fractals" is innoperate.


I tried a lot of modification but can't find a solution. I attach both versions. The original one (NsWave EAv1.0) and the modified (NsWave EAv1.0 inverse). I understand that when for example the up fractal was used for setting a trade, the opposite (down fractal) was set to 0 ; and it's not possible to calculate SL with 0. I defined a new value for down fractal with "dnforstop" to return a value not linked with the upfractal. but it doesn't work.


I attach both EA, and it would be great if you could have a glance at it.


Thanks a lot


Thierry

William Roeder
18292
William Roeder  
thierrybl:
The choice "stop using fractals" is innoperate.the opposite (down fractal) was set to 0 ; and it's not possible to calculate SL with 0.

I tried a lot of modification but can't find a solution.

  1.       double   upfractal = GetFractal(MODE_UPPER),
                   dnfractal = GetFractal(MODE_LOWER);
                   
          double   upforstop = iFractals(Symbol(), 0, MODE_UPPER, LookBack),
                   dnforstop = iFractals(Symbol(), 0, MODE_LOWER, LookBack);
    :
          if ( dnfractal != 0 && timenowdn != Time[0] && Hour() >= StartTrading && Hour() < StopTrading ){
                if ( UseFractalForStop ) StopLoss = (upforstop - Bid)/Point+(Ask-Bid)+Offset;
    :
                OrderSend(Symbol(),OP_SELL,
    
    1. There is no difference between upfractal and upforStop, and likewise the down variables. As coded they will always be exactly the same. Why do you have two sets of variables?

    2. When you find candle 2 (GetFractal/LookBack) is a down fractal you try to set the Stop Loss for your sell order, to the up fractal. Why do you think the candle is both at the same time? Unlikely. And that is why you get zero.

    3. Perhaps you should search for the last actual up fractal instead of assuming that is is always candle 2 (LookBack.)

    4. Do you really want to sell after a down fractal?

  2. Show us your attempt (using CODE button) and state the nature of your problem.
              No free help
              urgent help.


  3. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

thierrybl
87
thierrybl  

Hello,


Thanks a lot for your answer.

I'm sorry to post in the wrong section. I didn't notice I was in Mql5 section and not Mql4.

You are completely right with upfractal and upforStop. I thought that if  dnfractal != 0 it means that upfractal ==0 and that was the reason why I get 0. I thought it was automatically looking for the last fractal but that in my case the opposite fractal was set to 0. That's why I added upforstop and dnforstop to have another reference for the fractal.

Indeed I made copy/paste for dnforstop and upforstop and LookBack is an error. It is an extern variable in the original EA to avoid repaint of the fractal.

I confirm that my aim is to SELL immediately after a down fractal appears and is confirmed, and set the SL at the last up fractal.(and reverse of course)

The next steps will be to add the possibility to choose the direction of trading and to add filters as moving average and pivot points.

I hope I am clear but I am French and English is not my native language !

Thanks for your help and I 'll try to correct my Ea with your note.

Thierry

thierrybl
87
thierrybl  

Hello,


I modifed the code in order to get the nearest up or dn fractal, and have SL plotted on it. I have modified the yellow parts. If it works like that I will add the possibility to look for 2sd nearest fractal for stop if too close of the current price.

I don't know yet if it works as market is close. I will try Monday.


Thanks

Thierry

extern int        Magic = 910;
extern double     Lots = 0.10;               // Used if AutoMoneyManagement = false
extern bool       AutoMoneyManagement = false;// Will automatically compute lots size based on PercentToRisk     
extern double     PercentToRisk = 1;         // Percent amount of balance to risk on trade
extern int        LookBack = 3;              // Bars back from current open to look for a Fractal
extern int        Slippage = 5;              // Amount of pip slippage allowed without causing a requote
extern bool       UseFractalForStop = true; // Uses the last fractal to the current open to compute StopLoss
extern double     MaximumStop = 350;          // Maximum allowable stop amount in pips
extern double     RR_Ratio = 2;              // Reward:risk ratio
extern double     Offset   = 0;              // distance above or below bar for stoploss
 
extern string     Note = "Trading hours are Broker time";
extern int        StartTrading = 7;          // Start of trading session
extern int        StopTrading = 17;          // end of trading session

string            Comments = "NSWave ";
datetime          timenowup = 0, timenowdn = 0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
      Comment( "Copyright © 2008, SjCoInc" );
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
      double   Risk = PercentToRisk/100,StopLoss,TakeProfit;
      
      double   upfractal = GetFractal(MODE_UPPER),
               dnfractal = GetFractal(MODE_LOWER);
               
               //+------------------------------------------------------------------+

//--- declaration of variables
int n,UpperFractal_1,UpperFractal_2,LowerFractal_1,LowerFractal_2;

//--- finding the bar index of the first nearest upper fractal

   for(n=0; n<(Bars-1);n++)
     {
      if(iFractals(NULL,1440,MODE_UPPER,n)!=NULL)
         break;
      UpperFractal_1=n+1;
     }
//--- finding the bar index of the second nearest upper fractal
   for(n=UpperFractal_1+1; n<(Bars-1);n++)
     {
      if(iFractals(NULL,1440,MODE_UPPER,n)!=NULL)
         break;
      UpperFractal_2=n+1;
     }


//--- finding the bar index of the first nearest lower fractal
   for(n=0; n<(Bars-1);n++)
     {
      if(iFractals(NULL,1440,MODE_LOWER,n)!=NULL)
         break;
      LowerFractal_1=n+1;
     }
//--- finding the bar index of the second nearest lower fractal
   for(n=LowerFractal_1+1; n<(Bars-1);n++)
     {
      if(iFractals(NULL,1440,MODE_LOWER,n)!=NULL)
         break;
      LowerFractal_2=n+1;
     }
               
           if ( upfractal != 0 && timenowup != Time[0] && Hour() >= StartTrading && Hour() < StopTrading )
         {
            StopLoss = (High[1] - Close[1]+(Ask-Bid))/Point + Offset;
                      
            if ( UseFractalForStop ) StopLoss = LowerFractal_1 + Offset * Point
           ;
            if ( StopLoss > MaximumStop ) StopLoss = MaximumStop;
            TakeProfit = NormalizeDouble(StopLoss * RR_Ratio, 0 );
            timenowup = Time[0];
            if (AutoMoneyManagement)
               Lots = NormalizeDouble(AccountBalance()*Risk/StopLoss/(MarketInfo(Symbol(), MODE_TICKVALUE)),2);
            
            OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Bid-StopLoss*Point,Ask+TakeProfit*Point, Comments+" Buy",Magic,0,Red); 
            Comment("Lower ", upfractal, "   StopLoss ", StopLoss, "   TakeProfit ", TakeProfit, "   Lots ", Lots );
         }    
         
      if ( dnfractal != 0 && timenowdn != Time[0] && Hour() >= StartTrading && Hour() < StopTrading )
         {
            StopLoss = (Close[1] - Low[1]+(Ask-Bid))/Point + Offset;
            
            if ( UseFractalForStop ) StopLoss = UpperFractal_1 + Offset * Point;
            if ( StopLoss > MaximumStop ) StopLoss = MaximumStop;
            TakeProfit = NormalizeDouble(StopLoss * RR_Ratio, 0 );
            timenowdn = Time[0];
            if (AutoMoneyManagement)
               Lots = NormalizeDouble(AccountBalance()*Risk/StopLoss/(MarketInfo(Symbol(), MODE_TICKVALUE)),2);
            
            OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Ask+StopLoss*Point,Bid-TakeProfit*Point, Comments+" Sell",Magic,0,Lime);
            Comment("Upper ", dnfractal, "   StopLoss ", StopLoss, "   TakeProfit ", TakeProfit, "   Lots ", Lots );
         }            
                 
                               
//----
   return(0);
  }
//+------------------------------------------------------------------+

double GetFractal(int mode)
   {
      double tempup=0, tempdn=0, fup=0, fdn=0;
      tempup = iFractals(Symbol(), 0, MODE_UPPER, LookBack);
      tempdn = iFractals(Symbol(), 0, MODE_LOWER, LookBack);
      if(tempup != 0) fup = tempup;
      if(tempdn != 0) fdn = tempdn;

      if ( fup != 0 && fdn == 0 && mode == MODE_UPPER ) return(fup);
      if ( fdn != 0 && fup == 0 && mode == MODE_LOWER ) return(fdn);
      return(0);
   }
William Roeder
18292
William Roeder  
thierrybl: I don't know yet if it works as market is close.

That is what the debugger and the visual tester is for.

thierrybl
87
thierrybl  

Hello,


In those few days I tried a lot of other pieces of code to have it works, but it doesn't work the right way.

Fractals for stops (UpperFractal_1 and LowerFractal_1) return integer values (1,2,3,4, ...) and Stops are just 1 or 2 points to the entry price. It doesn't consider the fractal value. You can see on the attched print screen.

I search in MQL4 forum for days but I couldn't find code to return value of the last fractal up or down.

I really don't know what to do now.

It would be very nice if you give me the piece of code missing.


Thanks for your help


Thierry

To add comments, please log in or register