can someone give me a basic example on how to perform a trade with stop-loss and take-profit on eurusd? i have been trying for multiple days to figure this out but nothing seems to work. I Do a market order and then my limitOrder get filled first so i should make profit but the backtest shows only (unrealized) loss. help would be very much appreciated!

 

I have been trying for a few days to get a very basic algorithm to work however suprisingly enough i can't seem to figure out how add stop loss/take profit to my orders. I have tried different api functions like MarketOrder,LimitOrder,StopMarketOrder etc. i always end up with unrealized profit?

this is what i do now. when there is a sell/buy signal i will do a market order and then a limit order as my take profit and a StopMarketOrder as my stop loss. In the logs i can see that on my first and only trade (i do only one trade for debugging puroposes) the LimitOrder eventually getsFilled. I catch this in the function onOrderEvent and then i do Transactions.CancelOpenOrders() or Transactions.CancelOrder(id). The StopMarketOrder then gets canceled. However when i run the backtest i make a loss and the loss stays unrealized. how can this be?

namespace QuantConnect.Algorithm.CSharp { /// <summary> /// Basic template algorithm simply initializes the date range and cash. This is a skeleton /// framework you can use for designing an algorithm. /// </summary> public class BasicTemplateAlgorithm : QCAlgorithm { private Symbol _eurusd; private RelativeStrengthIndex _rsi; private DateTime startdate; private DateTime enddate; private OrderTicket order; private OrderTicket limit; private OrderTicket stop; private bool trading; /// <summary> /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. /// </summary> public override void Initialize() { trading = false; startdate = new DateTime(2018,1,1); enddate = new DateTime(2018,12,14); SetStartDate(startdate); //Set Start Date SetEndDate(enddate); //Set End Date SetCash(100000); //Set Strategy Cash AddForex("EURUSD",Resolution.Hour,Market.Oanda); SetBrokerageModel(BrokerageName.OandaBrokerage); var lot = Securities["EURUSD"].SymbolProperties.LotSize; Debug("the lot size is "+ lot); var orderQuantity = 20180.12m; var order = Securities["EURUSD"].SymbolProperties.ContractMultiplier; Debug("the order size is "+ Math.Round(orderQuantity/lot)*lot); _eurusd = QuantConnect.Symbol.Create("EURUSD", SecurityType.Forex, Market.Oanda); _rsi = RSI(_eurusd,14,MovingAverageType.Wilders,Resolution.Hour); } /// <summary> /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here. /// </summary> /// <param name="data">Slice object keyed by symbol containing the stock data</param> public override void OnData(Slice data) { if (!_rsi.IsReady){ return; } QuoteBar quote = data[_eurusd]; Decimal price = quote.Close; Log("the bar price is "+ price); if (trading){ return; } decimal p = 1.001m; if (_rsi <= 30 && _rsi >= 25){ decimal limit = price+0.0046m; decimal stop = price-0.0050m; order = MarketOrder(_eurusd,1,false); Log("the price is " + price); Log("the limit is "+ limit); Log("the stop is "+ stop); //Console.WriteLine("limit: "+limit); //Console.WriteLine("stop: "+stop); this.limit = LimitOrder(_eurusd,1,limit); this.stop = StopMarketOrder(_eurusd,1,stop); //LimitOrder(_eurusd,1,limit); trading = true; } if (_rsi >= 70 && _rsi <= 75){ decimal limit = price-0.0046m; decimal stop = price+0.0050m; Log("the direction is Sell"); Log("the price is "+ price); Log("the limit is "+ limit); Log("the stop is "+ stop); order = MarketOrder(_eurusd,1,false); this.limit = LimitOrder(_eurusd,1,limit); this.stop = StopMarketOrder(_eurusd,1,stop); Log("The orderid for market order is "+ order.OrderId); Log("the order id for limit order is "+ this.limit.OrderId); Log("the order id for stop order is "+ this.stop.OrderId); trading = true; } } public override void OnOrderEvent(OrderEvent orderevent){ Log("OrderEvent occured"); Log("OrderEvent Status " + orderevent.Status); Log("The Order Id is " + orderevent.OrderId); if (orderevent.OrderId == 2){ if (orderevent.Status != null){ if (orderevent.Status == OrderStatus.Filled){ Transactions.CancelOrder(1); Transactions.CancelOrder(2); Transactions.CancelOrder(3); } } } //Log("OrderStatus marketorder" + order.Status); //Log("OrderStatus limitorder" + limit.Status); //Log("OrderStatus stoporder" + stop.Status); //Console.WriteLine("an order event happened"); //Console.WriteLine("the order status: "+orderevent.Status); //Console.WriteLine("the order fillprice "+orderevent.FillPrice); /*if (orderevent.Status == OrderStatus.Filled){ order.Cancel(); }*/ } } }

 

can someone give me a very basic example on how to do an order on eurusd with a stop loss and take profit? and also in such a way that the profit doesn't stay unrealized?

Author