|
M808
2016-08-26 02:45:37
|
|
Hi QS,
I have loaded trade tick, bid tick and ask tick data into QS.
My long entry signal is based on 5 minute bars. However I want my long exit to be a long trailing stop that trails all changes in the bid price accordingly and fills on the bid price accordingly.
I've been having trouble coding this long trailing stop and would really appreciate a code example.
Thanks.
|
|
|
|
QuantShare
2016-08-26 06:29:09
0
|
|
Do you want to use this for backtesting or live trading?
|
|
|
|
M808
2016-08-26 15:04:09
0
|
|
Both backtesting and live trading.
|
|
|
|
QuantShare
2016-08-27 03:40:12
0
|
|
This cannot be done in backtesting mode. In live/demo/paper trading, you should use the money management script.
In the "OnMarketData" event, you have something like this:
if(MarketData.Type == MarketDataType.Bid)
{
MMPosition pos = Portfolio.GetPosition(MarketData.Symbol, "long");
if(pos != null)
{
if(pos.Var1 is _StopOrder)
{
// Update order
_StopOrder order = pos.Var1 as _StopOrder;
order.Price = MarketData.Price;
order.SubmitChanges();
}
else
{
// Create order
_StopOrder order = Orders.StopOrder(MarketData.Price, -1, 100);
pos.Var1 = order;
pos.ClosePosition(order);
}
}
}
|
|
|
|
M808
2016-08-27 16:23:43
0
|
|
Thanks. Can this be done in backtesting mode with regular (not trailing) stop losses and take profits?
|
|
|
|
QuantShare
2016-08-28 05:37:09
0
|
|
I am not sure if I understand correctly your question but for a stop or take profit, just add the appropriate stop in the trading system. You do not need a money management script for that.
|
|
|
|
M808
2016-09-01 02:51:33
0
|
|
Here is what I mean.
Is there a way in backtesting (and live trade mode) to have my long stoploss (not long trailing stoploss) monitor all changes in the Bid price?
For instance, lets assume I enter a long trade based on a signal from a 10 minute bar and have a 5 cent stoploss. Lets assume I am filled at $101 because at the close of the 10 minute bar that caused my entry the ask price was $101 and the Bid price was $100.99. Lets assume a few seconds later the bid price dropped to 100.95 (while the Ask and last price stayed the same).
Is there a way to have this drop in Bid price trigger my stoploss and have my stoploss fill at 100.95 (in both backtesting and live trading)?
|
|
|
|
QuantShare
2016-09-01 10:23:20
0
|
|
It can be done only in live trading. Backtesting cannot be done based on bid/ask changes. Only tick data triggers events.
Regarding live mode, yes this can be done. You need to modify the script I have sent you above to do that. If you need more info please contact support by email.
|
|
|
|
Alexander Horn
2016-12-27 03:55:27
0
|
|
Hi QS,
could you please reveal what is held in the MMPosition Var1 to Var3 and _TradingOrder Var1 Properties? From above it seems _TradingOrder Var1 is the ordertype, but the others? And where do you hold the IB Order reference #, and how to access it?
Another suggestion: In Orders.AddShort/LongPosition we can assign a MMPositionSettings directly when creating the order, for example to assign to custom Category. When creating Child or Linked orders there seems to be no way to assign a MMPositionSettings directly, or am I wrong?
|
|
|
|
QuantShare
2016-12-27 04:48:22
0
|
|
Nothing is held in the Var1 to Var3. These are variables you can use to store data yourself.
To get Order ID in the "_TradingOrder" object, please use "Status.OrderID" / "Status.RequestID"
And yes there is no way to assign an MMPositionSettings directly when adding an OCA or OSO orders.
|
|
|
|