Use _TRACE, not GfxTextOut for debugging CBT, was: Marketposition again

Hello !

Sorry for lame question again. I know that this topic was already discussed but still I can't produce a solution. I'm trying to make a script that will just show me entry and exit levels if we are in/out of position. So obviously some kind of market position is necessary. So far I have tried this -

if( Status( "action" ) == actionPortfolio      )
    bo = GetBacktesterObject();
    bo.PreProcess();    //  Do pre-processing
    for (i = 0; i < BarCount; i++)    //  Loop through all bars
     for (sig = bo.GetFirstSignal(i); sig; sig = bo.GetNextSignal(i))
            if (sig.IsEntry() )   
                bo.EnterTrade(i, sig.Symbol, False, sig.Price, sig.PosSize);
                if (sig.Type()==1) {GfxTextOut("We entered Long",10,70); GfxTextOut("Entry price is "+sig.Price(),10,100); } 
                if (sig.Type()==3) {GfxTextOut("We entered Short",10,70); GfxTextOut("Entry price is "+sig.Price(),10,100);} 
                if (sig.Type()==2) {GfxTextOut("We have sold and waiting for a new signal",10,70);} 
                if (sig.Type()==4) {GfxTextOut("We have covered and waiting for a new signal",10,70);} 
        }    //  End of for loop over signals at this bar
		bo.HandleStops(i);    //  Handle programmed stops at this bar
		bo.UpdateStats(i, 1);    //  Update MAE/MFE stats for bar
        bo.UpdateStats(i, 2);    //  Update stats at bar's end
     bo.PostProcess();    //  Do post-processing

then I just "apply" script and nothing happens, no text on chart. Well, I suppose that Status( "action" ) is actually not an actionPortfolio because we didn't backtested but just want to see something on chart. Okay, let's change

if( Status( "action" ) == actionPortfolio      )


if( Status( "action" ) == actionIndicator  )

It gives a runtime error. Predictable outcome because backtester is needed for portfolio testing. Okay, then another option -

 if (sig.IsEntry() )    //  Process long entries
         bo.EnterTrade(i, sig.Symbol, False, sig.Price, sig.PosSize);
//this row is outside CBT - 
GfxTextOut("Entry is "+entryprice,10,30);

That doesn't work also due to portfolio backtester. Error is "variable is not initialized" , we have no instance of portfolio object so far and all variables inside CBT are inaccessible.

Okay, then let's imagine that we are connected to IB (no autotrading, AB is just assisting us) and we want to see on chart text indicating whether we are in position and what was the entry price. How to implement that ?

Thank you everyone for your help !

I’m not an expert but no one has chimes in.

Think you may be tackling it the wrong way.

If you plan on auto trading through IB then look at the IB controller documentation.

Believe theirs methods to call positions, it’d be just as simple as plotting those calls.

From my understanding backtester and live trading are different tasks. Live trading would require writing AFL for your system, using calls to get orders, executions, positions, etc.

Custom backtester is just a method to manipulate the backtest engine.

GfxTextOut is not custom backtester function. It is chart only.
Then entire if() statement is not entered in chart because it is for custom backtester.

You need to go back to the basics and learn basic debugging techniques: How do I debug my formula? and use _TRACE() function.

1 Like