Subversion Repositories Integrator Subversion

Rev

Rev 760 | Rev 764 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 760 Rev 762
Line 1... Line 1...
1
package br.com.kronus.core;
1
package br.com.kronus.core;
2
2
3
import br.com.ec.core.util.VerificadorUtil;
-
 
4
import br.com.kronus.core.Candle;
-
 
5
-
 
-
 
3
import java.math.BigDecimal;
6
import java.util.ArrayList;
4
import java.util.ArrayList;
7
import java.util.List;
-
 
8
5
9
/**
6
/**
10
 * Detector + Monitor de Gatilhos (Referência, G1, G2, G3, G4)
7
 * Detector + Monitor de Gatilhos (Referência, G1, G2, G3, G4)
11
 *
8
 *
12
 * REGRAS IMPLEMENTADAS (resumo):
9
 * REGRAS IMPLEMENTADAS (resumo):
Line 55... Line 52...
55
 */
52
 */
56
//package br.com.kronus.strategy.gatilhos;
53
//package br.com.kronus.strategy.gatilhos;
57
54
58
import java.util.ArrayList;
55
import java.util.ArrayList;
59
import java.util.List;
56
import java.util.List;
-
 
57
import java.util.List;
-
 
58
-
 
59
import br.com.sl.domain.model.Candle;
-
 
