Insufficient funds for futures

I am receiving the following error message:
"C_BA not entered because of insufficient funds or wrong position size/value (reqEntryPrice:1500, reqEntryPosSize:1 (value=1), ReqLotSize: 1)"

reqEntryPrice must be the margin deposit, because that is the Margin Deposit value in Information.

I'm frustrated because the documentation doesn't clearly delineate Futures from Stocks/Mutual Funds. Settings are jumbled together. I've experimented. I've done Explorations. I've done _TraceF. I've searched the Forum and the website.

If I do a points only test, it will work fine. I'm trying to learn how to do a simple dollar test on a single Future.

Here is my entire code:

// Backtest settings
PositionSize = 1;
SetBacktestMode( backtestRegular );
SetOption( "InitialEquity", 50000 );
SetOption( "AllowSameBarExit", True );
SetOption( "ActivateStopsImmediately", True );
SetOption( "FuturesMode", True );
SetOption( "MaxOpenPositions", 1 );
//SetOption( "MinShares", 1 );
SetOption( "AccountMargin", 50 );
SetOption( "CommissionMode", 2 );
setOption( "CommissionAmount", 0 );
SetOption( "EveryBarNullCheck", True );
SetOption( "DisableRuinStop", True );
//SetOption( "PriceBoundChecking", True );
 
// Common Calculations
midpt = ( H + L ) / 2;							// bar midpt
h1 = Ref( H, -1 );								// Yesterday's High
l1 = Ref( L, -1 );								// Yesterday's Low
c1 = Ref( C, -1 );								// Yesterday's Close
c2 = Ref( C, -2 );								// Close 2 days ago
th = Max( High, Ref( Close, -1 ) );				// True High
tl = Min( Low, Ref( Close, -1 ) );				// True Low
tr = th - tl;									// True Range
tr1 = Ref( tr, -1 );							// Yesterday's True Range
df = MA( C, 3 );								// Directional Filter
df1 = Ref( df, -1 );							// Directional Filter one day ago
df2 = Ref( df, -2 );							// Directional Filter two days ago
tf = df2;										// Trade Filter (Filters out normal signal)

// Two Different Entry Points
rawbb = c1 + tr1;								// Raw Buy Band, i.e. Normal Signal
rawsb = c1 - tr1;								// Raw Sell Band, i.e. Normal Signal
rawbb1 = Ref( rawbb, -1 );						// Yesterday's Raw Buy Band
rawsb1 = Ref( rawbb, -1 );						// Yesterday's Raw Sell Band
bb = Max( tf, rawbb );							// Buy Band: Max of Normal Signal and TF
sb = Min( tf, rawsb );							// Short Band: Min of Normal Signal and TF

// Preliminary FILTER ONE calculations
madiff = abs( df - MA( C, 18 ) );				// Absvalue Difference between the 18-Day and 3-Day MAs
trav = Ref( MA( tr, 3 ), 4 );					// Average True Range back 4 days
fltr1 = madiff < ( 0.5 * trav );				// Filter One signal to take trades in either direction

// Buy Conditions for each trade type
bcnd1 = c1 < df2;								// Condition 1: Yesterday's Close < DF 2 days ago
bcnd2 = H >= bb;								// Condition 3: High >= Buy Band (i.e., Market made it to Entry value)
bcnd_a = bcnd1 && bcnd2;						// Conditions for Trabos Last/First Combo as Type 1
bcnd3 = c2 > df1;								// Close 2 days back > yesterday's DF, i.e., fake setup of Short
bcnd4 = rawsb1 < df1;							// Yesterday's Raw SB < yesterday's DF
bcnd5 = l1 <= rawsb1;							// Yesterday's Low <= yesterday's Raw SB
bcnd6 = H >= rawbb;								// Raw BB is hit
bcnd_b = bcnd3 && bcnd4 && bcnd5 && bcnd6;		// Conditions for Rev Trabos Last as Type 2
bcnd_c = Ref( fltr1, -1 ) && bcnd6;				// Conditions for Filter1 as Type 3

