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

[amibroker] Solution: separate long and short max open positions



PureBytes Links

Trading Reference Links

I use both longs and shorts on a daily basis, and I've always had to
merge the backtesting results in a spreadsheet to get a sense of how
to balance the system. This approach to an enormous amount of time. I
finally got around to sorting out the custom backtest procedure to
solve this problem.

So, if you'd like to have two separate ranked queues, one for longs
and one for shorts, use the following code as a starting point. You'll
need to add your buy/sell and short/cover criteria, along with a
positionscore rule. 

I close my positions at market close. If you potentially close you
positions early in the day and then open new ones, you may need to
move the exit loop before the entry loop... not really sure.

============================================================
SetCustomBacktestProc("");

MaxLong = <max number of longs>;
LongPS = <position size of longs, e.g. -10>;
MaxShort = <max number of shorts>;
ShortPS = <position size of shorts, e.g. -10>;

if( Status("action") == actionPortfolio ); 
{ 
  bo = GetBacktesterObject(); 
  bo.PreProcess(); // Initialize backtester 
  
  for(bar=0; bar<BarCount; bar++) { 
    numLongOpen = 0;
    numShortOpen = 0;

    bContinue = True; 
    // Look for Entry Signals
    for ( sig=bo.GetFirstSignal(bar); sig AND bContinue;
sig=bo.GetNextSignal(bar)) {
      if( sig.IsEntry() AND sig.Price != -1 AND IsNull(
bo.FindOpenPos( sig.Symbol ) ) ) {
        // Enter Long
        if( sig.IsLong() AND numLongOpen < MaxLong ) {  
          if( bo.EnterTrade(bar, sig.symbol, True, sig.Price, LongPS )
== 0 ) {
            //If trade doesn't go through don't do anymore trades
            bContinue = False; 
          }
          numLongOpen = numLongOpen + 1;
        // Enter Short
        } else if ( sig.IsLong() == 0 AND numShortOpen < MaxShort ) {  
          if( bo.EnterTrade(bar, sig.symbol, False, sig.Price, ShortPS
) == 0 ) {
            //If trade doesn't go through don't do anymore trades
            bContinue = False; 
          }
          numShortOpen = numShortOpen + 1;
        }
      } 
    } 
    // exit signals 
    for ( sig=bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar) ) {
      if (sig.IsExit() AND sig.Price != -1 ) {
        // Exit Signal 
        bo.ExitTrade(bar,sig.symbol,sig.Price); 
      } 
    } 
    // update stats
    bo.UpdateStats(bar,2); 
  } 
  bo.PostProcess(); 
}

Short = <criteria>;
ShortPrice = <price>;
Cover = <criteria>;
CoverPrice = <price>;
Buy = <price>;
BuyPrice = <price>;
Sell = <criteria>;
SellPrice = <price>;
PositionScore = IIf(Buy, <buyscore>, <shortscore>);




Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

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

For other support material please check also:
http://www.amibroker.com/support.html
 
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:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto: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/