Back

Noise in the Market

Hi,  I have been struggling to learn to code but love QuantConnect with the good documentation, great commnunity, and education.  I have an idea for a strategy that I think would be interesting to explore.  If you open a 4 hour chart on any highly traded security and zoom it out dramatically and extend/condense the bars to many months/years back you will see larger trends emerge.  What you will notice is there is a lot of "noise" in the market with reversals(mean reversion) happening more than trends.

What I think to myself when I see a chart in this way is that at some point you could have placed a trade in that noise and it would have been profitable at some point.  Time is the key issue because if I were manually watching the trade emotions and draw down would affect me and cause me to exit the trade and that's where the noise comes in.  Thinking about this premise I would like to code a simple algo to test it.  

We would use one indicator, a 30 SMA on a 4 hour chart.  I will use Forex for the example, say EURUSD.  Turn the algo on, if the current price is above the 30 SMA, just Buy, if below it Initiates a Sell order.  Then we give it time.......  The rule would be that the trade has 10 days to become profitable, if at 10 days it hasn't hit the profit target the trade is closed and a new order is opened in the direction of the SMA rule above, then a new 10 days is given.  The profit target could be 20 pips, and say it hits the profit target it would just initiate a new order in the direction based on the SMA above.  The variables that could be tinkered with would be the profit target and the number of days to let the trade run. Drawdown could get awful but could Time win? I would just be interested to see how many profitable trades you get just by placing them and letting Time run its course?  Maybe this has been tried before... let me know your thoughts.  Thanks!

Update Backtest








check this out....its a simple moving average strategy.....

you can fire an event before market close every day and count days if its profitable.

al least something to start with:

https://www.quantconnect.com/forum/discussion/3189/a-simple-tactical-etf-strategy
0

Thanks for the info Michael!  After doing some reading I have tried my very first attempt at coding.  I am having a hard time figuring out how to correctly declare my variables and much more, but I think I have the logic correct down in the OnSlice Data section.  If someone could please look at the code and give me guidance I would greatly appreciate it.  The strategy I'm trying to accomplish is noted above.  Thanks for any help in advance.

namespace QuantConnect
{

public class TimeBasedAlgo : QCAlgorithm
{

public override void Initialize()
{

SetStartDate(2017, 1, 1);
SetEndDate(2018, 1, 1);
SetCash(5000);

SetBrokerageModel(BrokerageName.OandaBrokerage);

AddSecurity(SecurityType.Forex, "EURUSD", Resolution.Tick);

var smaDaily = SMA("EURUSD", 24, Resolution.Hour);

var dayCount = Date.Rules.Day; // I need to be able to count number of days after trade was placed?
var tradeTime = DateRules.Time.Day; // Not sure how to set time that trade was taken
var totalTime = dayCount + tradeTime;


var currentPrice = Securities["EURUSD"].HasData; // or maybe OnData(Slice data) used here??

var longTrade = MarketOrder("EURUSD", 10000);
var shortTrade = MarketOrder("EURUSD", -10000);




}

public override void OnData(Slice data)
{
if (!Portfolio["EURUSD"].Invested and currentPrice > smaDaily) // Trying to say that currently NOT invested and current price above SMA then initiate Buy Order
{
longTrade;
LimitOrder("EURUSD",+20 pips, "Profit Target 20 Pips"); // I don't know how to set target profit and cancel this order if 10 days is up?
Log("Purchased EURUSD on " + Time.ToShortDateString());
}

if (!Portfolio["EURUSD"].Invested and currentPrice < smaDaily) // Trying to say that currently NOT invested and current price below SMA then initiate Sell Order
{
shorTrade;
LimitOrder("EURUSD",+20 pips, "Profit Target 20 Pips"); // I don't know how to set target profit and cancel this order if 10 days is up?
Log("Sold EURUSD on " + Time.ToShortDateString());
}

if (Portfolio["EURUSD"].IsLong and totalTime > 10 )
{
shortTrade;
Log("Closed Long Trade " + Time.ToShortDateString());
}

if (Portfolio["EURUSD"].IsShort and totalTime > 10 )
{
longTrade;
Log("Closed Short Trade " + Time.ToShortDateString());
}

}
}
}
0

I updated the version of the code where I try to automatically put in a Profit Target and then cancel the order if the Time is up.  Still bad coding but trying to get my thoughts on papers as they say.  Thanks for any help.

-John

