Ok, so I had a go at it :-) Not sure that this is how you should do things but 
it is my first attempt. The below code does not work because High[bar] and 
Low[bar] allways returns zero. Hints?

Thanks,
/Fredrik

SetOption("UseCustomBacktestProc",True);
if( Status("action") == actionPortfolio )
{
 bo = GetBacktesterObject();
 bo.PreProcess();

 TradeEntryPrice = False;
 ProfitTarget = False;
 TrailingSL = False;

  for(bar=0; bar<BarCount; bar++)
  {
   bo.ProcessTradeSignals( bar );

   for( Sig = bo.GetFirstSignal(bar); Sig; Sig=bo.GetNextSignal(bar) )
   {
    if( Sig.IsEntry() ) 
    { 
     TradeEntryPrice = Sig.Price;
     ProfitTarget = --something---;
    }// if  

    if( Sig.IsExit() ) 
    { 
     TradeEntryPrice = False;
     ProfitTarget = False;
     TrailingSL = False;
    }// if
   }// for

   //Profit Target
   if (TradeEntryPrice AND High[bar]>=ProfitTarget)
   {
    bo.ExitTrade(bar,"T5", Min(ProfitTarget,Open[bar]), 2);
    TradeEntryPrice = False;
    ProfitTarget = False;
    TrailingSL = False;
   }

   ----
   //Calculate Trailing SL
   ----

   //Trailing SL
   if (TradeEntryPrice AND Low[bar]<TrailingSL)
   {
    bo.ExitTrade(bar,"T5", Min(TrailingSL,Open[bar]), 3);
    TradeEntryPrice = False;
    ProfitTarget = False;
    TrailingSL = False;
   }
  } //for

  bo.PostProcess(); // Finalize backtester
} // if
                        

Buy = --- something ---
Sell = --- something ---


--- In [email protected], "broman1003" <fredrik_broman1...@...> wrote:
>
> Mike,
> Thanks, I'll have a go at it. I have managed to avoid using the customer 
> backtester up till now but I guess it is time well spent learning it.
> Cheers,
> /Fredrik
> 
> --- In [email protected], "Mike" <sfclimbers@> wrote:
> >
> > You can get the bar index of the trade by using ValueWhen and the trade's 
> > entry date as follows (from within custom backtester code):
> > 
> >     bars = BarIndex(); 
> >     dates = DateTime();
> >     bo = GetBacktesterObject();
> > 
> >     ...
> > 
> >     for ( trade = bo.getFirstOpenPos(); trade; trade = bo.getNextOpenPos() 
> > ) 
> >     { 
> >         entryBar = LastValue( ValueWhen( trade.EntryDateTime == dates, bars 
> > ) ); 
> > 
> >      ...
> > 
> > There may be a better way of handling your stop loss, but the above should 
> > should at least answer your question.
> > 
> > Mike
> > 
> > --- In [email protected], "broman1003" <fredrik_broman1003@> wrote:
> > >
> > > Hi,
> > > I would like to get hold of the bar where a trade was entered when 
> > > backtesting.
> > > The reason I need it is that I would like to implement a trailing SL that 
> > > is depending on the highest value the price has hit since the trade was 
> > > initiated.
> > > 
> > > All hints are appreciated.
> > > Thanks,
> > > /Fredrik
> > >
> >
>


Reply via email to