// Buy Signals by Trade Type
type1by = bcnd_a && NOT( bcnd_b || bcnd_c );	// Type 1 Buy Signals that are also not either of the other types
type2by = bcnd_b && NOT( bcnd_a || bcnd_c );	// Type 2 Buy Signals that are also not either of the other types
type3by = bcnd_c && NOT( bcnd_a || bcnd_b );	// Type 3 Buy Signals that are also not either of the other types

// Short Conditiosn for each trade type
scnd1 = c1 > df2;								// Condition 1: Yesterday's Close > DF 2 days ago
scnd2 = l <= sb;								// Condition 3: Low <= Sell Band (i.e., Market made it to Entry value)
scnd_a = scnd1 && scnd2;						// Conditions for Trabos Last/First Combo as Type 1
scnd3 = c2 < df1;								// Close 2 days back < yesterday's DF, i.e., fake setup of Long
scnd4 = rawbb1 > df1;							// Yesterday's Raw BB > yesterday's DF
scnd5 = h1 >= rawbb1;							// Yesterday's High >= yesterday's Raw BB
scnd6 = l <= rawsb;								// Raw SB is hit
scnd_b = scnd3 && scnd4 && scnd5 && scnd6;		// Conditions for Rev Trabos Last as Type 2
scnd_c = Ref( fltr1, -1 ) && scnd6;				// Conditions for Filter1 as Type 3

// Short Signals by Trade Type
type1sl = scnd_a && NOT( scnd_b || scnd_c );	// Type 1 Buy Signals that are also not either of the other types
type2sl = scnd_b && NOT( scnd_a || scnd_c );	// Type 2 Buy Signals that are also not either of the other types
type3sl = scnd_c && NOT( scnd_a || scnd_b );	// Type 3 Buy Signals that are also not either of the other types

// Combined Buy Signals & Combined Short Signals
by = type1by || type2by || type3by;				// Buy when any of mutually exclusive buys trigger
sl = type1sl || type2sl || type3sl;             // Sell when any of mutually exclusive sells trigger
bycase = WriteIf( type1by == 1, "Bcase1",
         writeif( type2by == 1, "Bcase2",
         "Bcase3" ) );							// Which Buy Type triggered this trade?
slcase = writeif( type1sl == 1, "Scase1",
         writeif( type2sl == 1, "Scase2",
         "Scase3" ) );							// Which Short Type triggered this trade?

// Common Calculations for determining whether High or Low occurred first
r = H - L;										// Daily Range
rtop3 = H - r / 3;								// Value beginning the top third of Range
rbot3 = r / 3 + L;								// Value beginning the bottom third of Range0
rtop25 = H - ( 0.25 * r );						// Value beginning the top 25% of Range
rbot25 = L + ( 0.25 * r );						// Value beginning the bottom 25% of Range
topo = O == H;									// High occurred first because O = H; this is strongest case
boto = O == L;									// Low occurred first because O = L; this is strongest case
topc = C == H;									// Low occurred first because C = H; this is 2nd strongest case
botc = C == L;									// High occurred first because C = L; this is 2nd strongest case
upper1o = O >= rtop25;							// Open in upper 25% of range
upper2o = O >= rtop3;							// Open in upper third of range
upper3o = O > midpt;							// Open in upper half of range
lower1o = O <= rbot25;							// Open in lower 25% of range
lower2o = O <= rbot3;							// Open in lower third of range
lower3o = O < midpt;							// Open in lower half of range
upper1c = C >= rtop25;							// Close in upper 25% of range
upper2c = C >= rtop3;							// Close in upper third of range
upper3c = C > midpt;							// Close in upper half of range
lower1c = C <= rbot25;							// Close in lower 25% of range
lower2c = C <= rbot3;							// Close in lower third of range
lower3c = C < midpt;							// Close in lower half of range

