| Overall Statistics |
|
Total Trades 5 Average Win 0% Average Loss -1.54% Compounding Annual Return -39.487% Drawdown 3.300% Expectancy -1 Net Profit -2.877% Sharpe Ratio -4.207 Loss Rate 100% Win Rate 0% Profit-Loss Ratio 0 Alpha -0.317 Beta -0.022 Annual Standard Deviation 0.079 Annual Variance 0.006 Information Ratio -7.098 Tracking Error 0.137 Treynor Ratio 15.077 Total Fees $10.00 |
namespace QuantConnect
{
public class BasicTemplateAlgorithm : QCAlgorithm
{
//parameters go here
const decimal StopLossPercent = 0.02m;
const decimal TakeProfitPercent = 0.04m;
private OrderTicket CurrentOrder;
private OrderTicket StopLoss;
private OrderTicket ProfitTarget;
int quantity = 10000;
decimal price = 0;
decimal tolerance = 0m; //0.1% safety margin in prices to avoid bouncing.
DateTime sampledToday = DateTime.Now;
SimpleMovingAverage smaShort;
SimpleMovingAverage smaLong;
String symbol = "USDCAD" ;
private Identity yesterdayClose;
int holdings = 0;
//int leverage = 50; Enable for borrowing abilities (max is 50x for fx, 2x? for equities)
public override void Initialize()
{
//AddSecurity(SecurityType.Equity, Symbol, Resolution.Minute); Uncomment this for stocks
AddSecurity(SecurityType.Forex, symbol, Resolution.Minute);
//Set backtest dates here
SetStartDate(2016, 3, 1);
SetEndDate(2016, 3, 18);
//Set Backtest cash amount here
SetCash(100000);
//MA's go here
smaShort = SMA(symbol, 5, Resolution.Minute);
smaLong = SMA(symbol, 25, Resolution.Minute);
// this will get us an indicator that represents our symbol's closing price
yesterdayClose = Identity(symbol, Resolution.Minute, Field.Close);
}
public void OnData(TradeBars data)
{
// set price based on previous minute bar
price = data[symbol].Close;
//Algo buying logic below
if (!Portfolio.Invested && smaShort > smaLong && price > smaShort)
{
// Calculate quantity based on available cash
var quantity = (int) (Portfolio.Cash / price);
// Buy Stock
CurrentOrder = Order(symbol, quantity);
// Set StopLoss order
StopLoss = StopMarketOrder(symbol, -quantity, price * (1m - StopLossPercent));
// Set Profit Target
ProfitTarget = LimitOrder(symbol, -quantity, price * (1m + TakeProfitPercent));
}
}
// If the StopLoss or ProfitTarget is filled, cancel the other
// If you don't do this, then the ProfitTarget or StopLoss order will remain outstanding
// indefinitely, which will cause very bad behaviors in your algorithm
public override void OnOrderEvent(OrderEvent orderEvent)
{
// Ignore OrderEvents that are not closed
if (!orderEvent.Status.IsClosed())
{
return;
}
// Defensive check
if (ProfitTarget == null || StopLoss == null)
{
return;
}
var filledOrderId = orderEvent.OrderId;
// If the ProfitTarget order was filled, close the StopLoss order
if (ProfitTarget.OrderId == filledOrderId)
{
StopLoss.Cancel();
}
// If the StopLoss order was filled, close the ProfitTarget
if (StopLoss.OrderId == filledOrderId)
{
ProfitTarget.Cancel();
}
}
}
}