Loading...
Searching...
No Matches
Strategies

Development of a strategy

The Strategy API allows you to create strategies based on the Indicators API to automate your trading process.

In order to develop a strategy it is necessary to:

  1. Create a class library project for .NET 10 (net10.0-windows) in Visual Studio or another IDE. A ready-to-use project file is shown below.
  2. Add references to ATAS.Strategies.dll and the other platform assemblies, which are in the directory with the installed program (see the project file below).
  3. Create a strategy class and inherit it from ChartStrategy class.
  4. Realize the logic of the strategy operation.
  5. Compile the library.
  6. Place the resulting dll file into the strategies directory:
    • APPDATA%\ATAS\Strategies for the classic ATAS platform;
    • APPDATA%\ATAS X\Strategies for ATAS X on Windows;
    • ~/Library/Application Support/ATAS/Strategies for ATAS X on macOS.
  7. Click on the blinking button for the changes in the list of strategies to take effect.
Updating strategy list

A project file for a strategy library (the platform is installed in the default folder):

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<LangVersion>latest</LangVersion>
<!-- Folder of the installed platform; change it if the platform is installed elsewhere. -->
<ATAS_BASE>C:\Program Files (x86)\ATAS Platform</ATAS_BASE>
</PropertyGroup>
<!-- Platform assemblies are referenced from the installed platform.
Private=false keeps them out of the build output: copy only your own DLL to the Strategies folder. -->
<ItemGroup>
<Reference Include="ATAS.Strategies">
<HintPath>$(ATAS_BASE)\ATAS.Strategies.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="ATAS.Indicators">
<HintPath>$(ATAS_BASE)\ATAS.Indicators.dll</HintPath>
<Private>false</Private>
</Reference>
<!-- Built-in indicators (SMA, EMA and others); needed only if the strategy uses them. -->
<Reference Include="ATAS.Indicators.Technical">
<HintPath>$(ATAS_BASE)\ATAS.Indicators.Technical.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="ATAS.DataFeedsCore">
<HintPath>$(ATAS_BASE)\ATAS.DataFeedsCore.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="OFT.Attributes">
<HintPath>$(ATAS_BASE)\OFT.Attributes.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="OFT.Rendering">
<HintPath>$(ATAS_BASE)\OFT.Rendering.dll</HintPath>
<Private>false</Private>
</Reference>
<Reference Include="Utils.Common">
<HintPath>$(ATAS_BASE)\Utils.Common.dll</HintPath>
<Private>false</Private>
</Reference>
</ItemGroup>
</Project>

This project file targets the classic ATAS platform. How to build a library for ATAS X is described in the «Developing indicators for ATAS X» article.


Chart Strategies

Functionality of Chart Strategies allows receiving and processing the whole set of data available in indicators and performing trading operations on the basis of these data.
The ChartStrategy class is inherited from the Indicator class. Thus, strategies have the full functionality of indicators.

In addition to the functionality of indicators, strategies have additional properties and methods responsible for trading functionality:

Properties:

Main strategy methods:

  • OpenOrderAsync - method of opening a new order
  • ModifyOrderAsync - method of modifying an order
  • CancelOrderAsync - method of cancelling an order
  • OpenOrder, ModifyOrder, CancelOrder - obsolete synchronous versions of the methods above; use the asynchronous methods instead (see the «Managing orders in trading» article)
  • ShrinkPrice (protected) - drops the part of the transferred price that is not a multiple of the trading instrument tick size. The price is not rounded to the nearest tick: for a positive price the result is the nearest tick below.
  • RaiseShowNotification (protected) - method, allowing visualization of notifications in the platform

Strategy virtual methods, which, if necessary, have to be redefined in the created strategy:

  • OnStarted - is called when a strategy is started
  • OnSuspended - is called when a strategy is suspended by the SuspendAsync method, after OnStopping and before OnStopped. The platform itself does not suspend chart strategies: when a chart with a strategy is closed, the strategy is stopped.
  • OnStopping - is called before stopping a strategy. The strategy is still started at this moment, so orders placed from this method (for example, to close the position) are still sent.
  • OnStopped - is called when a strategy has been stopped
  • OnCurrentPositionChanged - is called when changing the strategy current position
  • CanProcess - returns whether it is possible to trade at this moment. By default, the method checks whether a strategy has been started and the most recent bar is processed at this moment.

If you override the handlers of the indicator in a strategy, call their base implementation: the strategy calculates its position and PnL in OnNewMyTrade and OnBestBidAskChanged, and without the base call these values are not updated.

Strategy lifecycle.

  • A strategy does not start if no instrument or portfolio is selected on the chart; an error notification is shown instead.
  • Orders are sent only while the strategy is started: in any other state the order methods return without sending anything.
  • The platform stops the strategy itself when the instrument, the time frame or the portfolio of the chart is changed, when the chart history is reloaded and when the chart is closed. When the connection is lost, the strategy is stopped too, and it is started again automatically when the same instrument and portfolio become available.
  • When a strategy is stopped, its position and its lists of orders and trades are reset.

Strategy positions.

Every strategy holds its position inside. This position could differ from the general position on the account.
The account general position could be received through the TradingManager.Position property.
The strategy internal position could be received through the CurrentPosition and AveragePrice properties.

Important! When a strategy is stopped, its internal position is reset to zero. It is extremely desirable to redefine the OnStopping method and realize the logic of closing internal positions and cancelling the posted orders in it.

You can find the chart strategy in the list of strategies. For this it is necessary:

  1. open the context menu with the right mouse button,
  2. click on the strategy list icon,
  3. in the opened window of the list of strategies, select the strategy you need,
  4. click the Add button.
  5. you can enable / disable the strategy by pressing the control button.
Loading Chart strategy

Additional order options

When developing a strategy, you can set specific order options using the TradingManager strategy property.

Reduced only

Reduced only orders are a risk control mechanism that ensures that when executed, they will only reduce an existing position, preventing any unintended increase in exposure. This feature is particularly important in markets where precise control over positions is crucial for effective risk management.

private void TrySetReduceOnly(Order order)
{
var flags = TradingManager?
.GetSecurityTradingOptions()?
.CreateExtendedOptions(order.Type);
if (flags is not IOrderOptionReduceOnly ro)
return;
ro.ReduceOnly = true;
order.ExtendedOptions = flags;
}

Post only

When you place a Post only order, you are telling the exchange that you want your order to be treated as a maker order only. In other words, you want to ensure that your order doesn't execute as a taker order, which would result in you paying trading fees. If your Post only order would execute immediately as a taker order, it will be canceled instead.

private void TrySetPostOnly(Order order)
{
var flags = TradingManager?
.GetSecurityTradingOptions()?
.CreateExtendedOptions(order.Type);
if (flags is not IOrderOptionPostOnly po)
return;
po.PostOnly = true;
order.ExtendedOptions = flags;
}

Close on trigger

A Close on trigger order is a type of trading order that is executed when a specified trigger condition is met in the market. It's often used for risk management and to automatically close positions or execute specific actions when a certain price level is reached.

private void TrySetCloseOnTrigger(Order order)
{
var flags = TradingManager?
.GetSecurityTradingOptions()?
.CreateExtendedOptions(order.Type);
if (flags is not IOrderOptionCloseOnTrigger ct)
return;
ct.CloseOnTrigger = true;
order.ExtendedOptions = flags;
}