// Conditions for which came first: High or Low
hf1 = topo;										// Strongest case: High hit first
hf2 = botc;										// 2nd strongest case: High hit first
hf3 = upper2o && lower2c;						// Strong case: High hit first
hf4 = upper1o && lower3c;						// Less strong case: High hit first.
lf1 = boto;										// Strongest case: Low hit first
lf2 = topc;										// 2nd strongest case: Low hit first
lf3 = lower2o && upper2c;						// Strong case: Low hit first
lf4 = lower1o && upper3c;						// Less strong case: Low hit first.
hf = hf1 || hf2 || hf3 || hf4;					// High First: any condition known to show High First
lf = lf1 || lf2 || lf3 || lf4;					// Low First: any condition known to show Low First
uf = NOT( hf || lf );							// Unknown First - don't know which is first

// Random determination of which came first: Profit Exit or Stop
rand_val = round( 9 * mtRandomA() );			// Random numbers
xf = rand_val <= 6;								// Profit hit first randomly 60% of the time
sf = 1 - xf;									// Stop hif first randomly 40% of the time

// Buy & Short Execution Prices
buyat1 = Max( O, bb );							// Execution Price is the Open or Buy Band, whichever is higher
buyat2 = Max( O, rawbb );							// Execution Price is the Open or Raw Buy Band, whichever is higher
shortat1 = Min( O, sb );							// Execution Price is the Open or Sell Band, whichever is lower
shortat2 = Min( O, rawsb );						// Execution Price is the Open or Raw Sell Band, whichever is lower

// Prepopulate variables for loop
lflat = True;									// Trading Long is flat, i.e., no Long trade
lexit = Null;									// Long Exit value set to Null
lstop = Null;									// Long Stop value set to Null
sflat = True;									// Trading Short is flat, i.e., no Short trade
sexit = Null;									// Short Exit value set to Null
sstop = Null;									// Short Stop value set to Null
ddd = DateNum();



// Loop through all bars

for( n = 0; n < BarCount; n++ )
{
    datex = DateTimeFormat( "%Y-%m-%d", DateTimeConvert( 2, ddd[n] ) );
    bcont = False;									// Once match is made "continue to next iteration" becomes true
    scont = False;									// Once match is made "continue to next iteration" becomes true

    //if( n <= 1672 )								// Use this to go to problem spot and walk it forward
    //{
    //cs = 1;
    //Buy[n] = Null;
    //Sell[n] = Null;
    //bcont = 1;
    //}

    if( lflat && NOT( By[n] || bcont ) )				// Long Market is flat and there is NO entry today
    {
        Buy[n] = 0;
        Sell[n] = 0;
        BuyPrice[n] = Null;
        SellPrice[n] = Null;
        _TRACEF( datex + ": No Entry" );
        bcont = True;
    }

    if( lflat && By[n] && NOT bcont )						// Long Market is flat and there IS an entry today
    {
        Buy[n] = by[n];

        switch( bycase )
        {
            case "Bcase1":
                BuyPrice[n] = buyat1[n];

            default:
                BuyPrice[n] = buyat2[n];
        }

        SellPrice[n] = Null;
        Sell[n] = 0;
        lexit = H[n];
        lstop = L[n];
        lflat = False;
        _TRACEF( datex + ": Entry Today at: " + BuyPrice[n] );
        bcont = True;
    }

    // Market is in Long Trade - Process the Trade through Exit

    if( ( O[n] >= lexit ) && NOT( lflat || bcont ) )   				// Gap Open (gap in relation to Target)
    {
        Sell[n] = 1;
        SellPrice[n] = o[n];
        BuyPrice[n] = Null;
        lexit = Null;
        lflat = True;
        _TRACEF( datex + ": Exit on Gap Open at: " + SellPrice[n] );
        bcont = True;
    }

    if( ( H[n] >= lexit ) && ( L[n] <= lstop ) && NOT( lflat || bcont ) ) // Target or Stop Hit Directly
    {
        Sell[n] = 1;
        SellPrice[n] = IIf( hf[n], lexit, IIf( lf[n], Min(lstop,O[n]), iif( xf[n], lexit, Min(lstop,O[n]) ) ) );
        BuyPrice[n] = Null;
        lexit = Null;
        lflat = True;
        _TRACEF( datex + ": Exit directly at SellPrice: " + SellPrice[n] + " ;  hf = " + hf[n] + " ; lf = " + lf[n] + " ; xf = " + xf[n] );
        bcont = True;
    }

    if( ( h[n] >= lexit ) && NOT( lflat || bcont ) )					// Target Hit Indirectly
    {
        Sell[n] = 1;
        SellPrice[n] = lexit;
        BuyPrice[n] = Null;
        lexit = Null;
        lflat = True;
        _TRACEF(datex + ": Exit indirectly at Target at " + SellPrice[n]);
        bcont = True;
    }

    if( ( L[n] <= lstop ) && NOT( lflat || bcont ) )  					// Stop Hit Indirectly
    {
        Sell[n] = 1;
        SellPrice[n] = Min(lstop,O[n]);
        BuyPrice[n] = Null;
        lexit = Null;
        lflat = True;
        _TRACEF(datex + ": Stop hit indirectly at: " + Sellprice[n]);
        bcont = True;
    }

    else																// No Signal
    {
        if (NOT bcont){
			Sell[n] = 0;
			SellPrice[n] = Null;
			BuyPrice[n] = Null;
			_TRACEF(datex + ": No Signal");
			bcont = True;}
    }
}

