I'd like to ask a question about how to deal with dependent variables.
C1=MA(X,10)
C2=MA(C1,10)
C3=C2.Minus(C1)
C4=MA(C3,10)
Final=C4.Minus(C3)
An illustration:Final=C4.Minus(C3), I save its two parameters C4,C3.
C4(MA ok and firstTime in MA)--> C3(MA NotOK,not fstTime)---> C2(MA ok)--->C1(MA ok)
0-------------------> 10--------------> 10+10---->10+10+10=30
I created a class at first to get all the informtion of each line
public class IndicatorInform
{
char[] parenthesis = new []{'(',')'};
char[] equal = new char[] { '=' };
char[] comma = new char[] { ';' };
char[] all = new char [] { '.','<'};
string text=null;
string funcname=null;
string[] args=null;
public void IndicatorInform (string expression, out string text,out string funcName,out string [] args)
{
string [] parts= expression.Split(equal);
text = parts[0];
if( parts[1].Contains(";"))
{
string[] subparts = parts[1].Split(parenthesis);
funcname = subparts[0];
args = subparts[1].Split(comma);
if(args.Count.equal(2))
{
funcarg = args[0];
period = Convert.ToDouble(args[1]);
}
}
else
{
if (parts[1].Contains("Plus"))
funcname = "Plus";
if (parts[1].Contains("Minus"))
funcname = "Minus";
if (parts[1].Contains("Multi"))
funcname = "Multi";
if (parts[1].Contains("Div"))
funcname = "Div";
parts[1] = parts[1].Replace(funcname, "");
args=parts[1].Split(all);
}
}
public double Shifts {get; set;}
public double period { get; set; }
public string Funcname { get; set; }
public string text { get; set; }
public string funcarg { get; set; }
}
And then I created a Dictonary and begin to deal with dependant variables
Dictionary<string,SubIndicator> Dico=new Dictionary<string,SubIndicator> ;
foreach (var line in richTextBox1.Lines)
{
SubIndicator SubInc = new SubIndicator();
Dico.Add(SubInc.text,SubInc);
}
int incre=0;
double tempvalue=0;
foreach( string element in Dico.Keys)
{
string[] tempo=null;
if(Dico[element].text.Contains("Final"))
{
tempo=Dico[element].args;
}
else
{
if(tempo.Contains(Dico[element].text))
{
if(Dico[element].Funcname.Contains("MA") )
{
if (incre.Equals(0))
{tempvalue=Dico[element].period;
incre++;}
else
{
Dico[element].Shifts=tempvalue+Dico[element].period;
tempvalue =Dico[element].Shifts;
}
}
else
{
Dico[element].Shifts=tempvalue;
}
}
}
My algorithm works for the case above.But how to deal a more complicated case such as
C1=MA(X,10)
C2=MA(X,20)
C3=MA(C1,5)
C4=MA(C2,10)
C5=MA(C3,15)
C6=MA(C4,10)
Final=C6.Minus(C5)
C6(fst Time)---->C4--->C2 C5(fst Time)--->C3--> C1
0-->10+10-->10+10+20 0--> 15+5-->15+5+10
Thanks for your help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…