Tradematic Support Center
Guides, articles, videos and links for Tradematic users and developers.
Setting position size depending on market volatility
101438ARTICLE CODE EDITOR EXAMPLE DYNAMIC POSITION SIZE MARKET VOLATILITYIn this example we will see function, which set each position size depending on market volatility at the moment of position opening.
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.
To measure market volatility, we want to use ATR indicator with parameter value of 60. (You can use any relevant indicators).
Formula to calculate position size looks this way:
POSITION_SIZE = 0.0065 * Close / ATR(60)
To calculate this formula, we need to get Close price value at the moment of position opening (p.EntryBar):
// get Close price value at the moment of position opening double close = p.Symbol.Close[p.EntryBar];
Also we need to get ATR parameter value for the bar of position opening:
// get ATR parameter value for the bar of position opening double atr = ATR.Value(p.EntryBar, p.Symbol.High, p.Symbol.Low, p.Symbol.Close, 60);
We have:
public override PositionSize CalculatePositionSize(Position p, double cash, double equity) { // get Close price value at the moment of position opening double close = p.Symbol.Close[p.EntryBar]; // get ATR parameter value for the bar of position opening double atr = ATR.Value(p.EntryBar, p.Symbol.High, p.Symbol.Low, p.Symbol.Close, 60); return new PositionSize(PositionSizeMode.PercentOfEquity, 0.0065 * close / atr); }
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) { // get Close price value at the moment of position opening double close = p.Symbol.Close[p.EntryBar]; // get ATR parameter value for the bar of position opening double atr = ATR.Value(p.EntryBar, p.Symbol.High, p.Symbol.Low, p.Symbol.Close, 60); return new PositionSize(PositionSizeMode.PercentOfEquity, 0.0065 * close / atr); } 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, ""); } } } } } }