I am currently implementing a strategy that tries to detect short-term, intraday trends and ride them for a while until a profit target or a stop loss is hit.
The strategy should detect intraday momentum based on a few common indicators:
if self.eurUsd.macd.Current.Value>0 \
and self.eurUsd.macd.Current.Value > self.eurUsd.macd.Signal.Current.Value \
and self.eurUsd.bb.UpperBand.Current.Value>self.eurUsd.bbUpperPrevious \
and self.eurUsd.rsi.Current.Value>0.7:
self.Debug("Place Order!")
If the conditions are true, an order should be placed in the following way:
stopLoss = self.eurUsd.atr.Current.Value * 0.1
profitTarget = self.eurUsd.atr.Current.Value * 0.15
currentPrice = data[self.eurUsd.symbol].Price
stopLossPrice = currentPrice - stopLoss
profitTargetPrice = currentPrice + profitTarget
limitPrice = self.eurUsd.bb.UpperBand.Current.Value
#Place an order with the following features:
# - enter 100% long position once price of currency pair is above limitPrice
# - close long position once the price of the currency pair is
# below stopLossPrice or above profitTargetPrice
The strategy is still in an early development stage and obviously, all comments are highly appreciated.
Currently I am stuck at implementing an order placement with the following conditions:
- Open 100% long position, once the price of the currency pair is above the limitPrice
- Close the long position, once the price of the currency pair is above the profitTargetPrice or below the stopLossPrice
See the attached code for all details.
Filib Uster
I have tried to implement the order state machine based on an Alexandre Catarino 's answer to a similar question .
The orders are placed in the following way:
self.Buy(self.eurUsd.symbol, 1000)
self.LimitOrder(self.eurUsd.symbol, -1000, profitTargetPrice)
self.StopMarketOrder(self.eurUsd.symbol, -1000, stopLossPrice)
In the OnOrderEvent, either the StopLoss or the LimitOrder is canceled, once the other one is filled
def OnOrderEvent(self, orderEvent):
order = self.Transactions.GetOrderById(orderEvent.OrderId)
if order.Status == OrderStatus.Filled:
if order.Type == OrderType.Limit or order.Type == OrderType.Limit:
self.Transactions.CancelOpenOrders(order.Symbol)
if order.Status == OrderStatus.Canceled:
self.Log(str(orderEvent))
The trade summary of the backtest however shows, that each filled market order is immediately followed by a filled Limit and StopMarket order, when actually only one of them should follow each market order:
What is the problem, here?
Filib Uster
Emberassingly, the problem was a wrong indendation of the OnOrderEvent. After fixing it, the order state machine works as expected.
Apollos Hill
Hi thanks for sharing. Why are you only buying the EURUSD? Its been short trend over the past months when you posted this until now. Also, i've never found stop loss orders to work out with algo trading.
Filib Uster
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!