Back

Error Help.

Hi, I've been trying to post my entire code in a backtest b/c I could use some assistance but I can't even get it to build yet.  Could someone please assist me with errors I am getting with this line of code preventing me from building my algo.  Thanks!

if(!Portfolio["EURUSD"].Invested and currentPrice > smaDaily and totalTime == 0)

// Errors I'm getting on this line: ) expected
// Embedded statement cannot be a declaration or labeled statement
// ; expected
// Invalid expression term '>'
Update Backtest








If you are writing in C# logical "and" in your if statement is & or &&, not the word "and".

& allows short circuit evaluation. Most of the time people mean &&. If you are unsure, most of the time use &&.

0

Ahhh, thank you, silly mistake.  That will help me focus on the bigger problems with my code. Thanks for taking the time to look!

0

No problem. I need to correct myself though so wrong information is not propagated.

&& is short-circui evaluation

& evaluates both sides

opposite of my first post.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-and-operator

0

You can best download Visual Studio and then code there and then copy-paste back as you need data. This is a bit tedious, but it will speed up with avoiding typo's. Microsoft's intellisense will also suggest how to fix your code at some points.

0

Thank you for the responses!! I have been struggling for days on this.  I was hoping to use it to learn.  Since I can't get it even ready to backtest could someone copy and past the code I have attached here into their editor and give me some pointers.  The logic was suppose to be: If uninvested, buy if above SMA, sell if below SMA.  Set a profit target, if it hits re-enter trade according to SMA position.  When trade initiated it starts a timer, which is main variable, number of days, in this case 10.  If profit target hasn't been reached in 10 days, trade is closed, all profit orders cancelled.  Then it re-opens base on SMA again and gets a new 10 days.  etc.

namespace QuantConnect
{
public class TimeBasedAlgo : QCAlgorithm
{
public override void Initialize()
{
SetStartDate(2017, 1, 1);
SetEndDate(2018, 1, 1);
SetCash(5000);
SetBenchmark("SPY");
SetBrokerageModel(BrokerageName.OandaBrokerage);
AddSecurity(SecurityType.Forex, "EURUSD", Resolution.Tick);
SetWarmUp(TimeSpan.FromDays(7));
var smaDaily = SMA("EURUSD", 24, Resolution.Hour);
var dayCount = TimeSpan.FromDays(10);
var tradeTime = 0;
var totalTime = 0;
var currentPrice = 0;
var longTrade = MarketOrder("EURUSD", 10000);
var shortTrade = MarketOrder("EURUSD", -10000);
var longProfitTarget = LimitOrder("EURUSD",20, 20);
var ProfitTarget = LimitOrder("EURUSD",-20, 20);
}
public override void OnData(Slice data)
{
if(!Portfolio["EURUSD"].Invested & currentPrice > smaDaily & totalTime == 0)
{
longTrade;
longProfitTarget;
totalTime = tradeTime + dayCount;
Log("Purchased EURUSD on " + Time.ToShortDateString());
}
if (!Portfolio["EURUSD"].Invested && currentPrice < smaDaily && totalTime == 0)
{
shorTrade;
shortProfitTarget; //
totalTime = tradeTime + dayCount;
Log("Sold EURUSD on " + Time.ToShortDateString());
}
if (Portfolio["EURUSD"].IsLong && totalTime > 10 )
{
shortTrade;
List<OrderTicket> cancelledOrders = Transactions.CancelOpenOrders("EURUSD"); // I think
totalTime = 0;
Log("Closed Long Trade " + Time.ToShortDateString() + "and cancelled all orders");
}
if (Portfolio["EURUSD"].IsShort && totalTime > 10 )
{
longTrade;
List<OrderTicket> cancelledOrders = Transactions.CancelOpenOrders("EURUSD");
totalTime = 0;
Log("Closed Short Trade " + Time.ToShortDateString()) + "and cancelled all orders";
}

}
}
}
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