hi Rosh, how about this

 
hi Rosh, recently I asked someone to code another indicator for me, and I was thinking of using it to make a trading system, however I noticed a minor problem with this indicator, I was hoping if you can fix it. It's called the Trail Stop Level indicator, i found it helpful to keep you in the trend, it originally came from VT trader, I later had it translated into MQL4.

the indicator works fine except it's not drawing on the last bar(the red bar), which means it displays a 1 bar delay, I was hoping to use it in an EA someday, so it would be great if it works in realtime.

I had a look at the code, it seems the logic is correct, identical to the VT-trader's version, however I'm not sure why VT can draw it in realtime but not in MT4. Is there anyway you can adjust the code so it will plot for the current bar as well?

Here is the code of this indicator in MQL4:

//+------------------------------------------------------------------+ 
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
 
#property indicator_chart_window
#property indicator_buffers 1
 
//---- BUFFER
double OutPut[];
 
//---- INPUT(S)
extern double Factor = 2.824;
extern int ATRPeriods = 14;
extern int Periods = 25;
 
//+------------------------------------------------------------------+
//+                                 INIT                             |
//+------------------------------------------------------------------+
int init()      
{ 
   //---- INDICATOR STYLE
   SetIndexBuffer(0, OutPut);
   SetIndexStyle( 0, DRAW_LINE, STYLE_SOLID, 1, SteelBlue);
   SetIndexDrawBegin(0, Periods );
   
   //----
   return(0); 
}
//+------------------------------------------------------------------+
//+                               DEINIT                             |
//+------------------------------------------------------------------+
int deinit() { return(0); }
//+------------------------------------------------------------------+
//+                                START                             |
//+------------------------------------------------------------------+
int start()    
{ 
   //---- Find MaxRecords
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) { counted_bars--; }
   int MaxRecords = Bars-counted_bars;
   
   for( int i = MaxRecords - 1 ; i > 0; i-- )
   {
      double Sum = 0.0;
      for( int k = 0; k < Periods ; k++ )
      {
        Sum += iATR( NULL, 0 , ATRPeriods, i+k );
      }
      double DeltaStop =  NormalizeDouble( ( Sum/Periods ) * Factor, 4 );
      double Price = Close[i];
      double Prev = OutPut[i+1];
 

// The following code is doing the actual calculation of the Trail Stop Level
 
         if( Price == Prev )
         {
            OutPut[i] = Prev;
         }
         else if ( Close[i+1] <= Prev && Price < Prev )
         {
            OutPut[i] = MathMin( Prev,   Price + DeltaStop ) ;
         }
         else if ( Close[i+1] >= Prev && Price > Prev )
         {
            OutPut[i] = MathMax( Prev,   Price - DeltaStop ) ;
         }
         else if ( Price > Prev )
         {
            OutPut[i] = Price - DeltaStop;
         }
         else
         {
            OutPut[i] = Price + DeltaStop;
         }
   }
   //----
   return(0); 
}

Thanks alot in advance ;-) please take care
 
No problem :)

//+------------------------------------------------------------------+
//|                                                   Trail Stop.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        https://www.metaquotes.net/ru/ |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
 
#property indicator_chart_window
#property indicator_buffers 1
 
//---- BUFFER
double OutPut[];
 
//---- INPUT(S)
extern double Factor = 2.824;
extern int ATRPeriods = 14;
extern int Periods = 25;
 
//+------------------------------------------------------------------+
//+                                 INIT                             |
//+------------------------------------------------------------------+
int init()      
{ 
   //---- INDICATOR STYLE
   SetIndexBuffer(0, OutPut);
   SetIndexStyle( 0, DRAW_LINE, STYLE_SOLID, 1, SteelBlue);
   SetIndexDrawBegin(0, Periods );
   
   //----
   return(0); 
}
//+------------------------------------------------------------------+
//+                               DEINIT                             |
//+------------------------------------------------------------------+
int deinit() { return(0); }
//+------------------------------------------------------------------+
//+                                START                             |
//+------------------------------------------------------------------+
int start()    
{ 
   //---- Find MaxRecords
   int counted_bars=IndicatorCounted();
   if(counted_bars<0) return(-1);
   if(counted_bars>0) { counted_bars--; }
   int MaxRecords = Bars-counted_bars;
   
   for( int i = MaxRecords - 1 ; i >= 0; i-- )
      {
      double Sum = 0.0;
      for( int k = 0; k < Periods ; k++ )
         {
         Sum += iATR( NULL, 0 , ATRPeriods, i+k );
         }
      double DeltaStop =  NormalizeDouble( ( Sum/Periods ) * Factor, 4 );
      double Price = Close[i];
      double Prev = OutPut[i+1];
 
 
// The following code is doing the actual calculation of the Trail Stop Level
 
         if( Price == Prev )
            {
               OutPut[i] = Prev;
            }
         else if ( Close[i+1] <= Prev && Price < Prev )
            {
            OutPut[i] = MathMin( Prev,   Price + DeltaStop ) ;
            }
            else if ( Close[i+1] >= Prev && Price > Prev )
               {
               OutPut[i] = MathMax( Prev,   Price - DeltaStop ) ;
               }
               else if ( Price > Prev )
                  {
                  OutPut[i] = Price - DeltaStop;
                  }
                  else
                     {
                     OutPut[i] = Price + DeltaStop;
                     }
      }
   //----
   return(0); 
}

I want to point your attention at this line:
for( int i = MaxRecords - 1 ; i >= 0; i-- )
 
hello Rosh, it's working as it should now, I'm truely grateful for your assistance ;-)

I'd like to make an EA using this indicator together with the Smoothed ADX that you programmed for me earlier, I used to have this system with VT, but VT has stability problems and does not support intra bar executions(can only issue orders at the close of the bar). Here is the screenshot of what the system should look like:



as you can see, this system is simple, only enters the market when the trend starts, once position is opened, follow the trend as long as possible until the price crosses the trail stop line.

so are you interested in making this EA together? if so I suppose we can discuss further about the system details, doesn't hurt in trying ;-)

be in touch, and thank you!

Kevin

hunter_eye@hotmail.com
Reason: