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 )
on
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);
entryprice=sig.Price();
}
//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 !