Overall Statistics |
Total Trades 3 Average Win 0% Average Loss -0.19% Compounding Annual Return -5.017% Drawdown 16.400% Expectancy -1 Net Profit -0.155% Sharpe Ratio 0.314 Loss Rate 100% Win Rate 0% Profit-Loss Ratio 0 Alpha -2.593 Beta 226.128 Annual Standard Deviation 0.776 Annual Variance 0.603 Information Ratio 0.298 Tracking Error 0.776 Treynor Ratio 0.001 Total Fees $0.00 |
using System; using System.Collections.Generic; using NodaTime; using QuantConnect.Data; using QuantConnect.Data.Custom; namespace QuantConnect.Algorithm.CSharp { /// <summary> /// Daily Fx demonstration to call on and use the FXCM Calendar API /// </summary> public class DailyFxAlgorithm : QCAlgorithm { /// <summary> /// Add the Daily FX type to our algorithm and use its events. /// </summary> private string symbol = "EURUSD"; private OrderTicket buyOrder; private OrderTicket sellOrder; decimal stop = 0.0005m; decimal limit = 0.0002m; int buyQty = 10000; int sellQty = -10000; decimal price; decimal limitPrice; decimal stopPrice; decimal trailPercent; decimal stopLimitBuy; decimal stopLossBuy; decimal stopLimitSell; decimal stopLossSell; // int buyOrder; // int sellOrder; public override void Initialize() { SetTimeZone(TimeZones.Utc); SetStartDate(2018, 6, 25); //Set Start Date SetEndDate(2018, 07, 05); //Set End Date SetCash(1000); //Set Strategy Cash AddData<DailyFx>("DFX", Resolution.Tick, DateTimeZone.Utc); AddForex(symbol, Resolution.Tick, Market.Oanda); SetBrokerageModel(BrokerageName.OandaBrokerage); // Schedule.On(DateRules.EveryDay(), TimeRules.Every(TimeSpan.FromMinutes(60)), () => // { // Transactions.CancelOpenOrders(symbol); // }); // Schedule.On(DateRules.EveryDay(), TimeRules.Every(TimeSpan.FromMinutes(60)), () => // { // Liquidate(symbol); // }); } public override void OnData(Slice QuoteBars) { // } /// <summary> /// Trigger an event on a complete calendar event which has an actual value. /// </summary> public void OnData(DailyFx calendar) { price = Securities[symbol].Close; // Trigger for order if(calendar.Importance != FxDailyImportance.High) // return; { if(!Portfolio.Invested) { if(calendar.Meaning == FxDailyMeaning.Better) { OrderTicket buyOrder = LimitOrder(symbol, buyQty, price + limit); stopLossBuy = price - stop; Log("BUY: Price: "+price+", Limit: "+(price + limit)+", Ticket: "+buyOrder+"Stoploss 1: "+stopLossBuy); // Transactions.CancelOpenOrders(symbol); //Log("BUY: StopLimit: "+stopLimitBuy+"StopLoss: "+stopLossBuy); } else if(calendar.Meaning == FxDailyMeaning.Worse) { OrderTicket sellOrder = LimitOrder(symbol, sellQty, price - limit); stopLossSell = price + stop; Log("SELL: Price: "+price+", Limit: "+(price - limit)+", Ticket: "+buyOrder+"Stoploss: "+stopLossSell); // Transactions.CancelOpenOrders(symbol); } } } // var newBuyStop = buyOrder.Get(OrderField.StopPrice) - stop; stopLossBuy = Math.Max(stopLossBuy, price - stop); Log("Stop;oss 2: "+stopLossBuy); stopLossBuy = stopLossBuy; Log("Stoploss 3: "+stopLossBuy); // var newSellStop = buyOrder.Get(OrderField.StopPrice) - stop; stopLossSell = Math.Min(stopLossSell, price + stop); stopLossSell = stopLossSell; if(Portfolio.Invested) { Transactions.CancelOpenOrders(symbol); // Update StopLossSell & stopLossBuy // https://www.quantconnect.com/forum/discussion/1072/setting-a-stop-loss-and-take-profit if(Portfolio[symbol].Quantity > 0) { if(price < stopLossBuy) { Liquidate(symbol); Log("BUY Close: "+price); } } // if(Portfolio[symbol].Quantity < 0) // { // if(price > stopLossSell) // { // Liquidate(symbol); // Log("SELL Close: "+price); // } // } } } } }