background

Tradematic Support Center

Guides, articles, videos and links for Tradematic users and developers.

Examples of setting position size in Tradematic Trader

48289ARTICLE CODE EDITOR POSITION SIZE CALCULATEPOSITIONSIZE POSITIONSIZE POSITIONSIZEMODE FUNCTION

In this article we will see several ways how to set entry position size.

Types of position size.

In strategy properties it is possible to choose one of the following types of position size:
- Fixed sum
- Fixed quantity
- Percent of equity
- Max. risk (%)
- Function in script

You can choose first two types of position size if you want to backtest your strategy without accounting capitalization – positions will be opened for the same sum or for the same instrument quantity.

“Percent of equity” type means that Tradematic opens position for a certain percent of equity. For example, if percent of equity value is set as 50, your portfolio costs $100000, and the price of instrument equals $10, position will be opened for 5000 instrument quantity ($50000) when trading signal arrives.

“Max. risk (%)” type of position size will be described in another article.

If none of these position size types suits your needs, you can develop your own one using “Function in script” position size type.

 

Setting position size with “Function in script”.

To control position size in the strategy pre class='prettyprint', we need to choose “Function in script”position size type. Then we need to open strategy pre class='prettyprint' editor at the “Algorithm” tab, then click “Code editor”.

Let’s describe performance of this function in more details.

To control position size in the strategy pre class='prettyprint', we need to use CalculatePositionSize function in TradeMatic.Script class.
This function returns position size (PositionSize object), and receives the following parameters:

  • Position p — position description, we need it to know instrument name
  • double cash — available funds at the moment of position opening
  • double equity — assets amount at the moment of position opening

To use this function, we need to set “Function in script” as a “Position size” type in strategy properties. Then we need to open strategy pre class='prettyprint' editor at the “Algorithm” tab, then click “Code editor”.

 

Example #1. Strategy buys first instrument for 40% of equity and the second one — for 60%.

As an example, we want to buy Apple stocks for 40% and Facebook stocks for 60%.

Here is the function:

public override PositionSize CalculatePositionSize(Position p, double cash, double equity)
{ 
	if(p.Symbol.SymbolName == "APPLE INC") 
	{  
		return new PositionSize(PositionSizeMode.PercentOfEquity, 40); 
	} 
	else if(p.Symbol.SymbolName == "FACEBOOK INC-CLASS A") 
	{  
		return new PositionSize(PositionSizeMode.PercentOfEquity, 60); 
	} 

	return null;
}

Don’t forget to choose these instruments at the "Instruments" tab of strategy properties.
As an example, we use "SMA-9" strategy pre class='prettyprint' as a basis.

Final pre class='prettyprint' of our strategy looks the following way:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using TradeMatic;
using TradeMatic.Indicators;

namespace ScriptNamespace
{ 
	class MyScript : Script 
	{  
		private StrategyParameter parameter0;  
		private StrategyParameter parameter1;  

		public MyScript()  
		{   
			parameter0 = CreateParameter("SMA Period", 9, 0, 100, 1);   
			parameter1 = CreateParameter("SMA Period", 9, 0, 100, 1);  
		}  

		public override PositionSize CalculatePositionSize(Position p, double cash, double equity)  
		{   
			if(p.Symbol.SymbolName == "APPLE INC")   {    return new PositionSize(PositionSizeMode.PercentOfEquity, 40);   }   
			else if(p.Symbol.SymbolName == "FACEBOOK INC-CLASS A")   {    return new PositionSize(PositionSizeMode.PercentOfEquity, 60);   }   
			return null;  
		}     

		public override void Execute()  
		{   
			// Display indicators on the chart   
			PlotSeries(PricePane, SMA.Series(Close, parameter0.ValueInt), Color.Red, LineStyle.Solid, 1);   
			PlotSeries(PricePane, SMA.Series(Close, parameter1.ValueInt), Color.Red, LineStyle.Solid, 1);   

			// Initialization   
			// Main cycle   
			for (int bar = 9; bar < Symbol.Count; bar++)   
			{    
				if (IsLastPositionActive)    
				{     
					if (CrossUnder(bar, Close, SMA.Series(Close, parameter1.ValueInt)))     
					{      
						SellAtClose(bar, LastPosition, "");     
					}    
				}    
				else    
				{     
					if (CrossOver(bar, Close, SMA.Series(Close, parameter0.ValueInt)))     
					{      
						BuyAtClose(bar, "");     
					}    
				}   
			}  
		} 
	}
}

