Skip to content

How to Develop Your Own Expert

dwmcqueen edited this page Sep 9, 2013 · 3 revisions

Introduction

Experts are autonomous systems designed to operate by its own. They don't need human interaction to take decisions about when to open and close operations.

This tutorial describes how to create a simple expert by inheriting from PlatformManagedExpert class. This is a good starting point and allows for a natural evolution to more the advanced solutions that the platform provides.

OFxP has an internal script engine. Experts are programmed in C#, so it will help you if you have some C#/.Net background.

If you want to develop your experts in VB.NET or anyother language supported by .NET framework, you can still do it using Visual Studio and importing your assembly in OFxP (see section "Expert development using Visual Studio 2008" below).

Inside OFxP, an Expert is an item that can operate on many pairs (EURUSD for ex.), on many time frames (60M for ex.).

Creating an expert

Creating an expert is rather easy, just open Experts Manager (Components->Platform Components->Create Experts Manager) and click on New Expert and a new editor will be opened and populated with a sample expert based on RSI indicator.

If you want to edit the code, in the code editor interface, just click Edit button on the tool strip menu.

Experts Manager

` using System.Collections.Generic; using System.Text; using System.Threading; using CommonSupport; using CommonFinancial; using ForexPlatform;

///

/// Make sure you inherit PlatformManagedExpert with your expert. /// [Serializable] [UserFriendlyName("Default Managed Expert")] public class MyTradeExpert : PlatformManagedExpert { int _buyValue = 60; public int BuyValue { get { return _buyValue; } set { _buyValue = value; } }
int _sellValue = 35;
public int SellValue
{
    get { return _sellValue; }
    set { _sellValue = value; }
}

/// <summary>
/// 
/// </summary>
public MyTradeExpert(IExpertSessionManager sessionManager, string name)
    : base(sessionManager, name)
{
}

/// <summary>
/// Handle startup actions.
/// Return false if you fail to initialize.
/// </summary>
protected override bool OnStart()
{
    // Here is an example how to use an indicator.
    // Create / Acquire indicator.
    Indicator indicator = this.GetIndicator("Rsi");

    // Change an input parameter.
    indicator.Parameters["optInTimePeriod"] = 20;

    // Make sure values are updated with new parameter.
    // This is not mandatory, but needed if you use result values of the 
    // indicator immediately after assigning its parameters.
    indicator.Calculate();

    return true;
}

/// <summary>
/// Handle all actions need to be done on stopping.
/// </summary>
protected override void OnStop()
{
}

/// <summary>
/// Quote data was updated.
/// </summary>
protected override void OnQuoteUpdate(DataProviderUpdateType updateType)
{
    Indicator indicator = this.GetIndicator("Rsi");

    if (indicator.Results.GetValueSetCurrentValue(0).HasValue == false)
    {
        return;
    }

    Trace(indicator.Results.GetValueSetCurrentValue(0).Value.ToString());

    // Get the value of the [0] index result set of this indicator.
    // Each indicator can have many result sets - each represeting a "line" on the chart.
    if (indicator.Results.GetValueSetCurrentValue(0) > BuyValue)
    {// If the Rsi result is above BuyValue, take some action.
        
        if (this.OpenAndPendingOrders.Count == 0)
        {// Open an order if none are already opened.
            this.OpenBuyOrder(1);
        }
    }
    else if (indicator.Results.GetValueSetCurrentValue(0) < SellValue)
    { // If the Rsi result is below SellValue, take some other action.
        
        if (this.OpenAndPendingOrders.Count > 0)
        {// Close the first open order.
            //this.OpenAndPendingOrders[0].Close();
            this.CloseAllOrders();
        }
    }
}

}`

Your expert must use PlatformManagedExpert as base class (you can add an user friendly name to your expert using UserFriendlyName decorator).

Expert Parameters

BuyValue and SellValue are expert parameters. If you make public your expert parameters, you will be able to modify them inside expert management interface.

What you need to implement

Inside your expert class there are four methods you need to implement :

  • Constructor The expert receives the session manager and the expert name in the constructor.

The SessionManager provides the expert with ways to control and access the sessions that are attached to. Each session contains data about a pair and a time frame. You can read more about that in the Architecture Overview page but for now it is all you need to know to develop your expert.

  • OnStart Method You can put you initialization tasks here. The sample code adds RSI indicator to the system, setting the period to 20.

  • OnStop Method OnStop method handle all actions need to be done on stopping.

  • OnQuoteUpdate Method Here is where you put the meat of your expert, the decisions about when to open and close operations goes here. This method is called by the platform each time quotes are updated.

The sample RSI expert provided goes long when RSI value goes above BuyValue and if RSI value goes below SellValue all orders are closed.

Running your expert

Once you have your code complete and compiled without errors, try executing Run Expert->Run StandAlone. {SCREENSHOT} A new tab will be opened, like the showed in the picture above, and now you have to initialize your expert adding a new session (click Initialize). This session could be a back testing or live session.

If you are doing a back test, click "Run" to start the expert. On Live session expert starts automatically. Also on back testing sessions you can set execution speed and go in step-by-step mode.

Expert development using Visual Studio

You can also develop your experts using Visual Studio (Express versions are ok). Simply start your project (you can use your preferred language here as long as it is supported by .Net framework) and add 4 references :

  • Arbiter.dll
  • CommonFinancial.dll
  • CommonSupport.dll
  • ForexPlatform.dll

You can include as many experts as you want in your assembly and load them by using "Import Assembly" in the Experts Manager.