Filter = 1;
AddColumn(O,"Open", 1.7 );
AddColumn( H, "High", 1.7 );
AddColumn( L, "Low", 1.7 );
AddColumn( c, "Close", 1.7 );
AddColumn( buy, "Buy", 1.0 );
AddColumn( sell, "Sell", 1.0 );
AddColumn( buyprice, "BuyPrice", 1.7 );
AddColumn( SellPrice, "SellPrice", 1.7 );

Note:

  • I have played around with the settings at the top. Their current state is just the state of my last test.
  • Exploration / Trace / Scan show that the signals are correct.

I'm sure there is something simple I'm missing, but I haven't a clue as to what it is.

Many thanks in advance for any help anyone can give me.

Thanks.

Matt

Use this line as first line:

SetPositionSize(1, spsShares);// spsShares refers to shares and contracts

Remove the original one. Because this one says

Trade with $1.00.
So that's why you get insufficient funds message as position size is much too low.

See here:

AFL Function Reference - SETPOSITIONSIZE

  • values below -2000 encode share count,
  • values between -2000 and -1000 encode % of current position
  • values between -1000 and 0 encode % of portfolio equity
  • values above 0 encode dollar value

So instead it should have been

PositionSize = -2001;// trade 1 share/contract

which is equal to using

SetPositionSize(1, spsShares);// spsShares refers to shares and contracts

And this is not correct setting for futures as it is is for trading stocks on margin.
So you should set to
SetOption( "AccountMargin", 100 );
for futures/forex/CFD


Your settings are unknown to reader.
But nevertheless I think it should work now with above modifications.

2 Likes

fxshrat,

Thanks! The problem was solely SetPositionSize.

Regarding the AccountMargin setting, I had it set at 100 for almost all of my testing of various settings. Only in my last test did I set it to 50 as just a way of experimentation.

I really appreciate the tip.

Is there anywhere where I can see a list of just-use-these-functions-now, so that I don't get bogged down in old technology?

Thanks.

Matt

Not knowing of such lists.
But have just created thread for that

4 Likes

Your code looks into the future here:

trav = Ref( MA( tr, 3 ), 4 );	4 should be -4
1 Like

Yeah, I caught that. In there are a bunch of errors in the code logic. I was just using it to learn AmiBroker. I cleaned it up now.

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