Hey everyone,
This is my first trading strategy I've programmed - it's a code file of the 50% retracement entry.
Every 24 hours, it's supposed to identify a draw between a low and high, and buy or sell depending on the direction of a draw if price retraces to the 50% level. In this file, the 50% level is front-run by 5 % of the draw, and the stop is placed at the 45% level.
It doesn't work, unfortunately - I'm getting the error "could not create instance of QC Algorithm Class"
I'm new to C# - can someone more experienced in programming tell me what's going on? And if you could examine the actual strategy and critique my code, that would be great for me to learn how to do this.
namespace QuantConnect
{
/*
* QuantConnect University: Full Basic Template:
*
* The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
* We have explained some of these here, but the full algorithm can be found at:
* https://github.com/QuantConnect/Lean/tree/master/Algorithm
*/
public class Fifty_Retracement : QCAlgorithm
{
public decimal draw_low = 0;
public decimal draw_high = 0;
string symbol = "SPY";
public decimal _last = 0;
public decimal high = 0;
public decimal low = 0;
public decimal[] Arrayhigh = new decimal[3];
public decimal[] Arraylow = new decimal[3];
public DateTime curr_time = DateTime.Now;
public DateTime draw_date = new DateTime(0,0,0);
public bool draw_completed = true;
public bool long_draw = false;
public bool in_trade = false;
public decimal draw_start = 0;
public decimal draw_end = 0;
public bool draw_reset = false;
public bool retracement_hit = false;
public string draw_type = "";
public bool no_draw = true;
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
//Start and End Date range for the backtest:
SetStartDate(2016, 1, 1);
SetEndDate(DateTime.Now.Date.AddDays(-1));
//Cash allocation
SetCash(5000);
//Add as many securities as you like. All the data will be passed into the event handler:
AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);
}
private void RotatePriceArray()
{
for (int i = 0; i < 2; i++)
{
Arraylow[i] = Arraylow[i + 1];
Arrayhigh[i] = Arrayhigh[i + 1];
}
Arraylow[2] = low;
Arrayhigh[2] = high;
}
//public TimeRules draw_time = new Time(0,0,0);
//Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
public void OnData(TradeBars data)
{
//Schedule.On(DateRules.EveryDay(symbol), TimeRules.Every(TimeSpan.FromMinutes(1)), () =>
//{
high = data[symbol].High;
_last = data[symbol].Close;
low = data[symbol].Low;
curr_time = data[symbol].Time;
}
//RotatePriceArray();
//make an if statement for if it's all 0s (beginning of the day)
public bool newhigh ()
{
if (Arrayhigh[1] > Arrayhigh[2] && Arrayhigh[1] > Arrayhigh[0])
{
return true;
}
else
{
return false;
}
}
public bool newlow ()
{
if (Arraylow[1] < Arraylow[2] && Arraylow[1] < Arraylow[0])
{
return true;
}
else
{
return false;
}
}
//Need to put the below in a class
public void first_function()
{
if (no_draw)
{
if (newhigh ())
{
draw_start = Arrayhigh[1];
draw_begin();
draw_date = DateTime.Now;
draw_type = "short";
no_draw = false;
}
//This doesn't work -> can't make draw_start both low and high
//we need to separate low and high draws and see which gets taken out first
if (newlow ())
{
draw_start = Arraylow[1];
draw_begin();
draw_date = DateTime.Now;
draw_type = "long";
no_draw = false;
}
}
}
//list of states: no_trade, draw_begin, draw_complete, search for retracement, enter_trade
public void draw_begin()
{
if (draw_type == "long")
{
//new anchor case
if (low < draw_start)
{
draw_reset = true;
draw_start = low;
//reset draw time and date
}
if (newhigh ())
{
draw_end = high;
}
}
if (draw_type == "short")
{
if (high > draw_start)
{
draw_reset = true;
draw_start = high;
}
//reset draw time and date
if (newlow ())
{
draw_end = low;
}
}
//24 hours after draw start, start looking for retracement
/*Schedule.On(DateRules.On(draw_date + 1),TimeRules.At(Time.draw_time))
{
retracement();
} */
}//end of draw_begin
public void retracement()
{
//SANH/SANL cases
if (draw_type == "long")
{
if (high > draw_end)
{
draw_end = high;
}
}
if (draw_type == "short")
{
if (low < draw_end)
{
draw_end = low;
}
}
//Trade entry
if (_last <= (draw_start + draw_end)*(Convert.ToDecimal(0.55)) || _last >= (draw_start + draw_end)*(Convert.ToDecimal(0.45)))
{
retracement_hit = true;
if (long_draw)
{
in_trade = true;
Order(symbol, 1);
}
if (!long_draw)
{
in_trade = true;
Order(symbol, -1);
}
}
//Trade exit
if (in_trade)
{
if (draw_type == "long")
{
//Stop Loss
if (_last <= (draw_high + draw_low)*(Convert.ToDecimal(0.45)))
{
Order(symbol, -1);
}
//Exit
if (_last >= draw_high)
{
Order(symbol, -1);
}
}
if (draw_type == "short")
{
//Stop Loss
if (_last >= (draw_high + draw_low)*(Convert.ToDecimal(0.55)))
{
Order(symbol, 1);
}
//Exit
if (_last <= draw_high)
{
Order(symbol, 1);
}
}
}//if(in_trade)
}//end bracket for retracement()
//});
}
}
Alexandre Catarino
We cannot set a DateTime object with Year, Month and Day being zero.
Please make the following change:
// Bug public DateTime draw_date = new DateTime(0,0,0); // Fix public DateTime draw_date = new DateTime();
Support and Resist
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!