| Overall Statistics |
|
Total Trades 6 Average Win 0% Average Loss -0.01% Compounding Annual Return -0.126% Drawdown 0.000% Expectancy -1 Net Profit -0.019% Sharpe Ratio -3.079 Loss Rate 100% Win Rate 0% Profit-Loss Ratio 0 Alpha -0.001 Beta 0 Annual Standard Deviation 0 Annual Variance 0 Information Ratio -3.679 Tracking Error 0.163 Treynor Ratio -5.381 Total Fees $6.00 |
using System.Threading;
using System.Threading.Tasks;
namespace QuantConnect
{
public partial class QCUMartingalePositionSizing : QCAlgorithm
{
int iPeriod = 15;
decimal iTP = 10.0m;
decimal iSL = 5.0m;
decimal iLeverage = 4m;
decimal iVolume = 1m;
string iSymbol = "MSFT";
RelativeStrengthIndex iRsi = null;
Dictionary<int, OrderTicket> iTickets = new Dictionary<int, OrderTicket>();
Dictionary<int, OrderTicket> iLimits = new Dictionary<int, OrderTicket>();
Dictionary<int, OrderTicket> iStops = new Dictionary<int, OrderTicket>();
public override void Initialize()
{
var resolution = Resolution.Daily;
SetCash(25000);
SetBenchmark(iSymbol);
SetStartDate(2017, 10, 1);
SetEndDate(DateTime.Now.Date);
SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage);
AddSecurity(SecurityType.Equity, iSymbol, resolution, true, iLeverage, false);
iRsi = RSI(iSymbol, iPeriod, MovingAverageType.Simple, resolution);
}
public void OnData(TradeBars data)
{
if (CanOpen() == 1)
{
GoLong(iSymbol, iVolume);
return;
}
if (CanClose() == 1)
{
Liquidate();
return;
}
}
public override void OnOrderEvent(OrderEvent orderEvent)
{
}
protected OrderTicket GoLong(string symbol, decimal size)
{
var ticket = MarketOrder(symbol, size, false);
iTickets[ticket.OrderId] = ticket;
var process = new Thread(() => {
Transactions.WaitForOrder(ticket.OrderId);
if (Transactions.GetOrderById(ticket.OrderId).Status != OrderStatus.Filled)
{
return;
}
iLimits.Select(o => Transactions.CancelOrder(o.Key));
iStops.Select(o => Transactions.CancelOrder(o.Key));
iLimits.Clear();
iStops.Clear();
var volume = Portfolio[symbol].Quantity;
var price = Securities[symbol].Price;
decimal priceDn = (decimal) (price - iSL);
decimal priceUp = (decimal) (price + iTP);
Log("### Price : " + price + " vs SL : " + (priceDn) + " vs TP : " + (priceUp));
var orderSL = StopMarketOrder(symbol, volume, priceDn, "SL #" + ticket.OrderId.ToString());
var orderTP = StopMarketOrder(symbol, -volume, priceUp, "TP #" + ticket.OrderId.ToString());
iStops[orderSL.OrderId] = orderSL;
iLimits[orderTP.OrderId] = orderTP;
});
process.Start();
return ticket;
}
protected int CanOpen()
{
if (iRsi.IsReady)
{
if (iRsi < 70 && iRsi > 50)
{
return -1;
}
if (iRsi > 30 && iRsi < 50)
{
return 1;
}
}
return 0;
}
protected int CanClose()
{
return 0;
}
}
}