-
Notifications
You must be signed in to change notification settings - Fork 6
/
SmartTrade.cs
359 lines (336 loc) · 14.9 KB
/
SmartTrade.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#region Using declarations
using System;
using NinjaTrader.Cbi;
#endregion
namespace NinjaTrader.NinjaScript.SmartStrategies
{
public class SmartTrade
{
/// <summary>
/// Long = Buy order going long
/// Short = Short sell order going short
/// </summary>
public enum PositionType { Long, Short }
/// <summary>
/// None = No orders submitted
/// Submitted = entry order submitted
/// InMarket = order at least partially filled
/// Completed = Stop loss or profit target fully filled and entry order filled or cancelled
/// </summary>
public enum MarketStatus { None, Submitted, InMarket, Completed }
/// <summary>
/// None = Trade hasn't completed
/// StopLossHit = Stop loss was hit and the trade is a loss
/// ProfitTargetHit = Profit target was hit and trade is a win
/// Mixed = Profit target partially filled, and the rest was stop lossed
/// </summary>
public enum CompletionType { None, TotalLoss, TotalProfit, Mixed, Cancelled }
/// <summary>
/// None = nothing has been filled
/// Full = order has been fully filled
/// Partial = order has been partially filled
/// </summary>
public enum FillType { None, Full, Partial }
public PositionType Position { get; private set; }
public MarketStatus Status { get; private set; }
public CompletionType Completion { get; private set; }
public FillType Fill { get; private set; }
public Configuration Config { get; private set; }
/// <summary>
/// How many actually got filled
/// </summary>
public int FillCount { get; private set; }
/// <summary>
/// How many successfully made a profit
/// </summary>
public int ProfitCount { get; private set; }
/// <summary>
/// How many got stopped out
/// </summary>
public int StopLossCount { get; private set; }
/// <summary>
/// The entry order for entering the trade
/// </summary>
public Order EntryOrder { get; private set; }
/// <summary>
/// The stop loss order used for stoplosses
/// </summary>
public Order StopLoss { get; private set; }
/// <summary>
/// The profit target order used for profit target
/// </summary>
public Order ProfitTarget { get; private set; }
/// <summary>
/// The bar index the trade was first submitted one
/// </summary>
public int SubmittedBarIndex { get; private set; }
public double ProfitTargetPrice
{
get
{
if (EntryOrder == null) return Position == PositionType.Long ? Config.Strategy.GetCurrentAsk() + Config.InitialProfitTarget : Config.Strategy.GetCurrentBid() - Config.InitialProfitTarget;
return Position == PositionType.Long ? EntryOrder.AverageFillPrice + Config.InitialProfitTarget : EntryOrder.AverageFillPrice - Config.InitialProfitTarget;
}
}
public double StopLossPrice
{
get
{
if (EntryOrder == null) return Position == PositionType.Long ? Config.Strategy.GetCurrentAsk() - Config.InitialStopLoss : Config.Strategy.GetCurrentBid() + Config.InitialStopLoss;
return Position == PositionType.Long ? EntryOrder.AverageFillPrice - Config.InitialStopLoss : EntryOrder.AverageFillPrice + Config.InitialStopLoss;
}
}
// Events you can register to. You're welcome :)
// OnSubmitted = when the first entry order is submitted. Called immediately after successful Submit
// OnMarketEntered = when any quantity from the entry order is first filled
// OnCompleted = when all trades have resolved and the trade is done
// OnStoppedOut = called each time some quantity is filled on a stop loss
// OnProffited = called each time some quantity is filled on a profit target
// OnFilled = called each time some quantity is filled on an entry order
public event Action<SmartTrade> OnSubmitted, OnMarketEntered, OnCompleted, OnStoppedOut, OnProfitted, OnFilled;
private Guid OCOID;
/// <summary>
/// Creates a new smart trade. Does not submit on creation.
/// You must call <see cref="Submit"/> first.
/// </summary>
/// <param name="config">The trade configuration</param>
/// <param name="position">The position you want to take with this trade</param>
public SmartTrade(Configuration config, PositionType position)
{
Config = config;
Position = position;
Init();
}
~SmartTrade()
{
Config.Strategy.OrderUpdated -= OrderUpdatedCallback;
}
private void Init()
{
GenerateUniqueOCOID();
Config.Strategy.OrderUpdated += OrderUpdatedCallback;
}
/// <summary>
/// Attempts to submit the entry order for the trade
/// </summary>
/// <returns>Whether the entry order was submitted successfully</returns>
public bool Submit()
{
if (Status != MarketStatus.None) return false;
OrderAction action = Position == PositionType.Long ? OrderAction.Buy : OrderAction.SellShort;
EntryOrder = Config.Strategy.SubmitOrderUnmanaged(Config.Strategy.BarsInProgressIndex, action, Config.OrderType, Config.Quantity, Config.InitialLimitPrice, Config.InitialStopPrice, "", Config.SignalName);
if (EntryOrder != null)
{
SubmittedBarIndex = Config.Strategy.CurrentBar;
Status = MarketStatus.Submitted;
Config.Strategy.VPrint("T: Status Updated: Submitted");
if (OnSubmitted != null) OnSubmitted(this);
}
return EntryOrder != null;
}
/// <summary>
/// Cancels the trade. A trade can only be cancelled
/// if it's not in market or completed yet
/// </summary>
/// <returns>whether cancellation was successful</returns>
public bool TryCancel()
{
if (Status != MarketStatus.Submitted) return false;
Config.Strategy.CancelOrder(EntryOrder);
return true;
}
private void GenerateUniqueOCOID()
{
OCOID = Guid.NewGuid();
}
private void OrderUpdatedCallback(Order order)
{
if (order == null) return;
if (order != EntryOrder && order != StopLoss && order != ProfitTarget) return;
if (order.OrderState == OrderState.Rejected)
{
throw new Exception("An order was rejected! Strategy failure!");
}
// If our stop loss or profit target are filled before we fully fill the entry order
// then we should just cancel the original order
if (order == StopLoss || order == ProfitTarget)
{
if (order.OrderState == OrderState.Filled && EntryOrder.OrderState != OrderState.Filled)
{
Config.Strategy.VPrint("T: Cancelling Remaining Entry Orders");
Config.Strategy.CancelOrder(EntryOrder); // OCO will handle the other orders
}
}
if (order == EntryOrder && order.Filled > 0)
{
// If our order has been filled and we don't have a StopLoss yet then create one and a profit target
// for the filled amount
if (StopLoss == null && Config.AutoProfitAndStop)
{
Config.Strategy.VPrint("T: Creating Stop Loss and Profit Target");
OrderAction action = Position == PositionType.Long ? OrderAction.Sell : OrderAction.BuyToCover;
StopLoss = Config.Strategy.SubmitOrderUnmanaged(Config.Strategy.BarsInProgressIndex, action, OrderType.StopMarket, order.Filled, 0, StopLossPrice, OCOID.ToString(), "Stop Loss");
ProfitTarget = Config.Strategy.SubmitOrderUnmanaged(Config.Strategy.BarsInProgressIndex, action, OrderType.Limit, order.Filled, ProfitTargetPrice, 0, OCOID.ToString(), "Profit Target");
if (StopLoss == null || ProfitTarget == null)
{
// Just in case, throw exception
throw new Exception("Failed to create Stop Loss or Profit Target order. This shouldn't happen.");
}
}
// if order filled is greater than our stop loss and our stop loss hasn't been completely filled
// update the stop loss and profit target quantities
if (StopLoss != null && order.Filled > StopLoss.Quantity && StopLoss.OrderState != OrderState.Filled && ProfitTarget.OrderState != OrderState.Filled)
{
Config.Strategy.VPrint("T: Adjusting Stop Loss and Profit Target Quantities To New Fill Count");
Config.Strategy.ChangeOrder(StopLoss, order.Filled, StopLoss.LimitPrice, StopLoss.StopPrice);
Config.Strategy.ChangeOrder(ProfitTarget, order.Filled, ProfitTarget.LimitPrice, ProfitTarget.StopPrice);
}
// NOTE: We changed and modified any orders before updating states or callbacks
if (order.Filled != FillCount)
{
Filled();
}
// Ensure fill count and fill type are updated before market status callback
if (Status == MarketStatus.None)
{
MarketEntered();
}
}
if (order == EntryOrder && order.OrderState == OrderState.Cancelled && Status == MarketStatus.Submitted)
{
Status = MarketStatus.Completed;
Completion = CompletionType.Cancelled;
Config.Strategy.VPrint("T: Trade Cancelled");
Config.Strategy.VPrint("T: Status Updated: Completed");
if (OnCompleted != null) OnCompleted(this);
}
if (order == StopLoss)
{
if (StopLossCount != StopLoss.Filled)
{
StoppedOut();
}
}
if (order == ProfitTarget)
{
if (ProfitCount != ProfitTarget.Filled)
{
Profitted();
}
}
// Completion events are always last so all data is filled first!
if ((order == StopLoss || order == ProfitTarget) && ProfitTarget != null && StopLoss != null)
{
if (StopLoss.Filled + ProfitTarget.Filled == EntryOrder.Filled)
{
CompleteTrade();
}
}
}
private void Filled()
{
if (EntryOrder.Filled == EntryOrder.Quantity)
{
Fill = FillType.Full;
}
else
{
Fill = FillType.Partial;
}
FillCount = EntryOrder.Filled;
Config.Strategy.VPrint("T: Entry Filled! Total: " + FillCount);
if (OnFilled != null) OnFilled(this);
}
private void MarketEntered()
{
Status = MarketStatus.InMarket;
Config.Strategy.VPrint("T: Status Updated: Market Entered");
if (OnMarketEntered != null) OnMarketEntered(this);
}
private void StoppedOut()
{
StopLossCount = StopLoss.Filled;
Config.Strategy.VPrint("T: Stopped Out " + StopLossCount);
if (OnStoppedOut != null) OnStoppedOut(this);
}
private void Profitted()
{
ProfitCount = ProfitTarget.Filled;
Config.Strategy.VPrint("T: Profited " + ProfitCount);
if (OnProfitted != null) OnProfitted(this);
}
private void CompleteTrade()
{
if (ProfitTarget.Filled == EntryOrder.Filled)
{
Completion = CompletionType.TotalProfit;
Config.Strategy.VPrint("T: Total Profit");
}
else if (StopLoss.Filled == EntryOrder.Filled)
{
Completion = CompletionType.TotalLoss;
Config.Strategy.VPrint("T: Total Loss");
}
else
{
Completion = CompletionType.Mixed;
Config.Strategy.VPrint("T: Mixed Results");
}
Status = MarketStatus.Completed;
Config.Strategy.VPrint("T: Status Updated: Completed");
Config.Strategy.OrderUpdated -= OrderUpdatedCallback;
if (OnCompleted != null) OnCompleted(this);
}
public struct Configuration
{
/// <summary>
/// The smart strategy this trade executes on
/// </summary>
public BaseSmartStrategy Strategy;
/// <summary>
/// The entry order type for this trade
/// </summary>
public OrderType OrderType;
/// <summary>
/// The target quantity of the trade
/// </summary>
public int Quantity;
/// <summary>
/// If limit order, set this for limit price
/// </summary>
public double InitialLimitPrice;
/// <summary>
/// If stop order, set this for stop price
/// </summary>
public double InitialStopPrice;
/// <summary>
/// Optional signal name
/// </summary>
public string SignalName;
/// <summary>
/// Whether profit target and stop loss are managed automatically
/// </summary>
public bool AutoProfitAndStop;
/// <summary>
/// Currency amount below entry point to place auto stop loss
/// </summary>
public double InitialStopLoss;
/// <summary>
/// Currency amount above entry point to place auto profit target
/// </summary>
public double InitialProfitTarget;
public Configuration(BaseSmartStrategy strategy, OrderType orderType, int quantity, double initialLimitPrice, double initialStopPrice, string signalName, bool autoProfitAndStop, double initialStopLoss, double initialProfitTarget)
{
Strategy = strategy;
OrderType = orderType;
Quantity = quantity;
InitialLimitPrice = initialLimitPrice;
InitialStopPrice = initialStopLoss;
SignalName = signalName;
AutoProfitAndStop = autoProfitAndStop;
InitialStopLoss = initialStopLoss;
InitialProfitTarget = initialProfitTarget;
}
}
}
}