1 #region Using statements 2 using System; 3 using System.Drawing; 4 using System.Collections.Generic; 5 using System.Linq; 6 using RightEdge.Common; 7 using RightEdge.Common.ChartObjects; 8 using RightEdge.Indicators; 9 #endregion 10 11 public class MySystem : MySystemBase 12 { 13 public override void Startup() 14 { 15 // NOTE: You also need to enable "Allow Position Reversal" in the paper trader settings 16 PositionManager.PositionOverfilledAction = PositionAction.DoNothing; 17 } 18 } 19 20 public class MySymbolScript : MySymbolScriptBase 21 { 22 public SMA FlipPoint; 23 24 public override void Startup() 25 { 26 FlipPoint = new SMA((int) SystemParameters["SMAPeriod"], Close); 27 } 28 29 public override void NewBar() 30 { 31 if (double.IsNaN(FlipPoint.Current)) 32 { 33 // Not enough data yet to have an indicator value, so don't trade 34 return; 35 } 36 37 Position pos = OpenPositions.SingleOrDefault(); 38 if (pos == null) 39 { 40 PositionSettings settings = new PositionSettings(); 41 if (Close.Current < FlipPoint.Current) 42 { 43 // Go short 44 settings.PositionType = PositionType.Short; 45 } 46 else 47 { 48 // Go long 49 settings.PositionType = PositionType.Long; 50 } 51 52 settings.OrderType = OrderType.Market; 53 54 pos = OpenPosition(settings); 55 if (pos.Error != null) 56 { 57 OutputWarning(pos.Error); 58 } 59 } 60 else 61 { 62 OrderSettings stopSettings = new OrderSettings(); 63 stopSettings.OrderType = OrderType.Stop; 64 stopSettings.StopPrice = FlipPoint.Current; 65 stopSettings.Size = pos.CurrentSize * 2; 66 if (pos.Type == PositionType.Short) 67 { 68 stopSettings.TransactionType = TransactionType.Cover; 69 } 70 else 71 { 72 stopSettings.TransactionType = TransactionType.Sell; 73 } 74 75 Order order = pos.SubmitOrder(stopSettings); 76 if (order.Error != null) 77 { 78 OutputWarning(order.Error); 79 } 80 } 81 82 83 } 84 85 public override void OrderFilled(Position position, Trade trade) 86 { 87 // This method is called when an order is filled 88 89 } 90 91 public override void OrderCancelled(Position position, Order order, string information) 92 //public override void OrderCancelled(Position position, Order order, OrderReasonCode reasonCode, string information) 93 { 94 // This method is called when an order is cancelled or rejected 95 if (!order.CancelPending) 96 { 97 OutputWarning("Unexpected order cancel: " + order.ToString() + " " + information); 98 } 99 } 100 } 101