Home › Forums › Trading System Mentor Course Community › AmiBroker Coding and AFL › Stoploss
- This topic is empty.
-
AuthorPosts
-
October 7, 2017 at 4:41 pm #101715ZachSwannMember
In an effort to make life easier I have added ATR, %, SAR and 1 dollar stop loss together so they can be optimized in one go.
I was sure of the SAR common parameters.
Still a work in progress suggestions?_SECTION_BEGIN(“StopLoss Optimisation”);
//ATR Stoploss
PF = Paramfield(“Price Field”,1); //Price field default to High
ATRP = Param(“ATR Stoploss Period”,20,1,100,0.1);
ATRV1 = Param(“ATR Variable”,0.3,0,10,0.01);
ATRV2 = Param(“ATR Variable”,0.6,0,10,0.01);
ATRV3 = Param(“ATR Variable”,0.9,0,10,0.01);
ATRV4 = Param(“ATR Variable”,1.2,0,10,0.01);
ATRV5 = Param(“ATR Variable”,1.5,0,10,0.01);
ATRV6 = Param(“ATR Variable”,1.8,0,10,0.01);
ATRV7 = Param(“ATR Variable”,2.1,0,10,0.01);
ATRV8 = Param(“ATR Variable”,2.4,0,10,0.01);
ATRV9 = Param(“ATR Variable”,2.7,0,10,0.01);
ATRV10 = Param(“ATR Variable”,3,0,10,0.01);
ATRV11 = Param(“ATR Variable”,3.3,0,10,0.01);
ATRV12 = Param(“ATR Variable”,3.6,0,10,0.01);
ATRV13 = Param(“ATR Variable”,3.9,0,10,0.01);
ATRV14 = Param(“ATR Variable”,4.2,0,10,0.01);
ATRV15 = Param(“ATR Variable”,4.5,0,10,0.01);
ATRV16 = Param(“ATR Variable”,4.8,0,10,0.01);
ATRV17 = Param(“ATR Variable”,5.2,0,10,0.01);
ATRV18 = Param(“ATR Variable”,5.5,0,10,0.01);
ATRV19 = Param(“ATR Variable”,5.8,0,10,0.01);
ATRV20 = Param(“ATR Variable”,6.1,0,10,0.01);
ATRV21 = Param(“ATR Variable”,6.5,0,10,0.01);
ATRV22 = Param(“ATR Variable”,7,0,10,0.01);
ATRV21 = Param(“ATR Variable”,8,0,10,0.01);
ATRV22 = Param(“ATR Variable”,9,0,10,0.01);
ATRV23 = Param(“ATR Variable”,10,0,10,0.01);
ATRSt1 = PF – (ATR(ATRP)*ATRV1);// 0
ATRSt2 = PF – (ATR(ATRP)*ATRV2);// 1
ATRSt3 = PF – (ATR(ATRP)*ATRV3);// 2
ATRSt4 = PF – (ATR(ATRP)*ATRV4);// 3
ATRSt5 = PF – (ATR(ATRP)*ATRV5);// 4
ATRSt6 = PF – (ATR(ATRP)*ATRV6);// 5
ATRSt7 = PF – (ATR(ATRP)*ATRV7);// 6
ATRSt8 = PF – (ATR(ATRP)*ATRV8);// 7
ATRSt9 = PF – (ATR(ATRP)*ATRV9);// 8
ATRSt10 = PF – (ATR(ATRP)*ATRV10);// 9
ATRSt11 = PF – (ATR(ATRP)*ATRV11);// 10
ATRSt12 = PF – (ATR(ATRP)*ATRV12);// 11
ATRSt13 = PF – (ATR(ATRP)*ATRV13);// 12
ATRSt14 = PF – (ATR(ATRP)*ATRV14);// 13
ATRSt15 = PF – (ATR(ATRP)*ATRV15);// 14
ATRSt16 = PF – (ATR(ATRP)*ATRV16);// 15
ATRSt17 = PF – (ATR(ATRP)*ATRV17);// 16
ATRSt18 = PF – (ATR(ATRP)*ATRV18);// 17
ATRSt19 = PF – (ATR(ATRP)*ATRV19);// 18
ATRSt20 = PF – (ATR(ATRP)*ATRV20);// 19
ATRSt21 = PF – (ATR(ATRP)*ATRV21);// 20
ATRSt22 = PF – (ATR(ATRP)*ATRV22);// 21
ATRSt23 = PF – (ATR(ATRP)*ATRV23);// 22
//
//% Stoploss
StopP2 = 1-Param(“Trailing Stop %”,2,1,100,0.1)/100;// 23
StopP4 = 1-Param(“Trailing Stop %”,4,1,100,0.1)/100;// 24
StopP6 = 1-Param(“Trailing Stop %”,6,1,100,0.1)/100;// 25
StopP8 = 1-Param(“Trailing Stop %”,8,1,100,0.1)/100;// 26
StopP10 = 1-Param(“Trailing Stop %”,10,1,100,0.1)/100;// 27
StopP12 = 1-Param(“Trailing Stop %”,12,1,100,0.1)/100;// 28
StopP14 = 1-Param(“Trailing Stop %”,14,1,100,0.1)/100;// 29
StopP16 = 1-Param(“Trailing Stop %”,16,1,100,0.1)/100;// 30
StopP18 = 1-Param(“Trailing Stop %”,18,1,100,0.1)/100;// 31
StopP20 = 1-Param(“Trailing Stop %”,20,1,100,0.1)/100;// 32
StopP22 = 1-Param(“Trailing Stop %”,22,1,100,0.1)/100;// 34
StopP24 = 1-Param(“Trailing Stop %”,24,1,100,0.1)/100;// 35
StopP26 = 1-Param(“Trailing Stop %”,26,1,100,0.1)/100;// 36
StopP28 = 1-Param(“Trailing Stop %”,28,1,100,0.1)/100;// 37
StopP30 = 1-Param(“Trailing Stop %”,30,1,100,0.1)/100;// 38
//
//Channel Stoploss
CP1 = Param(“Channel Period”,5,1,100,0.1);// 39
CP2 = Param(“Channel Period”,7,1,100,0.1);// 40
CP3 = Param(“Channel Period”,10,1,100,0.1);// 41
CP4 = Param(“Channel Period”,15,1,100,0.1);// 42
CP5 = Param(“Channel Period”,20,1,100,0.1);// 43
CP6 = Param(“Channel Period”,25,1,100,0.1);// 44
CP7 = Param(“Channel Period”,30,1,100,0.1);// 45
CP8 = Param(“Channel Period”,40,1,100,0.1);// 46
CP9 = Param(“Channel Period”,50,1,100,0.1);// 47
CP10 = Param(“Channel Period”,60,1,100,0.1);// 48
CP11 = Param(“Channel Period”,70,1,100,0.1);// 49
CP12 = Param(“Channel Period”,80,1,100,0.1);// 50
CP13 = Param(“Channel Period”,90,1,100,0.1);// 51
CP14 = Param(“Channel Period”,100,1,100,0.1);// 52
CP15 = Param(“Channel Period”,120,1,100,0.1);// 53
CP16 = Param(“Channel Period”,140,1,100,0.1);// 54
CP17 = Param(“Channel Period”,160,1,100,0.1);// 55
CP18 = Param(“Channel Period”,180,1,100,0.1);// 56
CP19 = Param(“Channel Period”,200,1,100,0.1);// 57
CStop1 = LLV(L-0.01,CP1);// 39
CStop2 = LLV(L-0.01,CP2);// 40
CStop3 = LLV(L-0.01,CP3);// 41
CStop4 = LLV(L-0.01,CP4);// 42
CStop5 = LLV(L-0.01,CP5);// 43
CStop6 = LLV(L-0.01,CP6);// 44
CStop7 = LLV(L-0.01,CP7);// 45
CStop8 = LLV(L-0.01,CP8);// 46
CStop9 = LLV(L-0.01,CP9);// 47
CStop10 = LLV(L-0.01,CP10);// 48
CStop11 = LLV(L-0.01,CP11);// 49
CStop12 = LLV(L-0.01,CP12);// 50
CStop13 = LLV(L-0.01,CP13);// 51
CStop14 = LLV(L-0.01,CP14);// 52
CStop15 = LLV(L-0.01,CP15);// 53
CStop16 = LLV(L-0.01,CP16);// 54
CStop17 = LLV(L-0.01,CP17);// 55
CStop18 = LLV(L-0.01,CP18);// 56
CStop19 = LLV(L-0.01,CP19);// 57
//
//SAR
sar1 = SAR(0.001,0.1);// 58
sar2 = SAR(0.002,0.1);// 59
sar3 = SAR(0.005,0.1);// 60
sar4 = SAR(0.010,0.1);// 61
sar5 = SAR(0.015,0.1);// 62
sar6 = SAR(0.020,0.1);// 63
sar7 = SAR(0.030,0.1);// 64
sar8 = SAR(0.040,0.1);// 65
sar9 = SAR(0.050,0.1);// 66
sar10 = SAR(0.060,0.1);// 67
sar11 = SAR(0.080,0.1);// 68
sar12 = SAR(0.001,0.15);// 69
sar13 = SAR(0.002,0.15);// 70
sar14 = SAR(0.005,0.15);// 71
sar15 = SAR(0.010,0.15);// 72
sar16 = SAR(0.015,0.15);// 73
sar17 = SAR(0.020,0.15);// 74
sar18 = SAR(0.030,0.15);// 75
sar19 = SAR(0.040,0.15);// 76
sar20 = SAR(0.050,0.15);// 77
sar21 = SAR(0.001,0.2);// 78
sar22 = SAR(0.002,0.2);// 79
sar23 = SAR(0.005,0.2);// 80
sar24 = SAR(0.010,0.2);// 81
sar25 = SAR(0.015,0.2);// 82
sar26 = SAR(0.020,0.2);// 83
sar27 = SAR(0.030,0.2);// 84
sar28 = SAR(0.040,0.2);// 85
sar29 = SAR(0.050,0.2);// 86
sar30 = SAR(0.060,0.2);// 87
sar31 = SAR(0.080,0.2);// 88
sar32 = SAR(0.001,0.4);// 89
sar33 = SAR(0.002,0.4);// 90
sar34 = SAR(0.005,0.4);// 91
sar35 = SAR(0.010,0.4);// 92
sar36 = SAR(0.015,0.4);// 93
sar37 = SAR(0.020,0.4);// 94
sar38 = SAR(0.030,0.4);// 95
sar39 = SAR(0.040,0.4);// 96
sar40 = SAR(0.050,0.4);// 97
sar41 = SAR(0.060,0.4);// 98
sar42 = SAR(0.080,0.4);// 99
sar43 = SAR(0.001,0.6);// 100
sar44 = SAR(0.002,0.6);// 101
sar45 = SAR(0.005,0.6);// 102
sar46 = SAR(0.010,0.6);// 103
sar47 = SAR(0.015,0.6);// 104
sar48 = SAR(0.020,0.6);// 105
sar49 = SAR(0.030,0.6);// 106
sar50 = SAR(0.040,0.6);// 107
sar51 = SAR(0.050,0.6);// 108
sar52 = SAR(0.060,0.6);// 109
sar53 = SAR(0.080,0.6);// 110
sar54 = SAR(0.001,0.;// 111
sar55 = SAR(0.002,0.;// 112
sar56 = SAR(0.005,0.;// 113
sar57 = SAR(0.010,0.;// 114
sar58 = SAR(0.015,0.;// 115
sar59 = SAR(0.020,0.;// 116
sar60 = SAR(0.030,0.;// 117
sar61 = SAR(0.040,0.;// 118
sar62 = SAR(0.050,0.;// 119
sar63 = SAR(0.060,0.;// 120
sar64 = SAR(0.080,0.;// 121stoplossoptimize = Optimize(“Stoploss”,0,0,122,1);
stoploss = IIf(stoplossoptimize == 0, ATRSt1,
IIf(stoplossoptimize == 1, ATRSt2,
IIf(stoplossoptimize == 2, ATRSt3,
IIf(stoplossoptimize == 3, ATRSt4,
IIf(stoplossoptimize == 4, ATRSt5,
IIf(stoplossoptimize == 5, ATRSt6,
IIf(stoplossoptimize == 6, ATRSt7,
IIf(stoplossoptimize == 7, ATRSt8,
IIf(stoplossoptimize == 8, ATRSt9,
IIf(stoplossoptimize == 9, ATRSt10,
IIf(stoplossoptimize == 10, ATRSt11,
IIf(stoplossoptimize == 11, ATRSt12,
IIf(stoplossoptimize == 12, ATRSt13,
IIf(stoplossoptimize == 13, ATRSt14,
IIf(stoplossoptimize == 14, ATRSt15,
IIf(stoplossoptimize == 15, ATRSt16,
IIf(stoplossoptimize == 16, ATRSt17,
IIf(stoplossoptimize == 17, ATRSt18,
IIf(stoplossoptimize == 18, ATRSt19,
IIf(stoplossoptimize == 19, ATRSt20,
IIf(stoplossoptimize == 20, ATRSt21,
IIf(stoplossoptimize == 21, ATRSt22,
IIf(stoplossoptimize == 22, ATRSt23,
IIf(stoplossoptimize == 23, StopP2,
IIf(stoplossoptimize == 24, StopP4,
IIf(stoplossoptimize == 25, StopP6,
IIf(stoplossoptimize == 26, StopP8,
IIf(stoplossoptimize == 27, StopP10,
IIf(stoplossoptimize == 28, StopP12,
IIf(stoplossoptimize == 29, StopP14,
IIf(stoplossoptimize == 30, StopP16,
IIf(stoplossoptimize == 31, StopP18,
IIf(stoplossoptimize == 32, StopP20,
IIf(stoplossoptimize == 33, StopP22,
IIf(stoplossoptimize == 34, StopP24,
IIf(stoplossoptimize == 35, StopP26,
IIf(stoplossoptimize == 36, StopP28,
IIf(stoplossoptimize == 37, StopP30,
IIf(stoplossoptimize == 38, CStop1,
IIf(stoplossoptimize == 39, CStop2,
IIf(stoplossoptimize == 40, CStop3,
IIf(stoplossoptimize == 41, CStop4,
IIf(stoplossoptimize == 42, CStop5,
IIf(stoplossoptimize == 43, CStop6,
IIf(stoplossoptimize == 44, CStop7,
IIf(stoplossoptimize == 45, CStop8,
IIf(stoplossoptimize == 46, CStop9,
IIf(stoplossoptimize == 47, CStop10,
IIf(stoplossoptimize == 48, CStop11,
IIf(stoplossoptimize == 49, CStop12,
IIf(stoplossoptimize == 50, CStop13,
IIf(stoplossoptimize == 51, CStop14,
IIf(stoplossoptimize == 52, CStop15,
IIf(stoplossoptimize == 53, CStop16,
IIf(stoplossoptimize == 54, CStop17,
IIf(stoplossoptimize == 55, CStop18,
IIf(stoplossoptimize == 56, CStop19,
IIf(stoplossoptimize == 57, sar1,
IIf(stoplossoptimize == 58, sar2,
IIf(stoplossoptimize == 59, sar3,
IIf(stoplossoptimize == 60, sar4,
IIf(stoplossoptimize == 61, sar5,
IIf(stoplossoptimize == 62, sar6,
IIf(stoplossoptimize == 63, sar7,
IIf(stoplossoptimize == 64, sar8,
IIf(stoplossoptimize == 65, sar9,
IIf(stoplossoptimize == 66, sar10,
IIf(stoplossoptimize == 67, sar11,
IIf(stoplossoptimize == 68, sar12,
IIf(stoplossoptimize == 69, sar13,
IIf(stoplossoptimize == 70, sar14,
IIf(stoplossoptimize == 71, sar15,
IIf(stoplossoptimize == 72, sar16,
IIf(stoplossoptimize == 73, sar17,
IIf(stoplossoptimize == 74, sar18,
IIf(stoplossoptimize == 75, sar19,
IIf(stoplossoptimize == 76, sar20,
IIf(stoplossoptimize == 77, sar21,
IIf(stoplossoptimize == 78, sar22,
IIf(stoplossoptimize == 79, sar23,
IIf(stoplossoptimize == 80, sar24,
IIf(stoplossoptimize == 81, sar25,
IIf(stoplossoptimize == 82, sar26,
IIf(stoplossoptimize == 83, sar27,
IIf(stoplossoptimize == 84, sar28,
IIf(stoplossoptimize == 85, sar29,
IIf(stoplossoptimize == 86, sar30,
IIf(stoplossoptimize == 87, sar31,
IIf(stoplossoptimize == 88, sar32,
IIf(stoplossoptimize == 89, sar33,
IIf(stoplossoptimize == 90, sar34,
IIf(stoplossoptimize == 91, sar35,
IIf(stoplossoptimize == 92, sar36,
IIf(stoplossoptimize == 93, sar37,
IIf(stoplossoptimize == 94, sar38,
IIf(stoplossoptimize == 95, sar39,
IIf(stoplossoptimize == 96, sar40,
IIf(stoplossoptimize == 97, sar41,
IIf(stoplossoptimize == 98, sar42,
IIf(stoplossoptimize == 99, sar43,
IIf(stoplossoptimize == 100, sar44,
IIf(stoplossoptimize == 101, sar45,
IIf(stoplossoptimize == 102, sar46,
IIf(stoplossoptimize == 103, sar47,
IIf(stoplossoptimize == 104, sar48,
IIf(stoplossoptimize == 105, sar49,
IIf(stoplossoptimize == 106, sar50,
IIf(stoplossoptimize == 107, sar51,
IIf(stoplossoptimize == 108, sar52,
IIf(stoplossoptimize == 109, sar53,
IIf(stoplossoptimize == 110, sar54,
IIf(stoplossoptimize == 111, sar55,
IIf(stoplossoptimize == 112, sar56,
IIf(stoplossoptimize == 113, sar57,
IIf(stoplossoptimize == 114, sar58,
IIf(stoplossoptimize == 115, sar59,
IIf(stoplossoptimize == 116, sar60,
IIf(stoplossoptimize == 117, sar61,
IIf(stoplossoptimize == 118, sar62,
IIf(stoplossoptimize == 119, sar63,
IIf(stoplossoptimize == 120, sar64,1)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))));//Last one is a one dollar stop
_SECTION_END();October 8, 2017 at 7:11 pm #107828SaidBitarMemberNormally on Trend Following systems i test many types of trailing stops but never thought of adding all of them to the system so i can switch between them.
but instead of having this lengthy code you can just run optimize on the values and no need to mention all of them one by one but non the less it is good idea. I am interested what you find works the best.
-
AuthorPosts
- You must be logged in to reply to this topic.