Now let’s backtest our strategy and see the results at the “Trades” tab.

We have used 40% of equity on Apple, and 60% – on Facebook.

 

Example #2. Different position size for long and short positions.

For example, we want to go long for instrument quantity of 4, and we want to go short for instrument quantity of 2.

Here is the function:

public override PositionSize CalculatePositionSize(Position p, double cash, double equity)
{        
	// long position? 
	if(p.PositionType == PositionType.Long) 
	{                
		// open long position for instrument quantity of 4  
		return new PositionSize(PositionSizeMode.FixedShare, 4); 
	} 
	else 
	{                
		// open short position for instrument quantity of 2  
		return new PositionSize(PositionSizeMode.FixedShare, 2); 
	}
}

To use percent of equity (e.g. 25%) instead of instrument fixed quantity to set position size:

return new PositionSize(PositionSizeMode.PercentOfEquity, 25);

As an example, again we take "SMA-9" strategy pre class='prettyprint' as a basis.

Here is the final pre class='prettyprint' of our strategy:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using TradeMatic;
using TradeMatic.Indicators;

namespace ScriptNamespace
{
	class MyScript : Script
	{
		private StrategyParameter parameter0;
		private StrategyParameter parameter1;
		private StrategyParameter parameter2;
		private StrategyParameter parameter3;

		public MyScript()
		{
			parameter0 = CreateParameter("SMA Period (LE)", 9, 0, 100, 1);
			parameter1 = CreateParameter("SMA Period (LX)", 9, 0, 100, 1);
			parameter2 = CreateParameter("SMA Period (SE)", 9, 0, 1000, 1);
			parameter3 = CreateParameter("SMA Period (SX)", 9, 0, 1000, 1);
		}

		public override PositionSize CalculatePositionSize(Position p, double cash, double equity)
		{
			// long position?
			if(p.PositionType == PositionType.Long)
			{
				// open long position for instrument quantity of 4
				return new PositionSize(PositionSizeMode.FixedShare, 4);
			}
			else
			{
				// open short position for instrument quantity of 2  
				return new PositionSize(PositionSizeMode.FixedShare, 2);
			}
		}

		public override void Execute()
		{
			// Display indicators on the chart
			PlotSeries(PricePane, SMA.Series(Close, parameter0.ValueInt), Color.Red, LineStyle.Solid, 1);
			PlotSeries(PricePane, SMA.Series(Close, parameter1.ValueInt), Color.Red, LineStyle.Solid, 1);
			PlotSeries(PricePane, SMA.Series(Close, parameter2.ValueInt), Color.Red, LineStyle.Solid, 1);
			PlotSeries(PricePane, SMA.Series(Close, parameter3.ValueInt), Color.Red, LineStyle.Solid, 1);

			// Initialization

			// Main cycle
			for (int bar = 9; bar < Symbol.Count; bar++)
			{
				if (MarketPosition == 1)
				{
					// Close long position
					if ((CrossUnder(bar, Close, SMA.Series(Close, parameter1.ValueInt))))
					{
						SellAtClose(bar, LastPosition, "");
					}
				}
				else if (MarketPosition == -1)
				{
					// Close short position
					if ((CrossOver(bar, Close, SMA.Series(Close, parameter3.ValueInt))))
					{
						CoverAtClose(bar, LastPosition, "");
					}
				}

				if (MarketPosition == 0)
				{
					// Open long position
					if ((CrossOver(bar, Close, SMA.Series(Close, parameter0.ValueInt))))
					{
						BuyAtClose(bar, "");
					}
				}

				if (MarketPosition == 0)
				{
					// Open short position
					if ((CrossUnder(bar, Close, SMA.Series(Close, parameter2.ValueInt))))
					{
						ShortAtClose(bar, "");
					}
				}
			}
		}
	}
}

Now let’s backtest our strategy and see the results at the “Trades” tab.

This website uses cookies. By continuing to use this website, you consent to our Privacy Policy. OK