Hi,
With realtime trading you need to think a little differently then if
you were backtesting: It is quite likely that your AFL code will be
run multiple times during the same bar. If you are running it from a
chart, then the code will be run every time this is refreshed - this
could be many times per second. Check your intraday settings in
preferences for this. I myself run my systems from the Analysis
window: The frequency is much less, but it may run several times for
the same bar nonetheless.
It is important to understand that if a signal is true on the bar,
then it is true every time the AFL is run, so multiple signals are
only natural and need to be taken into consideration.
Also you must be prepared for inaccuracies as you will never be able
to exactly match an event such as the open of a new bar in your real
time trading. The more liquid the instrument, the better it will be.
A) You must address multiple buy/sell signals by checking whether you
already have an open position. Alternatively, use static variables so
that you do not repeat an order while on the same bar. You can then
reset the variable by detecting a change of bar.
B) If you want to attempt to get as close as possible to the open of
the next bar, then what I suggest you do is to store your order
signals into static variables when they occur (anytime in the current
bar). Then, in your IB order logic, detect the change of bar to fire
the orders and reset these order static variables. I find this to be
virtually impossible to acheive using AA window for autotrading. I
personally resolve this problem by using the smallest timeframe
possible on the AA Window and let the trade happen at any point during
the bar where the signal occurs. For backtesting I take an average of
the bar as my price. I find this inaccuracy to be almost irrelevant
when compared to other forms of slippage.
I suggest you refer to the Amibroker User Knowledge Base:
http://www.amibroker.org/userkb/ <http://www.amibroker.org/userkb/>
which has a lot of valuable information on the subject.
As regards to detecting the change of bar, I did not have the time to
find this code for you but I have several instances of it on this
knowledgebase and/or in other posts.
eToke
--- In [email protected] <mailto:amibroker%40yahoogroups.com>,
"shakerlr" <ljr...@...> wrote:
>
> After posting this message:
> http://finance.groups.yahoo.com/group/amibroker/message/137490
<http://finance.groups.yahoo.com/group/amibroker/message/137490>
>
> Which brings up a point on signals on the current bar in real time.
Namely, they may occur many times until that bar is finally
written/printed. I tried my strategy with the bar replay tool and
found that I indeed was generating multiple signals to the (IB)
auto-trader.
>
> I tried users/Thomas' suggestion of using
> buy=ref(buy, -1), but I still get multiple signals.
>
> http://finance.groups.yahoo.com/group/amibroker/message/132762
<http://finance.groups.yahoo.com/group/amibroker/message/132762>
> http://finance.groups.yahoo.com/group/amibroker/message/132761
<http://finance.groups.yahoo.com/group/amibroker/message/132761>
>
> Is there something else that I am missing to make the buy on prev
bar work. All I REALLY want to do is have a signal generated on the
current bar, and then act (open a trade) on that signal on the NEXT
bar open. Basically simulating the settradedelay(1,1,1,1) function,
but with real trading.
>
> here is my code below...you will see multiple traces showing
multiple buy signals. Note that useIB is false so AT is turned off.
>
> useIB=0;
>
> starttime=TimeNum() >= 094500 AND TimeNum() <=154500;
> test=0;
> Minprice=5;
> PositionSize = -20; // use minuse for -xx% of equity
>
> if (useIB)
> {
> ibc = GetTradingInterface("IB");
> }
>
> //delay=Optimize("tradeDelay", 1, 1, 1, 1);
> //SetTradeDelays(delay, delay, delay, delay);
>
> Buy = Cross((Close),(EMA(Close,9)));
>
> // this should delay the signal for the next bar - right?
> Buy=Ref(Buy, -1);
>
> Buystop = Ref(EMA(Close,9),-1);
> BuyPrice = Max(Buystop,Low);
>
> // not worried about delaying the sell signal until I get the buy
signal working
> Sell = Cross(EMA(Close,9),(Close));
> Sellstop = Ref(EMA(Close,9),-1);
> SellPrice = Min(sellstop,High);
>
> Buy = ExRem(Buy,Sell);
> Sell = ExRem(Sell,Buy);
>
> if (LastValue(Buy))
> {
> _TRACE("BUYprice: " + BuyPrice + ", time=" + TimeNum());
>
> if (useIB)
> {
> ibc = GetTradingInterface("IB");
>
> // check if we are connected OK
> if( ibc.IsConnected() )
> {
> // check if we do not have already open position on this stock
> if( ibc.GetPositionSize( Name() ) == 0 )
> {
> // transmit order
> ibc.PlaceOrder( Name(), "Buy", 10, "MKT", 0, 0, "Day", True );
> }
> }
> }
> }
>
> if (LastValue(Sell))
> {
> _TRACE("Sellprice: " + SellPrice + ", time=" + TimeNum());
>
> if (useIB)
> {
> ibc = GetTradingInterface("IB");
>
> // check if we are connected OK
> if( ibc.IsConnected() )
> {
> // need to have open position
> if( ibc.GetPositionSize( Name() ) > 0 )
> {
> // transmit order
> ibc.PlaceOrder( Name(), "Sell", 10, "MKT", 0, 0, "Day", True );
>
> StaticVarSet(Name(), 0);
> }
> }
> }
> }
>
> shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
> PlotShapes( shape, IIf( Buy, colorBlue, colorRed ), 0, IIf( Buy,
Low, High ) );
> PlotShapes(IIf(Buy ==1, shapeUpArrow,shapeNone),colorBlue,0,0,10);
> PlotShapes(IIf(Sell ==1, shapeHollowUpArrow,shapeNone),colorRed,0,0,10);
>