Automated Trading BuyPrice Question

I have this simple algo that I am migrating to automated paper trading with IB, and I am
struggling to decide what the BuyPrice should be.

In backtesting, I set the BuyPrice to Open, and I am unclear what the "equivalent" is in real-time trading.

I have access to real-time Bid/Ask/Last data, so that is what I am struggling with.

Here is a code snippet...

SetTradeDelays( 0, 0, 0, 0 );

BuySignal =  
			Cross( Close, MA( Close, 20, 2 ) )
		AND SPYup ;

Buy =  Ref( BuySignal, -1 ); 
BuyPrice = Open * 1.0000; 
Sell =  
         Cross( MA( Close, 20 ), Close  ); 

Would one recommend that I set

BuyPrice = Open;


BuyPrice = GetRTData( "Last" );

Which is better? (Or perhaps some other value )

What would one recommend in order to emulate the backtest as close as possible?

Thanks in advance...

1 Like

Use last price (for example).

LastPrice = LastValue( C );


Thanks for responding, but I am not sure why you would use that price. Is there anyway you could explain why you recommend

LastPrice = LastValue( C );

Maybe it's obvious, but I just don't understand why - Thanks

It is last available stored price not just close price of bar.
You may use any other price for setting limit, stop price (number).

But if you place market order then you do not define entry, exit price.

For example

ib.PlaceOrder(Name(), "Buy", shares = 1, "MKT", 0, 0, "Day", True);

Thanks for your help here...

DoI need to change my code in terms of when the Buy signal is generated?

That is from

SetTradeDelays( 0, 0, 0, 0 );
Buy =  Ref( BuySignal, -1 );
BuyPrice = LastValue( Close );


SetTradeDelays( 0, 0, 0, 0 );
Buy =  BuySignal; // I.e., Without the "Ref(  , -1 )"
BuyPrice = LastValue( Close );

I'm just getting real confused here. I am having trouble with the relationship between the "backtest" and a "live" trading system.

I.e., can I just use the same "backtest" code and just change the BuyPrice?

I hope I am making sense...
to best emulate backtest results?

Backtest is different from live trading.

Please take a look at the examples of the IB Controller help.