In C #, how can we achieve the same style of expression?

Source: Internet
Author: User
The following is a macd formula.

DIFF: EMA (close, short)-EMA (close, long );
DEA: EMA (diff, M );

Macd: 2 * (DIFF-DEA), colorstick;

 

Its variables can be directly added and subtracted * division, but we need to know that the close in it is actually an array type. We can see from the summary analysis. In C #, we can use Operator Overloading to add and subtract arrays or perform operations. But the array is the built-in type of the system. We cannot reload it with operators, so we need to construct a type ourselves.

In the qianfa stock automatic trading software, I created a floatlist type,

Public class floatlist: List <float>
{
Public floatlist ()
{}

Public floatlist (INT length): Base (length)
{}

Public floatlist (ienumerable <float> input)
: Base (input)
{}

Public static floatlist operator + (floatlist input, float m)
{
Floatlist list = new floatlist ();
For (INT I = 0; I <input. Count; I ++)
{
List. Add (input [I] + M );
}
Return list;
}

... Other omissions
}

Because the data in the stock is actually float type.

 

In this way, you can reload operators on it.

Next, you need to implement a global function, which is implemented through the parent class. Other methods can be used for scripting.

In this way, you only need to let your class inherit the basefunctions class.

Let's look at an example.

Public class macdhelper: basefunctions
{
/// <Summary>
/// DIFF: EMA (close, short)-EMA (close, long );
/// DEA: EMA (diff, M );
/// Macd: 2 * (DIFF-DEA), colorstick;
/// </Summary>
/// <Param name = "list"> </param>
/// <Param name = "? "> </Param>
/// <Returns> </returns>
Public macdinfo calculate (list <stocklog> list, int L, int S, int m)
{
This. stocklogs = List;
VaR diff = EMA (close, S)-EMA (close, L );
VaR DEA = EMA (diff, M );
VaR macd = (diff-DEA) * 2;


Macdinfo info = new macdinfo ();
Info. DEA = DEA;
Info. Diff = diff;
Info. macd = macd;

Return Info;
}

}

 

If this is something that inspires you to charge your http://lanhaifeng.taobao.com/

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.