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

Re: Combined Equity Curves



PureBytes Links

Trading Reference Links

Phil Lane <logical@xxxxxxxxxxxxx> wrote:
> I've been using Doug Demming's global variable stuff to feed
> numbers generated by a system into an indicator. And it works
> pretty well. 
> I was thinking I could rig each system to store information in
> global variables, and then create one giant indicator to combine
> everything and display it. Except it's not entirely obvious how. 

You could do something like this.  I just whipped this out off the 
top of my head and I haven't tested this code, but it should give you 
a good start.

Note:  This code assumes all systems run with the same bar interval, 
e.g. all are 60-min bars.  If some of your systems run with different 
bar intervals, you'll have to get a little fancier.  If your systems 
run with daily bars, you should modify the code that indexes into the 
equity array as indicated by the comment.  

This code allocates global memory for 24 hrs of bars a day.  If your 
systems run only during S&P RTH hours, for example, you could save 
some global memory by making the equity-indexing code a little 
smarter so it subtracts out non-trading hours.  But since a chart 
can't hold more than 13000 bars, one system's data takes no more than 
13000*4=52k bytes.  Even if you're trading 6 hours out of 24, and you 
end up wasting memory for the other 18 hours, each system still only 
eats up only 200k.  Unless you're going to combine dozens of systems 
and leave it running all the time, the compressed indexing probably 
isn't worth it.   

Gary (wish I could trade as well as I program :-) F.

=========================

In each system:

{ This code stores the closed equity for N systems.
  All equity values for the same bar are stored contiguously, e.g.:
  (Base-1)   (Base)
  StartDate  Sys0Bar1  Sys1Bar1  Sys2Bar1 ... Sys0Bar2  Sys1Bar2 ...

  System #0 is the "master" system, and must be run *before*
  all other systems.  Its starting date is stored as the starting
  date for the equity data, and all other systems key off that date.
  No system should have a starting date earlier than System #0,
  although the code shouldn't bomb if one does.
}

{ Constants:  
    NSys:      number of systems to be combined
    MySysNum:  ID# of the current system, 0 through NSys-1
    Base:      location in global memory to store the system data
    MinPerDay: # minutes per day (duh :-)
}

Vars: NSys(10), MySysNum(0), Base(1001), MinPerDay(1440);

Vars: NowDate(0), NowTime(0), StartDate(0), SysEquity(0);

{ Get current date/time.  Store base date in global memory,
  just before the array of system data.
  Must store base date & compute minute offset from the base
  when we know the start-date of the chart, since
  computing the absolute minute (MinPerDay*NowDate+NowTime) 
  exceeds the precision of the TS floating-point numbers. 
}

NowDate = DateToJulian(Date);
NowTime = TimeToMinutes(Time);

if (CurrentBar = 1) then begin
  { "Master" system stores the date; all others read that date }
  if (MySysNum = 0) then begin
    StartDate = NowDate;
    Save_TS_Data(Base-1, StartDate);
    end
  else
    Load_TS_Data(Base-1, &StartDate);
  end;

{ Retrieve & store the system equity values.
  Must check for StartDate <> 0 because TS sometimes doesn't 
  set CurrentBar = 1 until the 2nd or 3rd bar !! 
  Check for NowDate >= StartDate in case system #N starts before
  system #0. 
}

if (StartDate <> 0) and (NowDate >= StartDate) then begin
  { If trading daily bars, just use TimeIndex = NowDate-StartDate; }
  TimeIndex = 
     (MinPerDay * (NowDate-StartDate) + NowTime) / BarInterval;
  Sys Equity = I_ClosedEquity;    
                        { Or use I_OpenEquity if you prefer }
  Save_TS_Data(Base + Nsys*TimeIndex + MySysNum, SysEquity);
  end;

======================

In the EquityCurve indicator:  
(You may want to run this on the same chart as your "master" system)

Vars: NSys(10), Base(1001), MinPerDay(1440);

Vars: NowDate(0), NowTime(0), StartDate(0), SysNum(0), 
      SysEquity(0), Eq(0);

{ Get current & base date. }

NowDate = DateToJulian(Date);
NowTime = TimeToMinutes(Time);

if (CurrentBar = 1) then 
    Load_TS_Data(Base-1, &StartDate);

{ Retrieve equity for each system for this bar, sum & plot }

if (StartDate <> 0) and (NowDate >= StartDate) then begin
  TimeIndex = 
     (MinPerDay * (NowDate-StartDate) + NowTime) / BarInterval;
  SysEquity = 0;
  for SysNum = 0 to NSys-1 begin
    Load_TS_Data(Base + Nsys*TimeIndex + MySysNum, &Eq);
    SysEquity = SysEquity + Eq;
    end;
  end;

Plot1(SysEquity, "Equity");