Here is my attempt to build a hedge strategy using the CustomBacktestProcedure. It is supposed to offset my total long dollars with the appropriate amount of SPY short dollars.
///////////// BACKTESTER OBJECT ////////////////////////////////////////////////
SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.PreProcess();
for ( i = 0; i < BarCount; i++ )
{
bo.ProcessTradeSignals( i );
OkToHedge = 1;
HedgeThreshPct = 1;
TotalLongInv = 0;
TotalShortInv = 0;
SPYshortShares = 0;
Diff = 0;
for ( openpos = bo.GetFirstOpenPos(); openpos; openpos = bo.GetNextOpenPos() )
{
if (openpos.IsLong)
{
TotalLongInv += openpos.EntryPrice * openpos.Shares;
}
else
{
TotalShortInv += openpos.EntryPrice * openpos.Shares;
SPYshortShares = openpos.Shares;
}
Diff = TotalLongInv - TotalShortInv;
}
_TRACE("$TotalLongInv " + NumToStr(TotalLongInv, 1.0) + " $TotalShortInv: " + NumToStr(TotalShortInv, 1.0) + " $Diff: " + NumToStr(Diff, 1.0) );
if (OkToHedge == 1)
{
if(TotalLongInv > 0 AND TotalShortInv == 0)
{
spyC = Foreign("SPY","C");
bo.EnterTrade( i, "SPY", 0, spyC[i], Diff/spyC[i]);
_TRACE("i: " + NumToStr(i, 1.0) + " L>0,S=0. CurrSPYshares: " + NumToStr(-SPYshortShares) + " $Diff: " + NumToStr(Diff) + " SPY C: " + NumToStr(spyC[i]) );
_TRACE("initially Shorting " + NumToStr(Diff/spyC[i]) + " SPY shares");
}
else if(TotalLongInv > 0 AND TotalShortInv > 0 AND TotalLongInv + (TotalLongInv * HedgeThreshPct * .01) > TotalShortInv)
{
spyC = Foreign("SPY","C");
bo.ScaleTrade( i, "SPY", 0, spyC[i], abs(Diff)/spyC[i]);
_TRACE("i: " + NumToStr(i, 1.0) + " L>0+%,S>0. CurrSPYshares: " + NumToStr(-SPYshortShares) + " $Diff: " + NumToStr(Diff) + " SPY C: " + NumToStr(spyC[i]) );
_TRACE("-Scaling " + NumToStr(Diff/spyC[i]) + " SPY shares");
}
else if(TotalLongInv > 0 AND TotalShortInv > 0 AND TotalLongInv - (TotalLongInv * HedgeThreshPct * .01) < TotalShortInv)
{
spyC = Foreign("SPY","C");
bo.ScaleTrade( i, "SPY", 1, spyC[i], abs(Diff)/spyC[i]);
_TRACE("i: " + NumToStr(i, 1.0) + " L>0-%,S>0. CurrSPYshares: " + NumToStr(-SPYshortShares) + " $Diff: " + NumToStr(Diff) + " SPY C: " + NumToStr(spyC[i]) );
_TRACE("+Scaling " + NumToStr(Diff/spyC[i]) + " SPY shares");
}
else if(TotalLongInv = 0 AND TotalShortInv > 0)
{
spyC = Foreign("SPY","C");
bo.ExitTrade(i, "SPY", spyC[i], 1);
_TRACE("i: " + NumToStr(i, 1.0) + " L=0,S>0. CurrSPYshares: " + NumToStr(-SPYshortShares) + " $Diff: " + NumToStr(Diff) + " SPY C: " + NumToStr(spyC[i]) );
_TRACE("Exiting all SPY shares");
}
}
}
bo.PostProcess();
}
But as the following trace log shows, it is not working properly. What am I doing wrong here?