Hi everyone,
I'm trying to make an algorithm for crossing EMA on BTCUSDT. Use a window to check if just crossed but I'm not able to use timeconsolidator. Below the 5 minute consolidator is not working, and I'm asking also what's difference between data[symbol].Close and Securities[Symbol].close
#region imports
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Drawing;
using QuantConnect;
using QuantConnect.Algorithm.Framework;
using QuantConnect.Algorithm.Framework.Selection;
using QuantConnect.Algorithm.Framework.Alphas;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Algorithm.Framework.Execution;
using QuantConnect.Algorithm.Framework.Risk;
using QuantConnect.Parameters;
using QuantConnect.Benchmarks;
using QuantConnect.Brokerages;
using QuantConnect.Util;
using QuantConnect.Interfaces;
using QuantConnect.Algorithm;
using QuantConnect.Indicators;
using QuantConnect.Data;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Custom;
using QuantConnect.DataSource;
using QuantConnect.Data.Fundamental;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Notifications;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Orders.Fills;
using QuantConnect.Orders.Slippage;
using QuantConnect.Scheduling;
using QuantConnect.Securities;
using QuantConnect.Securities.Equity;
using QuantConnect.Securities.Future;
using QuantConnect.Securities.Option;
using QuantConnect.Securities.Forex;
using QuantConnect.Securities.Crypto;
using QuantConnect.Securities.Interfaces;
using QuantConnect.Storage;
using QuantConnect.Data.Custom.AlphaStreams;
using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm;
using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm;
#endregion
namespace QuantConnect.Algorithm.CSharp
{
public class SmoothGreenWolf : QCAlgorithm
{
private ExponentialMovingAverage _fast;
private ExponentialMovingAverage _slow;
private Symbol symbol;
public RollingWindow<IndicatorDataPoint> _fast_window;
public RollingWindow<IndicatorDataPoint> _slow_window;
public override void Initialize()
{
SetTimeZone(TimeZones.Paris);
SetStartDate(2022, 1, 2); // Set Start Date
SetEndDate(2022, 12, 31); // Set End Date
SetCash("USDT", 100000);
SetBrokerageModel(BrokerageName.Binance, AccountType.Cash);
DefaultOrderProperties = new BinanceOrderProperties
{
TimeInForce = TimeInForce.GoodTilCanceled,
PostOnly = false
};
symbol = AddCrypto("BTCUSDT").Symbol;
Securities[symbol].FeeModel = new ConstantFeeModel(0);
// create two moving averages
_fast = EMA(symbol, 30);
_slow = EMA(symbol, 60);
_fast_window = new RollingWindow<IndicatorDataPoint>(2);
// Here is where the RollingWindow is updated with the latest SMA observation.
_fast.Updated += (object sender, IndicatorDataPoint updated) =>
{
_fast_window.Add(updated);
};
_slow_window = new RollingWindow<IndicatorDataPoint>(2);
_slow.Updated += (object sender, IndicatorDataPoint updated) =>
{
_slow_window.Add(updated);
};
// Not Working
var fiveMinuteConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(5));
SubscriptionManager.AddConsolidator(symbol, fiveMinuteConsolidator);
RegisterIndicator(symbol, _fast, fiveMinuteConsolidator);
RegisterIndicator(symbol, _slow, fiveMinuteConsolidator);
}
public override void OnData(Slice data)
{
if (_fast_window.IsReady && _slow_window.IsReady) {
if (_fast_window[0] > _slow_window[0])
{
if (!Portfolio.Invested)
{
// Or Securities["SPY"].Close;
MarketOrder(symbol, Portfolio.CashBook["USDT"].Amount/Math.Round(data[symbol].Close));
}
}
else
{
if (Portfolio.Invested)
{
MarketOrder(symbol, -Portfolio.CashBook["BTC"].Amount);
}
}
}
}
}
}
Non Compete
Hi @de-fi not sure if this would be helpful but check out my response here:
https://www.quantconnect.com/forum/discussion/5047/registerindicator-returns-error-quot-quot-this-is-a-forward-only-indicator-quot-when-following-example-in-docs/p1/comment-43403
Louis Szeto
Hi De Fi
EMA is a helper method to create an automatic indicator, so your current implementation will double update the EMA indicator by (1) default minute resolution bar for crypto data subscription, and (2) 5-minute bar by the RegisterIndicator method. Instead, you should use a manual updating version – ExponentialMovingAverage(30), then registor to update it by 5-minute bar:
As per the second query, data[symbol].Close is the Close price of symbol in the received slice of OnData handler, while Securities[Symbol].Close is the cache value of that. Note that not all updatest OnData data slice contains data for the referenced symbol, but Securities[Symbol].Close will be available if the first close price has been recorded although not gauranteed to be updatest.
Best
Louis
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.
De Fi
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!