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

Re: [amibroker] exiting flat positions



PureBytes Links

Trading Reference Links




Very slick, Howard (and TJ). I love the idea of the PSAR's initial 
level starting at the user's whim. One weird observation: the term 'Mp' 
turns blue in my AA, signifying it is an AB function, and I get a syntax error, 
yet Mp is not an AB function when I do a search on it. So, I had to rename Mp to 
Mktp, and it works fine. What could be giving this error? Also, do you have 
a short version to go along with the long version? Thanks for sharing this with 
the board. 
 
Al Venosa
<BLOCKQUOTE dir=ltr 
>
  ----- Original Message ----- 
  <DIV 
  >From: 
  Howard 
  Bandy 
  To: <A title=amibroker@xxxxxxxxxxxxxxx 
  href="">amibroker@xxxxxxxxxxxxxxx 
  Sent: Sunday, December 14, 2003 8:59 
  AM
  Subject: RE: [amibroker] exiting flat 
  positions
  
  
  <SPAN 
  >Hi Dave 
  –
  <SPAN 
  > 
  <SPAN 
  >Have you tried the 
  Parabolic trailing stop?  Tomasz posted the afl code for it a while 
  back.  I have modified it so that I can set the initial level at a point 
  of my own choosing, rather than always at the price at which the reversal took 
  place.  If desired, it would be easy to add code to track the number of 
  bars since entry and the profit to date, and exit trades based on that 
  information.
  <SPAN 
  > 
  <SPAN 
  >A simplified version 
  which handles only long trades is copied here.
  <SPAN 
  > 
  <SPAN 
  >Howard
  <SPAN 
  > 
  <SPAN 
  >============================================================
  <SPAN 
  > 
  <SPAN 
  >/////////////////////////////////
  <SPAN 
  >// HB 
  Parabolic Stop for Long positions
  <SPAN 
  >//
  <SPAN 
  >// Written 
  by: <SPAN 
  >Tomasz 
  Janeczko
  <SPAN 
  >//     
  Modified by:  Howard Bandy to allow setting of initial 
  level
  <SPAN 
  >//                                
  at the start of each new long position.
  <SPAN 
  >//
  <SPAN 
  >// Parabolic 
  SAR re-implemented in
  <SPAN 
  >// native 
  AFL.
  <SPAN 
  >//
  <SPAN 
  >// Example of 
  for/if/else control statements
  <SPAN 
  >//
  <SPAN 
  >// 
  Requires:
  <SPAN 
  >// AmiBroker 
  4.31.1 
  <SPAN 
  >//
  <SPAN 
  > 
  <SPAN 
  > 
  <SPAN 
  > 
  <SPAN 
  >//     
  Set trade delays to trade the day the buy and sell signals are 
  received.
  <SPAN 
  > 
  <SPAN 
  >SetTradeDelays<FONT 
  face="Courier New" color=black size=1><SPAN 
  >(<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  >);
  <SPAN 
  > 
  <SPAN 
  >//     
  Trading system entry logic goes here.
  <SPAN 
  >//     
  Exit will be made by the price dropping through the 
  <SPAN 
  >//     
  parabolic level.
  <SPAN 
  > 
  <SPAN 
  >//     
  For example, use moving average crossover entry.
  <SPAN 
  > 
  <SPAN 
  >MALen = 
  <SPAN 
  >Param<FONT 
  face="Courier New" color=black size=1><SPAN 
  >(<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >"MALen"<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >30<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >100<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >);
  <SPAN 
  >MAvg = 
  <SPAN 
  >MA<FONT 
  face="Courier New" color=black size=1><SPAN 
  >(<SPAN 
  >C,MALen);
  <SPAN 
  >Buy<FONT 
  face="Courier New" color=black size=1><SPAN 
  > = 
  <SPAN 
  >Cross<FONT 
  face="Courier New" color=black size=1><SPAN 
  >(<SPAN 
  >C,MAvg);
  <SPAN 
  > 
  <SPAN 
  >//     
  Code below assumes that entry will be made at the close of the day the 
  
  <SPAN 
  >//     
  buy signal is generated.
  <SPAN 
  > 
  <SPAN 
  >//     
  Setting TradeAtStop to 1 assumes that there is a stop in 
  place
  <SPAN 
  >//     
  and the trade exits intraday at the stop price.
  <SPAN 
  >//     
  Setting TradeAtStop to 0 assumes that intraday exit cannot take 
  place
  <SPAN 
  >//     
  (as in mutual fund end-of-day trading) and the trade takes 
  place
  <SPAN 
  >//     
  at the close of the signal day.
  <SPAN 
  > 
  <SPAN 
  >TradeAtStop = 
  <SPAN 
  >Param<FONT 
  face="Courier New" color=black size=1><SPAN 
  >(<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >"TradeAtStop"<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >);
  <SPAN 
  > 
  <SPAN 
  >//     
  Set the initial stop level at the lowest low for some number of 
  days.
  <SPAN 
  >//     
  Other ways to set the initial level include ATR, Standard Deviation, 
  etc.
  <SPAN 
  > 
  <SPAN 
  >LBDays = 
  <SPAN 
  >Param<FONT 
  face="Courier New" color=black size=1><SPAN 
  >(<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >"LBDays"<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >3<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >10<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >);
  <SPAN 
  > 
  <SPAN 
  >//     
  Set the Acceleration factor and Maximum Acceleration.
  <SPAN 
  > 
  <SPAN 
  >IAF = 
  <SPAN 
  >Param<FONT 
  face="Courier New" color=black size=1><SPAN 
  >(<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >"IAF"<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0.02<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0.001<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0.1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0.001<FONT 
  face="Courier New" color=black size=1><SPAN 
  >);       
  <SPAN 
  >// 
  acceleration factor
  <SPAN 
  >MaxAF = 
  <SPAN 
  >Param<FONT 
  face="Courier New" color=black size=1><SPAN 
  >(<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >"MaxAF"<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0.2<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0.001<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >1.0<FONT 
  face="Courier New" color=black size=1><SPAN 
  >,<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0.001<FONT 
  face="Courier New" color=black size=1><SPAN 
  >);     
  <SPAN 
  >// max 
  acceleration
  <SPAN 
  > 
  <SPAN 
  >Psar = 
  <SPAN 
  >Close;        
  <SPAN 
  >// 
  initialize
  <SPAN 
  >mp = 
  <SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  >;                    
  <SPAN 
  >// flat 
  initial Market Position
  <SPAN 
  >Sell<FONT 
  face="Courier New" color=black size=1><SPAN 
  > = 
  <SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  >;                  
  <SPAN 
  >// clear sell 
  signals array
  <SPAN 
  >af = 
  IAF;        <FONT 
  face="Courier New" color=green size=1><SPAN 
  >// initial 
  acceleration factor
  <SPAN 
  >hp = <SPAN 
  >High [ <FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  > 
  ];
  <SPAN 
  >lp = <SPAN 
  >Low [ <FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  > 
  ];
  <SPAN 
  > 
  <SPAN 
  >for<FONT 
  face="Courier New" color=black size=1><SPAN 
  >( i = 
  <SPAN 
  >2<FONT 
  face="Courier New" color=black size=1><SPAN 
  >; i < 
  BarCount; i++ 
)
  <SPAN 
  >{
  <SPAN 
  >       
  <SPAN 
  >//     
  Check for exit from long position
  <SPAN 
  > 
  <SPAN 
  >       
  <SPAN 
  >if<FONT 
  face="Courier New" color=black size=1><SPAN 
  > (  (mp 
  == <SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >) <SPAN 
  >AND (<SPAN 
  >Low[i] < Psar[i-<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >])  
  )
  <SPAN 
  >       
  {
  <SPAN 
  >              
  Sell[i] = <FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >;
  <SPAN 
  >              
  <SPAN 
  >if<FONT 
  face="Courier New" color=black size=1><SPAN 
  > 
  (TradeAtStop)
  <SPAN 
  >              
  {
  <SPAN 
  >                     
  SellPrice[i] = 
  Psar[i-<SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >];
  <SPAN 
  >              
  }
  <SPAN 
  >              
  <SPAN 
  >else
  <SPAN 
  >              
  {
  <SPAN 
  >                     
  SellPrice[i] = <SPAN 
  >Close[i];
  <SPAN 
  >              
  }
  <SPAN 
  >              
  mp = <SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  >;
  <SPAN 
  >       
  }
  <SPAN 
  > 
  <SPAN 
  >       
  <SPAN 
  >//     
  Continuation of long position -- adjust stop
  <SPAN 
  > 
  <SPAN 
  >       
  <SPAN 
  >if<FONT 
  face="Courier New" color=black size=1><SPAN 
  > ( mp == 
  <SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  > 
  )
  <SPAN 
  >       
  {
  <SPAN 
  >              
  <SPAN 
  >if<FONT 
  face="Courier New" color=black size=1><SPAN 
  > (<SPAN 
  >High[i] > Hp)
  <SPAN 
  >              
  {
  <SPAN 
  >                     
  Hp = High[i];
  <SPAN 
  >                     
  af = af + IAF;
  <SPAN 
  >                     
  <SPAN 
  >if<FONT 
  face="Courier New" color=black size=1><SPAN 
  > (af > 
  MaxAF) af = MaxAF;
  <SPAN 
  >              
  }
  <SPAN 
  >              
  psar [ i ] = psar [ i-<FONT face="Courier New" color=fuchsia 
  size=1><SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  > ] + af * ( 
  hp - psar [ i-<FONT face="Courier New" color=fuchsia 
  size=1><SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  > ] 
  );
  <SPAN 
  >       
  }
  <SPAN 
  >       
  <SPAN 
  >else
  <SPAN 
  >       
  {
  <SPAN 
  >              
  <SPAN 
  >//     
  not in a long position.
  <SPAN 
  >              
  <SPAN 
  >//     
  value of psar is not important.
  <SPAN 
  >              
  <SPAN 
  >//     
  set the psar level so it will plot on the price graph.
  <SPAN 
  > 
  <SPAN 
  >              
  psar[i] = Close[<SPAN 
  >BarCount-<FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >];
  <SPAN 
  >       
  }
  <SPAN 
  > 
  <SPAN 
  >       
  <SPAN 
  >//     
  Check for new long position
  <SPAN 
  > 
  <SPAN 
  >       
  <SPAN 
  >if<FONT 
  face="Courier New" color=black size=1><SPAN 
  > (  (mp 
  == <SPAN 
  >0<FONT 
  face="Courier New" color=black size=1><SPAN 
  >) <SPAN 
  >AND (<SPAN 
  >Buy[i])  )
  <SPAN 
  >       
  {
  <SPAN 
  >              
  BuyPrice[i] = <SPAN 
  >Close[i];
  <SPAN 
  >              
  Lp = <SPAN 
  >LLV<FONT 
  face="Courier New" color=black size=1><SPAN 
  >(<SPAN 
  >Low,LBDays);
  <SPAN 
  >              
  Psar[i] = Lp[i];
  <SPAN 
  >              
  Hp = High[i];
  <SPAN 
  >              
  af = IAF;
  <SPAN 
  >              
  mp = <SPAN 
  >1<FONT 
  face="Courier New" color=black size=1><SPAN 
  >;
  <SPAN 
  >       
  }
  <SPAN 
  >}
  <SPAN 
  > 
  <SPAN 
  >Plot<FONT 
  face="Courier New" color=black size=1><SPAN 
  >( <SPAN 
  >Close, <FONT 
  face="Courier New" color=fuchsia size=1><SPAN 
  >"Price"<FONT 
  face="Courier New" color=black size=1><SPAN 
  >, <SPAN 
  >colorBlack, <SPAN 
  >styleCandle );
  <SPAN 
  >Plot<FONT 
  face="Courier New" color=black size=1><SPAN 
  >( MAvg, 
  <SPAN 
  >"MAvg"<FONT 
  face="Courier New" color=black size=1><SPAN 
  >, <SPAN 
  >colorBlue,<SPAN 
  >styleLine);
  <SPAN 
  >Plot<FONT 
  face="Courier New" color=black size=1><SPAN 
  >( psar, 
  <SPAN 
  >"SAR"<FONT 
  face="Courier New" color=black size=1><SPAN 
  >, <SPAN 
  >colorRed, <SPAN 
  >styleDots | <SPAN 
  >styleNoLine | <SPAN 
  >styleThick );
  <SPAN 
  >  
  <SPAN 
  > 
  <SPAN 
  > 
  <SPAN 
  >============================================================
  <SPAN 
  > 
  <DIV 
  >
  <SPAN 
  >-----Original 
  Message-----From: Dave 
  Merrill [mailto:dmerrill@xxxxxxx] <SPAN 
  >Sent: Friday, December 12, 2003 2:45 
  PMTo: 
  amibroker@xxxxxxxxxxxxxxx<SPAN 
  >Subject: [amibroker] exiting flat 
  positions
  <SPAN 
  > 
  <FONT face="Courier New" 
  size=2>Obviously, losses are a problem. But so 
  are positions that hang in there<FONT face="Courier New" 
  size=2><FONT 
  face="Courier New">forever taking up available cash but going nowhere, without 
  hitting profittargets or 
  stops.How would you code 
  that, assuming you're dealing with a system that tries 
  todump losers but let winners run 
  as long as they're advancing.<FONT 
  face="Courier New">I tried starting from a modest stoploss, with the stop 
  percentage advancingevery day 
  until it becomes negative, enforcing the requirement to make 
  aprofit or get off the bus. I'm 
  not certain, but I think it's working, just<FONT 
  face="Courier New">not very profitable in the context I tried 
  it:.Here's the 
  code:<FONT 
  face="Courier New">----------------<FONT 
  face="Courier New">bars_since_buy = NZ(BarsSince(buy), 
  BarCount);bars_since_short = 
  NZ(BarsSince(short), BarCount);<FONT 
  face="Courier New">bars_since_entry = IIf(bars_since_buy < 
  bars_since_short, bars_since_buy,<FONT 
  face="Courier New">bars_since_short);<FONT 
  face="Courier New">stoploss_rise = Optimize("stoploss rise", .5, .1, 1, 
  .1);stoploss = 13 - 
  (stoploss_rise * bars_since_entry);<FONT 
  face="Courier New">ApplyStop(stopTypeLoss, stopModePercent, stoploss, false, 
  true, 0);<FONT 
  face="Courier New">----------------<FONT 
  face="Courier New">Anyone see any problems with the implementation? Any other 
  ideas foravoiding sitting in 
  stagnant positions?<FONT 
  face="Courier New">Dave
  
    
    
      <TD 
       
      bgColor=#ffffcc>
        <FONT 
        face="Times New Roman" color=#003399 size=2><SPAN 
        >Send BUG 
        REPORTS to <SPAN 
        >bugs@xxxxxxxxxxxxx<FONT 
        face="Courier New" size=2><SPAN 
        ><FONT 
        face="Courier New">Send SUGGESTIONS to 
        suggest@xxxxxxxxxxxxx<FONT 
        face="Courier New">-----------------------------------------<FONT 
        face="Courier New">Post AmiQuote-related messages ONLY to: 
        <SPAN 
        >amiquote@xxxxxxxxxxxxxxx<FONT 
        face="Courier New" size=2> 
        <SPAN 
        ><FONT 
        face="Courier New">(Web page: <A 
        href="">http://groups.yahoo.com/group/amiquote/messages/)<FONT 
        face="Courier New">--------------------------------------------<FONT 
        face="Courier New">Check group FAQ at: <A 
        href="">http://groups.yahoo.com/group/amibroker/files/groupfaq.html 
        <SPAN 
        >Your use of Yahoo! Groups is subject to the <A 
        href="">Yahoo! Terms of 
        Service. 
        
        Send 
        BUG REPORTS to bugs@xxxxxxxxxxxxxSend SUGGESTIONS to 
        suggest@xxxxxxxxxxxxx-----------------------------------------Post 
        AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx (Web 
        page: <A 
        href="">http://groups.yahoo.com/group/amiquote/messages/)--------------------------------------------Check 
        group FAQ at: <A 
        href="">http://groups.yahoo.com/group/amibroker/files/groupfaq.html 
        Your use of Yahoo! Groups is subject to the <A 
        href="">Yahoo! Terms of 
        Service. 
 
 
---Outgoing mail is certified Virus Free.Checked by AVG 
anti-virus system (<A 
href="">http://www.grisoft.com).Version: 6.0.551 / 
Virus Database: 343 - Release Date: 12/11/2003






Yahoo! Groups Sponsor


  ADVERTISEMENT 









Send BUG REPORTS to bugs@xxxxxxxxxxxxx
Send SUGGESTIONS to suggest@xxxxxxxxxxxxx
-----------------------------------------
Post AmiQuote-related messages ONLY to: amiquote@xxxxxxxxxxxxxxx 
(Web page: http://groups.yahoo.com/group/amiquote/messages/)
--------------------------------------------
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html



Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.