Points of extendability

You can extend TradePlatform.NET functionality to achieve more complex scenarios. Basically you have following opportunities:

  • Extend .NET ExpertAdvisor with custom functions which can be called from MQL.
  • Extend MQL SDK by adding MQL functions which are available for .NET ExpertAdvisor. These can be standard MQL function as well as custom functions.
  • Use higher abstraction than .NET ExpertAdvisor to implement completely custom communication scenarios.

Custom .NET ExpertAdvisor function

To make custom function available from MQL you need to do 2 important steps:

  1. Add custom function to your .NET ExpertAdvisor or intermediate abstraction and include one in resolver:

public abstract class CustomExpertAdvisor : ExpertAdvisor
{
    public override string ResolveMethod(string methodName, List<string> parameters)
    {
       switch (methodName)
       {
          case "CustomFunction":
             CustomFunctionName(parameters[0], int.Parse(parameters[1]));
             return "";
          default:
             return base.ResolveMethod(methodName, parameters);
       }
    }

    protected void CustomFunction(string prm1, int prm2)
    {
            // some code
    }
}

2. Add function on MQL side which will be access point for MQL code:

void API_CustomFunction(string prm1, int prm2)
{
	System_NET_CallFunction(System_NET_ApplicationName, "CustomFunction", prm1+ "|" + prm2);
}

Similar example can be found in Model Project.

Custom MQL function for .NET ExpertAdvisor

To make MQL function available for .NET ExpertAdvisor you have complete 2 important steps:

 1. Create extension method for MqlExpert class which will be accessible in .NET ExpertAdvisor:

 public static bool CutomMQLFunction(this MqlExpert script, string prm1, int prm2)
 {
     string retrunValue = script.CallMql("CutomMQLFunction", prm1, prm2);
     return Convertor.ToBoolean(retrunValue);
 }

 2. Add function resolver on MQL side in System_NET_MQL_Custom function:

string System_NET_MQL_Custom(string message[])
{
   if(message[1] == "CustomMQLFunction")
      return(CustomMQLFunction(message[2], StrToInteger(message[3])));
      
   return("###NORESULT###");
}

Similar example can be found in Model Project.

Custom MQL script handler

While implementing .NET ExpertAdvisor with TradePlatform.MT4.SDK.ExpertAdvisor abstraction you must have Init(), Start(), DeInit() methods implementation as this level of abstraction meant as standard MQL script behavior. Nevertheless, you can use higher level class to create your own abstractions. Sample below demonstrates how such abstraction can be done:

public abstract class CustomBase : MqlExpert
{
   protected abstract int CustomMethod1(string prm);
   protected abstract int CustomMethod2(string prm);

   public override string ResolveMethod(string methodName, List<string> parameters)
   {
       switch (methodName)
       {
           case "CustomMethod1":
               return CustomMethod1(parameters[0]).ToString();
           case "CustomMethod2":
               return CustomMethod2(parameters[0]).ToString();
           default:
               throw new System.Exception("No method found");
       }
   }
}

MQL side functions to access .NET side have same look as usual:

int API_CustomMethod1(string prm)
{
    string result = System_NET_CallFunction(System_NET_ApplicationName, "CustomMethod1", prm);
    return(StrToInteger(result));
}

int API_CustomMethod2(string prm)
{
    string result = System_NET_CallFunction(System_NET_ApplicationName, "CustomMethod2", prm);
    return(StrToInteger(result));
}

Similar example can be found under Source Control

Last edited Oct 29, 2012 at 9:38 PM by vladkalos, version 2

Comments

No comments yet.