namespace QuantConnect
{

public class TimeBasedAlgo : QCAlgorithm
{

public override void Initialize()
{

SetStartDate(2017, 1, 1);
SetEndDate(2018, 1, 1);
SetCash(5000);

SetBrokerageModel(BrokerageName.OandaBrokerage);

AddSecurity(SecurityType.Forex, "EURUSD", Resolution.Tick);

var smaDaily = SMA("EURUSD", 24, Resolution.Hour);

var dayCount = Date.Rules.Day; // I need to be able to count number of days after trade was placed?
var tradeTime = DateRules.Time.Day; // Not sure how to set time that trade was taken
var totalTime = dayCount + tradeTime;


var currentPrice = Securities["EURUSD"].HasData; // or maybe OnData(Slice data) used here??

var longTrade = MarketOrder("EURUSD", 10000);
var shortTrade = MarketOrder("EURUSD", -10000);

var longProfitTarget = LimitOrder("EURUSD",+20 pips, "Profit Target 20 Pips"); // ??
var shortProfitTarget = LimitOrder("EURUSD",-20 pips, "Profit Target 20 Pips"); // ??

}

public override void OnData(Slice data)
{
if (!Portfolio["EURUSD"].Invested and currentPrice > smaDaily) // Trying to say that currently NOT invested and current price above SMA then initiate Buy Order
{
longTrade;
longProfitTarget; // I don't know how to set target profit and cancel this order if 10 days is up?
Log("Purchased EURUSD on " + Time.ToShortDateString());
}

if (!Portfolio["EURUSD"].Invested and currentPrice < smaDaily) // Trying to say that currently NOT invested and current price below SMA then initiate Sell Order
{
shorTrade;
shortProfitTarget; // I don't know how to set target profit and cancel this order if 10 days is up?
Log("Sold EURUSD on " + Time.ToShortDateString());
}

if (Portfolio["EURUSD"].IsLong and totalTime > 10 )
{
shortTrade;
limitOrderTicket.Cancel(longProfitTarget);
Log("Closed Long Trade " + Time.ToShortDateString() + "and cancelled all orders");
}

if (Portfolio["EURUSD"].IsShort and totalTime > 10 )
{
longTrade;
limitOrderTicket.Cancel(shortProfitTarget);
Log("Closed Short Trade " + Time.ToShortDateString()) + "and cancelled all orders";
}

}
}
}
0

Ok, I keep trying to refine the code.  I read the documentation then comeback and try to get closer.  Any help is greatly appreciated, I'll keep trying.  Thanks!

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); // I need to be able to count number of days after trade was placed?
int tradeTime; // Not sure how to set time that trade was taken
int totalTime = 0;


var currentPrice = slice.Ticks("EURUSD"); // or maybe OnData(Slice data) used here??

var longTrade = MarketOrder("EURUSD", 10000);
var shortTrade = MarketOrder("EURUSD", -10000);

var longProfitTarget = LimitOrder("EURUSD",+20 pips, "Profit Target 20 Pips"); // ??
var shortProfitTarget = LimitOrder("EURUSD",-20 pips, "Profit Target 20 Pips"); // ??

}

public override void OnData(Ticks data)
{
if (!Portfolio["EURUSD"].Invested and currentPrice > smaDaily and totalTime == 0) // Trying to say that currently NOT invested and current price above SMA then initiate Buy Order
{
longTrade;
longProfitTarget; // I don't know how to set target profit, will cancel this order if 10 days is up, with logic below
totalTime = tradeTime + dayCount;
Log("Purchased EURUSD on " + Time.ToShortDateString());

}

if (!Portfolio["EURUSD"].Invested and currentPrice < smaDaily and totalTime == 0) // Trying to say that currently NOT invested and current price below SMA then initiate Sell Order
{
shorTrade;
shortProfitTarget; // I don't know how to set target profit and cancel this order if 10 days is up?
totalTime = tradeTime + dayCount;
Log("Sold EURUSD on " + Time.ToShortDateString());
}

if (Portfolio["EURUSD"].IsLong and 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 and totalTime > 10 )
{
longTrade;
List<OrderTicket> cancelledOrders = Transactions.CancelOpenOrders("EURUSD")
totalTime = 0;
Log("Closed Short Trade " + Time.ToShortDateString()) + "and cancelled all orders";
}

}
}
}
0

you can add a backtest if you want....it is better than the whole code which cant be read properly...... :)

0

I actually don't have the code proper enough to backtest so I will try attaching it as a notebook.  I may have to start a new thread as you pointed out the whole code has made this all a mess...

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