During backtest I am not sure how my code can detect that a position is Closed, due to an ApplyStop command. Suppose I code something like this:
stopAmount = 0.0005;
ApplyStop( stopTypeLoss, stopModePoint, stopAmount, True );
bo = GetBacktesterObject(); // Get backtester object
bo.PreProcess(); // Do pre-processing (always required)
if (MYcondition)
Buy[index] = true;
bo.ProcessTradeSignals( index ); // Process trades at bar (always required)
openpos = bo.GetFirstOpenPos();
if (openpos.IsOpen)
printf("The position is still Open!\n");
Given that I want to have just one position open at a time. Is it enough to check for (openpos.IsOpen == True), to see if the position I opened from Buy[index] = true; is still ON?
Is this the simplest way to do it? I mean, do I have to use the bo = GetBacktesterObject(); ?
PS: I have not tested this code. I have just assembled pieces and ideas from many articles I read.
As I am studying your example and the documentation, I now realise that my question should have been titled: Detecting if the position is still Open, during actionIndicator
Suppose this script runs in Indicator/Chart mode. It only generates Buy signals. But how about StopLoss signals? Does the ApplyStop generate such signals? Ie does it change the Sell array?
My test says it does not. I can see the StopLoss signals in the backtester, but I do not see any down arrows in the chart.
m = MA( Close, 20 );
Buy = Cross( Close, m );
Sell = 0;
//Sell = Cross( m, Close );
Short = Cover = 0;
stopAmount = 0.002;// e.g. 20 pips EURUSD
ApplyStop( stopTypeLoss, stopModePoint, stopAmount, True );
/* Plot Buy and Sell Signal Arrows */
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, colorGreen, 0, IIf( Buy, Low, High ) );
What I am trying to do is a script that runs in indicator mode, runs with live data and generates signals. The user (me) looks at the signals and enters them in a different live trading platform. Both Amibroker script and the live platform operate with same stoploss settings (ie 10 pips). When the trade is stopped by a stoploss the amibroker script whould be able to know this.
This is what I am missing: How will the ApplyStop inform my script in IndicatrMode that there was a StopLoss?
And ofcourse I need to be able to test all these in backtest mode. Can this be done with one script? Do I need to write different script for live mode and different for backtesting? In this case I am afraid that the algorithm may not be exactly the same.
You said backtesting in thread subject and were using CBT code.
Now you talk about live trading all of the sudden.
Yes, live trading is different from backtesting.
If in live trading using IB then you can retrieve open position list via API.
Take a look at IBcontroller manual.
Also take a look at PlaceOrder etc. (well, read all of IBcontroller manual)
After reading your example I understood. This is exactly what I wanted: I needed to call the EQUITY function and check from (SELL == 2).
I am not using IB so no need to use their API. By Live system I mean AB on live feed giving signals that a human operator can manually enter in a trading platform. And somehow the same script to be able to go through backtesting.