Overall Statistics |
Total Trades 0 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio NaN Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha 0 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio NaN Tracking Error NaN Treynor Ratio NaN Total Fees $0.00 |
namespace QuantConnect { /* * QuantConnect University - Creating and Updating Limit Orders * * This algorithm walks through an example of creating and updating a limit order. * The orders are stored in the Transactions Manager inside the algorithm. * * For the demonstration we will place a buy limit order for 1 SD below of microsoft's price, * and every day update it until its filled */ public class QCULimitOrders : QCAlgorithm { //Access for the order we'll place int _limitOrderId = 0; LimitOrder _limitOrder; string _symbol = "GBPUSD"; decimal _price = 0; int _rebalancePeriod = 20; DateTime _updatedDate; public SimpleMovingAverage SMA_Fast; public SimpleMovingAverage SMA_Slow; string direction = "bullish"; int quantity = 1000; //Initialize the data and resolution you require for your strategy: public override void Initialize() { SetStartDate(2013, 1, 1); SetEndDate(DateTime.Now.Date.AddDays(-1)); SetCash(25000); AddSecurity(SecurityType.Forex, _symbol, Resolution.Minute); SMA_Fast = SMA(_symbol, 60, Resolution.Minute); SMA_Slow = SMA(_symbol, 600, Resolution.Minute); } //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol. public void OnData(TradeBars data) { //if (data.Contains("MSFT")) _price = data[_symbol].Close; //Create the first order if (_limitOrderId == 0) { //Set our first order to less than MS's price: _limitOrderId = LimitOrder(_symbol, (int)(Portfolio.Cash/_price), (_price * 0.95m)); _limitOrder = (LimitOrder)Transactions.GetOrderById(_limitOrderId); Debug("Created first limit order with " + _symbol + " Price: " + _price.ToString("C") + " id: " + _limitOrderId); } if(!SMA_Slow.IsReady) return; //Update the limit price once per week: if (direction == "bullish" && Portfolio[_symbol].Quantity <= 0) { if (Portfolio[_symbol].Quantity < 0) { Order(_symbol, -Portfolio[_symbol].Quantity); } _limitOrder.Price = data[_symbol].High + .0001m; _limitOrder.Quantity = quantity; Transactions.UpdateOrder(_limitOrder); } else if (direction == "bearish" && Portfolio[_symbol].Quantity >= 0) { if (Portfolio[_symbol].Quantity > 0) { Order(_symbol, -Portfolio[_symbol].Quantity); } _limitOrder.Price = data[_symbol].Low - .0001m; _limitOrder.Quantity = -quantity; Transactions.UpdateOrder(_limitOrder); } else { Log("Current position is " + Portfolio[_symbol].Quantity + " and current order is quantity:"+ _limitOrder.Quantity + " @ price:" + _limitOrder.Price); } } /* * For our plotting we can show the limit price and MSFT to check if its hit. */ public override void OnEndOfDay() { Plot("Limit Plot", _symbol, _price); Plot("Limit Plot", "Limit", _limitOrder.LimitPrice); } } }