-
Notifications
You must be signed in to change notification settings - Fork 1
/
chasm.cs
184 lines (162 loc) · 7.5 KB
/
chasm.cs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
///-----------------------------------------------------------------
/// Class: Chasm BOT
/// Description: cAlgo trading bot
/// Author: Antonio Cosentino
/// Version: 1.0
/// Updated: 05/09/2017
///-----------------------------------------------------------------
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;
namespace cAlgo.indicators
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Chasm : Robot
{
string version_number = "1.0";
// Declarations
private int currenthour;
private int currentminute;
private string stringdate;
private bool is_position_open;
private string scenario;
private double today_open;
private double today_high;
private double today_low;
private double bid_price;
private double ask_price;
private double yesterday_close;
private double yesterday_high;
private double yesterday_low;
private int kindex;
///
[Parameter("LONG Trailing Stop (%)", DefaultValue = 8, MinValue = 1, MaxValue = 50)]
public double long_stop { get; set; }
[Parameter("SHORT Trailing Stop (%)", DefaultValue = 4, MinValue = 1, MaxValue = 50)]
public double short_stop { get; set; }
[Parameter("Diff Ticks", DefaultValue = 2, MinValue = 1, MaxValue = 1000)]
public double diffticks { get; set; }
[Parameter("GAP Up Long?", DefaultValue = true)]
public bool gap_up_mode { get; set; }
[Parameter("GAP Down Long?", DefaultValue = false)]
public bool gap_down_mode { get; set; }
[Parameter("N. Contracts", DefaultValue = 100, MinValue = 1, MaxValue = 100000)]
public int ncontracts { get; set; }
protected override void OnStart()
{
is_position_open = false;
kindex = 0;
Positions.Closed += PositionsOnClosed;
Print("Chasm {0} Started", version_number);
Print("Server time is {0}", Server.Time.AddHours(0));
}
protected override void OnBar()
{
Print("");
kindex = MarketSeries.Close.Count - 1;
today_open = MarketSeries.Open[kindex];
yesterday_close = MarketSeries.Close[kindex - 1];
yesterday_high = MarketSeries.High[kindex - 1];
yesterday_low = MarketSeries.Low[kindex - 1];
if (today_open > yesterday_close)
{
scenario = "GAPUP";
}
else if (today_open < yesterday_close)
{
scenario = "GAPDOWN";
}
else
{
scenario = null;
}
if (scenario != null)
{
Print("Scenario is: {0}", scenario);
Timer.Start(3600);
}
}
protected override void OnTimer()
{
Timer.Stop();
today_high = MarketSeries.High[kindex];
today_low = MarketSeries.Low[kindex];
if (!is_position_open)
{
if (scenario == "GAPUP" && gap_up_mode)
{
double stoploss_price = ask_price - (ask_price * (long_stop / 100));
double stoploss_pips = (ask_price - stoploss_price) / Symbol.PipSize;
double takeprofit_pips = ((today_high / Symbol.PipSize) + diffticks) - (ask_price / Symbol.PipSize);
double takeprofit_price = ask_price + (takeprofit_pips * Symbol.PipSize);
Print("Buy at {0} with {1} takeprofit and {2} stop loss", ask_price, takeprofit_price, stoploss_price);
ExecuteMarketOrder(TradeType.Buy, Symbol, ncontracts, "Chasm", stoploss_pips, takeprofit_pips);
is_position_open = true;
}
else if (scenario == "GAPUP" && !gap_up_mode)
{
double stoploss_price = bid_price + (bid_price * (short_stop / 100));
double stoploss_pips = (stoploss_price - bid_price) / Symbol.PipSize;
double takeprofit_pips = (bid_price / Symbol.PipSize) - ((today_low / Symbol.PipSize) - diffticks);
double takeprofit_price = bid_price - (takeprofit_pips * Symbol.PipSize);
if (takeprofit_price < bid_price)
{
Print("Sell at {0} with {1} takeprofit and {2} stop loss", ask_price, takeprofit_price, stoploss_price);
ExecuteMarketOrder(TradeType.Sell, Symbol, ncontracts, "Chasm", stoploss_pips, takeprofit_pips);
is_position_open = true;
}
}
else if (scenario == "GAPDOWN" && gap_down_mode)
{
double stoploss_price = ask_price - (ask_price * (long_stop / 100));
double stoploss_pips = (ask_price - stoploss_price) / Symbol.PipSize;
//double takeprofit_pips = ((yesterday_low / Symbol.PipSize) + diffticks) - (ask_price / Symbol.PipSize); // trying instead with today low
double takeprofit_pips = ((today_low / Symbol.PipSize) + diffticks) - (ask_price / Symbol.PipSize);
double takeprofit_price = ask_price + (takeprofit_pips * Symbol.PipSize);
if (takeprofit_price > ask_price)
{
Print("Buy at {0} with {1} takeprofit and {2} stop loss", ask_price, takeprofit_price, stoploss_price);
ExecuteMarketOrder(TradeType.Buy, Symbol, ncontracts, "Chasm", stoploss_pips, takeprofit_pips);
is_position_open = true;
}
}
else if (scenario == "GAPDOWN" && !gap_down_mode)
{
double stoploss_price = bid_price + (bid_price * (short_stop / 100));
double stoploss_pips = (stoploss_price - bid_price) / Symbol.PipSize;
double takeprofit_pips = (bid_price / Symbol.PipSize) - ((today_low / Symbol.PipSize) - diffticks);
double takeprofit_price = bid_price - (takeprofit_pips * Symbol.PipSize);
Print("Sell at {0} with {1} takeprofit and {2} stop loss", ask_price, takeprofit_price, stoploss_price);
ExecuteMarketOrder(TradeType.Sell, Symbol, ncontracts, "Chasm", stoploss_pips, takeprofit_pips);
is_position_open = true;
}
}
}
protected override void OnTick()
{
// Time Vars
stringdate = Server.Time.AddHours(0).ToString("HH:mm");
currenthour = int.Parse(stringdate.Substring(0, 2));
currenthour = Convert.ToInt32(stringdate.Substring(0, 2));
currentminute = int.Parse(stringdate.Substring(3, 2));
currentminute = Convert.ToInt32(stringdate.Substring(3, 2));
///
bid_price = Symbol.Bid;
ask_price = Symbol.Ask;
}
private void PositionsOnClosed(PositionClosedEventArgs args)
{
var pos = args.Position;
Print("Position closed with €{0} profit", pos.GrossProfit);
is_position_open = false;
}
protected override void OnStop()
{
Print("Chasm Stopped");
}
}
}