Log in to see Cloud of Tags

Wealth-Lab Wiki

WealthScript Techniques | Pyramiding (Adding to position)

RSS

Problem

From time to time, Wealth-Lab users ask how to pyramid their positions:

Lets say I've entered a long position on a MA crossover and wish to add to my position as the price increases -

1.Is it possible to express the following in wealthscript code?

Add to my position every time the price goes up by, say, 10% of the entry price, up to a maximum number of, say, 4 times. eg. entry @ 100 add to position @ 110 add to position @ 120 etc. (maximum 2 more times)

2. Continuing on from the above, is it possible to express the following in wealthscript code?

Lets say I have a StopLoss in place of 5% below entry price, move this StopLoss up 10% every time I add to my position. eg. entry @ 100 stop @ 95 add to position @ 110 move stop up to 105

Solution

This code illustrates several concepts like the usage of boolean conditions that determine whether it's possible to pyramid trades or not, how to move the stops, and how to use the ActivePositions loop:


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 ); } } }

Important Disclaimer: The information provided by Wealth-Lab is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.  The owner of Wealth-Lab.com assumes no liability resulting from the use of the material contained herein for investment purposes. By using this web site, you agree to the terms of this disclaimer and our Terms of Use.


ScrewTurn Wiki. Some of the icons created by FamFamFam.