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

AW: Forward Optimization



PureBytes Links

Trading Reference Links

Dear Mark Brown,

how come you do some research... and you never even have the time to look at
Wealth Lab Developer 2. If you would have taken the time (some time ago I
emailed you... remember???) you would know that our users actually allready
programmed that straight into our script code.... no big deal. Below you
find the code of a very nice and most talented person. He was nice enough to
publish the code on our "Public ChartScript". It reminds me of times when
you were doing it here on the Eskimo group. You will also find the code here
http://www.wealth-lab.com/cgi-bin/WealthLab.DLL/editsystem?id=4288. And you
can test systems then on the web on a portfolio of stocks/futures. Once you
buy the software you can test it at home on EOD or ID data. You will find a
description of the system after following the link.

So I hope I was of help for you as you were often years ago for me when I
was still a unhappy TS user :-)... like you are still as it seems.

Volker Knapp
Wealth-Lab Inc.
http://www.wealth-lab.com
http://www.wealth-lab.de


var O, C, H, L, EP, PTP: float;
var FIRSTBAR: boolean;
var WAGER, DEBUG, DEBUGT, DEBUG2, OPTVALUEBEG, OPTVALUEEND, OPTVALUESTEP,
OPTBARNUM, TRADEBARNUM, TOTTRADEENTERED, TOTTRADEEXITED, OPTVALUENUM,
OPTVALUESERIES, RSISERIES_, PROFITTARGETPERCENT, RSILENGTH1, RSICUTOFF,
CHUNKBARNUM, STARTBAR, AVAILBARS, CHUNKNUM, TRADEMAX, TRADEACTIVE, OTNUM,
OTMAX, DAYSINTRADE, OPTNUM, STARTOPTBAR, ENDOPTBAR, OPTVALUE, J, BAR,
SHARENUMBER: integer;
var RSIPane, i, StartTradeBar, EndTradeBar, P, PBB, TradeNum: integer;
var BestProfit, BestValue: float;
var RSIValue: float;
var RSIValuePane: integer;

wager := 5000;

debug := 0;			{Exit Logic}
debugT := 0;			{Optimization Trades}
debug2 := 0;			{Other logic}

optValueBeg := 2;		{First value to optimize}
optValueEnd := 7;		{Last value to optimize}
optValueStep := 1;		{Optimize every nth value}
optBarNum := 125;               {Length of optimization Period}
tradeBarNum := 20;              {Length of trading period using optimized
results}

totTradeEntered := 0;
totTradeExited := 0;

optValueNum := trunc((optValueEnd - optValueBeg) / optValueStep) + 1;
optValueSeries := createSeries();
rsiSeries_ := createSeries();

profitTargetPercent := 5;
InstallProfitTarget( profitTargetPercent );
RSIlength1 := 70;               {110=best profit, 34 = best rar}
RSIcutoff := 20;                {20 = best rar}

chunkBarNum := optBarNum + tradeBarNum;  {Number of bars in each chunk of
optimize/trade}
startBar := RSIlength1;
availBars := (barcount - startBar) - optBarNum;
chunkNum := trunc((availBars) / tradeBarNum);  {Number of opt/trade chunks}
                                               {Dont forget that they
overlap}
{This is setting startBar to the beginning of the first trading window}
startBar := (BarCount - (chunkNum * tradeBarNum)) - optBarNum;

if debug2 = 1 then print ('barcount = ' + intToStr(barcount));
if debug2 = 1 then print ('optBarNum = ' + intToStr(optBarNum));
if debug2 = 1 then print ('tradeBarNum = ' + intToStr(tradeBarNum));
if debug2 = 1 then print ('chunkBarNum = ' + intToStr(chunkBarNum));
if debug2 = 1 then print ('availBars = ' + intToStr(availBars));
if debug2 = 1 then print ('chunkNum = ' + intToStr(chunkNum));
if debug2 = 1 then print ('Last bar = ' + intToStr(barcount - 1));

{Trade Table}

tradeNum := 0;
tradeMax := 100;
tradeActive := 0;

var tradeBar, tradePriceEntry,
    tradePriceExit, tradeShares, tradeProfit: array[0..100] of float;

{Optimization Table}

otNum := 0;
otMax := 100;

var otValue, otProfit, otTradeNum: array[0..100] of float;
var tradeDirection: array[0..100] of string;

daysInTrade := 3;

{Perform all Optimizations for all chunks}

for optNum := 0 to chunkNum - 1 do begin
   if debug2 = 1 then print ('OptNum = ' + intToStr(optNum) +
' ---------------------------');
   startOptBar := (startBar + (optNum * tradeBarNum)) - 1;
   endOptBar := startOptBar + optBarNum;
   if debug2 = 1 then print(
         '   startOptBar = ' + intToStr(startOptBar)
       + '   endOptBar = ' + intToStr(endOptBar)
        );
   {perform a single optimization across a single optimization chunk,
testing all values}
   optValue := optValueBeg;
   for otNum := 0 to optValueNum - 1 do begin
      {Experimentally trade for a single un-optimized value}
      tradeNum := 0;
      tradeActive := 0;
      for j := 0 to tradeMax - 1 do begin
         tradeProfit[j] := 0.0;
      end;
      for bar := startOptBar to endOptBar do begin

         {Optimization Exit Logic}

         if tradeActive = 1 then begin
            O := priceOpen( bar );
            C := priceClose( bar );
            H := priceHigh( bar );
            L := priceLow( bar );
            firstBar := bar = tradeBar[tradeNum];
            {Exit Based Upon Days In trade}
            if bar - tradeBar[tradeNum] >= DaysInTrade then begin
               {SellAtMarket( Bar,  P, 'LongDaysExpired' );}
               tradeActive := 0;
               tradePriceExit[tradeNum] := O;
            end;
            if (tradeActive = 1)
            and (tradeDirection[tradeNum] = 'L')
            and (not firstBar) then begin
               {Entry Price}
               EP := tradePriceEntry[tradeNum];
               {Profit Target Price}
               PTP := EP + ((profitTargetPercent * EP) / 100);
               if debug = 1 then begin
                  print ('EP = ' + floatToStr(EP));
                  print ('PTP = ' + floatToStr(PTP));
               end;
               if (O >= PTP) then begin
                  tradeActive := 0;
                  tradePriceExit[tradeNum] := O;
               end else begin
                  if (H >= PTP) then begin
                     tradeActive := 0;
                     tradePriceExit[tradeNum] := PTP;
                  end;
               end;
            end;
            if (tradeActive = 1)
            and (tradeDirection[tradeNum] = 'S')
            and (not firstBar) then begin
               {Entry Price}
               EP := tradePriceEntry[tradeNum];
               {Profit Target Price}
               PTP := EP - ((profitTargetPercent * EP) / 100);
               if debug = 1 then begin
                  print ('EP = ' + floatToStr(EP));
                  print ('PTP = ' + floatToStr(PTP));
               end;
               if (O <= PTP) then begin
                  tradeActive := 0;
                  tradePriceExit[tradeNum] := O;
               end else begin
                  if (L <= PTP) then begin
                     tradeActive := 0;
                     tradePriceExit[tradeNum] := PTP;
                  end;
               end;
            end;
            {Check to see if trade just exited}
            if tradeActive = 0 then begin
               totTradeExited := totTradeExited + 1;
               if tradeDirection[tradeNum] = 'L' then
                  tradeProfit[tradeNum] := (tradePriceExit[tradeNum] -
tradePriceEntry[tradeNum]) * tradeShares[tradeNum]
               else
                  tradeProfit[tradeNum] := (tradePriceEntry[tradeNum] -
tradePriceExit[tradeNum]) * tradeShares[tradeNum];
               if debugT = 1 then print(
               '   trade exit --- date = ' + datetostr(getdate(bar)) + '   '
+
                  'exit price = ' + formatFloat('####.##',
tradePriceExit[tradeNum]) + '   ' +
                  'profit = ' + formatFloat('####.##',
tradeProfit[tradeNum])
               );
               tradeNum := tradeNum + 1;
            end;
         end;

         {Optimization Entry Logic}

         if (priceClose( bar ) > 5)
         and (tradeActive = 0) then begin
            if (RSI(bar, #close, optValue) < RSIcutoff)
            or (RSI(bar, #close, optValue) > (100 - RSIcutoff)) then begin
               if bar < (barcount - 1) then begin
                  tradeActive := 1;
                  if (RSI(bar, #close, optValue) < RSIcutoff)
                  then tradeDirection[tradeNum] := 'L'
                  else tradeDirection[tradeNum] := 'S';
                  tradeBar[tradeNum] := bar + 1;
                  tradePriceEntry[tradeNum] := priceOpen(bar + 1);
                  tradePriceExit[tradeNum] := 0.0;
                  shareNumber := round(wager / priceOpen(bar + 1));
                  {lots := shareNumber / 100;
                  wholeLots := round(lots);
                  if wholeLots = 0 then wholeLots := 1;
                  shareNumber := wholeLots * 100;}
                  tradeShares[tradeNum] := shareNumber;
                  tradeProfit[tradeNum] := 0.0;
                  totTradeEntered := totTradeEntered + 1;
                  {
                  if debugT = 1 then print(
                  '   ---trade entry --- date = ' + datetostr(getdate(bar +
1)) + '   ' +
                     'entry price = ' + formatFloat('####.##', priceOpen(bar
+ 1)) + '   ' +
                     'rsiValue = ' + intToStr(optValue) + '   ' +
                     'shares = ' + intToStr(tradeShares[tradeNum]) + '   ' +
                     'direction = ' + tradeDirection[tradeNum]
                  );
                  }
               end;
            end;
         end;
      end;
      {Determine total profit of optimized value}
      otValue[otNum] := optValue;
      otProfit[otNum] := 0.0;
      for j := 0 to tradeNum do begin
         otProfit[otNum] := otProfit[otNum] + tradeProfit[j];
      end;
      if tradeActive = 0 then tradeNum := tradeNum - 1;
      otTradeNum[otNum] := tradeNum + 1;
      if debug2 = 1 then print('   tradeNum = ' + intToStr(tradeNum + 1)
          + '   value = '    + intToStr(optValue)
          + '   profit = '   + formatFloat('####.##',  otProfit[otNum])
           );
      optValue := optValue + optValueStep;
   end;

   {Determine best optimized value}

   bestProfit := 0.0;
   bestValue := 0;
   for i := 0 to otNum - 1 do begin
      if otProfit[i] > bestProfit then begin
         bestProfit := otProfit[i];
         bestValue := otValue[i];
      end;
   end;

   {Place the best value into the optimized value series}

   startTradeBar := endOptBar + 1;
   endTradeBar := startTradeBar + tradeBarNum - 1;
   for bar := startTradeBar to endTradeBar do begin
      setSeriesValue(bar, optValueSeries, bestValue);
   end;

   {Print the results}
   {
   if debug2 = 1 then print (
             '   Best value = ' + intToStr(bestValue)
           + '   Best Profit = ' + formatFloat('####.##', bestProfit)
         );
   }
end;

if debug2 = 1 then print(
        ' totTradeEntered = ' + intToStr(totTradeEntered)
       +' totTradeExited = ' + intToStr(totTradeExited)
     );

{Perform Actual Trading}

if debug2 = 1 then print ('Actual Trading startBar = ' +
intToStr(startBar));

for Bar := startBar to BarCount - 1 do begin
  ApplyAutoStops( Bar );
  if LastPositionActive then begin
      P := LastPosition();
      PBB := PositionEntryBar( P );
      if ((Bar + 1 - PBB ) >= daysInTrade) then begin
         if positionLong(p) then begin
            SellAtMarket( Bar + 1, P, '' );
         end else begin
            CoverAtMarket( Bar + 1, P, '' );
         end;
      end;
  end else begin
     if priceClose( bar ) > 5 then begin
        optValue := trunc(getSeriesValue(bar, optValueSeries));
        if optValue <> 0 then begin
           rsiValue := RSI(bar, #close, optValue);
           setSeriesValue(bar, rsiSeries_, rsiValue);
           if (rsiValue < RSIcutoff) then begin
              BuyAtMarket(Bar + 1,'');
           end;
           if (rsiValue > (100 - RSIcutoff)) then begin
              ShortAtMarket(Bar + 1,'');
           end;
        end;
     end;
  end;
end;

{ Plot indicators }

RSIPane := CreatePane( 075, true, true );
PlotSeries( RSISeries(#close, RSIlength1 ), RSIPane, #blue, #Thin );
PlotSeries( rsiSeries_, RSIPane, #fuchsia, #Thin );
DrawText( 'RSI', RSIPane, 4, 4, 004, 10 );
DrawHorzLine(RSIcutoff, RSIPane, #blue, #dotted );
DrawHorzLine((100 - RSIcutoff), RSIPane, #blue, #dotted );

rsiValuePane := CreatePane( 075, true, true );
PlotSeries( optValueSeries, rsiValuePane, #fuchsia, #Thin );
DrawText( 'OptimizedRsiValue', RSIvaluePane, 4, 4, 004, 10 );

-----Ursprüngliche Nachricht-----
Von: Mark Brown [mailto:markbrown@xxxxxxxxxxxxx]
Gesendet: Donnerstag, 21. Februar 2002 19:48
An: Omega List
Betreff: Forward Optimization


Hello Omega,

I  am  wondering  if  someone  who  understands  forward  optimization
processes  could  please  explain the whole idea of the concept to me.
What  is the goal of it? How do you use it?  What features do you need
that  are  seemingly  unobtainable using currently available software.
What software do you use, are there and references online?  I am doing
some research for a friend who is building a new software product, and
he has the bright idea he needs to include this feature.

--

Have Great Day,  Mark Brown

°¨¨°º©[ WWW.MARKBROWN.COM ]©º°¨¨°