60
import br.com.sl.domain.util.BigDecimalUtils;
60
61
61
/**
62
/**
62
* Detector de padrões de gatilhos (GR, G1, G2, G3, G4)
63
* Detector de padrões de gatilhos (GR, G1, G2, G3, G4)
63
* trabalhando tanto em modo backtest quanto em modo tempo real.
64
* trabalhando tanto em modo backtest quanto em modo tempo real.
64
*
65
*
Line 251... Line 252...
251
                // primeiro direcional após A ainda é comprador → A não é último da pernada
252
                // primeiro direcional após A ainda é comprador → A não é último da pernada
252
                return new ResultadoPadrao(null, idxRef);
253
                return new ResultadoPadrao(null, idxRef);
253
            }
254
            }
254
255
255
            // 0.3 – A tem o maior topo da pernada compradora
256
            // 0.3 – A tem o maior topo da pernada compradora
256
            double topoRef = ref.getMaxima();
-
 
-
 
257
            BigDecimal topoRef = ref.getMaxima();
257
            for (int i = inicioTrendCompra; i <= idxRef; i++) {
258
            for (int i = inicioTrendCompra; i <= idxRef; i++) {
258
                Candle c = candles.get(i);
259
                Candle c = candles.get(i);
259
                if (c.getMaxima() > topoRef) {
-
 
-
 
260
                if (BigDecimalUtils.ehMaiorQue(c.getMaxima(), topoRef)) {
260
                    return new ResultadoPadrao(null, idxRef);
261
                    return new ResultadoPadrao(null, idxRef);
261
                }
262
                }
262
            }
263
            }
263
264
264
            int idxPrimeiroVendedor = idxPrimeiroDirecionalAposRef;
265
            int idxPrimeiroVendedor = idxPrimeiroDirecionalAposRef;
Line 285... Line 286...
285
                    return new ResultadoPadrao(null, lastIndex);
286
                    return new ResultadoPadrao(null, lastIndex);
286
                }
287
                }
287
288
288
                lastIndex = i;
289
                lastIndex = i;
289
290
290
                if (c.getMinima() < ref.getMinima()) {
-
 
-
 
291
                if (BigDecimalUtils.ehMenorQue(c.getMinima(), ref.getMinima())) {
291
                    idxG1 = i;
292
                    idxG1 = i;
292
                    g1 = c;
293
                    g1 = c;
293
                    break;
294
                    break;
294
                }
295
                }
295
            }
296
            }
Line 337... Line 338...
337
338
338
                ultimoCompradorTrend = c;
339
                ultimoCompradorTrend = c;
339
                idxUltimoCompradorTrend = i;
340
                idxUltimoCompradorTrend = i;
340
                lastIndex = i;
341
                lastIndex = i;
341
342
342
                if (c.getMaxima() > ref.getMaxima()) {
-
 
-
 
343
                if (BigDecimalUtils.ehMaiorQue(c.getMaxima(), ref.getMaxima())) {
343
                    rompeuTopoRef = true;
344
                    rompeuTopoRef = true;
344
                    break;
345
                    break;
345
                }
346
                }
346
            }
347
            }
347
348
348
            if (rompeuTopoRef || idxUltimoCompradorTrend == -1) {
349
            if (rompeuTopoRef || idxUltimoCompradorTrend == -1) {
349
                return new ResultadoPadrao(null, lastIndex);
350
                return new ResultadoPadrao(null, lastIndex);
350
            }
351
            }
351
352
352
            boolean fechamentoDentroRegiaoRef =
353
            boolean fechamentoDentroRegiaoRef =
353
                    ultimoCompradorTrend.getFechamento() >= ref.getMinima() &&
-
 
354
                    ultimoCompradorTrend.getFechamento() <= ref.getMaxima();
-
 
-
 
354
                        BigDecimalUtils.ehMaiorOuIgualQue(ultimoCompradorTrend.getFechamento(), ref.getMinima()) &&
-
 
355
                        BigDecimalUtils.ehMenorOuIgualQue(ultimoCompradorTrend.getFechamento(), ref.getMaxima());
355
356
356
            if (!fechamentoDentroRegiaoRef) {
357
            if (!fechamentoDentroRegiaoRef) {
357
                return new ResultadoPadrao(null, lastIndex);
358
                return new ResultadoPadrao(null, lastIndex);
358
            }
359
            }
359
360
Line 394... Line 395...
394
                    break;
395
                    break;
395
                }
396
                }
396
397
397
                lastIndex = i;
398
                lastIndex = i;
398
399
399
                boolean rompeFundoG2 = c.getMinima() < g2.getMinima();
-
 
400
                boolean topoMenorOuIgualRef = c.getMaxima() <= ref.getMaxima();
-
 
-
 
400
                boolean rompeFundoG2 = BigDecimalUtils.ehMenorQue(c.getMinima(), g2.getMinima());
-
 
401
                boolean topoMenorOuIgualRef = BigDecimalUtils.ehMenorOuIgualQue(c.getMaxima(), ref.getMaxima());
401
402
402
                if (rompeFundoG2 && topoMenorOuIgualRef) {
403
                if (rompeFundoG2 && topoMenorOuIgualRef) {
403
                    idxG3 = i;
404
                    idxG3 = i;
404
                    g3 = c;
405
                    g3 = c;
405
                    break;
406
                    break;
Line 418... Line 419...
418
            int proxIdx = idxG3 + 1;
419
            int proxIdx = idxG3 + 1;
419
            if (proxIdx < n) {
420
            if (proxIdx < n) {
420
                Candle c = candles.get(proxIdx);
421
                Candle c = candles.get(proxIdx);
421
                lastIndex = proxIdx;
422
                lastIndex = proxIdx;
422
423
423
                if (c.getMaxima() > ref.getMaxima()) {
-
 
-
 
424
                if (BigDecimalUtils.ehMaiorQue(c.getMaxima(), ref.getMaxima())) {
424
                    g4 = c;
425
                    g4 = c;
425
                    log(String.format("RefC[%d], G1[%d], G2[%d], G3[%d] => G4 em [%d]",
426
                    log(String.format("RefC[%d], G1[%d], G2[%d], G3[%d] => G4 em [%d]",
426
                            idxRef, idxG1, idxG2, idxG3, proxIdx));
427
                            idxRef, idxG1, idxG2, idxG3, proxIdx));
427
                } else {
428
                } else {
428
                    log(String.format("RefC[%d], G1[%d], G2[%d], G3[%d]: próximo candle [%d] não é G4.",
429
                    log(String.format("RefC[%d], G1[%d], G2[%d], G3[%d]: próximo candle [%d] não é G4.",
Line 495... Line 496...
495
                // primeiro direcional após B ainda é vendedor
496
                // primeiro direcional após B ainda é vendedor
496
                return new ResultadoPadrao(null, idxRef);
497
                return new ResultadoPadrao(null, idxRef);
497
            }
498
            }
498
499
499
            // 0.3 – B tem o MENOR fundo da pernada vendedora
500
            // 0.3 – B tem o MENOR fundo da pernada vendedora
500
            double fundoRef = ref.getMinima();
-
 
-
 
501
            BigDecimal fundoRef = ref.getMinima();
501
            for (int i = inicioTrendVenda; i <= idxRef; i++) {
502
            for (int i = inicioTrendVenda; i <= idxRef; i++) {
502
                Candle c = candles.get(i);
503
                Candle c = candles.get(i);
503
                if (c.getMinima() < fundoRef) {
-
 
-
 
504
                if (BigDecimalUtils.ehMenorQue(c.getMinima(), fundoRef)) {
504
                    return new ResultadoPadrao(null, idxRef);
505
                    return new ResultadoPadrao(null, idxRef);
505
                }
506
                }
506
            }
507
            }
507
508
508
            int idxPrimeiroComprador = idxPrimeiroDirecionalAposRef;
509
            int idxPrimeiroComprador = idxPrimeiroDirecionalAposRef;
Line 528... Line 529...
528
                    return new ResultadoPadrao(null, lastIndex);
529
                    return new ResultadoPadrao(null, lastIndex);
529
                }
530
                }
530
531
531
                lastIndex = i;
532
                lastIndex = i;
532
533
533
                if (c.getMaxima() > ref.getMaxima()) {
-
 
-
 
534
                if (BigDecimalUtils.ehMaiorQue(c.getMaxima(), ref.getMaxima())) {
534
                    idxG1 = i;
535
                    idxG1 = i;
535
                    g1 = c;
536
                    g1 = c;
536
                    break;
537
                    break;
537
                }
538
                }
538
            }
539
            }
Line 581... Line 582...
581
582
582
                ultimoVendedorTrend = c;
583
                ultimoVendedorTrend = c;
583
                idxUltimoVendedorTrend = i;
584
                idxUltimoVendedorTrend = i;
584
                lastIndex = i;
585
                lastIndex = i;
585
586
586
                if (c.getMinima() < ref.getMinima()) {
-
 
-
 
587
                if (BigDecimalUtils.ehMenorQue(c.getMinima(), ref.getMinima())) {
587
                    rompeuFundoRef = true;
588
                    rompeuFundoRef = true;
588
                    break;
589
                    break;
589
                }
590
                }
590
            }
591
            }
591
592
592
            if (rompeuFundoRef || idxUltimoVendedorTrend == -1) {
593
            if (rompeuFundoRef || idxUltimoVendedorTrend == -1) {
593
                return new ResultadoPadrao(null, lastIndex);
594
                return new ResultadoPadrao(null, lastIndex);
594
            }
595
            }
595
596
596
            boolean fechamentoDentroRegiaoRef =
597
            boolean fechamentoDentroRegiaoRef =
597
                    ultimoVendedorTrend.getFechamento() >= ref.getMinima() &&
-
 
598
                    ultimoVendedorTrend.getFechamento() <= ref.getMaxima();
-
 
-
 
598
                        BigDecimalUtils.ehMaiorOuIgualQue(ultimoVendedorTrend.getFechamento(), ref.getMinima()) &&
-
 
599
                        BigDecimalUtils.ehMenorOuIgualQue(ultimoVendedorTrend.getFechamento(), ref.getMaxima());
599
600
600
            if (!fechamentoDentroRegiaoRef) {
601
            if (!fechamentoDentroRegiaoRef) {
601
                return new ResultadoPadrao(null, lastIndex);
602
                return new ResultadoPadrao(null, lastIndex);
602
            }
603
            }
603
604
Line 637... Line 638...
637
                    break;
638
                    break;
638
                }
639
                }
639
640
640
                lastIndex = i;
641
                lastIndex = i;
641
642
642
                boolean rompeTopoG2 = c.getMaxima() > g2.getMaxima();
-
 
643
                boolean fundoMaiorOuIgualRef = c.getMinima() >= ref.getMinima();
-
 
-
 
643
                boolean rompeTopoG2 = BigDecimalUtils.ehMaiorQue(c.getMaxima(), g2.getMaxima());
-
 
644
                boolean fundoMaiorOuIgualRef = BigDecimalUtils.ehMaiorOuIgualQue(c.getMinima(), ref.getMinima());
644
645
645
                if (rompeTopoG2 && fundoMaiorOuIgualRef) {
646
                if (rompeTopoG2 && fundoMaiorOuIgualRef) {
646
                    idxG3 = i;
647
                    idxG3 = i;
647
                    g3 = c;
648
                    g3 = c;
648
                    break;
649
                    break;
Line 661... Line 662...
661
            int proxIdx = idxG3 + 1;
662
            int proxIdx = idxG3 + 1;
662
            if (proxIdx < n) {
663
            if (proxIdx < n) {
663
                Candle c = candles.get(proxIdx);
664
                Candle c = candles.get(proxIdx);
664
                lastIndex = proxIdx;
665
                lastIndex = proxIdx;
665
666
666
                if (c.getMinima() < ref.getMinima()) {
-
 
-
 
667
                if (BigDecimalUtils.ehMenorQue(c.getMinima(), ref.getMinima())) {
667
                    g4 = c;
668
                    g4 = c;
668
                    log(String.format("RefV[%d], G1[%d], G2[%d], G3[%d] => G4 em [%d]",
669
                    log(String.format("RefV[%d], G1[%d], G2[%d], G3[%d] => G4 em [%d]",
669
                            idxRef, idxG1, idxG2, idxG3, proxIdx));
670
                            idxRef, idxG1, idxG2, idxG3, proxIdx));
670
                } else {
671
                } else {
671
                    log(String.format("RefV[%d], G1[%d], G2[%d], G3[%d]: próximo candle [%d] não é G4.",
672
                    log(String.format("RefV[%d], G1[%d], G2[%d], G3[%d]: próximo candle [%d] não é G4.",
Line 692... Line 693...
692
  * Outside em relação ao GR:
693
  * Outside em relação ao GR:
693
  *  - atual.max > GR.max e atual.min < GR.min
694
  *  - atual.max > GR.max e atual.min < GR.min
694
  */
695
  */
695
 private boolean isOutsideReferencia(Candle referencia, Candle atual) {
696
 private boolean isOutsideReferencia(Candle referencia, Candle atual) {
696
     if (referencia == null || atual == null) return false;
697
     if (referencia == null || atual == null) return false;
697
     return atual.getMaxima() > referencia.getMaxima()
-
 
698
             && atual.getMinima() < referencia.getMinima();
-
 
-
 
698
     return BigDecimalUtils.ehMaiorQue(atual.getMaxima(), referencia.getMaxima())
-
 
699
             && BigDecimalUtils.ehMenorQue(atual.getMinima(), referencia.getMinima());
699
 }
700
 }
700
701
701
 // =====================================================================
702
 // =====================================================================
702
 // MODO TEMPO REAL (candle a candle)
703
 // MODO TEMPO REAL (candle a candle)
703
 // =====================================================================
704
 // =====================================================================
Line 745... Line 746...
745
         }
746
         }
746
     }
747
     }
747
748
748
     return null;
749
     return null;
749
 }
750
 }
750
}
-
 
-
 
751
-
 
752
}