x: ..."> x: ..."> Executing trades in a certain timeframe by Edvinas Jablonskis - QuantConnect.com
Back

Executing trades in a certain timeframe

self.AddForex("EURUSD", Resolution.Daily)

slices = self.History(["EURUSD"], 1000, Resolution.Daily)

price = self.Securities["EURUSD"].Price


if price > x:
self.SetHoldings("EURUSD",5)

elif price < x:
self.Liquidate("EURUSD")

Hello folks, Have a question on how some of these commands work. the set holdings loop checks the criteria for a buy every day right? please correct me if im wrong. I intend the liquidate line to remove any buys,

There is also a problem. After the first 10 or so trades the code starts to execute uneven quantities of buys. I want it to be consistently 5 as i wrote above. Also short trades are showing up in the backtest, which to me makes no sense. 

Update Backtest








Can you share your complete code? I do not understand what you mean. If you have minute resolution and request a marketorder buy, the order is sent to the market the next minutes.

If I look at the source code of the unit tests, it seems that setholdings takes also the commision (fee) and margin into account:

/// <summary>
/// Calculate the order quantity to achieve target-percent holdings.
/// </summary>
/// <param name="symbol">Security object we're asking for</param>
/// <param name="target">Target percentag holdings, this is an unlevered value, so
/// if you have 2x leverage and request 100% holdings, it will utilize half of the
/// available margin</param>
/// <returns>Order quantity to achieve this percentage</returns>
public decimal CalculateOrderQuantity(Symbol symbol, decimal target)
{
var security = Securities[symbol];
var price = security.Price;

// can't order it if we don't have data
if (price == 0) return 0;

// if targeting zero, simply return the negative of the quantity
if (target == 0) return -security.Holdings.Quantity;

// this is the value in dollars that we want our holdings to have
var targetPortfolioValue = target * Portfolio.TotalPortfolioValue;
var currentHoldingsValue = security.Holdings.HoldingsValue;

// remove directionality, we'll work in the land of absolutes
var targetOrderValue = Math.Abs(targetPortfolioValue - currentHoldingsValue);
var direction = targetPortfolioValue > currentHoldingsValue ? OrderDirection.Buy : OrderDirection.Sell;

// determine the unit price in terms of the account currency
var unitPrice = new MarketOrder(symbol, 1, UtcTime).GetValue(security);
if (unitPrice == 0) return 0;

// calculate the total margin available
var marginRemaining = Portfolio.GetMarginRemaining(symbol, direction);
if (marginRemaining <= 0) return 0;

// continue iterating while we do not have enough margin for the order
decimal marginRequired;
decimal orderValue;
decimal orderFees;
var feeToPriceRatio = 0m;

// compute the initial order quantity
var orderQuantity = targetOrderValue / unitPrice;

// rounding off Order Quantity to the nearest multiple of Lot Size
orderQuantity -= orderQuantity % security.SymbolProperties.LotSize;

do
{
// reduce order quantity by feeToPriceRatio, since it is faster than by lot size
// if it becomes nonpositive, return zero
orderQuantity -= feeToPriceRatio;
if (orderQuantity <= 0) return 0;

// generate the order
var order = new MarketOrder(security.Symbol, orderQuantity, UtcTime);
orderValue = order.GetValue(security);
orderFees = security.FeeModel.GetOrderFee(security, order);

// find an incremental delta value for the next iteration step
feeToPriceRatio = orderFees / unitPrice;
feeToPriceRatio -= feeToPriceRatio % security.SymbolProperties.LotSize;
if (feeToPriceRatio < security.SymbolProperties.LotSize)
{
feeToPriceRatio = security.SymbolProperties.LotSize;
}

// calculate the margin required for the order
marginRequired = security.MarginModel.GetInitialMarginRequiredForOrder(security, order);

} while (marginRequired > marginRemaining || orderValue + orderFees > targetOrderValue);

// add directionality back in
return (direction == OrderDirection.Sell ? -1 : 1) * orderQuantity;
}
0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed