Hello,

Hoping someone can help with debugging this trading system code.
I'vebeen building BarCount loops for the better part of 9 months now,
andthought I had the hang of it. Yet this simple problem seems to have
thebetter of me!

First, a simple test trading code that does work. The followingcode
tests some entry and exit ranges, and takes a position when agiven range
is true. I optimize over the various ranges, as well asLong/Short to see
what works best. I am testing Forex, and left out thebacktester settings
for simplicity here.

If you optimize or backtest this code, over any given month, you will
see output as expected:

//-----------------------------------------------------------------
//      Variables for Optimization
//-----------------------------------------------------------------


LongOrShort = Optimize("LongOrShort", 1, 1, 2, 1);
EntryNum    = Optimize("EntryNum",    1, 1, 3, 1);
ExitNum     = Optimize("ExitNum",     1, 1, 3, 1);


//-----------------------------------------------------------------
//      Set up Range Tests for Entries and Exits
//-----------------------------------------------------------------

myMA1       = MA(Close, 7);

Entry1      = Close >= myMA1 + 0.0001  && Close < myMA1 + 0.0003;
Entry2      = Close >= myMA1 + 0.0003  && Close < myMA1 + 0.0005;
Entry3      = Close >= myMA1 + 0.0005;

Exit1       = Close >= myMA1 + 0.0002  && Close < myMA1 + 0.0006;
Exit2       = Close >= myMA1 + 0.0006  && Close < myMA1 + 0.0010;
Exit3       = Close >= myMA1 + 0.0010;


Entry       = VarGet("Entry" + EntryNum);
Exit        = VarGet("Exit"  + ExitNum );


//-----------------------------------------------------------------
//      Trading System
//-----------------------------------------------------------------

Buy  = Short = 0;

    if(LongOrShort == 1)

          Buy   = Entry;

    else

        Short = Entry;


Cover = Sell = Exit;

So now all I want to do is run the exact same code as above, but with
aBarCount loop, so that I can calculate some basic statistics for
eachtrade over the optimization parameters. For example, I might want
tofind out the average profit for trades, the frequency of which
rangeswere used for entries, etc. While these trivial examples may
beavailable in the AB optimization report, my real-world use involves
allsorts of different stats that are not available.

After I calculate the stats, I want to dump them to a log file. Pretty
simple in theory.

But the following code suffers from these problems:


    1. If you run an optimization, all results are zero, instead of being
identical to the above code.
    2. If you run a backtest, no trades are ever taken, instead of
behaving identical to the above code
    3. The log file contains repeated rows. It should only contain one
row per optimization sweep
    4. The ProfitAvg stat is not calculated correctly. Entries are all
-10 Billion or so (!)

Can anyone spot why this is not working? I get no errors, and
tracesindicate that my loops are being correctly processed. Any input
muchappreciated!


//-----------------------------------------------------------------
//      Variables for Optimization
//-----------------------------------------------------------------


LongOrShort = Optimize("LongOrShort", 1, 1, 2, 1);
EntryNum    = Optimize("EntryNum",    1, 1, 3, 1);
ExitNum     = Optimize("ExitNum",     1, 1, 3, 1);


//-----------------------------------------------------------------
//      Set up Range Tests for Entries and Exits
//-----------------------------------------------------------------

myMA1       = MA(Close, 7);

Entry1      = Close >= myMA1 + 0.0001  && Close < myMA1 + 0.0003;
Entry2      = Close >= myMA1 + 0.0003  && Close < myMA1 + 0.0005;
Entry3      = Close >= myMA1 + 0.0005;

Exit1       = Close >= myMA1 + 0.0002  && Close < myMA1 + 0.0006;
Exit2       = Close >= myMA1 + 0.0006  && Close < myMA1 + 0.0010;
Exit3       = Close >= myMA1 + 0.0010;


Entry       = VarGet("Entry" + EntryNum);
Exit        = VarGet("Exit"  + ExitNum );


//-----------------------------------------------------------------
//      Trading System with BarCount Loop
//-----------------------------------------------------------------


FileName     = "F:\\Stats Log File.csv";

wasLong      = wasShort       = BuySignal    =
ShortSignal  = SellSignal     = CoverSignal  =
SoldCount    = CoverCount     = EntryFreq    = 0;

ValueAtBuy   = ValueAtShort   = ProfitLevel  =
ProfitSum    = ProfitAvg      = Null;

LongContractCount  = 0;
ShortContractCount = 0;


for (i = 0; i < BarCount-1; i++)
{
   wasLong  =  LongContractCount  > 0;
   wasShort =  ShortContractCount > 0;

   // Long Exit
   if (wasLong && Exit[i])
   {
      SellSignal[i] = 1;
      LongContractCount = 0;
      SoldCount++;
      ProfitLevel = SellPrice[i] - ValueAtBuy;
      ProfitSum  += ProfitLevel;
    }

    // Short Exit
    if (wasShort && Exit[i])
    {
      CoverSignal[i] = 1;
      ShortContractCount = 0;
      CoverCount++;
      ProfitLevel = ValueAtShort - SellPrice[i];
      ProfitSum  += ProfitLevel;
     }


   // Long entry
    if ( Entry[i] && LongOrShort == 1 )
    {
       BuySignal[i] = 1;
       EntryFreq++;
       LongContractCount = 1;
       ValueAtBuy = BuyPrice[i];
     }

   // Short entry
    if ( Entry[i] && LongOrShort == 2 )
    {
       ShortSignal[i] = 1;
       EntryFreq++;
       ShortContractCount = 1;
       ValueAtShort = ShortPrice[i];
     }
}

Sell  = SellSignal;
Cover = CoverSignal;
Buy   = BuySignal;
Short = ShortSignal;


//-----------------------------------------------------------------
//      Calculate Some Basic Stats
//-----------------------------------------------------------------


if(LongOrShort == 1)

    ProfitAvg = ProfitSum/SoldCount;

else

    ProfitAvg = ProfitSum/CoverCount;



//-----------------------------------------------------------------
//      Create a Text Header Row for the Log File
//-----------------------------------------------------------------

SetCustomBacktestProc( "" );

if ( Status( "ActionEx" ) == actionExOptimizeSetup )
{
     _TRACE( "Optimize Begin" );
     fh = fopen( FileName, "w" );

     if ( fh )
     {
        fputs( "LongOrShort, Entry, Exit, EntryFreq, ProfitAvg\n", fh );
        fclose( fh );
     }
}


//-----------------------------------------------------------------
//      Dump the Stats Values to the Log File
//-----------------------------------------------------------------


fh = fopen( FileName, "a" );

if ( fh )
{
    fputs( StrFormat( "%.0f,%.0f,%.0f,%.0f,%.5f\n",
                     LongOrShort, Entry, Exit, EntryFreq, ProfitAvg), fh
);
    fclose( fh );
   }


Reply via email to