Back

Basic setup to implement an algorithm

I just develop a proprietary algorithm to trade forex with Oanda, but I'll need much more things to really test and develop multiple systems. I'm evaluating QuantConnect solution, however I'm confused by the size of the project. I just wanted to do something simple: read bars from oanda, enter and exit randomly as a console application. I just do not know how to do something like this.

Which classes to use?, how do I tell my algorithm I'll use a certain broker?

My algorith was implemented ad a simple timer that read rates and bars and paased it to the algorithm, but lean seems much more complex than that.

Any ideas???

Update Backtest








Please check out the docs, specially the QuantConnect University section to lean how to use our API. The learning curve is not steep at all!

If we want to tell our algorithm we will use an specific broker, we can set the brokerage model (see Reality Modelling in the docs):

// Set brokerage model using helper methods:
SetBrokerageModel(BrokerageName.FxcmBrokerage); // Defaults to margin account
SetBrokerageModel(BrokerageName.TradierBrokerage, AccountType.Cash); //Or override account type

// Supported brokerage names:
BrokerageName.FxcmBrokerage
.OandaBrokerage
.TradierBrokerage
.InteractiveBrokersBrokerage


I wrote a simple algorithm that will enter/exit a long position when a random number is above 0.9.
 

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.


Thank you Alexander,

The issue I have is moving from my own framework that is very Oanda based to Lean, I basically have to relearn. Something I really want to do is to be able to run an algorithm on schedule on the cloud, I have a paid hosting on azure. I wanted to do something like the code below running under a timer to get data:

class Program
{
#region Methods

static void Main(string[] args)
{
var algorithm = new MyAlgorithm();
algorithm.Initialize();
algorithm.OnData(new Slice(DateTime.UtcNow,
new List<TradeBar> { new TradeBar(DateTime.UtcNow, MyAlgorithm._eurusd, 1.01m, 1.01m, 1.01m, 1.01m, 1, TimeSpan.FromMinutes(1)) }));
}

#endregion
}

public class MyAlgorithm : QCAlgorithm
{
#region Static Fields

public static readonly Symbol _eurusd = QuantConnect.Symbol.Create("EURUSD", SecurityType.Forex, Market.Oanda);

#endregion

#region Fields

TradeBarConsolidator _consolidator;

private Security forexSecuriy;

#endregion

#region Public Methods and Operators

/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All
/// algorithms must initialized.
/// </summary>
public override void Initialize()
{
this.SetStartDate(2014, 01, 01); //Set Start Date
this.SetEndDate(2014, 01, 01); //Set End Date
this.SetCash(5000);

//Specify the Oanda Brokerage.
this.SetBrokerageModel(BrokerageName.OandaBrokerage);

//Add as many securities as you like. All the data will be passed into the event handler:
this.forexSecuriy = this.AddSecurity(SecurityType.Forex, _eurusd.Value, Resolution.Minute);

this.SetBrokerageModel(BrokerageName.OandaBrokerage, AccountType.Margin);

this._consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(2));
this._consolidator.DataConsolidated += this.SpyFourHours;

var easternTimeZone = DateTimeZoneProviders.Tzdb["America/New_York"];
this.SubscriptionManager.Add(_eurusd, Resolution.Minute, easternTimeZone, easternTimeZone, false, false, false);
this.SubscriptionManager.AddConsolidator(_eurusd, this._consolidator);
}

/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="data">Slice object keyed by symbol containing the stock data</param>
public override void OnData(Slice data)
{
var bar = data.Bars.ContainsKey(_eurusd) ? data.Bars[_eurusd] : null;
var holdings = this.Portfolio[_eurusd.Value].Quantity;

this.forexSecuriy.SetMarketPrice(new Tick(DateTime.UtcNow, _eurusd, 1.01m, 1.01m));
this.TradeBuilder.SetMarketPrice(_eurusd, this.forexSecuriy.Price);
if (this.Portfolio.Invested) return;

OrderTicket newOrder = this.MarketOrder(_eurusd, 1);
var stopLossTicket = this.StopMarketOrder(_eurusd, 1, 1.01m);
this.Debug("Purchased Stock");
}

public void SpyFourHours(object o, TradeBar bar)
{
this.Log(this.Time.ToString("u") + " Close Price: " + bar.Close);
}

#endregion
}

I encoutered several issues doing this, I have no idea how is that the security gets the price, or how to place a stop loss, how to place a trailing stop, how to setup Oanda credentials.

I think QuantConnect is wonderful because all the indicators, the support, the backtesting, but my need is to be able to get the core of the algorithm and be able to execute it under something similar to a console application under a single thread.

For example, in my platform I passed the transaction processor as a dependency to the algorithm, so the order execution is made by the algorith itself, but it seems that with lean I'll have to execute the orders after the algorithm has executed.  Do you think it is worth pursuing the implementation under Lean? I'll appreciate the advise.  Thank you again for your help!!

0

If you really want to pursue this route, you need to read Lean docs, learn the code and, then, make the adaptations you think fit your needs.

I would advice you to just stick with Lean and translate your strategies into this platform. I believe it would save you a lot of time and prevent headaches! If there is something that you cannot accomplish with Lean, you can add a new feature (and propose the changes through Github).

About Azure, please checkout the recent forum thread "Has someone deployed a QuantConnect strategy on Azure?".
 

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.


Hey Eduardo:

 - Stop loss - https://www.quantconnect.com/docs#Trading-and-Orders
 - There is no trailing stop sorry; you'd need to manually implement this at this time.
 - How to setup the Oanda credentials -- Lean/Launcher/config.json - all your credentials go in here.
 - LEAN runs multi threaded; and we place no restrictions on how many threads your algorithm uses. Behind the scenes we're using other threads for data and transaction processing.
 - Market orders can be filled synchronously or async; but async programming is a reality of trading. We handle most of the work behind the scenes for you and you can use order events to make it easier.

 

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.


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