Hello,



I have included a pretty simple pyramiding script below that initially
buys on a DEMA crossover.  After the price increases incrementally 5%
from the price at the crossover, the script scales in.

What I would like to do is replace the heart of this script with a
function that returns the initial Buy Price and each incremental buy
price while the FASTMA is greater than the SLOWMA.  I am wanting to do
this to simplify the script and to add more complex features such as
trailing stops.  My problem is that I do not understand how to write
user-defined functions.  I am having a hard time making sense of the
logic.  Any help walking me through this process would be very much
appreciated.




Kind regards.




FastMALength = 100;         //Set Fast Moving Average

SlowMALength = 300;         //Set Slow Moving Average




FastMA = DEMA(Close, FastMALength);       //Fast Moving Average

SlowMA = DEMA(Close, SlowMALength);       //Slow Moving Average







bi = BarIndex();




Cond1 = MA(Volume, 100) > 100000;

Cond2 = OI >= 5;

Cond3 = Cross(FastMA, SlowMA);

Cond4 = bi != LastValue(bi);




SetBarsRequired(SlowMALength, 0);

SetOption("MaxOpenPositions", 25 );

PositionScore = MA(Volume, 100);




Buy =  Cond1 AND Cond2 AND Cond3;       //Buy Rule #1 - Fast MA Crosses
Above Slow MA

Sell = Cross(SlowMA, FastMA) OR bi == LastValue(bi);     //Sell Rule #1
- Fast MA Crosses Below Slow MA







BuyInc = 5;          //Incremental Buy Amount (%)

NextBuyARRAY = Null;         //Next Incremental Buy Value

priceatbuy = 0;          //Initial Buy Value

CounterARRAY = Null;         //Counts Number of Buys







for (  i =  0;  i < BarCount; i++ )

{




if(priceatbuy == 0 AND Buy[ i ] )

{

priceatbuy = BuyPrice[ i ];        //Set inital entry Price

}



if(priceatbuy > 0)

{



//Set next BuyPrice



counterARRAY[ i ] = Max( CounterARRAY[ i - 1], 1 + int( ( (Close[ i - 1
] - priceatbuy) / priceatbuy) / (BuyInc/100) ) );

NextBuyARRAY[ i ] = Max( NextBuyARRAY[ i ], priceatbuy * (1 + ((BuyInc /
100) * counterARRAY[ i ])));

if(Close[ i ]  > NextBuyARRAY[ i ])

{

Buy[ i ] = sigScaleIn;

BuyPrice[ i ] = Close[ i ];

}



}




if(Sell[ i ])

{

priceatbuy = 0;







}







}




Plot(Close, "Price", colorBlack, styleCandle);

Plot(NextBuyARRAY, "NextBuy", colorRed);

PlotOHLC(NextBuyARRAY, NextBuyARRAY, SlowMA, SlowMA, "", colorYellow,
styleCloud);




Reply via email to