Trailing Stop Indicator

 

Hi,

I am looking for help in creating an indicator that displays trailing stops so I can visualize different exit strategies for EAs.  I am quite new to programming in MQL4 so if anyone can help or point me in the right direction that would be greatly appreciated.


Regards,


Evan

 

Ok.

set up 8 indicator indexes. That lets you track up to 8 open trades.

insert the current value of the stoploss into the current bar for the indicator index you assign to each open trade.

There is no history of stop loss, you can only go forward with it.

 
phy:

Ok.

set up 8 indicator indexes. That lets you track up to 8 open trades.

insert the current value of the stoploss into the current bar for the indicator index you assign to each open trade.

There is no history of stop loss, you can only go forward with it.

Thank you for your response.  That makes a lot of sense, although I have a follow up question.  Is there no way to create an indicator that draws a line at Close[] +- the trailing value that has the same logic of a trailing stop?  The reason I ask is that I want to visualize different exit strategies over the last few years on my charts.  In addition, I want to be able to input more complex logic directly into an indicator to see the results.  Any help is very appreciated.


Regards,


Evan

 

Yes, you can draw a line on your chart using any calculation you think up.

 

Hello,

A survey is conducted in the context of the evolution of Expert Advisors.

Less than 2 minutes are needed to complete the form.

You will be automatically registered in a draw for an iPhone 16GB.

The results may be published on MorningNews.com & FinancialTimes.com

To participate: http://expertadvisor.questionform.com/public/Expert-Advisor
 
phy:

Yes, you can draw a line on your chart using any calculation you think up.

Here is the code that I came up with.  This is the first indicator that I have every tried to program so please go easy on me and let me know where it is that I have gone wrong, the indicator draws the close line only without the trailing factor.

 {

   int i,                           // Bar index

       Counted_bars, 

       n;             // Number of counted bars

       

   double trailup,

            trailup1;               // Trailing stop values

//--------------------------------------------------------------------

   Counted_bars=IndicatorCounted(); // Number of counted bars

   i=Bars-Counted_bars-1;           // Index of the first uncounted

   while(i>=0)                      // Loop for uncounted bars

     {

     trailup=trailvalue;

      if(Close[0]==Close[1])                                              //Price doesn't change

       {

       trailup=trailup1;

       }

      if(Close[0]<Close[1] &&  trailup1<(Close[1]-Close[0]))           //Price declining below stoploss, Stopped out and reset

       {

       trailup=0;

       }

      if(Close[0]<Close[1] &&  trailup1>(Close[1]-Close[0]))           // Price declines but not to stop loss level, stop remains in place

       {

       trailup=trailup1-(Close[1]-Close[0]);

       }

      if(Close[0]>Close[1] && trailup1+(Close[0]-Close[1])>trailvalue*Point)  // Price moves up when and moves stop loss up

       {

       trailup=trailvalue;

       }

      if(Close[0]>Close[1] && trailup1+(Close[0]-Close[1])<trailvalue*Point) // Price moves up but not enough to move stop

       {

       trailup=trailup1+(Close[0]-Close[1]);

       }

       trailup1=trailup;

     

     

     

     

      Buf_0[i]=Close[0]-trailup;             // Value of 0 buffer on i bar

  

     

     

      i--;                          // Calculating index of the next bar

     }


If there is a simpler logic, please let me know.  Also, if you could let me know the programming mistakes that would be a huge help.


Thanks,


Evan

Reason: