Your interactive trading laboratory!
 • 
5 users online

Overriding Position Size

I'm working on a sophisticated model in C# that implements a rotation style strategy, but moves into a safe haven security when a certain condition occurs. I just discovered something pretty cool about the framework that I hadn't preconceived. After calling PlaceTrade you're handed the Transaction object that represents the order. You can assign a value to the Quantity property to override the default position size. In effect you can, coupled with using the CurrentEquity and possibly CurrentCash properties, implement your own custom position sizing right in the code! Here's a snippet where I issue a buy on an external symbol to move 100% into the safe haven.

//myExternalBarHistory was stored in a static variable with a call to GetHistoryUnsynched
Transaction t = PlaceTrade(myExternalBarHistory, TransactionType.Buy, OrderType.Market, 0, 1);

//get current index into the external data
int idx = GetCurrentIndex(myExternalBarHistory);

//get the closing price as a basis price
double c = myExternalBarHistory.Close[idx];

//go into it with 100% of equity
t.Quantity = (int)(CurrentEquity / c);

This opens up some pretty interesting possibilities.

Attachment

Cancel

Responses

Very cool feature Glitch! I've adapted a code example you posted before to trade the equity curve. The idea is to capitalize on good times by trading twice the size if current equity is above its moving average:

using QuantaculaBacktest;
using System;
using QuantaculaCore;
using QuantaculaIndicators;
using QuantaculaChart;
using System.Drawing;
using System.Collections.Generic;

namespace Quantacula
{
	public class MyModel : UserModelBase
	{
		TimeSeries equity;
		TimeSeries equitySMA;

		//Configure your usual position size as % of equity (say 5% per position):
		double percentEquity = 5;

		public override void Initialize(BarHistory bars)
		{
			equity = new TimeSeries(bars.DateTimes);
			equitySMA = new TimeSeries(bars.DateTimes);
			Plot(equity, "Equity", Color.Green, PlotStyles.ThickHistogram, "Equity");
			Plot(equitySMA, "SMA(Equity)", Color.Black, PlotStyles.Line, "Equity");
		}

		public override void Execute(BarHistory bars, int idx)
		{
			equity[idx] = CurrentEquity;
			equitySMA[idx] = SMA.Calculate(idx, equity, 10);
			
			//Calculate the usual size in shares based on the close price
			double c = bars.Close[idx];
			var usualSize = (int)(CurrentEquity / c) / 100 * percentEquity;

			//Trade twice the size if equity is above its moving average
			var pctEquity = (equity[idx] > equitySMA[idx]) ? usualSize * 2 : usualSize;

			if (!HasOpenPosition(bars, PositionType.Long))
			{
				var stop = Highest.Series(bars.High, 20)[idx];
				PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, stop).Quantity = pctEquity;

				//As alternative...
				//var t = PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, stop);
				//t.Quantity = pctEquity;
			}
			else
			{
				var stop = Lowest.Series(bars.Low, 20)[idx];
				PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, stop);
			}
		}
	}
}
Very cool feature Glitch! I've adapted a code example you posted before to trade the equity curve. The idea is to capitalize on good times by trading twice the size if current equity is above its moving average: [CODE] using QuantaculaBacktest; using System; using QuantaculaCore; using QuantaculaIndicators; using QuantaculaChart; using System.Drawing; using System.Collections.Generic; namespace Quantacula { public class MyModel : UserModelBase { TimeSeries equity; TimeSeries equitySMA; //Configure your usual position size as % of equity (say 5% per position): double percentEquity = 5; public override void Initialize(BarHistory bars) { equity = new TimeSeries(bars.DateTimes); equitySMA = new TimeSeries(bars.DateTimes); Plot(equity, "Equity", Color.Green, PlotStyles.ThickHistogram, "Equity"); Plot(equitySMA, "SMA(Equity)", Color.Black, PlotStyles.Line, "Equity"); } public override void Execute(BarHistory bars, int idx) { equity[idx] = CurrentEquity; equitySMA[idx] = SMA.Calculate(idx, equity, 10); //Calculate the usual size in shares based on the close price double c = bars.Close[idx]; var usualSize = (int)(CurrentEquity / c) / 100 * percentEquity; //Trade twice the size if equity is above its moving average var pctEquity = (equity[idx] > equitySMA[idx]) ? usualSize * 2 : usualSize; if (!HasOpenPosition(bars, PositionType.Long)) { var stop = Highest.Series(bars.High, 20)[idx]; PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, stop).Quantity = pctEquity; //As alternative... //var t = PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, stop); //t.Quantity = pctEquity; } else { var stop = Lowest.Series(bars.Low, 20)[idx]; PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, stop); } } } } [/CODE]

Thanks atmosfear! I think this is a first for me in my software development career … creating an "emergent feature" that I only discover after an initial release ;)

Thanks atmosfear! I think this is a first for me in my software development career … creating an "emergent feature" that I only discover after an initial release ;)

Nice work, I finally got around to trying this code out for variable position sizing. Very straightforward to use. And I would note that doing this like atmosfear does, with percentEquity set, and using t.Quantity on all buys completely overrides the position sizing set in Model Settings. This should be obvious from the code, but just noting this for posterity.

Nice work, I finally got around to trying this code out for variable position sizing. Very straightforward to use. And I would note that doing this like atmosfear does, with percentEquity set, and using t.Quantity on all buys completely overrides the position sizing set in Model Settings. This should be obvious from the code, but just noting this for posterity.
Forum Tips

Please sign in if you want to participate in our forum.

Our forum uses Markdown syntax to format posts.

To embed code snippets, enclose them in [CODE][/CODE] tags.