2 Recursive Variable Declarations Referring to Each Other

I'm building a martingale system and the actual system is significantly more complex than the following code but the problem I can't wrap my head around is demonstrated in this much simpler code.

In essence, SharesToTrade should double the numOfShares from the previous bar while numOfShares should count the total number of shares since the trade was made. Each starting at 1 every time there's a new trade.

The thing I can't wrap my head around is how to get each variable to be a function of the other. Whichever variable is defined first uses the value of the initialization instead of the previous values of the variables.

The SumSince() function works to get the correct value of NumOfShares but only if SharesToTrade is the correct value. Alternatively, NumOfShares could also start with the same value it had the previous bar and then add the SharesToTrade at the end, but again I can't figure out how to get that first variable to refer to the last bar instead of the static initialization at the top.

Basically, NumOfShares can't be defined without either itself or SharesToTrade and SharesToTrade can't be defined without NumOfShares.

The code:

SharesToTrade = 100;
NumOfShares = 100;

Buy = C < O;

Sell = C > O;

newTrade = Buy AND NOT Ref(Buy, -1) AND NOT sell;

NumOfShares = IIf(newTrade, 1, SumSince(newTrade, SharesToTrade));

SharesToTrade = IIf(newTrade, 1, NumOfShares*2);



Plot(SharesToTrade, "Shares To Trade", colorLavender, styleStaircase);
Plot(NumOfShares, "Num of Shares", colorAqua, styleStaircase);
Plot(newTrade, "New Trade", colorBrown, styleStaircase);

Actual Result of code:
image

What I want it to do:
image

First I would note that the "what I want it to do" table is wrong because if "Num of shares" column shows the value AFTER the trade then in second trade "Num of shares" column should show 3, not 1.
Similar errors are done later. If that column shows the amount BEFORE trade, then first row should show zero.

What you want to achieve is not really "variable declarations" problem but the fact that you are using previous bar calculation to update next bar values which essentially can be written as recursive formula

 y( n + 1 ) = f( y( n ) );

where f() is any function. Such recursive formulation is often used for things like filtering (EMA, IIR) and sometimes it can be written using array functions (when specific function handling given case is available) but generally they are written easiest using a simple loop

for( i = 1; i < BarCount; i++ )
{
   something[ i ] = any_function( something[ i -1 ] );
}

So, you should simply write a loop. The problem of "referring to each other" is just solved by adding an extra variable.

newTrade = DayOfWeek() == 3;

nextTradeShares = 1;
totalShares = 0;

SharesToTrade = 0;

for( i = 0; i < BarCount; i++ )
{
    NumOfShares[ i ] = totalShares;
    
   if( newTrade[ i ] ) 
   {
      SharesToTrade[ i ] = nextTradeShares;
	  nextTradeShares = 2 * totalShares;
      totalShares += SharesToTrade[ i ] ;
	}
}
  
Filter = newTrade;
AddColumn( NumOfShares, "NOS");
AddColumn( SharesToTrade, "STT");

Caveat: the code above tries to reproduce the results given in the "desired result" table from OP poster post, however I think that that table is wrong, so the code is trying to achieve wrong result may be wrong too.

2 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.