[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: "smart" trail stop


  • Date: Mon, 30 Nov 2009 21:08:55 -0000
  • From: "de_techneut" <twistedcharts@xxxxxxxxx>
  • Subject: [amibroker] Re: "smart" trail stop

PureBytes Links

Trading Reference Links

Hi Mark,

thanks for sharing this code.
this will definitely help me in my further study of the market.

I am just starting in Amibroker so i'm not yet an accomplished programmer. (i doubt i ever will be)

I know it's probably an open door but here it is anyway:
a good stoploss is indeed essential to all trading systems.
what is equally important, imo, is a good profit target mechanism as a way to get out of a trade.

I find the classic pivots to do a marvelous job at that.
I usually calculate pivots depending on the time frame i'm trading, (around 30 to 60 bars) i.e. 1 hour pivots for a minute chart or weekly pivots for a hourly chart.

hope this helps you in some way.

Thanks for sharing,

Marc.



--- In amibroker@xxxxxxxxxxxxxxx, "r22mark" <mtf_79@xxx> wrote:
>
> Hi.
> 
> Money / risk management is an area of interest to me. I have tried to create a "smart" trailing stop, one that adjusts itself. There is also some stuff that people may not have encountered - Fixed Fractional Position Sizing with margin (to mimic CFD's) , various buy "filters", PositionScore, and the Graph / indicator plot code is handy for displaying trade arrows, and trade prices. Hope there is something of use for you. This is NOT a ready to trade system - you will likely find results are poor. It IS an example of what can be done other than a basic trail stop. This is an ongoing project. Plot it or drag it onto a chart to view / adjust the parameters.
> 
> The risk management side has:
> - initial stop set at 20%.
> - after 5 days stop raised /risk reduced by 50%
> - stop moved to break even after 10 days
> - after 15 days stop raised / risk reduced by 50%
> - at any time you have 3 lower lows, risk is reduced by 25%, every time.
> - these can all adjusted via parameters.
> 
> Again this is a code in progress, I use Plot() to verify what is happening at each bar. If you want to check my work, uncomment them. Let me know if you find any errors. 
> 
> So people. Comments? Ideas? Feedback? I haven't heard of anyone doing "smart" trail stops. Or am I am just wasting my time?
> 
> Mark
> 
> 
> SetTradeDelays (0,0,0,0);
> SetOption("InitialEquity", 100000); 
> SetOption("AccountMargin", 10);               // only put up 10% funds, buying power x 10
> SetOption("CommissionMode", 1);   
> SetOption("CommissionAmount", 0.10);          // .1% commission per entry / exit
> SetOption("MaxOpenPositions", 12);
> SetOption("AllowSameBarExit", True);
> 
> //  FIXED FRACTIONAL POSITION SIZING
> CapRisk = Param("CapitalRisk", 2, 1,5,0.5);  // capital risk %
> MaxCap = 10;                                 // max capital % in one trade (10% capital = 2% stop)
> StopPct = Param("StopPct", 20, 1,25,1)/100;   // stop % - initial risk
> FFRisk = Min(CapRisk/(StopPct*BuyPrice)*BuyPrice/10, MaxCap);
> "FF risk = " +WriteVal(ffrisk, 1.2);  
> SetPositionSize(FFRisk * 10, spsPercentOfEquity);
> 
> 
> //  POS SCORE - BANG FOR BUCK ($10,000)
> B4B = 10000/Ref(C,-1)*Ref(ATR(200),-1)/100;
> "10k BFB = " +WriteVal(B4B, 1.2);
> PositionScore = B4B;
> 
> 
> //  INDEX TREND FILTER - 2 x moving averages
> SetForeign("$DJ");    // change to whatever index is of interest
> IFa = Param("IF FMA", 30,0,100,5); IFb  = Param("IF SMA", 100,0,300,5);      
> IFx = EMA(C,IFa);  IFy = MA(C,IFb);  IndexFilter = IFx > IFy;
> //Plot(IFx, "IF FMA", colorBlue, styleLine);  Plot(IFy, "IF SMA", colorOrange, styleLine); 
> //PlotForeign("XAO", "All Ords", colorBlack, styleLine);
> //Plot(IndexFilter,"Index Filter", colorBlack, styleLine);
> RestorePriceArrays(); 
> 
> 
> //  TREND FILTER - 2 x moving averages
> j = Param("FMA", 30,0,50,5);        k = Param("SMA", 100,0,100,5);    
> FMA = EMA(C,j);    SMA = EMA(C,k);     TF = FMA > SMA;
> //Plot(FMA, "FMA", colorBlue, styleLine); Plot(SMA, "SMA", colorOrange, styleLine);
> 
> 
> // VOL FILTER - MONEY FLOW > $1 MILLION
> VF = EMA(V*C,21) > 1000000;
> Filter = VF; AddColumn(VF, "volFilter"); AddColumn(EMA(V*C,21), "V*C", 1.2);  // leaves approx 225 from ASX 300
> 
> BDC1 = Param("#buydays", 20, 5,50,5);
> BDC2 = Param("#buydays2", 70, 5,100,5);
> Cond1 = H > Ref(HHV(H,BDC1),-1) + 0.01;
> Cond2 = H > Ref(HHV(H,BDC2),-1) + 0.01;
> Buy = Cond1 AND Cond2 AND IndexFilter AND VF AND TF;
> BuyPrice = Max(O, Ref(HHV(H,BDC1),-1) + 0.01);
> 
> Sell = 0;
> 
> //  REDUCE RISK - RAISE STOP X 2, RAISE TO BREAKEVEN, TIGHTEN AFTER 3 DOWN DAYS
> MSD = Param("#DaysB4MoveStop1", 5, 0,50,1) -1;   // needs -1 to calculate correctly
> RSP = (100-Param("%raiseStop1By", 50, 0, 100, 5)) / 100;  
> MSD2 = Param("#DaysB4MoveStop2", 15, 0,50,1) -1;
> RSP2 = (100-Param("%raiseStop2By", 50, 0, 100, 5)) / 100;
> MSBE = Param("#DaysB4MoveStopBE", 10, 0,50,1) -1;
> TSDM = 1 + Param("%raiseStop3DownDays", 25, 0,100,5) / 100 * StopPct; WriteVal(tsdm);
> IS = BuyPrice - (BuyPrice*StopPct);  
> NS = Ref(C,-1) - (Ref(C,-1)*StopPct);            // initial stop
> NS2 = Ref(C,-1) - (Ref(C,-1)*StopPct*RSP);       // reduce risk
> NS3 = Ref(C,-1) - (Ref(C,-1)*StopPct*RSP*RSP2);  // reduce risk again
> 
> 
> TS = 0;  OpenPos = 0;  j = 0;  BES = Null;  TSD = 0;  LowDown = 0;
> TSa = Null;  Spa = Null;  ja = Null; nsa = Null;  ns2a = Null; 
> bpa = Null;  besa = Null;  ns3a = Null; TSDa = Null; Checka = Null;
> 
> for(i=1; i<BarCount; i++)
> { 
>   if(Buy[i] AND OpenPos == 0 AND TS[i] == 0) {
>     Buy[i] = 1;
>     bpa[i] = BuyPrice[i];
>     bes[i] = BuyPrice[i] * 1.005;    // .5% to cover comissions, etc
>     besa[i] = bes[i];
>     TS = IS[i];
>     OpenPos = 1;
>     j = 0;
>     TSD = L[i] - TS;
>     TSDa[i] = TSD;  
>   }
>   else Buy[i] = 0;
>   if(OpenPos == 1 AND TS[i] > 0 AND L[i] <= TS[i]) {
>     Sell[i] = 1;
>     SellPrice[i] = Min(O[i], TS[i]);
>     SPa[i] = SellPrice[i];
>     TS = 0;
>     OpenPos = 0;
>     j = 0;
>     BES[i] = 0;
>     TSD = 0;
>     TSDa[i] = TSD;
>     LowDown = 0;
>   }
>   else Sell[i] = 0;
>   // reduce risk after x days, first time
>   if(TS[i] > 0 AND j <= MSD) {   
>     TS = Max(TS[i], NS[i]);
>     TSa[i] = TS[i];
>     NSa[i] = NS[i];
>   }
>   if(TS[i] > 0 AND j > MSD) {    
>     TS = Max(TS[i], NS2[i]);
>     TSa[i] = TS[i]; 
>     NS2a[i] = NS2[i];
>   }
>   // reduce risk after x days, second time
>   if(TS[i] > 0 AND j >= MSD2) {    
>     TS = Max(TS[i], NS3[i]);
>     TSa[i] = TS[i]; 
>     NS3a[i] = NS3[i];
>   }
>   // raise stop to BE after x days
>   if(OpenPos == 1) {
>     BES[i] = Max(BES[i], BES[i-1]);
>     BESa[i] = BES[i];
>   }
>   if(OpenPos == 1 AND j == MSBE) {
>     TS = Max(TS[i], BES[i]);
>     TSa[i] = TS[i];
>     BESa[i] = BES[i];
>   }
>   // TSD getting smaller over last 3 days
>   if(TS[i] > 0) {
>     TSD[i] = L[i] - TS[i];
>     TSDa[i] = TSD[i]; 
>     if(TSD[i] < TSD[i-1] AND TSD[i-1] < TSD[i-2] AND TSD[i-2] < TSD[i-3]) {  
>       TS = Max(TS[i], TS[i] * TSDM);                                         
>       TSa[i] = TS[i];
>       LowDown = 1;
>       Checka[i] = LowDown;
>     }
>   }
>   j++; ja[i] = j;  
> }
> 
> Plot(TSa, "Trail Stop", colorBlue, styleLine);
> //Plot(bpa, "buyprice", colorGreen, styleLine);
> //Plot(spa, "sellprice2", colorRed, styleLine);
> //Plot(ja, "j", colorBlack, styleNoLine);
> //Plot(NSa, "NS", colorBlack, styleLine);
> //Plot(NS2a, "NS2", colorOrange, styleLine);
> //Plot(NS3a, "NS3", colorBrown, styleLine);
> //Plot(besa, "BES", colorBrightGreen, styleLine);
> //Plot(TSDa, "TSD", colorRed, styleLine);
> //Plot(Checka, "checkTSD", colorGreen, styleLine);
> 
> 
> //  GRAPH / INDICATOR PLOT
> arrows = ParamToggle("Show Buy / Sell arrows?", "Yes please", 0);
> if(arrows == 1) {
>   PlotShapes(Buy*shapeUpArrow,colorBrightGreen,0,Low);
>   PlotShapes(Sell*shapeDownArrow,colorRed,0,High);
> }
> bliss = ParamToggle("Show Buy / Sell Prices", "Oh yeah! ",0);
> if(bliss == 1) {
>   PlotShapes( IIf(Buy, shapeSmallCircle, shapeNone),colorBrightGreen, 0, BuyPrice, 0 );
>   PlotShapes( IIf( Sell, shapeSmallCircle, shapeNone),colorRed, 0 ,SellPrice, 0 );
>   FirstVisibleBar = Status( "FirstVisibleBar" );
>   Lastvisiblebar = Status("LastVisibleBar");
>   for( b = Firstvisiblebar; b <= Lastvisiblebar AND b < BarCount; b++)
>   {
>     if( Buy[b] ) PlotText("\n Buy\n "+NumToStr(BuyPrice[b],1.2),b,BuyPrice[b],colorBrightGreen);
>     else if( Sell[b] ) PlotText("\n Sell\n "+NumToStr(SellPrice[b],1.2),b,SellPrice[b],colorRed);
>   }
> }
>




------------------------------------

**** IMPORTANT PLEASE READ ****
This group is for the discussion between users only.
This is *NOT* technical support channel.

TO GET TECHNICAL SUPPORT send an e-mail directly to 
SUPPORT {at} amibroker.com

TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    amibroker-digest@xxxxxxxxxxxxxxx 
    amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/