using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class AddToPosition : WealthScript { protected override void Execute() { DataSeries maFast = EMAModern.Series(Close,20); DataSeries maSlow = EMAModern.Series(Close,100); int entries = 4; int cnt = 0; double lastEntryPrice = 0; for(int bar = maSlow.FirstValidValue; bar < Bars.Count; bar++) { for (int p = ActivePositions.Count - 1; p > -1; p-- ) { Position pos = ActivePositions[p]; lastEntryPrice = ActivePositions[ActivePositions.Count - 1].EntryPrice; bool priceGoesUp = ( High[bar] >= lastEntryPrice * 1.10 ); bool canPyramid = ( ActivePositions.Count < entries ) & priceGoesUp; if (CrossUnder(bar, maFast, maSlow)) { if( SellAtClose(bar, Position.AllPositions, "MA XU") ) { lastEntryPrice = 0; cnt = 0; canPyramid = false; break; } } else if( SellAtStop(bar+1, Position.AllPositions, lastEntryPrice * 0.9, "10%") ) { lastEntryPrice = 0; cnt = 0; canPyramid = false; break; } if( canPyramid ) { if( priceGoesUp ) if( BuyAtStop( bar+1, lastEntryPrice * 1.10, (cnt+1).ToString() ) != null ) { lastEntryPrice = LastPosition.EntryPrice; cnt++; } } } if ( ActivePositions.Count < 1 ) if (CrossOver(bar, maFast, maSlow)) if( BuyAtClose(bar, (cnt+1).ToString()) != null ) { lastEntryPrice = LastPosition.EntryPrice; cnt++; } } PlotSeries( PricePane, maFast, Color.Red, LineStyle.Dashed, 2 ); PlotSeries( PricePane, maSlow, Color.Blue, LineStyle.Solid, 2 ); } } }