SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Wed Nov 25, 2020 8:11 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Code Start Problems
PostPosted: Thu May 05, 2011 6:18 am 
Offline

Joined: Mon Dec 06, 2010 2:15 am
Posts: 47
I'm working on some code pieces, the entirety of which I've posted below. I've running this over futures tick data for a single month, Jan 08, nothing particular about this month, it's just a random selection.
In back testing the code, it fails to place any trades until about 1/2 way through the month, JAN 16th to be exact. I can't determine any reason for this, or in any fashion which I'm getting the code to hold back trades until 1/2 the month is over. I've tried it on other data sets and the same quarks appear.
Is there anything apparent which may be causing this? Any other suggestions for improving the code will also be received with gratitude.

Thank you,
Eric
------------------------------------CODE BELOW-------------------------------------

using System;
using System.Drawing;

using OpenQuant.API;
using OpenQuant.API.Indicators;

public class MyStrategy : Strategy
{
[Parameter ("Order Quantity")]
double Qty = 100000;

[Parameter ("Length of LinRegSlope", "LRS")]
int Length = 15;

[OptimizationParameter (5, 50, 5)]
[Parameter ("Length of ROC", "ROC")]
int LengthR = 10;

[OptimizationParameter (0.1, 0.4, 0.05)]
[Parameter ("Upper ROC Level", "ROCU")]
double rocU = 0.2;

[OptimizationParameter (-0.1, -0.4, 0.05)]
[Parameter ("Lower ROC Level", "ROCL")]
double rocL = -0.2;

[Parameter ("Color of ROC", "ROC")]
Color Color1 = Color.Yellow;

[Parameter ("Color of LRS", "LRS")]
Color Color2 = Color.Aqua;

//[Parameter ("StopMode", "Stop")]
//StopMode StopMode = StopMode.Absolue;

[Parameter ("Profit Limit OCA Level", "OCA")]
double LimitOCALevel = 0.001;

[Parameter ("Stop OCA Level", "OCA")]
double StopOCALevel = 0.003;

ROC roc;
LRS lrs;

int OCACount = 0;

Order marketOrder;
// Order buyLimitOrder;
Order limitOrder;
// Order stopOrder;

bool entryEnabled = true;

public override void OnStrategyStart()
{

roc = new ROC (Bars, LengthR, 0, Color1);
lrs = new LRS (Bars, Length, Color2);

// Draw(lrs, 0);
// Draw(roc, 0);
Draw(lrs, 3);
Draw(roc, 2);
}

public override void OnBar(Bar close)
{

Cross crossU = roc.Crosses(rocU, Bar);
Cross crossL = roc.Crosses(rocL, Bar);
// lrs lrsL = lrs(Bars, bar);

if (entryEnabled)
// if (!HasPosition)
{
if (Bars.Count > Length)
{
if (lrs.Ago(1)< -0.00001) // note: changed the .Ago override from the variable "Length" to the absolute 1.
{
if (crossU == Cross.Below)

{
marketOrder = MarketOrder (OrderSide.Sell, Qty, "Short Entry");
marketOrder.Send();

entryEnabled = false;

}
}
else
if (crossL == Cross.Above)
if (lrs.Ago(1)>0.00001)
{
{
marketOrder = MarketOrder (OrderSide.Buy, Qty, "Long Entry");
marketOrder.Send();

entryEnabled = false;
}
}
}
}
}

public override void OnPositionOpened()
{

// if (HasPosition)
if (Position.Side == PositionSide.Short)
{
double entryPriceShort = Portfolio.Transactions[Portfolio.Transactions.Count - 1].Price;
double profitTargetS = entryPriceShort - LimitOCALevel;
//double profitTargetS = marketOrder.AvgPrice - LimitOCALevel;
//double profitTragetL = LimitOCALevel - bar.Close;
limitOrder = BuyLimitOrder(Qty, profitTargetS, "Close Short, Profit");
limitOrder.OCAGroup = "OCA " + Instrument.Symbol + " " + OCACount;
limitOrder.Send();

double lossTargetS = entryPriceShort + StopOCALevel;
//double lossTargetS = marketOrder.AvgPrice + StopOCALevel;
//double lossTargetL = StopOCALevel + bar.Close;
limitOrder = BuyStopOrder(Qty, lossTargetS, "Close Short, Loss"); // changed stopOrder to limitOrder
limitOrder.OCAGroup = "OCA " + Instrument.Symbol + " " + OCACount;
limitOrder.Send(); // changed stopOrder to limitOrder

entryEnabled = true;
OCACount++;
}

else
{
if (Position.Side == PositionSide.Long)
{
double entryPriceLong = Portfolio.Transactions[Portfolio.Transactions.Count - 1].Price;
double profitTargetL = entryPriceLong + LimitOCALevel;

//double profitTragetL = LimitOCALevel + bar.Close;
limitOrder = SellLimitOrder(Qty, profitTargetL, "Close Long, Profit");
limitOrder.OCAGroup = "OCA " + Instrument.Symbol + " " + OCACount;
limitOrder.Send();

double lossTargetL = entryPriceLong - StopOCALevel;
//double profitTragetL = LimitOCALevel - bar.Close;
limitOrder = SellStopOrder(Qty, lossTargetL, "Close Long, Loss");// changed stopOrder to limitOrder
limitOrder.OCAGroup = "OCA " + Instrument.Symbol + " " + OCACount;
limitOrder.Send();// changed stopOrder to limitOrder

entryEnabled = true;
OCACount++;


}
}

}

public override void OnPositionClosed()
{

entryEnabled = true;
if (!(limitOrder.IsFilled || limitOrder.IsCancelled))
{
limitOrder.Cancel();
}

}
}


