-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSFISARCS.java
894 lines (796 loc) · 34 KB
/
SFISARCS.java
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
import java.util.*;
import com.dukascopy.api.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.CopyOnWriteArrayList;
import java.lang.reflect.*;
import java.math.BigDecimal;
/*
* The name is an acronym of the indicators used:
* SF = Stochastic Fast
* I = Ichimoku
* SAR = parabolic SAR
* C = CCI
* S = Stochastic
*/
public class SFISARCS implements IStrategy {
private CopyOnWriteArrayList<TradeEventAction> tradeEventActions = new CopyOnWriteArrayList<TradeEventAction>();
private static final String DATE_FORMAT_NOW = "yyyyMMdd_HHmmss";
private IEngine engine;
private IConsole console;
private IHistory history;
private IContext context;
private IIndicators indicators;
private IUserInterface userInterface;
public int defaultTakeProfit = 60;
@Configurable("defaultInstrument:")
public Instrument defaultInstrument = Instrument.EURUSD;
@Configurable("defaultSlippage:")
public int defaultSlippage = 5;
@Configurable("defaultStopLoss:")
public int defaultStopLoss = 30;
@Configurable("defaultPeriod:")
public Period defaultPeriod = Period.ONE_MIN;
// INDICATORS (STOCH, STOCHF, CCI, SAR, ICHIMOKU)
private double _stoch_K;
private double _stoch_D;
private double _stochf_K;
private double _stochf_D;
private double _stochf_shifted_D;
private double _stochf_shifted_K;
private Period _stoch_period = Period.TEN_MINS;
private int _stoch_shift = 0;
@Configurable("Stochastic oscillator fast K: ")
private int _stoch_fk_period = 5;
@Configurable("Stochastic oscillator slow K: ")
private int _stoch_sk_period = 3;
@Configurable("Stochastic oscillator slow D: ")
private int _stoch_sd_period = 3;
private Period _stochf_period = Period.FIVE_MINS;
private int _stochf_shifted_shift = 1;
private double _cci;
private double _shifted_cci;
private Period _cci_period = Period.ONE_HOUR;
@Configurable("CCI time period: ")
public int _cci_time_period = 42; // {12,24,32,64}
private int _cci_shift = 0;
private int _cci_shifted_shift = 1;
private double _sar;
private Period _sar_period = Period.DAILY; // default: Period.DAILY
private int _sar_shift = 0;
private double _sar_acc = 0.02;
private double _sar_max = 0.2;
private double _tenkansen;
private double _kijunsen;
private double _senkouA;
private double _senkouB;
private double _chinkou;
private double _tenkansen_shifted;
private double _kijunsen_shifted;
private double _senkouA_shifted;
private double _senkouB_shifted;
private double _chinkou_shifted;
private Period _ichimoku_period = Period.ONE_HOUR;
private int _ichimoku_shift = 0;
private int _ichimoku_shifted_shift = 26;
private int _ichimoku_tenkansen = 9;
private int _ichimoku_kijunsen = 26;
private int _ichimoku_senkou = 56;
//Custom variables
@Configurable("Stochastic Bear Threshold: ")
public int _stochBearTreshold = 30; // [10,30]
@Configurable("Stochastic Bull Threshold: ")
public int _stochBullTreshold = 55; // [70,90]
private double _lot;
private double _use_of_leverage_threshold = 100 ;
private double _pnl_threshold = 10;
private double _risk_per_trade = 2e3;
private double _max_risk = 0.1;
@Configurable("Max lot allowed: ")
public double _max_lot = 9.0; // [5,10]
@Configurable("Trailing step value")
public double _pips_trailing_step = 15; // [5,20]
private boolean _trailed = false;
private IOrder _last_position;
//Account Stuff
private String AccountId = "";
private String AccountCurrency = "";
private double Equity;
private double Leverage;
private double UseofLeverage;
private int OverWeekendEndLeverage;
private int MarginCutLevel;
private Tick LastTick = null ;
private IMessage LastTradeEvent = null ;
private Candle LastAskCandle = null ;
private Candle LastBidCandle = null ;
private List<IOrder> AllPositions = null ;
private List<IOrder> PendingPositions = null ;
private List<IOrder> OpenPositions = null ;
private boolean GlobalAccount;
public void onStart(IContext context) throws JFException {
this.context = context;
this.engine = context.getEngine();
this.console = context.getConsole();
this.history = context.getHistory();
this.indicators = context.getIndicators();
this.userInterface = context.getUserInterface();
subscriptionInstrumentCheck(defaultInstrument);
ITick lastITick = context.getHistory().getLastTick(defaultInstrument);
LastTick = new Tick(lastITick, defaultInstrument);
IBar bidBar = context.getHistory().getBar(defaultInstrument,
defaultPeriod, OfferSide.BID, 1);
IBar askBar = context.getHistory().getBar(defaultInstrument,
defaultPeriod, OfferSide.ASK, 1);
LastAskCandle = new Candle(askBar, defaultPeriod, defaultInstrument,
OfferSide.ASK);
LastBidCandle = new Candle(bidBar, defaultPeriod, defaultInstrument,
OfferSide.BID);
downloadIndicators();
subscriptionInstrumentCheck(Instrument.fromString("EUR/USD"));
console.getOut().println("Debug Started");
}
private void downloadIndicators(){
try{
if (indicators.getIndicator("ICHIMOKUVF") == null)
indicators.registerDownloadableIndicator("7842","ICHIMOKUVF");
if (indicators.getIndicator("ICHIMOKUVF") == null)
indicators.registerDownloadableIndicator("7842","ICHIMOKUVF");
if (indicators.getIndicator("CCI") == null)
indicators.registerDownloadableIndicator("1275","CCI");
if (indicators.getIndicator("CCI") == null)
indicators.registerDownloadableIndicator("1275","CCI");
if (indicators.getIndicator("SAR") == null)
indicators.registerDownloadableIndicator("1291","SAR");
if (indicators.getIndicator("STOCH") == null)
indicators.registerDownloadableIndicator("1279","STOCH");
if (indicators.getIndicator("STOCHF") == null)
indicators.registerDownloadableIndicator("8325","STOCHF");
if (indicators.getIndicator("STOCHF") == null)
indicators.registerDownloadableIndicator("8325","STOCHF");
}catch (JFException e){
e.printStackTrace();
}
}
public void onAccount(IAccount account) throws JFException {
AccountCurrency = account.getCurrency().toString();
Leverage = account.getLeverage();
AccountId= account.getAccountId();
Equity = account.getEquity();
UseofLeverage = account.getUseOfLeverage();
OverWeekendEndLeverage = account.getOverWeekEndLeverage();
MarginCutLevel = account.getMarginCutLevel();
GlobalAccount = account.isGlobal();
}
private void updateVariables(Instrument instrument) {
try {
AllPositions = engine.getOrders();
List<IOrder> listMarket = new ArrayList<IOrder>();
for (IOrder order: AllPositions) {
if (order.getState().equals(IOrder.State.FILLED)){
listMarket.add(order);
}
}
List<IOrder> listPending = new ArrayList<IOrder>();
for (IOrder order: AllPositions) {
if (order.getState().equals(IOrder.State.OPENED)){
listPending.add(order);
}
}
OpenPositions = listMarket;
PendingPositions = listPending;
} catch(JFException e) {
e.printStackTrace();
}
}
public void onMessage(IMessage message) throws JFException {
if (message.getOrder() != null) {
updateVariables(message.getOrder().getInstrument());
LastTradeEvent = message;
for (TradeEventAction event : tradeEventActions) {
IOrder order = message.getOrder();
if (order != null && event != null &&
message.getType().equals(event.getMessageType())&&
order.getLabel().equals(event.getPositionLabel())) {
Method method;
try {
method = this.getClass().getDeclaredMethod(
event.getNextBlockId(), Integer.class);
method.invoke(this, new Integer[] {event.getFlowId()});
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
tradeEventActions.remove(event);
}
}
}
}
public void onStop() throws JFException {
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
LastTick = new Tick(tick, instrument);
updateVariables(instrument);
}
public void subscriptionInstrumentCheck(Instrument instrument) {
try {
if (!context.getSubscribedInstruments().contains(instrument)) {
Set<Instrument> instruments = new HashSet<Instrument>();
instruments.add(instrument);
context.setSubscribedInstruments(instruments, true);
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public double round(double price, Instrument instrument) {
BigDecimal big = new BigDecimal("" + price);
big = big.setScale(instrument.getPipScale() + 1, BigDecimal.ROUND_HALF_UP);
return big.doubleValue();
}
public ITick getLastTick(Instrument instrument) {
try {
return (context.getHistory().getTick(instrument, 0));
} catch (JFException e) {
e.printStackTrace();
}
return null;
}
public void onBar(Instrument instrument, Period period,
IBar askBar, IBar bidBar) throws JFException {
LastAskCandle = new Candle(askBar, period, instrument, OfferSide.ASK);
LastBidCandle = new Candle(bidBar, period, instrument, OfferSide.BID);
updateVariables(instrument);
if(validBar(instrument, period)){
createIndicators();
strategy();
}
}
private boolean validBar(Instrument instrument, Period period){
return (instrument != null && instrument.equals(defaultInstrument)) &&
(period != null && period.equals(defaultPeriod));
}
private void createIndicators(){
createStoch();
createStochF();
createShiftedStochF();
createCCI();
createShiftedCCI();
createSAR();
createIchimoku();
createShiftedIchimoku();
}
private void strategy(){
lotControl();
if(AllPositions.size() == 0)
noPositionStrategy();
else
openedPositionStrategy();
}
private void lotControl(){
_lot = Equity * _max_risk/_risk_per_trade;
_lot = (_lot > _max_lot) ? _max_lot : _lot;
}
private void noPositionStrategy(){
this._trailed = false;
if (LastAskCandle.getClose() < _sar && bearishEvent()){
MarketOrder("SELL");
}
else if(LastBidCandle.getClose() > _sar && bullishEvent()){
MarketOrder("BUY");
}
}
private boolean bearishEvent(){
return _tenkansen < _kijunsen &&
LastBidCandle.getClose() < _senkouB_shifted &&
LastBidCandle.getClose() < _senkouA_shifted &&
_stoch_K < _stoch_D && _stoch_K > _stochBearTreshold &&
_stochf_K < _stochf_shifted_K && _stochf_K >_stochf_D;
}
private boolean bullishEvent(){
return _tenkansen > _kijunsen &&
LastBidCandle.getClose() > _senkouB_shifted &&
LastBidCandle.getClose() > _senkouA_shifted &&
_stoch_K > _stoch_D && _stoch_K < _stochBullTreshold &&
_stochf_K > _stochf_shifted_K && _stochf_K <_stochf_D;
}
private void openedPositionStrategy(){
if (UseofLeverage > _use_of_leverage_threshold){
CloseAllPositons();
}
else if (_use_of_leverage_threshold > UseofLeverage)
for (IOrder position : OpenPositions){
if(position.getState() == IOrder.State.OPENED||position.getState() == IOrder.State.FILLED)
_last_position = position;
if (_last_position.getProfitLossInPips() < _pnl_threshold)
ichimokuProtocol(_last_position);
else if (_last_position.getProfitLossInPips() > _pnl_threshold)
trailingStopProtocol(_last_position);
}
}
private void ichimokuProtocol(IOrder order){
if ((order.isLong() && ichimokuCloseLongEvent()) ||
(!order.isLong() && ichimokuCloseShortEvent())){
this._last_position = order;
ClosePosition();
console.getOut().println("Ichimoku protocol activated, closing position...");
}
}
private boolean ichimokuCloseLongEvent(){
return (_senkouA > _senkouB && _tenkansen < _kijunsen ) ||
_senkouA < _senkouB;
}
private boolean ichimokuCloseShortEvent(){
return (_senkouA < _senkouB && _tenkansen > _kijunsen ) ||
_senkouA > _senkouB;
}
private void trailingStopProtocol(IOrder order){
if(!this._trailed) {
updateTrailingStop(order);
this._trailed = true;
}
cciProtocol(order);
}
private void cciProtocol(IOrder order){
if (cciEvent(order)){
console.getOut().println("CCI Protocol activated, closing position...");
this._last_position = order;
ClosePosition();
}
}
private boolean cciEvent(IOrder order){
return (order.isLong() && (_cci < _shifted_cci || (_cci > _shifted_cci && _stoch_K < _stoch_D)))
||
(!order.isLong() && (_cci > _shifted_cci || (_cci < _shifted_cci && _stoch_K > _stoch_D)));
}
private void MarketOrder(String dir) {
ITick tick = getLastTick(defaultInstrument);
IEngine.OrderCommand command = (dir=="BUY") ? IEngine.OrderCommand.BUY :
IEngine.OrderCommand.SELL;
double stopLoss = (dir=="BUY") ? tick.getBid() - defaultInstrument.getPipValue() * defaultStopLoss :
tick.getBid() + defaultInstrument.getPipValue() * defaultStopLoss;
double takeProfit = (dir=="BUY") ? round(tick.getBid() + defaultInstrument.getPipValue() * defaultTakeProfit, defaultInstrument):
round(tick.getBid() - defaultInstrument.getPipValue() * defaultTakeProfit, defaultInstrument);
console.getOut().println("new Market Order: stopLoss = "+String.valueOf(stopLoss)+" | takeProfit= "+String.valueOf(takeProfit));
try {
_last_position = context.getEngine().submitOrder(getLabel(),
defaultInstrument, command, _lot, 0, defaultSlippage,
stopLoss, takeProfit, 0, "");
} catch (JFException e) {
e.printStackTrace();
}
}
private void updateTrailingStop(IOrder order) {
_pips_trailing_step = (_pips_trailing_step < 10) ? 10 : _pips_trailing_step;
if (_pips_trailing_step != order.getTrailingStep()) {
double stopLoss;
try {
if (order.isLong()) {
stopLoss = round(order.getOpenPrice() -
order.getInstrument().getPipValue() *
defaultStopLoss, order.getInstrument());
if ((stopLoss != order.getStopLossPrice()) && (stopLoss != 0)
&& (order.getState().equals(IOrder.State.OPENED) ||
order.getState().equals(IOrder.State.FILLED)))
order.setStopLossPrice(stopLoss, OfferSide.BID, _pips_trailing_step);
} else if(!order.isLong()){
stopLoss = round(order.getOpenPrice() +
order.getInstrument().getPipValue() *
defaultStopLoss, order.getInstrument());
if ((stopLoss != order.getStopLossPrice()) && (stopLoss != 0)
&& (order.getState().equals(IOrder.State.OPENED) ||
order.getState().equals(IOrder.State.FILLED)))
order.setStopLossPrice(stopLoss, OfferSide.ASK, _pips_trailing_step);
}
TradeEventAction event = new TradeEventAction();
event.setMessageType(IMessage.Type.ORDER_CHANGED_OK);
event.setPositionLabel(order.getLabel());
tradeEventActions.add(event);
console.getOut().println("new Stop loss (trailing updated): " + order.getStopLossPrice());
} catch (JFException e) {
e.printStackTrace();
}
}
}
private void ClosePosition(){
try {
if (_last_position != null && (_last_position.getState() == IOrder.State.OPENED ||
_last_position.getState() == IOrder.State.FILLED))
_last_position.close();
} catch (JFException e) {
e.printStackTrace();
}
}
private void CloseAllPositons(){
for (IOrder position : OpenPositions){
if (position.getState() == IOrder.State.OPENED ||
position.getState() == IOrder.State.FILLED)
_last_position = position;
ClosePosition();
}
}
private void createStoch() {
OfferSide[] offerside = new OfferSide[1];
IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
offerside[0] = OfferSide.BID;
appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
Object[] params = new Object[5];
params[0] = _stoch_fk_period;
params[1] = _stoch_sk_period;
params[2] = 0;// K -> SMA
params[3] = _stoch_sd_period;
params[4] = 0;// D -> SMA
try {
subscriptionInstrumentCheck(defaultInstrument);
long time = context.getHistory().getBar(defaultInstrument, _stoch_period, OfferSide.BID, _stoch_shift).getTime();
Object[] indicatorResult = context.getIndicators().calculateIndicator(defaultInstrument, _stoch_period, offerside,
"STOCH", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
if ((new Double(((double [])indicatorResult[0])[0])) == null)
this._stoch_K = Double.NaN;
else
this._stoch_K = (((double [])indicatorResult[0])[0]);
if ((new Double(((double [])indicatorResult[1])[0])) == null)
this._stoch_D = Double.NaN;
else
this._stoch_D = (((double [])indicatorResult[1])[0]);
} catch (JFException e) {
e.printStackTrace();
console.getErr().println(e);
this._stoch_K = Double.NaN;
this._stoch_D = Double.NaN;
}
}
private void createStochF() {
OfferSide[] offerside = new OfferSide[1];
IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
offerside[0] = OfferSide.BID;
appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
Object[] params = new Object[3];
params[0] = _stoch_fk_period;
params[1] = _stoch_sk_period;
params[2] = 0;
try {
subscriptionInstrumentCheck(defaultInstrument);
long time = context.getHistory().getBar(defaultInstrument, _stochf_period, OfferSide.BID, _stoch_shift).getTime();
Object[] indicatorResult = context.getIndicators().calculateIndicator(defaultInstrument, _stochf_period, offerside,
"STOCHF", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
if ((new Double(((double [])indicatorResult[0])[0])) == null)
this._stochf_K = Double.NaN;
else
this._stochf_K = (((double [])indicatorResult[0])[0]);
if ((new Double(((double [])indicatorResult[1])[0])) == null)
this._stochf_D = Double.NaN;
else
this._stochf_D = (((double [])indicatorResult[1])[0]);
} catch (JFException e) {
e.printStackTrace();
console.getErr().println(e);
this._stochf_K = Double.NaN;
this._stochf_D = Double.NaN;
}
}
private void createShiftedStochF() {
OfferSide[] offerside = new OfferSide[1];
IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
offerside[0] = OfferSide.BID;
appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
Object[] params = new Object[3];
params[0] = _stoch_fk_period;
params[1] = _stoch_sk_period;
params[2] = 0;
try {
subscriptionInstrumentCheck(defaultInstrument);
long time = context.getHistory().getBar(defaultInstrument, _stochf_period, OfferSide.BID, _stochf_shifted_shift).getTime();
Object[] indicatorResult = context.getIndicators().calculateIndicator(defaultInstrument, _stochf_period, offerside,
"STOCHF", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
if ((new Double(((double [])indicatorResult[0])[0])) == null) {
this._stochf_shifted_K = Double.NaN;
} else {
this._stochf_shifted_K = (((double [])indicatorResult[0])[0]);
}
if ((new Double(((double [])indicatorResult[1])[0])) == null) {
this._stochf_shifted_D = Double.NaN;
} else {
this._stochf_shifted_D = (((double [])indicatorResult[1])[0]);
}
} catch (JFException e) {
e.printStackTrace();
console.getErr().println(e);
this._stochf_shifted_K = Double.NaN;
this._stochf_shifted_D = Double.NaN;
}
}
private void createCCI() {
OfferSide[] offerside = new OfferSide[1];
IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
offerside[0] = OfferSide.BID;
appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
Object[] params = new Object[1];
params[0] = _cci_time_period;
try {
subscriptionInstrumentCheck(defaultInstrument);
long time = context.getHistory().getBar(defaultInstrument, _cci_period, OfferSide.BID, _cci_shift).getTime();
Object[] indicatorResult = context.getIndicators().calculateIndicator(defaultInstrument, _cci_period, offerside,
"CCI", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
if ((new Double(((double [])indicatorResult[0])[0])) == null)
this._cci = Double.NaN;
else
this._cci = (((double [])indicatorResult[0])[0]);
} catch (JFException e) {
e.printStackTrace();
console.getErr().println(e);
this._cci = Double.NaN;
}
}
private void createShiftedCCI() {
OfferSide[] offerside = new OfferSide[1];
IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
offerside[0] = OfferSide.BID;
appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
Object[] params = new Object[1];
params[0] = _cci_time_period;
try {
subscriptionInstrumentCheck(defaultInstrument);
long time = context.getHistory().getBar(defaultInstrument, _cci_period, OfferSide.BID, _cci_shifted_shift).getTime();
Object[] indicatorResult = context.getIndicators().calculateIndicator(defaultInstrument, _cci_period, offerside,
"CCI", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
if ((new Double(((double [])indicatorResult[0])[0])) == null)
this._shifted_cci= Double.NaN;
else
this._shifted_cci = (((double [])indicatorResult[0])[0]);
} catch (JFException e) {
e.printStackTrace();
console.getErr().println(e);
this._shifted_cci = Double.NaN;
}
}
private void createSAR() {
OfferSide[] offerside = new OfferSide[1];
IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
offerside[0] = OfferSide.BID;
appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
Object[] params = new Object[2];
params[0] = _sar_acc;
params[1] = _sar_max;
try {
subscriptionInstrumentCheck(defaultInstrument);
long time = context.getHistory().getBar(defaultInstrument, _sar_period, OfferSide.BID, _sar_shift).getTime();
Object[] indicatorResult = context.getIndicators().calculateIndicator(defaultInstrument, _sar_period, offerside,
"SAR", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
if ((new Double(((double [])indicatorResult[0])[0])) == null)
this._sar = Double.NaN;
else
this._sar = (((double [])indicatorResult[0])[0]);
} catch (JFException e) {
e.printStackTrace();
console.getErr().println(e);
this._sar = Double.NaN;
}
}
private void createIchimoku() {
OfferSide[] offerside = new OfferSide[1];
IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
offerside[0] = OfferSide.BID;
appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
Object[] params = new Object[3];
params[0] = _ichimoku_tenkansen;
params[1] = _ichimoku_kijunsen;
params[2] = _ichimoku_senkou;
try {
subscriptionInstrumentCheck(defaultInstrument);
long time = context.getHistory().getBar(defaultInstrument, _ichimoku_period, OfferSide.BID, _ichimoku_shift).getTime();
Object[] indicatorResult = context.getIndicators().calculateIndicator(defaultInstrument, _ichimoku_period, offerside,
"ICHIMOKUVF", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
if ((new Double(((double [])indicatorResult[0])[0])) == null)
this._tenkansen = Double.NaN;
else
this._tenkansen = (((double [])indicatorResult[0])[0]);
if ((new Double(((double [])indicatorResult[1])[0])) == null)
this._kijunsen = Double.NaN;
else
this._kijunsen = (((double [])indicatorResult[1])[0]);
if ((new Double(((double [])indicatorResult[2])[0])) == null)
this._chinkou = Double.NaN;
else
this._chinkou = (((double [])indicatorResult[2])[0]);
if ((new Double(((double [])indicatorResult[3])[0])) == null)
this._senkouA = Double.NaN;
else
this._senkouA = (((double [])indicatorResult[3])[0]);
if ((new Double(((double [])indicatorResult[4])[0])) == null)
this._senkouB = Double.NaN;
else
this._senkouB = (((double [])indicatorResult[4])[0]);
} catch (JFException e) {
e.printStackTrace();
console.getErr().println(e);
this._tenkansen = Double.NaN;
this._kijunsen = Double.NaN;
this._chinkou = Double.NaN;
this._senkouA = Double.NaN;
this._senkouB = Double.NaN;
}
}
private void createShiftedIchimoku() {
OfferSide[] offerside = new OfferSide[1];
IIndicators.AppliedPrice[] appliedPrice = new IIndicators.AppliedPrice[1];
offerside[0] = OfferSide.BID;
appliedPrice[0] = IIndicators.AppliedPrice.CLOSE;
Object[] params = new Object[3];
params[0] = _ichimoku_tenkansen;
params[1] = _ichimoku_kijunsen;
params[2] = _ichimoku_senkou;
try {
subscriptionInstrumentCheck(defaultInstrument);
long time = context.getHistory().getBar(defaultInstrument, _ichimoku_period, OfferSide.BID, _ichimoku_shifted_shift).getTime();
Object[] indicatorResult = context.getIndicators().calculateIndicator(defaultInstrument, _ichimoku_period, offerside,
"ICHIMOKUVF", appliedPrice, params, Filter.WEEKENDS, 1, time, 0);
if ((new Double(((double [])indicatorResult[0])[0])) == null)
this._tenkansen_shifted = Double.NaN;
else
this._tenkansen_shifted = (((double [])indicatorResult[0])[0]);
if ((new Double(((double [])indicatorResult[1])[0])) == null)
this._kijunsen_shifted = Double.NaN;
else
this._kijunsen_shifted = (((double [])indicatorResult[1])[0]);
if ((new Double(((double [])indicatorResult[2])[0])) == null)
this._chinkou_shifted = Double.NaN;
else
this._chinkou_shifted = (((double [])indicatorResult[2])[0]);
if ((new Double(((double [])indicatorResult[3])[0])) == null)
this._senkouA_shifted = Double.NaN;
else
this._senkouA_shifted = (((double [])indicatorResult[3])[0]);
if ((new Double(((double [])indicatorResult[4])[0])) == null)
this._senkouB_shifted = Double.NaN;
else
this._senkouB_shifted = (((double [])indicatorResult[4])[0]);
} catch (JFException e) {
e.printStackTrace();
console.getErr().println(e);
this._tenkansen_shifted = Double.NaN;
this._kijunsen_shifted = Double.NaN;
this._chinkou_shifted = Double.NaN;
this._senkouA_shifted = Double.NaN;
this._senkouB_shifted = Double.NaN;
}
}
class Candle {
IBar bar;
Period period;
Instrument instrument;
OfferSide offerSide;
public Candle(IBar bar, Period period, Instrument instrument, OfferSide offerSide) {
this.bar = bar;
this.period = period;
this.instrument = instrument;
this.offerSide = offerSide;
}
public Period getPeriod() {
return period;
}
public void setPeriod(Period period) {
this.period = period;
}
public Instrument getInstrument() {
return instrument;
}
public void setInstrument(Instrument instrument) {
this.instrument = instrument;
}
public OfferSide getOfferSide() {
return offerSide;
}
public void setOfferSide(OfferSide offerSide) {
this.offerSide = offerSide;
}
public IBar getBar() {
return bar;
}
public void setBar(IBar bar) {
this.bar = bar;
}
public long getTime() {
return bar.getTime();
}
public double getOpen() {
return bar.getOpen();
}
public double getClose() {
return bar.getClose();
}
public double getLow() {
return bar.getLow();
}
public double getHigh() {
return bar.getHigh();
}
public double getVolume() {
return bar.getVolume();
}
}
class Tick {
private ITick tick;
private Instrument instrument;
public Tick(ITick tick, Instrument instrument){
this.instrument = instrument;
this.tick = tick;
}
public Instrument getInstrument(){
return instrument;
}
public double getAsk(){
return tick.getAsk();
}
public double getBid(){
return tick.getBid();
}
public double getAskVolume(){
return tick.getAskVolume();
}
public double getBidVolume(){
return tick.getBidVolume();
}
public long getTime(){
return tick.getTime();
}
public ITick getTick(){
return tick;
}
}
protected String getLabel() {
String label;
label = "IVF" + getCurrentTime(LastTick.getTime()) + generateRandom(10000) + generateRandom(10000);
return label;
}
private String getCurrentTime(long time) {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(time);
}
private static String generateRandom(int n) {
int randomNumber = (int) (Math.random() * n);
String answer = "" + randomNumber;
if (answer.length() > 3) {
answer = answer.substring(0, 4);
}
return answer;
}
class TradeEventAction {
private IMessage.Type messageType;
private String nextBlockId = "";
private String positionLabel = "";
private int flowId = 0;
public IMessage.Type getMessageType() {
return messageType;
}
public void setMessageType(IMessage.Type messageType) {
this.messageType = messageType;
}
public String getNextBlockId() {
return nextBlockId;
}
public void setNextBlockId(String nextBlockId) {
this.nextBlockId = nextBlockId;
}
public String getPositionLabel() {
return positionLabel;
}
public void setPositionLabel(String positionLabel) {
this.positionLabel = positionLabel;
}
public int getFlowId() {
return flowId;
}
public void setFlowId(int flowId) {
this.flowId = flowId;
}
}
}