Back

ERROR/Need help

Hi,

first time doing this, do anyone se what i'm doing wrong here?

/Hampus

Update Backtest








Here is the code:

namespace QuantConnect.Algorithm.CSharp {
public class BasicTemplateAlgorithm : QCAlgorithm {

String symbol = "SPY";

decimal price;
decimal stopLossPercent = 0.0002m;
decimal takeProfitPercent = 0.0002m;

private OrderTicket stopLoss;
private OrderTicket takeProfit;

int size = 10000;

RelativeStrengthIndex rsi;
ExponentialMovingAverage ema100;
ExponentialMovingAverage ema200;
AverageDirectionalIndex adx;


public override void Initialize() {

AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);

rsi = RSI(symbol, 14, MovingAverageType.Simple, Resolution.Minute);
ema100 = EMA(symbol, 100, Resolution.Minute);
ema200 = EMA(symbol, 200, Resolution.Minute);
adx = ADX(symbol, 14, Resolution.Hour);

SetStartDate(2017, 10, 01);
SetEndDate(2017, 12, 01);
SetCash(100000);
}

public override void OnData(Slice data) {

price = data[symbol].close();

if (!Portfolio.Invested && rsi > 70 && ema100 > ema200 && adx > 30){
longOrder(price);
}
if(!Portfolio.Invested && rsi < 30 && ema200 > ema100 && adx > 30){
shortOrder(price);
}

}

public override void OnOrderEvent(OrderEvent orderEvent){

var filledOrderId = orderEvent.OrderId;

if (takeProfit != null && stopLoss != null){
if (takeProfit.OrderId == filledOrderId){
Log(Time+": Cancelling stop loss");
stopLoss.Cancel();
} if (stopLoss.OrderId == filledOrderId){
Log(Time+": Cancelling proffit target");
takeProfit.Cancel();
}
}
}

private void longOrder(decimal currentPrice) {
Order(symbol, size);
takeProfit = LimitOrder(symbol, -size, currentPrice*(1m + takeProfitPercent));
stopLoss = StopMarketOrder(symbol, -size, currentPrice*(1m - stopLossPercent));
}

private void shortOrder(decimal currentPrice) {
Order(symbol, -size);
takeProfit = LimitOrder(symbol, -size, currentPrice*(1m - takeProfitPercent));
stopLoss = StopMarketOrder(symbol, -size, currentPrice*(1m - stopLossPercent));
}
}
}
0

Here you are. Your mistakes.

1. Look at the console logs at the bottom

2. C# is a case-sensitive language, data[symbol].Close - it's not a method and it starts with capital "C"

3. "size" = 10000 is too big, your broker doesn't allow to trade that much, decreased to 100

4. when you add StopLoss, always use StopMarketOrder, for Long it should be below the price, for Short - above the price

5. when you add TakeProfit order, always use LimitOrder, for Long it should be above the price, for Short - below the price

Result: ~1%, this strategy will not make you rich, and on a bearish market, e.g. 2008-2009 it will take you down :)

1


Verry new to this so just testing around, maybe next one will make me rich ;)

Thank you so much for the help!

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