GP,

since you seem pretty handy with this stuff maybe you can have a look at the 
code below.  I write most of my exit code using loops. Below an example how I 
usually do it. In this example I code an nBar stop. Note that I let the j loop 
deliberately go to j < barcount since within the j loop I can put all other 
types of exit code and sometime I remove the nbar stop. 

Do you see any problems with this code?

thanks, ed



procedure nbarStop_proc(Buy,Short,nBar) { 

global Sell; 
global SellPrice; 
global BuyAdjusted; 

global Cover; 
global CoverPrice; 
global ShortAdjusted; 

// initialise arrays 
SellPrice = 0; 
Sell = 0; 
BuyAdjusted = 0; 

CoverPrice = 0; 
Cover = 0; 
ShortAdjusted = 0; 


for (i = 1; i < BarCount; i++) { 


   if (Buy[ i ] == 1) {    
    
      BuyAdjusted[ i ] = 1;       
       
      for (j = i; j < BarCount; j++) { 
                      
         if (j - i == nBar) { 

            Sell[ j ] = 5; 
            SellPrice[ j ] = O[ j ];             
            i = j; 
            break; 
                                     
                
         } else if (j == BarCount - 1) { 
          
            i = BarCount; 
          
         }             
          
       
      } 
       
   } else if (Short[ i ] == 1) {    
    
      ShortAdjusted[ i ] = 1; 
       
      for (j = i; j < BarCount; j++) {             
          
         if (j - i == nBar) { 

            Cover[ j ] = 5; 
            CoverPrice[ j ] = O[ j ]; 
            i = j; 
            break;          

         } else if (j == BarCount - 1) { 
          
            i = BarCount; 
          
         }             
          
      } 

   }    

} 

} 



SetBarsRequired(10000,10000); 

per = 20; 
nbar = 10; 

Buy = C < BBandBot(C,per,2); Buy = Ref(Buy,-1); 
BuyPrice = O; 

Short = C > BBandTop(C,per,2); Short = Ref(Short,-1); 
ShortPrice = O; 

nbarStop_proc(Buy,Short,nBar); 
Buy = BuyAdjusted; 
Short = ShortAdjusted; 

SetChartOptions(0, chartShowDates); 
GraphXSpace = 5; 
Plot(C,"C",1,64); 
Plot(BBandBot(C,per,2),"",colorGold,1); 
Plot(BBandTop(C,per,2),"",colorGold,1); 
PlotShapes(IIf(Buy,shapeUpTriangle,0),colorWhite, layer = 0, yposition = 
BuyPrice, offset = 0 ); 
PlotShapes(IIf(Short,shapeDownTriangle,0),colorYellow, layer = 0, yposition = 
ShortPrice, offset = 0 ); 
PlotShapes(IIf(Sell,shapeHollowDownTriangle,0),colorWhite, layer = 0, yposition 
= SellPrice, offset = 0 ); 
PlotShapes(IIf(Cover,shapeHollowUpTriangle,0),colorYellow, layer = 0, yposition 
= CoverPrice, offset = 0 );  

Reply via email to