Difference |
Difference, Distance, Product, Ratio, Sum, WeightedSum - this group of indicators calculates the following functions of two series of values:
To implement the described functions in your strategy you can use the following constructors:
WeightedSum- creates weighted sum indicator with default weights equal to 1 for each of two series
WeightedSum(Double, Double)- allows setting weight for each series
The value of the indicators is updated by two series type of method Add():
Use Result property to get the current value.
The strategy defines the base instrument to compute functions with other instruments on each BarClose event. The resulting charts are displayed for each instrument involved.
1public partial class PortfolioExecutor : PortfolioExecutorBase 2{ 3 public string baseSymbol; 4 public double basePrice; 5 6 public override void OnInit() 7 { 8 //Call Instruments OnInit 9 base.OnInit(); 10 11 //define the base symbol for calculating functions with //others 12 baseSymbol = Slice[0].Symbol.ToString(); 13 foreach(InstrumentExecutor instr in Slice) 14 { 15 instr.InitLines(baseSymbol); 16 } 17 } 18 public override void OnBarClose(Context context) 19 { 20 //get the base price to compute correlations with 21 basePrice = Slice[0].CurrentPrice; 22 //modify indicators 23 foreach(InstrumentExecutor instr in Slice) 24 { 25 instr.ModifyFunctionValues(basePrice); 26 } 27 28 //Call Instruments OnBarClose 29 base.OnBarClose(context); 30 } 31} 32 33public partial class InstrumentExecutor : InstrumentExecutorBase 34{ 35- #region LocalVariables 36 //indicators 37 Difference difference; 38 Distance distance; 39 Product product; 40 Ratio ratio; 41 Sum sum; 42 WeightedSum weightedSum; 43 44 //chart lines 45 Line differenceLine, distanceLine, productLine, ratioLine, sumLine, weightedSumLine; 46 47 #endregion 48 49- #region BuildingBlocks 50 //init lines: symbol is the base symbol which is in pair with this //instrument 51 public void InitLines(string symbol) 52 { 53 //init chart lines 54 differenceLine = Charting.CreateLine(symbol+"Diff", "Instrument", Symbol, Pens.Black, LineStyle.Line, Charting.PriceChart, 2); 55 56 distanceLine = Charting.CreateLine(symbol+"Dist", "Instrument", Symbol, Pens.Blue, LineStyle.Line, Charting.PriceChart, 3); 57 58 productLine = Charting.CreateLine(symbol+"Prod", "Instrument", Symbol, Pens.Green, LineStyle.Line, Charting.PriceChart, 4); 59 60 ratioLine = Charting.CreateLine(symbol+"Ratio", "Instrument", Symbol, Pens.Red, LineStyle.Line, Charting.PriceChart, 5); 61 62 sumLine = Charting.CreateLine(symbol+"Sum", "Instrument", Symbol, Pens.Brown, LineStyle.Line, Charting.PriceChart, 6); 63 64 weightedSumLine = Charting.CreateLine(symbol+"WSum", "Instrument", Symbol, Pens.DarkGreen, LineStyle.Line, Charting.PriceChart, 6); 65 } 66 public void ModifyFunctionValues(double price) 67 { 68 difference.Add(CurrentPrice, price); 69 distance.Add(CurrentPrice, price); 70 product.Add(CurrentPrice, price); 71 ratio.Add(CurrentPrice, price); 72 sum.Add(CurrentPrice, price); 73 weightedSum.Add(CurrentPrice, price); 74 } 75 #endregion 76 77- #region Events 78 public override void OnInit() 79 { 80 //init indicators 81 difference = new Difference (); 82 distance = new Distance (); 83 product = new Product (); 84 ratio = new Ratio (); 85 sum = new Sum (); 86 weightedSum = new WeightedSum (1.2, 0.7); 87 } 88 public override void OnBarClose() 89 { 90 //chart 91 differenceLine.Draw(CurrentTime, difference.Result); 92 distanceLine.Draw(CurrentTime, distance.Result); 93 productLine.Draw(CurrentTime, product.Result); 94 ratioLine.Draw(CurrentTime, ratio.Result); 95 sumLine.Draw(CurrentTime, sum.Result); 96 weightedSumLine.Draw(CurrentTime, weightedSum.Result); 97 } 98 #endregion 99}