forked from SpiritPhu/Ami_code-.afl-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Special Code Num.2
1627 lines (1343 loc) · 65.8 KB
/
Special Code Num.2
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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Downloaded From https://www.WiseStockTrader.com
//================= PARTH DESAI===============
//=======================MASTER STRATEGY=====================
//########################################################################
// SETUP ALL PARAMETERS
//########################################################################
//
DisplayAlerts = ParamToggle("Pop up Alerts","Off|On",0);
VoiceAlerts = ParamToggle("Voice Alerts","Off|On",0);
timeout = 1000;
StaticVarSetText( "CurrPos", "" );
function GetSecondNum()
{
Time = Now( 4 );
Seconds = int( Time % 100 );
Minutes = int( Time / 100 % 100 );
Hours = int( Time / 10000 % 100 );
SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds );
return SecondNum;
}
function PopupWindowEx( popupID, bodytext, captiontext, timeout, left, top )
{
displayText = bodytext + captiontext;
if ( ( StaticVarGetText( Name() + "prevPopup" + popupID ) != displayText ) OR ( StaticVarGet( Name() + "prevPopupTime" + popupID ) < GetSecondNum() ) )
{
StaticVarSetText( Name() + "prevPopup" + popupID, displayText );
StaticVarSet( Name() + "prevPopupTime" + popupID, GetSecondNum() + timeout );
if (DisplayAlerts ==1) {
PopupWindow( bodytext, Captiontext + popupID, timeout, Left, top );
PlaySound( "c:\\windows\\media\\ding.wav" );
}
if (VoiceAlerts ==1) Say (bodytext);
}
}
//=============================SETUP TREND======================================
//SetBarsRequired(100000,0);
pds = 20;
MAFAST = EMA( Close, 20 );
MASLOW = EMA( Close, 40 );
DonchianUpper = HHV( Ref( H, -1 ), pds ); // Highest high value of highs in last 20 periods
DonchianLower = LLV( Ref( L, -1 ), pds ); // Lowest low value of low in last 20 periods
DonchianMiddle = ( DonchianUpper + DonchianLower ) / 2;
UpTrend = C > ( LLV( L, 20 ) + 2 * ATR( 10 ) ) AND EMA( Close, 20 ) > EMA( Close, 40 );
DnTrend = C < ( HHV( H, 20 ) - 2 * ATR( 10 ) ) AND EMA( Close, 20 ) < EMA( Close, 40 );
Color = IIf( UpTrend, colorGreen, IIf( DnTrend, colorRed, colorCustom10 ) );
// Plots a 20 period Donchian channel
//Plot( C, "Price", Color, styleBar | styleThick );
//=============================DISPLAY PARAMS======================================
_SECTION_BEGIN("Price");
Buy=Sell=Short=Cover=0;
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, High %g, Low %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN("Magnified Market Price");
procedure DrawData (Text, x1, y1, x2, y2, BoxColor, FontSize)
{
GfxSetOverlayMode(0);
GfxSelectFont("Segoe UI", FontSize, 600);
//FS=Param("Font Size",FontSize,FontSize,100,1);
//GfxSelectFont("Segoe UI", FS, 900, italic = False, underline = False, True );
GfxSetBkMode(1);
//GfxGradientRect(x1, y1, x2, y2, colorFrom, colorTo);
GfxSelectSolidBrush( BoxColor);
GfxRoundRect( x1, y1, x2, y2, 7, 7 );
GfxDrawText(Text, x1, y1, x2, y2, 32|0|4|16);
}
GfxSetTextColor(colorWhite);
if(StaticVarGetText("firstflag")=="")
{
StaticVarSetText ("firstflag","0");
}
if(StaticVarGetText("firstflag"+Name())=="")
{
StaticVarSet(("BuyIndex" + Name()), 0);
StaticVarSet(("BuyCount" + Name()), 0);
StaticVarSet(("BuyFlag" + Name()), 0);
StaticVarSet(("BuyPrice" + Name()), 0);
StaticVarSet(("BuyQty" + Name()), 0);
StaticVarSet(("SellIndex" + Name()), 0);
StaticVarSet(("SellCount" + Name()), 0);
StaticVarSet(("SellFlag" + Name()), 0);
StaticVarSet(("SellPrice" + Name()), 0);
StaticVarSet(("SellQty" + Name()), 0);
StaticVarSet("LTPSave" + Name(), 0);
StaticVarSet("LTQSave" + Name(), 0);
StaticVarSet("VolumeTemp" + Name(), 0);
StaticVarSet("AskSave" + Name(), 0);
StaticVarSet("BidSave" + Name(), 0);
StaticVarSet("LastLTPColor" + Name(), colorGrey40);
StaticVarSet("LastLTQColor" + Name(), colorGrey40);
StaticVarSet("LastAskColor" + Name(), colorGrey40);
StaticVarSet("LastBidColor" + Name(), colorGrey40);
StaticVarSetText("firstflag"+Name(), "0");
}
CurrentAskPrice = LastValue(Aux1);
CurrentBidPrice = LastValue(Aux2);
CurrentTradedPrice = LastValue(C);
CurrentVolume = LastValue(Volume);
LTPTemp = StaticVarGet("LTPSave" + Name());
LTQTemp = StaticVarGet("LTQSave" + Name());
VolumeTemp = StaticVarGet("VolumeTemp" + Name());
AskTemp = StaticVarGet("AskSave" + Name());
BidTemp = StaticVarGet("BidSave" + Name());
CurrentLTQ = (CurrentVolume - VolumeTemp);
if(CurrentLTQ < 0)
{
CurrentLTQ = CurrentLTQ * -1;
}
if(CurrentLTQ == 0)
{
CurrentLTQ = LTQTemp;
}
LTPColor = StaticVarGet("LastLTPColor" + Name());
LTQColor = StaticVarGet("LastLTQColor" + Name());
AskColor = StaticVarGet("LastAskColor" + Name());
BidColor = StaticVarGet("LastBidColor" + Name());
if(LTPTemp > CurrentTradedPrice)
{
LTPColor = colorRed;
}
else if(LTPTemp < CurrentTradedPrice)
{
LTPColor = colorGreen;
}
if(LTQTemp > CurrentLTQ)
{
LTQColor = colorRed;
}
else if(LTQTemp < CurrentLTQ)
{
LTQColor = colorGreen;
}
StaticVarSet("LastLTPColor" + Name(), LTPColor);
StaticVarSet("LastLTQColor" + Name(), LTQColor);
StaticVarSet("LastAskColor" + Name(), AskColor);
StaticVarSet("LastBidColor" + Name(), BidColor);
StaticVarSet("LTPSave" + Name(), CurrentTradedPrice);
StaticVarSet("LTQSave" + Name(), CurrentLTQ);
StaticVarSet("VolumeTemp" + Name(), CurrentVolume);
StaticVarSet("AskSave" + Name(), CurrentAskPrice);
StaticVarSet("BidSave" + Name(), CurrentBidPrice);
X0 = 10;
Y0 = 100;
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
//DrawData ("" + CurrentTradedPrice +" ("+xx+"%)", X0, Y0, X0+320, Y0+50, LTPColor, 30);
DrawData ("" + C, X0, Y0, X0+150, Y0+50, LTPColor, 30);
DrawData (" ("+xx+"%) ", X0+155, Y0, X0+300, Y0+50, LTPColor, 24);
DrawData (" LTQ : " + NumToStr(CurrentLTQ,1,0), X0, Y0+55, X0+100, Y0+85, LTQColor, 12);
/*
FS=Param("Font Size",30,30,100,1);
GfxSelectFont("Arial", FS, 900, italic = False, underline = False, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor( ParamColor("Color",colorYellow) );
//Hor=Param("Horizontal Position",800,800,800,800);
Hor=Param("Horizontal Position",100,100,100,100);
Ver=Param("Vertical Position",25,25,250,50);
GfxTextOut(""+C,Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
GfxSelectFont("Arial", 12, 700, italic = False, underline = False, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor(ParamColor("Color",colorYellow) );
GfxTextOut(""+DD+" ("+xx+"%)", Hor+5, Ver+45 );
*/
_SECTION_END();
/*_SECTION_BEGIN("Name");
GfxSetOverlayMode(0);
GfxSelectFont("Tahoma", Status("pxheight")/8 );
GfxSetTextAlign( 0 );// center alignment
GfxSetTextColor( ColorHSB( 42, 42, 42 ) );
GfxSetBkMode(1); // transparent
Hor=Param("Horizontal Position",100,100,100,100);
Ver=Param("Vertical Position",360,360,250,50);
GfxTextOut(""+Name(), Hor+5, Ver+45 );
_SECTION_END();
*/
_SECTION_BEGIN( "GFX EMA" );
procedure Plotlinewidth( pvalue, ptitle, pcolor, pstyle, pmin, pmax, pxshift, plinewidth, pshowdate )
{
local pvalue, ptitle, pcolor, pstyle, pmin, pmax, pxshift, plinewidth, ppenstyle, pshowdate;
local Miny, Maxy;
local Lvb, fvb;
local pxwidth, pxheight;
local TotalBars, axisarea;
local i, x, y;
if ( plinewidth > 0 && Status( "action" ) == 1 && ( pstyle & styleLine == styleLine ) )
{
////GfxSetOverlayMode( 0 );
Miny = Status( "axisminy" );
Maxy = Status( "axismaxy" );
lvb = Status( "lastvisiblebar" );
fvb = Status( "firstvisiblebar" );
pxwidth = Status( "pxwidth" );
pxheight = Status( "pxheight" );
TotalBars = Lvb - fvb;
xaxisarea = 56;
if ( pshowdate )
yaxisarea = 10;
else
yaxisarea = 0;
i = 0;
x = 5 + i * ( pxwidth - xaxisarea - 10 ) / ( TotalBars + 1 );
y = 5 + yaxisarea + ( pvalue[i+fvb] - Miny ) * ( pxheight - yaxisarea - 10 ) / ( Maxy - Miny );
GfxMoveTo( x, pxheight - y );
for ( i = 1; i < TotalBars AND i < ( BarCount - fvb ); i++ )
{
GfxSelectPen( pcolor[i + fvb], plinewidth, 0 );
x = 5 + i * ( pxwidth - xaxisarea - 10 ) / ( TotalBars + 1 );
y = 5 + yaxisarea + ( pvalue[i+fvb] - Miny ) * ( pxheight - yaxisarea - 10 ) / ( Maxy - Miny );
GfxLineTo( x, pxheight - y );
}
}
}
RequestTimedRefresh( 2 );
_SECTION_END();
_SECTION_BEGIN( "Small Triggers" );
p1 = Param( "TL 1 Periods", 20, 5, 50, 1 );
p2 = Param( "TL 2 Periods", 5, 3, 25, 1 );
TL1 = LinearReg( C, p1 );
TL2 = EMA( TL1, p2 );
Col1 = IIf( TL1 > TL2, ParamColor( "TL Up Colour", colorBrightGreen ), ParamColor( "TL Dn Colour", colorCustom12 ) );
ShortTrend = WriteIf( TL1 > TL2, "Uptrend", "Downtrend" );
Plot( TL1, "LinearReg(20)", Col1, styleLine | styleNoLabel );
Plot( TL2, "EMA(5)", Col1, styleLine | styleNoLabel ); // | styleThick
_SECTION_END();
_SECTION_BEGIN( "Large Triggers" );
p3 = Param( "TL 3 Periods", 80, 5, 100, 1 );
p4 = Param( "TL 4 Periods", 20, 3, 100, 1 );
TL3 = LinearReg( C, p3 );
TL4 = EMA( TL3, p4 );
Col1 = IIf( TL3 > TL4, ParamColor( "TLL Up Colour", colorBlue ), ParamColor( "TLL Dn Colour", colorRed ) );
LongTrend = WriteIf( TL3 > TL4, "Uptrend", "Downtrend" );
Plot( TL3, "LinearReg(80)", Col1, styleLine | styleNoLabel );
Plot( TL4, "EMA(20)", Col1, styleLine | styleNoLabel );
_SECTION_END();
_SECTION_BEGIN( "Fibo Retrace and Extensions" );
fibs = ParamToggle( "Plot Fibs", "Off|On", 1 );
pctH = Param ( "Pivot Hi %", 0.325, 0.001, 2.0, 0.002 );
HiLB = Param ( "Hi LookBack", 1, 1, BarCount - 1, 1 );
pctL = Param ( "Pivot Lo %", 0.325, 0.001, 2.0, 0.002 );
LoLB = Param ( "Lo LookBack", 1, 1, BarCount - 1, 1 );
Back = Param ( "Extend Left = 2", 1, 1, 500, 1 );
Fwd = Param( "Plot Forward", 0, 0, 500, 1 );
text = ParamToggle( "Plot Text", "Off|On", 1 );
hts = Param ( "Text Shift", -33.5, -50, 50, 0.10 );
style = ParamStyle( "Line Style", styleLine, styleNoLabel );
x = BarIndex();
pRp = PeakBars( H, pctH, 1 ) == 0;
yRp0 = SelectedValue( ValueWhen( pRp, H, HiLB ) );
xRp0 = SelectedValue( ValueWhen( pRp, x, HiLB ) );
pSp = TroughBars( L, pctL, 1 ) == 0;
ySp0 = SelectedValue( ValueWhen( pSp, L, LoLB ) );
xSp0 = SelectedValue( ValueWhen( pSp, x, LoLB ) );
Delta = yRp0 - ySp0;
function fib( ret )
{
retval = ( Delta * ret );
Fibval = IIf( ret < 1.0
AND xSp0 < xRp0, yRp0 - retval, IIf( ret < 1.0
AND xSp0 > xRp0, ySp0 + retval, IIf( ret > 1.0
AND xSp0 < xRp0, yRp0 - retval, IIf( ret > 1.0
AND xSp0 > xRp0, ySp0 + retval, Null ) ) ) );
return FibVal;
}
x0 = Min( xSp0, xRp0 ) - Back;
x1 = ( BarCount - 1 );
r236 = fib( 0.236 );
r236I = LastValue ( r236, 1 );
r382 = fib( 0.382 );
r382I = LastValue ( r382, 1 );
r050 = fib( 0.50 );
r050I = LastValue ( r050, 1 );
r618 = fib( 0.618 );
r618I = LastValue ( r618, 1 );
r786 = fib( 0.786 );
r786I = LastValue ( r786, 1 );
e127 = fib( 1.27 );
e127I = LastValue ( e127, 1 );
e162 = fib( 1.62 );
e162I = LastValue ( e162, 1 );
e200 = fib( 2.00 );
e200I = LastValue ( e200, 1 );
e262 = fib( 2.62 );
e262I = LastValue ( e262, 1 );
e424 = fib( 4.24 );
e424I = LastValue ( e424, 1 );
p00 = IIf( xSp0 > xRp0, ySp0, yRp0 );
p00I = LastValue ( p00, 1 );
p100 = IIf( xSp0 < xRp0, ySp0, yRp0 );
p100I = LastValue ( p100, 1 );
color00 = IIf( xSp0 > xRp0, colorLime, colorRed );
color100 = IIf( xSp0 < xRp0, colorLime, colorRed );
numbars = LastValue( Cum( Status( "barvisible" ) ) );
fraction = IIf( StrRight( Name(), 3 ) == "", 3.2, 3.2 );
if ( fibs == 1 )
{
Plot( LineArray( xRp0 - Fwd, yRp0, x1, yRp0, Back ), "PR", 32, 8 | styleNoRescale, Null, Null, Fwd );
Plot( LineArray( xSp0 - Fwd, ySp0, x1, ySp0, Back ), "PS", 27, 8 | styleNoRescale, Null, Null, Fwd );
Plot( LineArray( x0 - Fwd, r236, x1, r236, Back ), "", 45, style | styleNoRescale, Null, Null, Fwd );
Plot( LineArray( x0 - Fwd, r382, x1, r382, Back ), "", 44, style | styleNoRescale, Null, Null, Fwd );
Plot( LineArray( x0 - Fwd, r050, x1, r050, Back ), "", 41, style | styleNoRescale, Null, Null, Fwd );
Plot( LineArray( x0 - Fwd, r618, x1, r618, Back ), "", 43, style | styleNoRescale, Null, Null, Fwd );
Plot( LineArray( x0 - Fwd, r786, x1, r786, Back ), "", 42, style | styleNoRescale, Null, Null, Fwd );
Plot( LineArray( x0 - Fwd, e127, x1, e127, Back ), "e127", 47, style | styleNoRescale, Null, Null, Fwd );
Plot( LineArray( x0 - Fwd, e162, x1, e162, Back ), "e162", 47, style | styleNoRescale, Null, Null, Fwd );
Plot( LineArray( x0 - Fwd, e200, x1, e200, Back ), "p200", 47, style | styleNoRescale, Null, Null, Fwd );
Plot( LineArray( x0 - Fwd, e262, x1, e262, Back ), "p262", 47, style | styleNoRescale, Null, Null, Fwd );
Plot( LineArray( x0 - Fwd, e424, x1, e424, Back ), "p424", 25, style | styleNoRescale, Null, Null, Fwd );
}
if ( text == 1 )
{
PlotText( " 0% = " + WriteVal( p00, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), p00I + 0.05, color00 );
PlotText( "23% = " + WriteVal( r236, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), r236I + 0.05, 45 );
PlotText( "38% = " + WriteVal( r382, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), r382I + 0.05, 44 );
PlotText( "50% = " + WriteVal( r050, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), r050I + 0.05, 41 );
PlotText( "62% = " + WriteVal( r618, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), r618I + 0.05, 43 );
PlotText( "78% = " + WriteVal( r786, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), r786I + 0.05, 42 );
PlotText( "100% = " + WriteVal( p100, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), p100I + 0.05, color100 );
PlotText( "127% = " + WriteVal( e127, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), e127I + 0.05, 47 );
PlotText( "162% = " + WriteVal( e162, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), e162I + 0.05, 47 );
PlotText( "200% = " + WriteVal( e200, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), e200I + 0.05, 47 );
PlotText( "262% = " + WriteVal( e262, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), e262I + 0.05, 47 );
PlotText( "424% = " + WriteVal( e424, fraction ), LastValue( BarIndex() ) - ( numbars / hts ), e424I + 0.05, 25 );
}
_SECTION_END();
_SECTION_BEGIN( "Pivot Finder for Amibroker" );
/* **********************************
Code to automatically identify pivots
********************************** */
// -- what will be our lookback range for the hh and ll?
farback = Param( "How Far back to go", 100,50, 5000, 10 );
nBars = Param( "Number of bars", 12, 5, 40 );
// -- Title.
/*Title = Name() + " (" + StrLeft( FullName(), 15 ) + ") O: " + Open + ",
H: " + High + ", L: " + Low + ", C: " + Close;
*/
// -- Plot basic candle chart
/*PlotOHLC( Open, High, Low, Close,
"BIdx = " + BarIndex() +
"\n" + "O = " + O + "\n" + "H = " + H + "\n" + "L = " + L
+ "\n" + "C ",
colorBlack, styleBar );
*/
GraphXSpace = 7;
// -- Create 0-initialized arrays the size of barcount
aHPivs = H - H;
aLPivs = L - L;
// -- More for future use, not necessary for basic plotting
aHPivHighs = H - H;
aLPivLows = L - L;
aHPivIdxs = H - H;
aLPivIdxs = L - L;
nHPivs = 0;
nLPivs = 0;
lastHPIdx = 0;
lastLPIdx = 0;
lastHPH = 0;
lastLPL = 0;
curPivBarIdx = 0;
// -- looking back from the current bar, how many bars
// back were the hhv and llv values of the previous
// n bars, etc.?
aHHVBars = HHVBars( H, nBars );
aLLVBars = LLVBars( L, nBars );
aHHV = HHV( H, nBars );
aLLV = LLV( L, nBars );
// -- Would like to set this up so pivots are calculated back from
// last visible bar to make it easy to "go back" and see the pivots
// this code would find. However, the first instance of
// _Trace output will show a value of 0
aVisBars = Status( "barvisible" );
nLastVisBar = LastValue( Highest( IIf( aVisBars, BarIndex(), 0 ) ) );
_TRACE( "Last visible bar: " + nLastVisBar );
// -- Initialize value of curTrend
curBar = ( BarCount - 1 );
curTrend = "";
if ( aLLVBars[curBar] <
aHHVBars[curBar] )
{
curTrend = "D";
}
else
{
curTrend = "U";
}
// -- Loop through bars. Search for
// entirely array-based approach
// in future version
for ( i = 0; i < farback; i++ ){
curBar = ( BarCount - 1 ) - i;
// -- Have we identified a pivot? If trend is down...
if ( aLLVBars[curBar] < aHHVBars[curBar] )
{
// ... and had been up, this is a trend change
if ( curTrend == "U" )
{
curTrend = "D";
// -- Capture pivot information
curPivBarIdx = curBar - aLLVBars[curBar];
aLPivs[curPivBarIdx] = 1;
aLPivLows[nLPivs] = L[curPivBarIdx];
aLPivIdxs[nLPivs] = curPivBarIdx;
nLPivs++;
}
// -- or current trend is up
}
else
{
if ( curTrend == "D" )
{
curTrend = "U";
curPivBarIdx = curBar - aHHVBars[curBar];
aHPivs[curPivBarIdx] = 1;
aHPivHighs[nHPivs] = H[curPivBarIdx];
aHPivIdxs[nHPivs] = curPivBarIdx;
nHPivs++;
}
// -- If curTrend is up...else...
}
// -- loop through bars
}
// -- Basic attempt to add a pivot this logic may have missed
// -- OK, now I want to look at last two pivots. If the most
// recent low pivot is after the last high, I could
// still have a high pivot that I didn't catch
// -- Start at last bar
curBar = ( BarCount - 1 );
candIdx = 0;
candPrc = 0;
lastLPIdx = aLPivIdxs[0];
lastLPL = aLPivLows[0];
lastHPIdx = aHPivIdxs[0];
lastHPH = aHPivHighs[0];
if ( lastLPIdx > lastHPIdx )
{
// -- Bar and price info for candidate pivot
candIdx = curBar - aHHVBars[curBar];
candPrc = aHHV[curBar];
if (
lastHPH < candPrc AND
candIdx > lastLPIdx AND
candIdx < curBar )
{
// -- OK, we'll add this as a pivot...
aHPivs[candIdx] = 1;
// ...and then rearrange elements in the
// pivot information arrays
for ( j = 0; j < nHPivs; j++ )
{
aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-
( j+1 )];
aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-( j+1 )];
}
aHPivHighs[0] = candPrc ;
aHPivIdxs[0] = candIdx;
nHPivs++;
}
}
else
{
// -- Bar and price info for candidate pivot
candIdx = curBar - aLLVBars[curBar];
candPrc = aLLV[curBar];
if (
lastLPL > candPrc AND
candIdx > lastHPIdx AND
candIdx < curBar )
{
// -- OK, we'll add this as a pivot...
aLPivs[candIdx] = 1;
// ...and then rearrange elements in the
// pivot information arrays
for ( j = 0; j < nLPivs; j++ )
{
aLPivLows[nLPivs-j] = aLPivLows[nLPivs-( j+1 )];
aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-( j+1 )];
}
aLPivLows[0] = candPrc;
aLPivIdxs[0] = candIdx;
nLPivs++;
}
}
// -- Dump inventory of high pivots for debugging
/*
for (k=0; k<nHPivs; k++) {
_TRACE("High pivot no. " + k
+ " at barindex: " + aHPivIdxs[k] + ", "
+ WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],
DateTime(), 1), formatDateTime)
+ ", " + aHPivHighs[k]);
}
*/
// -- OK, let's plot the pivots using arrows
//============== EXPLORATION ==============
Buy = aLPivs==1;
Sell = aHPivs==1;
//Buy = ExRem(Buy,Sell);
//Sell = ExRem(Sell,Buy);
BuyPrice = ValueWhen( Buy, O );
SellPrice = ValueWhen( Sell, O );
/*SellPrice=ValueWhen(Sell,C,1);
BuyPrice=ValueWhen(Buy,C,1);
Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy );
*/
autotrader= ParamToggle( "Automate Trade", "No|Yes", 0 );
if (autotrader == 1) SATC1.(Open, High, Low, Close, Volume, Aux1, Aux2, Buy, Sell, Sell, Buy);
for(i=BarCount-1;i>1;i--)
{
if(Buy[i] == 1)
{
entry = C[i];
sig = "BUY";
bars = i;
i = 0;
}
if(Sell[i] == 1)
{
sig = "SELL";
entry = C[i];
bars = i;
i = 0;
}
}
if ( sig =="BUY" )
{
PopupWindowEx( "ID:0", "Get Ready to BUY \n" + Name() + " @ " + BuyPrice, "Buy Alert -", timeout, 100, 1 );
}
else
{
PopupWindowEx( "ID:1", "Get Ready to SELL \n" + Name() + " @ " + SellPrice, "Sell Alert -", timeout, 100, 1 );
}
PlotShapes(shapeUpArrow*Buy,colorBrightGreen);
PlotShapes(shapeDownArrow*Sell,colorRed);
//PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, High, Offset = -15 );
//PlotShapes( IIf( Buy, shapeUpArrow , shapeNone ), colorBrightGreen, 0, Low, Offset = -15 );
//AlertIf( Sell, "SOUND C:\\Windows\\Media\\Ding.wav", "Sell " + C, 2, 1 + 2, 1 );
//AlertIf( Buy, "SOUND C:\\Windows\\Media\\Ding.wav", "Buy " + C, 1, 1 + 2, 1 );
dist = 1.1 * ATR( 15 );
for ( i=BarCount-1; i > 0; i-- )
{
if ( Buy[i] )
PlotText( "Buy@" + O[ i ], i, L[ i ] - dist[i], colorWhite, colorGreen);
if ( Sell[i] )
PlotText( "Sell@" + O[ i ], i, H[ i ] + dist[i], colorWhite, colorRed);
}
//============== MESSAGE BOARD ==============
messageboard = ParamToggle( "Message Board", "Show|Hide", 0 );
//CUSTOM CODE FOR MESSAGE BOARD
Hp = HHV( H, 40 );
Lp = LLV( L, 40 );
BarsSincebuy = BarsSince( Buy );
BarsSinceshort = BarsSince( Sell );
LastSignal = IIf( BarsSincebuy < BarsSinceshort, 1, -1 );
Sig = WriteIf( BarsSincebuy < BarsSinceshort, "BUY", "SELL" );
slPrice = IIf( LastSignal == 1, HighestSince( Buy, Lp ), LowestSince( Sell , Hp ) );
initialrisk = IIf( LastSignal == 1, BuyPrice - SLPrice, SLPrice - SellPrice );
CurrentPL = IIf( LastSignal == 1, C - BuyPrice, SellPrice - C );
entry = IIf( LastSignal == 1, BuyPrice, SellPrice );
bars = LastValue( IIf( BarsSincebuy < BarsSinceshort, BarsSincebuy, BarsSinceshort ) );
Offset = 15;
Clr = IIf( LastValue( LastSignal ) == 1, colorGreen, colorRed );
if (messageboard == 0 )
{
GfxSelectFont( "Tahoma", 11, 700 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );
GfxSetOverlayMode(0);
LongCaution = ShortCaution = "";
if ( SelectedValue( LastSignal ) == 1 )
{
GfxSelectSolidBrush( colorDarkGreen );
Datetim = "" + ValueWhen( Buy, Hour(), 1 ) + ":" + ValueWhen( Buy, Minute(), 1 );
tar1 = entry + ( entry * .0050 );
tar2 = entry + ( entry * .0092 );
tar3 = entry + ( entry * .0179 );
LongCaution = WriteIf(LongTrend == "Downtrend", " (Caution)", "");
ShortCaution = WriteIf(ShortTrend == "Downtrend", " (Caution)", "");
}
else
{
GfxSelectSolidBrush( colorDarkRed );
Datetim = "" + ValueWhen( Sell, Hour(), 1 ) + ":" + ValueWhen( Sell, Minute(), 1 );
tar1 = entry - ( entry * .0050 );
tar2 = entry - ( entry * .0112 );
tar3 = entry - ( entry * .0212 );
LongCaution = WriteIf(LongTrend == "Uptrend", " (Caution)", "");
ShortCaution = WriteIf(ShortTrend == "Uptrend", " (Caution)", "");
}
pxHeight = Status( "pxchartheight" );
xx = Status( "pxchartwidth" );
Left = 1100;
width = 310;
x = 1.5;
x2 = 300;
boxheight = 210;
y = pxHeight / 1;
GfxSelectPen( colorLightBlue, 1 );
GfxRoundRect( x, y - 210, x2, y , 7, 7 ) ;
GfxTextOut( ( " Trading System "),50, y-boxheight+10);
GfxTextOut( ( " ..........................................." ), 5, y-boxheight+20 );
GfxTextOut( ( "Last Signal" ), 13,y-boxheight+40 );
GfxTextOut( ( ": " + Datetim ), 130, y-boxheight+40 );
GfxTextOut( ( "" + sig + " Entry @" ), 13, y-boxheight+60 );
GfxTextOut( ( ": " + entry ), 130, y-boxheight+60 );
GfxTextOut( ( "Current P/L" ), 13, y-boxheight+80 );
GfxTextOut( ( ": " + WriteVal( IIf( sig == "BUY", ( C - entry ), ( entry - C ) ), 2.2 ) ), 130, y-boxheight+80 );
GfxTextOut( ( "Long Trend" ), 13, y-boxheight+100 );
GfxTextOut( ( ": " + LongTrend + LongCaution ), 130, y-boxheight+100 );
GfxTextOut( ( "Short Trend" ), 13, y-boxheight+120 );
GfxTextOut( ( ": " + ShortTrend + ShortCaution ), 130, y-boxheight+120 );
GfxTextOut( ( " ..........................................." ), 5, y-boxheight+130 );
GfxTextOut( ("Target1"), 13, y-boxheight+150);
GfxTextOut( ( ": " + tar1 ), 130, y-boxheight+150 );
GfxTextOut( ("Target2"), 13, y-boxheight+170);
GfxTextOut( ( ": " + tar2), 130, y-boxheight+170 );
GfxTextOut( ("Target3"), 13, y-boxheight+190);
GfxTextOut( ( ": " + tar3), 130, y-boxheight+190 );
}
_SECTION_END();
//SetChartBkGradientFill( ParamColor( "BgTop", ColorRGB( 0, 0, 0 ) ), ParamColor( "BgBottom", ColorRGB( 0, 0, 0 ) ), ParamColor( "titleblock", ColorRGB( 0, 0, 0 ) ) );
_SECTION_BEGIN("Volume At Price");
PlotVAPOverlay(Param("Lines", 1000, 100, 1000, 10), Param("Width", 15, 1, 100, 1), ParamColor("Color", colorBlue), ParamToggle("Side", "Left|Right", 1) | 4 *ParamToggle("Z-order", "On top|Behind", 1));
_SECTION_END();
//Volume Price Analysis AFL - VPA Version 3.0 -15-06-2015
// Revision Details
// V-2.0 AFL - fully re written for clarity, Minor bugs removed
// V-2.1 support and resistance line added
// V-2.2 Commentary for support and resistance line breaks Added.
// V-2.3 Revision detail- High volume Lines added
// V-2.4 Toggle switch for plotting S/R, High volume and Trend lines added
// V-2.5 Bar coloring option included - VSA based or Trend Based
// V-3.0 Trend detection Method changed to "Random Walk"
//===================Version V.3.0 ======================
//=========================================================================|
// VPA Basic Module |
//=========================================================================|
_SECTION_BEGIN("VPA Basic Module");
SetChartOptions(0,chartShowArrows|chartShowDates);
gxs=Param("GRAPH spaceing",10,5,50,5);
GraphXSpace = gxs;
SetChartBkColor(ParamColor("Outer panel",colorBlack)); // color of outer border
SetChartBkGradientFill( ParamColor("Inner panel upper",colorBlack),ParamColor("Inner panel lower",colorBlack));
//===================== Basic Definitions =======================================
volAvg = MA(V,90);
volMean = StDev(volAvg,30);
volUpBand3 = volAvg + 3*volMean;
volUpband2 = volAvg + 2*volMean;;
volUpBand1 = volAvg + 1*volMean;;
volDnBand1 = volAvg -1*volMean;
volDnBand2 = volAvg -2*volMean;
midprice = (H+L)/2;
spread = (H-L);
avgSpread = MA(spread,90);
wideRangeBar = spread>(1.5*avgSpread);
narrowRangeBar = spread<(0.7*avgSpread);
lowVolume = V<Ref(V,-1) AND V<Ref(V,-2);
upBar = C>Ref(C,-1);
downBar = C<Ref(C,-1);
highVolume = V>Ref(V,-1) AND Ref(V,-1)>Ref(V,-2);
closeFactor = C-L;
clsPosition = spread/closeFactor;
closePosition = IIf(closeFactor==0,avgSpread,clsPosition);
Vb = V>volAvg OR V>Ref(V,-1);
upClose = C>=((spread*0.7)+L);// close is above 70% of the Bar
downClose = C<=((spread*0.3)+L);// close is below the 30% of the bar
aboveClose = C>((spread*0.5)+L);// close is between 50% and 70% of the bar
belowClose = C<((spread*0.5)+L);// close is between 50% and 30% of the bar
midClose = C>((spread*0.3)+L) AND C<((spread*0.7)+L);// close is between 30% and 70% of the bar
veryLowClose = closePosition>4;//close is below 25% of the bar
veryHighClose = closePosition<1.35;// Close is above 80% of the bar
ClosePos = IIf(C<=((spread*0.3)+L),1,IIf(C<=((spread*0.5)+L),2,IIf(C<=((spread*0.7)+L),3,4)));
// 1 = downclose, 2 = belowclose, 3 = aboveClose, 4 = Upclose
Volpos = IIf(V>volAvg*2,1,IIf(V>volAvg*1.3,2,IIf(V>volAvg,3,IIf(V<volAvg AND V>volAvg*0.7,4,5))));
// 1 = Very High, 2 = High, 3 = Above Average, 4 = Less than Average, 5 = Low
freshGndHi = C > HHV(H,5);
freshGndLo = C < LLV(L,5);
//========================Trend Estimation =========================
j=MA(C,5);
trendLongTerm = LinRegSlope(j,40) ;
trendMediumTerm = LinRegSlope(j,10) ;
trendShortTerm = LinRegSlope(j,3);
tls=LinRegSlope(j,3);
_SECTION_END();
//=========================================================================|
// Trend Analysis Module |
//=========================================================================|
_SECTION_BEGIN("Trend Analysis");
SetChartOptions(0,chartShowArrows|chartShowDates);
minperiodsRWIst = Param ( "Short term Min Periods", 2, 1, 9, 1);
maxperiodsRWIst = Param ( "Short term Max Periods", 8, 1, 9, 1);
minperiodsRWIlt = Param ( "Long Term Min Periods", 10, 1, 32, 1);
maxperiodsRWIlt = Param ( "Long term Max Periods", 40, 1, 64, 1);
Ground = RWIHi (minperiodsRWIst, maxperiodsRWIst);
Sky = RWILo (minperiodsRWIst, maxperiodsRWIst);
j = RWI(minperiodsRWIlt, maxperiodsRWIlt);
k = RWI(minperiodsRWIst, maxperiodsRWIst);
j2 = RWIHi (minperiodsRWIlt, maxperiodsRWIlt);
k2 = RWILo (minperiodsRWIlt, maxperiodsRWIlt);
ja = Cross(j,1); // The followign section check the diffeent condition of the RWi above and below zero
jb = Cross(1,j); // In oder to check which trend is doing what
jc = Cross(-1,j);
jd = Cross(j,-1);
j2a = Cross(j2,1);
j2b = Cross(1,j2);
k2a = Cross(k2,1);
k2b = Cross(1,k2);
//Define the Major, Minor and Immediate trend Sttatus
upmajoron = j > 1 AND Ref(ja,-1);
upmajoroff = j < 1 AND Ref(jb,-1);
upminoron = j2 > 1 AND Ref(j2a,-1);
upminoroff = j2 < 1 AND Ref(j2b,-1);
dnmajoron = j < -1 AND Ref(jc,-1);
dnmajoroff = j > -1 AND Ref(jd,-1);
dnminoron = k2 > 1 AND Ref(k2a,-1);
dnminoroff = k2 < 1 AND Ref(k2b,-1);
upimd = IIf(ground > 1, 1,0);
dnimd = IIf(sky > 1, 1, 0);
upmajor = IIf(j>1,1,IIf(j<(-1),-1,0));
upminor = IIf(j2>1,1,-1);
dnminor = IIf(k2>1,1,-1);
_SECTION_END();
//======================================================================|
// VSA Signal generation |
//======================================================================|
_SECTION_BEGIN("Signal Generation");
upThrustBar = wideRangeBar AND downClose AND trendShortTerm>0 AND H>Ref(H,-1);//WRB and UHS and Fresh Ground
nut = wideRangeBar AND downClose AND freshGndHi AND HighVolume;// NEW SIGNAL
bc = wideRangeBar AND aboveclose AND V == HHV(V,60) AND upmajor==1;// NEW SIGNAL
upThrustBartrue = wideRangeBar AND downClose AND upmajor>0 AND H>Ref(H,-1);//occurs after a major uptrend
upThrustTHV = upThrustBartrue AND (VolPos == 2 OR VolPos == 1);
upThrustCond1 = Ref(upThrustBar,-1) AND downBar ;
upThrustCond2 = Ref(upThrustBar,-1) AND downBar AND VolPos == 2;
upThrustCond3 = upThrustBar AND VolPos ==1;
topRevBar = Ref(V,-1)>volAvg AND Ref(upBar,-1) AND Ref(wideRangeBar,-1) AND downBar AND downClose AND wideRangeBar AND trendLongTerm>0 AND H==HHV(H,10);
PseudoUpThrust = Ref(upBar,-1) AND Ref(V,-1)>1.5*volAvg AND downBar AND downClose AND NOT upThrustBar;
pseudoUtCond = Ref(PseudoUpThrust,-1) AND downBar AND downClose AND NOT upThrustBar;
trendChange = Ref(upBar,-1) AND H==HHV(H,5)AND downBar AND (downClose OR midClose) AND V>volAvg AND NOT wideRangeBar AND NOT PseudoUpThrust ;
sellCond1 = (upThrustCond1 OR upThrustCond2 OR upThrustCond3) ;
sellCond2 = Ref(sellCond1,-1)==0;
sellCond = sellCond1 AND sellCond2;
strengthDown0 = trendLongTerm<0 AND V>Ref(V,-1) AND Ref(downBar,-1) AND upBar AND (upClose OR midClose) AND trendShortTerm<0 AND trendMediumTerm<0;// strength after a long down trend
strengthDown = V>Ref(V,-1) AND Ref(downBar,-1) AND upBar AND (upclose OR midClose) AND trendShortTerm<0 AND trendMediumTerm<0;// Strength after a down trend