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

Re: [amibroker] AddCustomMetric() for open positions



PureBytes Links

Trading Reference Links

Graham:

I am aware that you can loop through open positions or trades the way you did.
However, what I was trying to do is to AddCustomMetric() just before closing a position by ExitTrade().
The metric is OvernightGap for the day (bar) of exit. 

 bo = GetBacktesterObject();
 bo.PreProcess(); // Initialize backtester
 for( bar=0; bar < BarCount; bar++)
 {
  eq =  bo.Equity;
  for ( sig=bo.GetFirstSignal(bar); sig; sig=bo.GetNextSignal(bar) )
  {
   if (sig.isExit())
   {
    pos = bo.FindOpenPos( sig.Symbol );
    if(pos) {
     lastC = pos.GetPrice(bar-1, "C");
     curO =pos.GetPrice(bar, "O");
     //!!!!!!!!!!!!!!!!!!!!! the folowing line crash amibroker!!!!!!!!
     pos.AddCustomMetric("OvernightGap", curO - lastC);
     if(bo.ExitTrade(bar,sig.symbol,sig.Price))
     { 
      _TRACE("EXIT: " + sig.symbol + "@" + sig.Price);
     }
    }
   }
  }
.......
}

  ----- Original Message ----- 
  From: Graham 
  To: amibroker@xxxxxxxxxxxxxxx 
  Sent: Tuesday, November 28, 2006 6:21 PM
  Subject: Re: [amibroker] AddCustomMetric() for open positions


  Why not use the functions that exist for this purpose



      // 'bo' variable holds Backtester object retrieved earlier 
     
      for( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos() ) 
       { 
            // openpos variable now holds Trade object

       }

  To iterate through closed trade list you should use GetFirstTrade() / GetNextTrade() methods of Backtester object, as shown below:

      for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) 
       { 
            // trade variable now holds Trade object

       }


  -- 
  Cheers
  Graham
  AB-Write >< Professional AFL Writing Service
  Yes, I write AFL code to your requirements
  http://www.aflwriting.com 


  On 29/11/06, Mark H <amibroker@xxxxxxxxxxxxx> wrote:
    OK, I verified that open positions work if I handle them AFTER the main loop (loop through all bars) in CBT.
    But it has problem within the main loop.

    Here is the code:

    If I put AddCustomMetric() after bo.ExitTrade(), it has no effect.

       if (sig.isExit())
       {
        pos = bo.FindOpenPos( sig.Symbol );
        if(pos) {
         lastC = pos.GetPrice(bar-1, "C");
         curO =pos.GetPrice(bar, "O");
         if(bo.ExitTrade(bar,sig.symbol,sig.Price))
         { 
          //!!!!!!!!!!!!!!!!!!!!! the folowing line has no effect!!!!!!!!
             pos.AddCustomMetric("OvernightGap", curO - lastC);
         _TRACE("EXIT: " + sig.symbol + "@" + sig.Price);
         }
        }
       }

    If I put it before bo.ExitTrade(), the crach warning window of AB popped up:

       if (sig.isExit())
       {
        pos = bo.FindOpenPos( sig.Symbol );
        if(pos) {
         lastC = pos.GetPrice(bar-1, "C");
         curO =pos.GetPrice(bar, "O");
         //!!!!!!!!!!!!!!!!!!!!! the folowing line crash amibroker!!!!!!!!
         pos.AddCustomMetric("OvernightGap", curO - lastC);
         if(bo.ExitTrade(bar,sig.symbol,sig.Price))
         { 
          _TRACE("EXIT: " + sig.symbol + "@" + sig.Price);
         }
        }
       }


      ----- Original Message ----- 
      From: Tomasz Janeczko 
      To: amibroker@xxxxxxxxxxxxxxx 
      Sent: Tuesday, November 28, 2006 1:58 PM
      Subject: Re: [amibroker] AddCustomMetric() for open positions



      Hello,

      Are you sure ? Open positions and closed trades are handled by exactly the same class internally and I see
      no reason why it should not work. If in doubt send code that allows to reproduce the problem to support.

      Best regards,
      Tomasz Janeczko
      amibroker.com
        ----- Original Message ----- 
        From: Mark H 
        To: amibroker@xxxxxxxxxxxxxxx 
        Sent: Tuesday, November 28, 2006 6:47 PM
        Subject: [amibroker] AddCustomMetric() for open positions


        Tomasz:

        AddCustomMetric() has no effect on open positions. I want to add some custom metrics based on some calculations using values returned by GetPrice(). 

        AddCustomMetric() works fine for closed positions (trades), but then GetPrice() no longer works.

        Is this by design? 

        Thanks,

        - Mark







   
Content-Description: "AVG certification"
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.430 / Virus Database: 268.14.19/556 - Release Date: 11/28/2006 3:22 PM