| Overall Statistics |
|
Total Trades 1 Average Win 0.00% Average Loss -1.23% Annual Return -0.596% Drawdown 6.500% Expectancy -1.000 Net Profit -1.229% Sharpe Ratio -0.1 Loss Rate 100% Win Rate 0% Profit-Loss Ratio 0.00 Trade Frequency Weekly trades |
using System;
using System.Collections;
using System.Collections.Generic;
namespace QuantConnect
{
using QuantConnect.Securities;
using QuantConnect.Models;
public partial class BasicTemplateAlgorithm : QCAlgorithm, IAlgorithm
{
string symbol = "IBM";
public override void Initialize()
{
SetStartDate(2012, 06, 01);
SetEndDate(DateTime.Now.Date.AddDays(-1));
SetCash(30000);
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
SetRunMode(RunMode.Series);
//SET YOUR OWN CUSTOM TRANSACTION MODEL
Securities[symbol].Model = new CustomTransactionModel(this);
}
public override void OnTradeBar(Dictionary<string, TradeBar> data)
{
if (Portfolio.HoldStock == false)
{
Order(symbol, 50);
Debug("Sent order for " + symbol);
}
}
}
}using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace QuantConnect {
using QuantConnect.Models;
using QuantConnect.Securities;
//CUSTOM USER TRANSACTION MODEL EXAMPLE
public class CustomTransactionModel : ISecurityTransactionModel {
QCAlgorithm algo;
public CustomTransactionModel(QCAlgorithm algoNamespace) {
//Setup your custom model.
algo = algoNamespace;
}
public virtual void Fill(Security vehicle, ref Order order) {
try {
switch (order.Type) {
case OrderType.Limit:
LimitFill(vehicle, ref order);
break;
case OrderType.Stop:
StopFill(vehicle, ref order);
break;
case OrderType.Market:
MarketFill(vehicle, ref order);
break;
}
} catch (Exception err) {
algo.Error("CustomTransactionModel.TransOrderDirection.Fill(): " + err.Message);
}
}
public virtual decimal GetSlippageApproximation(Security security, Order order)
{
//OVERRIDE SLIPPAGE SET TO 0
decimal slippage = 0;
return slippage;
}
public virtual void MarketFill(Security security, ref Order order) {
try {
//Calculate the model slippage: e.g. 0.01c
decimal slip = GetSlippageApproximation(security, order);
switch (order.Direction)
{
case OrderDirection.Buy:
order.Price = security.Price;
order.Price += slip;
break;
case OrderDirection.Sell:
order.Price = security.Price;
order.Price -= slip;
break;
}
//Market orders fill instantly.
order.Status = OrderStatus.Filled;
} catch (Exception err) {
algo.Error("CustomTransactionModel.TransOrderDirection.MarketFill(): " + err.Message);
}
}
public virtual void StopFill(Security security, ref Order order) {
try {
//If its cancelled don't need anymore checks:
if (order.Status == OrderStatus.Canceled) return;
//Check if the Stop Order was filled: opposite to a limit order
if (order.Direction == OrderDirection.Sell) {
//-> 1.1 Sell Stop: If Price below setpoint, Sell:
if (security.Price < order.Price) {
order.Status = OrderStatus.Filled;
}
} else if (order.Direction == OrderDirection.Buy) {
//-> 1.2 Buy Stop: If Price Above Setpoint, Buy:
if (security.Price > order.Price) {
order.Status = OrderStatus.Filled;
}
}
} catch (Exception err) {
algo.Error("CustomTransactionModel.TransOrderDirection.StopFill(): " + err.Message);
}
}
public virtual void LimitFill(Security security, ref Order order) {
//Initialise;
decimal marketDataMinPrice = 0;
decimal marketDataMaxPrice = 0;
try {
//If its cancelled don't need anymore checks:
if (order.Status == OrderStatus.Canceled) return;
//Depending on the resolution, return different data types:
MarketData marketData = security.GetLastData();
if (marketData.Type == MarketDataType.TradeBar) {
marketDataMinPrice = ((TradeBar)marketData).Low;
marketDataMaxPrice = ((TradeBar)marketData).High;
} else {
marketDataMinPrice = marketData.Price;
marketDataMaxPrice = marketData.Price;
}
//-> Valid Live/Model Order:
if (order.Direction == OrderDirection.Buy) {
//Buy limit seeks lowest price
if (marketDataMinPrice < order.Price) {
order.Status = OrderStatus.Filled;
}
} else if (order.Direction == OrderDirection.Sell) {
//Sell limit seeks highest price possible
if (marketDataMaxPrice > order.Price) {
order.Status = OrderStatus.Filled;
}
}
} catch (Exception err) {
algo.Error("CustomTransactionModel.TransOrderDirection.LimitFill(): " + err.Message);
}
}
public virtual decimal GetOrderFee(decimal quantity, decimal price)
{
//Modelled order fee to
return 0;
}
} // End Algorithm Transaction Filling Classes
} // End QC Namespace