// public override void OnStopExecuted(Stop stop)
// {
// if (Position.Side == PositionSide.Long)
// marketOrder.Send();
// if (Position.Side == PositionSide.Short)
// marketOrder.Send();
// }


Top
 Profile  
 
 Post subject: Re: Code Start Problems
PostPosted: Sun May 15, 2011 3:41 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Just a guess, but I see you're using a LinReg indicator that needs to load up 15 bars input before it pumps out a proper value. If you're using daily bars, that might explain the 15 days.

But you say you're using futures tick data, which is vastly more dense than daily bars. The 15 matches the 16th of the month pretty well, so I went with that as a guess.


Top
 Profile  
 
 Post subject: Re: Code Start Problems
PostPosted: Mon May 16, 2011 6:09 am 
Offline

Joined: Mon Dec 06, 2010 2:15 am
Posts: 47
I actually thought the same thing myself at first glance, alas the problem remains. I think what i'll do is quickly rescript the code and perhaps I'll come across an error which is otherwise disguised.


Top
 Profile  
 
 Post subject: Re: Code Start Problems
PostPosted: Mon May 16, 2011 6:22 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
I'll try to be more precise...

Your code references Bars as the series used for the indicators. The code itself does not specify the duration of the default Bars series. Instead, the size of the bars is set in the GUI controls (MarketData in the solution window?).

So if that setting has defaulted to daily bars, then the 15 length might indeed be the problem.

Certainly your code is not doing its trading on the actual tick data, else you would have some code under OnTrade, or OnQuote. But since you've only got OnBar, then you're using Bars, and maybe the Bar length is set to 1 day (unless you're using Range bars, or something exotic).

I'd say check the length of the bars to see if they are daily bars, in which case the length=15 of the indicators could be the cause of no trades until the 16th of the month.


Top
 Profile  
 
 Post subject: Re: Code Start Problems
PostPosted: Mon May 16, 2011 7:05 am 
Offline

Joined: Mon Dec 06, 2010 2:15 am
Posts: 47
hi kkkwj,

Thank you for the further discussion. Right now I'm using the GUI to set the bars to various tick lengths, normally between 144-1600 just to provide a basic range. Do you think there could be a problem here with how the GUI represents the tick array?


Thanks,
Eric


Top
 Profile  
 
 Post subject: Re: Code Start Problems
PostPosted: Mon May 16, 2011 7:15 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
I doubt the GUI is doing anything wrong. I suppose you've checked the simulation and trading date ranges in the properties window too? And you don't say what futures you're using, but some of them (like currencies) roll over on the 15th of the month, so there might not be any data before the 15th of the month.


Top
 Profile  
 
 Post subject: Re: Code Start Problems
PostPosted: Mon May 16, 2011 7:22 am 
Offline

Joined: Mon Dec 06, 2010 2:15 am
Posts: 47
I'll double check the data set, mostly I'm working with /EC (Euro Future) and /NQ, both sets purchased from TickData. I'm guessing the data and roll over process is solid as I've been running this data in MatLab for months without a problem.

From your basic overview, do you not see any glaring errors with the code? I only ask because c# coding is my personal weakest area of expertise.

Thank you.


Top
 Profile  
 
 Post subject: Re: Code Start Problems
PostPosted: Mon May 16, 2011 7:45 am 
Offline

Joined: Thu Jun 08, 2006 3:56 pm
Posts: 537
Location: BC Canada
Well, I didn't read your code in detail; I just took a look at the declarations and code, enough to see that you're doing a crossover strategy. I did NOT see anything in there that said, "No trades until halfway through the month."

It still feels like a daily bar thing to me, but it's hard to tell without seeing everything.

Maybe try changing the length of the crossover indicators, to see if the trading date moves back or forth with the changes.


Top
 Profile  
 
 Post subject: Re: Code Start Problems
PostPosted: Mon May 16, 2011 2:45 pm 
Offline

Joined: Mon Dec 06, 2010 2:15 am
Posts: 47
OK no problem, I'll mess around with it, and post a revision if I get it to work correctly. It's just a sample code I'm putting together for a presentation so I wanted to keep it simple such as a cross over, but it won't be a very useful presentation if it's not working ;)

Eric


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group