background

Tradematic Support Center

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

Spot Futures Strategy on the Example of Index Trading in Tradematic Trader

15760ARTICLE CODE EDITOR SPOT-FUTURE INDEX EXTERNAL INSTRUMENT GETEXTERNALSYMBOL

We continue the series of articles describing the nuances of using Tradematic Trader - a constructor of trading robots.
In this article, we will consider a fairly common problem such as "spot futures", ie. we analyze one instrument - we trade another, using the example of a strategy that analyzes the MICEX 10 index, and trading securities included in it.

Quite often, it becomes necessary to analyze one instrument and trade in another (or several at once).
Here are examples of strategies that use this technique:

Index trading

The strategy analyzes the dynamics of the index, and trades the securities included in it. The index chart is always smoother and less volatile, which improves the quality of many indicators and increases the profitability of the strategy. Besides, it is not technically possible to buy the index itself.

Spot-futures

The underlying asset is analyzed (for example, a Sberbank share), and the Sberbank futures are traded.
The meaning of this strategy is in significant cost savings (brokerage commissions are much lower on the FORTS market), as well as the actual use of free borrowed funds (instead of the full cost of the contract, only collateral is blocked).

Strategy creation

In this article, we will create a strategy that will analyze the MICEX 10 index and trade the stocks included in it.
In exactly the same way, you can create a "spot-futures" strategy, which analyzes the underlying asset and trades the futures.

We will again use our favorite SMA-9 strategy as a basis for clarity.
As an index, we take the MICEX10 index, which currently includes the following securities: VTB Jsc, GAZPROM Jsc, GMKNornik, LUKOIL, Rosneft, RusHydro, Sberbank, Sberbank-p, SevSt-jsc, Surgnfgz.

Important! All these securities, as well as the MICEX 10 index, should be in the list of instruments in the properties of the strategy.

Technical aspects

The tradematic launches a script for each of the instruments specified in the strategy properties. This means that in the pre class='prettyprint' we will have to check which instrument the script is currently running on, and: - if this is the MICEX index 10, then we need to exit, because we will not do anything about the index itself - if it is not the MICEX 10 index (i.e. one of the securities included in it), then we need to get the MICEX 10 index, analyze it, and if there is a signal, open / close a position on the current instrument.

To obtain an external instrument (in this case, the MICEX10 index), i.e. for a tool that is not currently the current one, the Script.GetExternalSymbol (string symbol, bool synchronize) function is used.

We need to get an external instrument named "MICEX 10", and based on it, build indicators and check conditions:

// Get the MICEX index10
Symbol micex10 = GetExternalSymbol("MICEX 10", true);

Drawing on charts and validation

We need to display the MICEX 10 index on all stock charts.

Create a new panel on the chart and display the SMA indicators built on the basis of the index on it:

// Render
ChartPane micexPane = CreatePane(50, false);
PlotSymbol(micexPane, micex10, Color.Green, Color.Red);
PlotSeries(micexPane, SMA.Series(micex10.Close, parameter0.ValueInt), Color.Red, LineStyle.Solid, 1);
PlotSeries(micexPane, SMA.Series(micex10.Close, parameter1.ValueInt), Color.Red, LineStyle.Solid, 1);
Do not forget to add a check to the Execute () function so that signals are not issued by the index itself:
// Check
if (Symbol.SymbolName == "MICEX 10")
{
	return;
}
In addition, the list of strategy instruments, in addition to the list of the MICEX10 index (it is given above), must include the index itself.

Final pre class='prettyprint'

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 void Execute()
		{
			// Check         
			if (Symbol.SymbolName == "MICEX 10")
			{
				return;
			}

			// Get the MICEX10 index       
			Symbol micex10 = GetExternalSymbol("MICEX 10", true);

			// Render         
			ChartPane micexPane = CreatePane(50, false);
			PlotSymbol(micexPane, micex10, Color.Green, Color.Red);
			PlotSeries(micexPane, SMA.Series(micex10.Close, parameter0.ValueInt), Color.Red, LineStyle.Solid, 1);
			PlotSeries(micexPane, SMA.Series(micex10.Close, parameter1.ValueInt), Color.Red, LineStyle.Solid, 1);
			
			// Initialization   
			
			// Main loop         
			for (int bar = 9; bar < micex10.Count; bar++)
			{
				if (IsLastPositionActive)
				{
					if (CrossUnder(bar, micex10.Close, SMA.Series(micex10.Close, parameter1.ValueInt)))
					{
						SellAtClose(bar, LastPosition, "");
					}
				}
				else
				{
					if (CrossOver(bar, micex10.Close, SMA.Series(micex10.Close, parameter0.ValueInt)))
					{
						BuyAtClose(bar, "");
					}
				}
			}
		}
	}
}
This website uses cookies. By continuing to use this website, you consent to our Privacy Policy. OK