news robot

Created at 08 Apr 2014, 17:10
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
ZE

zeevalgo

Joined 16.03.2014

news robot
08 Apr 2014, 17:10


hello

i want to take the news robot see code and make it work without the time parameter

just click and work

how do i reduce the code please

thankyou

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

using System;

using cAlgo.API;

using cAlgo.API.Indicators;

using cAlgo.Indicators;

 

namespace cAlgo.Robots

{

    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]

    public class NewsRobot : Robot

    {

        [Parameter("News Day (1-5)", DefaultValue = 1, MinValue = 1, MaxValue = 5)]

        public int NewsDay { get; set; }

 

        [Parameter("News Hour", DefaultValue = 14, MinValue = 0, MaxValue = 23)]

        public int NewsHour { get; set; }

 

        [Parameter("News Minute", DefaultValue = 30, MinValue = 0, MaxValue = 59)]

        public int NewsMinute { get; set; }

 

        [Parameter("Pips away", DefaultValue = 10)]

        public int PipsAway { get; set; }

 

        [Parameter("Take Profit", DefaultValue = 50)]

        public int TakeProfit { get; set; }

 

        [Parameter("Stop Loss", DefaultValue = 10)]

        public int StopLoss { get; set; }

 

        [Parameter("Volume", DefaultValue = 100000, MinValue = 10000)]

        public int Volume { get; set; }

 

        [Parameter("Seconds Before", DefaultValue = 10, MinValue = 1)]

        public int SecondsBefore { get; set; }

 

        [Parameter("Seconds Timeout", DefaultValue = 10, MinValue = 1)]

        public int SecondsTimeout { get; set; }

 

        [Parameter("One Cancels Other")]

        public bool Oco { get; set; }

 

        private bool _ordersCreated;

        private const string Label = "News Robot";

 

        protected override void OnStart()

        {

            MarketData.GetMarketDepth(Symbol).Updated += PlaceStopOrders;

            Positions.Opened += OnPositionOpened;

        }

 

        protected override void OnTick()

        {

            PlaceStopOrders();

        }

 

        void PlaceStopOrders()

        {

            if ((int)Server.Time.DayOfWeek == NewsDay && !_ordersCreated)

            {

                var triggerTime = new DateTime(Server.Time.Year, Server.Time.Month, Server.Time.Day, NewsHour, NewsMinute, 0);

 

                if (Server.Time <= triggerTime && (triggerTime - Server.Time).TotalSeconds <= SecondsBefore)

                {

                    _ordersCreated = true;

                    var expirationTime = triggerTime.AddSeconds(SecondsTimeout);

 

                    var sellOrderTargetPrice = Symbol.Bid - PipsAway * Symbol.PipSize;

                    PlaceStopOrder(TradeType.Sell, Symbol, Volume, sellOrderTargetPrice, Label, StopLoss, TakeProfit, expirationTime);

 

                    var buyOrderTargetPrice = Symbol.Ask + PipsAway * Symbol.PipSize;

                    PlaceStopOrder(TradeType.Buy, Symbol, Volume, buyOrderTargetPrice, Label, StopLoss, TakeProfit, expirationTime);

                }

            }

        }

 

        private void OnPositionOpened(PositionOpenedEventArgs args)

        {

            var position = args.Position;

            if (position.Label == Label && position.SymbolCode == Symbol.Code)

            {

                if (Oco)

                {

                    foreach (var order in PendingOrders)

                    {

                        if (order.Label == Label && order.SymbolCode == Symbol.Code)

                        {

                            CancelPendingOrderAsync(order);

                        }

                    }

                }

                Stop();

            }

        }

    }

}


@zeevalgo