1 #region Using statements 2 using System; 3 using System.Drawing; 4 using System.Collections.Generic; 5 using System.Reflection; 6 using RightEdge.Common; 7 using RightEdge.Common.ChartObjects; 8 using RightEdge.Indicators; 9 #endregion 10 11 #region System class 12 public class MySystem : MySystemBase 13 { 14 public override void Startup() 15 { 16 // Perform initialization or set system wide options here 17 18 } 19 20 public override void Shutdown() 21 { 22 foreach (var ss in SymbolScripts) 23 { 24 ss.Shutdown(); 25 } 26 } 27 } 28 #endregion 29 30 public class MySymbolScript : MySymbolScriptBase 31 { 32 Dictionary<Frequency, FrequencyScript> _frequencyScripts = 33 new Dictionary<Frequency, FrequencyScript>(); 34 35 public override void Startup() 36 { 37 foreach (TimeSpan span in new [] 38 { TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(15), TimeSpan.FromMinutes(30) }) 39 { 40 Frequency freq = GetFrequency(span); 41 FrequencyScript script = new FrequencyScript(this, freq); 42 _frequencyScripts[freq] = script; 43 44 script.Startup(); 45 46 // Register indicators in frequency script 47 SystemData.IndicatorManager.RegisterMembers(script, Symbol); 48 49 // Set indicators to correct frequency 50 SetIndicatorFrequencies(script); 51 52 // Wire up new bar and tick events 53 freq.NewBar += (sender, args) => script.NewBar(); 54 freq.NewTick += (sender, args) => script.NewTick(args.PartialBar, args.Tick); 55 56 } 57 } 58 59 private void SetIndicatorFrequencies(FrequencyScript script) 60 { 61 // By default, the indicators registered in the frequency script will 62 // use the system frequency, so we need to find them (using reflection) 63 // and set the frequency to the correct value. 64 Type type = script.GetType(); 65 FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); 66 foreach (FieldInfo field in fields) 67 { 68 if (typeof(ISeries).IsAssignableFrom(field.FieldType)) 69 { 70 // Found an ISeries 71 ISeries series = (ISeries) field.GetValue(script); 72 SystemData.IndicatorManager.SetFrequency(series, script.Frequency); 73 } 74 } 75 76 } 77 78 public override void NewBar() 79 { 80 81 } 82 83 public override void OrderFilled(Position position, Trade trade) 84 { 85 // This method is called when an order is filled 86 87 } 88 89 public override void OrderCancelled(Position position, Order order, string information) 90 { 91 // This method is called when an order is cancelled or rejected 92 93 } 94 95 public void Shutdown() 96 { 97 OutputMessage("Total bars: " + Bars.Count); 98 } 99 } 100 101 public class FrequencyScript 102 { 103 readonly MySymbolScript _symbolScript; 104 readonly Frequency _frequency; 105 public FrequencyScript(MySymbolScript symbolScript, Frequency frequency) 106 { 107 _symbolScript = symbolScript; 108 _frequency = frequency; 109 } 110 111 public Frequency Frequency { get { return _frequency; } } 112 public MySymbolScript SymbolScript { get { return _symbolScript; } } 113 public SystemData SystemData { get { return _symbolScript.SystemData; } } 114 115 public RList<BarData> Bars { get { return Frequency.Bars; } } 116 117 public ISeries Open { get { return Frequency.Open; } } 118 public ISeries Low { get { return Frequency.Low; } } 119 public ISeries High { get { return Frequency.High; } } 120 public ISeries Close { get { return Frequency.Close; } } 121 public ISeries Volume { get { return Frequency.Volume; } } 122 123 public BollingerBandLower BBL; 124 125 public void Startup() 126 { 127 BBL = new BollingerBandLower(50, 2, Close); 128 } 129 130 public void NewBar() 131 { 132 if (!SystemData.InLeadBars && !double.IsNaN(BBL.Current)) 133 { 134 PositionSettings settings = new PositionSettings(); 135 settings.PositionType = PositionType.Long; 136 settings.OrderType = OrderType.Limit; 137 settings.LimitPrice = BBL.Current; 138 settings.Description = string.Format("{0} BBL hit", Frequency.FrequencySettings.ToString()); 139 140 SymbolScript.OpenPosition(settings); 141 142 } 143 } 144 145 public void NewTick(BarData partialBar, TickData tick) 146 { 147 148 } 149 } 150