Profile Picture

launch multiple simulations in one click

Posted By remi 5 Years Ago
Message
Posted Friday March 30 2012
Hello, I would like to be able to launch a serie of simulations.

Let's say, dealing with moving average of different periods.

All of that, programmatically! {5;10;20;50}

What is the good way to do it?

Thank you.

Posted Friday March 30 2012
Create a system parameter for the value you want to vary. Then run an optimization.

If you want to programmatically control the value for the system parameters, then you can create an optimization plugin.

Hopefully this gets you started, if you need more help let us know.

Thanks,
Daniel
Posted Friday March 30 2012
Thank you! Very convenient.

I tried to define two parameters and then play with them but I end up with this error message :

An exception of type System.ArgumentOutOfRangeException was thrown.
Value must be between 0 and -1, value was 0

It occurs on the test. It cannot retrieve SMAShort.Current and all the other values.

Am I missing something please?

private static List<DateTime> listDates= new List<DateTime>();

private static string positionToOpen = null;

private static string positionToClose = null;

private static SMA SMAShort = null;

private static SMA SMALong = null;

public override void Startup()

{

// Perform initialization here

if (SMAShort == null) SMAShort = new SMA((int)SystemParameters["PeriodShort"], OtherSymbols[curveName].Close);

if (SMALong == null) SMALong = new SMA((int)SystemParameters["PeriodLong"], OtherSymbols[curveName].Close);

SMAShort.ChartSettings.Color = Color.Blue;

SMALong.ChartSettings.Color = Color.Red;

}

public override void NewBar()

{

if (Bars.Count < 2)

return;

if (Symbol.Name.Equals(curveName))

{

cursingDates.Add(Bars.Current.BarStartTime);

return;

}

if (listDates.Contains(Bars.Current.BarStartTime))

{

if (SMAShort.Current > SMALong.Current && SMAShort.LookBack(1) <= SMALong.LookBack(1))

{

if (Symbol.Name.Equals(indexName1))

{

positionToOpen = indexName1;

positionToClose = indexName2;

}

}

else if (SMAShort.Current < SMALong.Current && SMAShort.LookBack(1) >= SMALong.LookBack(1))

{

if (Symbol.Name.Equals(indexName2))

{

positionToOpen = indexName2;

positionToClose = indexName1;

}

}

}

if (Bars.Current.BarStartTime.DayOfWeek.Equals(DayOfWeek.Tuesday)

|| Bars.Current.BarStartTime.DayOfWeek.Equals(DayOfWeek.Wednesday))

{

if (positionToClose != null && positionToClose.Equals(Symbol.Name))

{

foreach (Position position in OtherSymbols[positionToClose].OpenPositions)

position.CloseAtMarket();

positionToClose = null;

}

if (positionToOpen != null && positionToOpen.Equals(Symbol.Name))

{

if (OpenPositions.Count == 0) OpenPosition(PositionType.Long, OrderType.Market);

positionToOpen = null;

}

}

}

thank you for your time and help.

Posted Friday March 30 2012
Can you include the whole error message? The stack trace should include information about which line the error occurred on.

Thanks,
Daniel
Posted Friday March 30 2012
An exception of type System.ArgumentOutOfRangeException was thrown.
Value must be between 0 and -1, value was 0
Parameter name: nBars
   at RightEdge.Common.RList`1.LookBack(Int32 nBars)
   at RightEdge.Common.SeriesCalculatorBaseWithValues.LookBack(Int32 nBars)
   at RightEdge.Common.SeriesCalculatorBase.get_Current()
   at MySymbolScript.NewBar()
   at RightEdge.Common.SymbolScriptCollection`1.NewBar()
   at MySystemBase.NewBar()
   at RightEdge.Common.Internal.SystemRunner.CallSystemNewBar(FrequencyNewBarEventArgs args)
   at RightEdge.Common.Internal.SystemRunner.ProcessBarEvents(IEnumerable`1 eventList)
   at RightEdge.Common.FrequencyManager.SendPendingBars()
   at RightEdge.Common.FrequencyManager.UpdateTime(DateTime dateTime)
   at RightEdge.Common.Internal.SystemRunner._tickGenerator_NewBar(Object sender, NewBarEventArgs e)
   at RightEdge.Common.TickGenerator.ProcessBar(NewBarEventArgs args)
   at RightEdge.Common.Internal.SystemRunner.ProcessBar(NewBarEventArgs newBars)
   at RightEdge.Shared.SystemWrapper.RunSystem(SystemData systemData, SharedSystemRunData runData, ServiceFactory brokerFactory)
   at RightEdge.Shared.SystemWrapper.RunSystem(String filename, ServiceFactory brokerFactory, PluginSettings dataStoreSettings)
   at RightEdge.Shared.SystemWrapper.RunSystem(String filename, ServiceFactory brokerFactory, PluginSettings dataStoreSettings)
   at RightEdge.Shared.TradingModuleWrapper.Run(String filename)
   at RightEdge.Shared.TradingModuleWrapper.RunSystem(SharedSystemRunData systemRunData)
   at RightEdge.Shared.OptimizationRunner.RightEdge.Common.IOptimizationServices.RunSystem(SystemRunSettings runSettings, SystemProgressUpdate progressCallback)
   at RightEdge.Common.OptimizationPlugin.RunSystem(SystemRunSettings runSettings, SystemProgressUpdate progressCallback)
   at RightEdge.Optimization.DefaultOptimizationPlugin.RunOptimization(SystemRunSettings runSettings)
   at RightEdge.Shared.OptimizationRunner.<ProgressDialog_Loaded>b__3()

   at RightEdge.Common.RList`1.LookBack(Int32 nBars)
   at RightEdge.Common.SeriesCalculatorBaseWithValues.LookBack(Int32 nBars)
   at RightEdge.Common.SeriesCalculatorBase.get_Current()
   at MySymbolScript.NewBar() in C:\Documents and Settings\rdesreum\Application Data\First\First.cs:line 62

Edited: Friday March 30 2012 by remi
Posted Monday April 02 2012
I found !

You need to explicitely call SMA.NewBar() in the New Bar function.

Posted Monday April 02 2012
You shouldn't call SMA.NewBar() yourself. The IndicatorManager handles this for you and if you do it yourself you will probably get incorrect values.

Your stack trace didn't have line number information. To enable this, right click on your system name in the trading system tree, click on properties, and check the "Generate Debug Info" in the dialog that comes up.

It looks like you didn't include all the code for your system so I wasn't able to test it out myself. I think it probably has to do with the fact that your SMAs are based on another symbol. Different symbols may not have bars for all the same date, so the "if (Bars.Count < 2)" check doesn't guarantee that there will have been any bars for the curveName symbol, so your SMAs don't necessarily have a value which gives the exception when you try to access the Current value.

Probably the best way to fix this is just to check the Count of the SMAs in addition to the count of the Bars.

Thanks,
Daniel
Posted Monday April 02 2012
When I run my program without exception handling, RightEdge pops up an exception message with what I pasted you in the previous message :

System.ArgumentOutOfRangeException: Value must be between 0 and -1, value was 0
Parameter name: nBars
   at RightEdge.Common.RList`1.LookBack(Int32 nBars)
   at RightEdge.Common.SeriesCalculatorBaseWithValues.LookBack(Int32 nBars)
   at RightEdge.Common.SeriesCalculatorBase.get_Current()
   at MySymbolScript.NewBar() in c:\Documents and Settings\rdesreum\Application Data\First\First.cs:line 75
   at RightEdge.Common.SymbolScriptCollection`1.NewBar()
   at MySystemBase.NewBar() in c:\Documents and Settings\rdesreum\Application Data\First\BaseClasses.cs:line 31
   at RightEdge.Common.Internal.SystemRunner.CallSystemNewBar(FrequencyNewBarEventArgs args)
   at RightEdge.Common.Internal.SystemRunner.ProcessBarEvents(IEnumerable`1 eventList)
   at RightEdge.Common.FrequencyManager.SendPendingBars()
   at RightEdge.Common.FrequencyManager.UpdateTime(DateTime dateTime)
   at RightEdge.Common.Internal.SystemRunner._tickGenerator_NewBar(Object sender, NewBarEventArgs e)
   at RightEdge.Common.TickGenerator.ProcessBar(NewBarEventArgs args)
   at RightEdge.Common.Internal.SystemRunner.ProcessBar(NewBarEventArgs newBars)
   at RightEdge.Shared.SystemWrapper.RunSystem(SystemData systemData, SharedSystemRunData runData, ServiceFactory brokerFactory)
   at RightEdge.Shared.SystemWrapper.RunSystem(String filename, ServiceFactory brokerFactory, PluginSettings dataStoreSettings)
   at RightEdge.Shared.SystemWrapper.RunSystem(String filename, ServiceFactory brokerFactory, PluginSettings dataStoreSettings)
   at RightEdge.Shared.TradingModuleWrapper.Run(String filename)
   at RightEdge.Shared.TradingModuleWrapper.RunSystem(SharedSystemRunData systemRunData)
   at RightEdge.Shared.OptimizationRunner.RightEdge.Common.IOptimizationServices.RunSystem(SystemRunSettings runSettings, SystemProgressUpdate progressCallback)
   at RightEdge.Common.OptimizationPlugin.RunSystem(SystemRunSettings runSettings, SystemProgressUpdate progressCallback)
   at RightEdge.Optimization.DefaultOptimizationPlugin.RunOptimization(SystemRunSettings runSettings)
   at RightEdge.Shared.OptimizationRunner.<ProgressDialog_Loaded>b__3()

The line 75 is :

if (SMAShort.Current > SMALong.Current && SMAShort.LookBack(1) <= SMALong.LookBack(1))

Here is my full code :

#region Using statements
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using RightEdge.Common;
using RightEdge.Common.ChartObjects;
using RightEdge.Indicators;
#endregion
#region System class
public class MySystem : MySystemBase
{
public override void Startup()
{
// Perform initialization or set system wide options here
}
}
#endregion
public class MySymbolScript : MySymbolScriptBase
{
// private ISeries curve1;
// private ISeries curve2;
private String indexName1 = "Index1";
private String indexName2 = "Index2";
private String curveName = "Curve";
private static List<DateTime> cursingDates = new List<DateTime>();
private static string positionToOpen = null;
private static string positionToClose = null;
private static SMA SMAShort = null;
private static SMA SMALong = null;
public override void Startup()
{
// Perform initialization here
if (SMAShort == null) SMAShort = new SMA((int)SystemParameters["PeriodShort"], OtherSymbols[curveName].Close);
if (SMALong == null) SMALong = new SMA((int)SystemParameters["PeriodLong"], OtherSymbols[curveName].Close);
//if (SMAShort == null) SMAShort = new SMA((int)SystemParameters["PeriodShort"]);
//if (SMALong == null) SMALong = new SMA((int)SystemParameters["PeriodLong"]);
SMAShort.ChartSettings.Color = Color.Blue;
SMALong.ChartSettings.Color = Color.Red;
}
public override void NewBar()
{
//SMALong.NewBar();
//SMAShort.NewBar();
if (Bars.Count < 2)
return;
//SMALong.SetInputs(OtherSymbols[curveName].Close);
//SMAShort.SetInputs(OtherSymbols[curveName].Close);
if (Symbol.Name.Equals(curveName))
{
cursingDates.Add(Bars.Current.BarStartTime);
return;
}
if (cursingDates.Contains(Bars.Current.BarStartTime))
{
if (SMAShort.Current > SMALong.Current && SMAShort.LookBack(1) <= SMALong.LookBack(1))
{
if (Symbol.Name.Equals(indexName1))
{
positionToOpen = indexName1;
positionToClose = indexName2;
}
}
else if (SMAShort.Current < SMALong.Current && SMAShort.LookBack(1) >= SMALong.LookBack(1))
{
if (Symbol.Name.Equals(indexName2))
{
positionToOpen = indexName2;
positionToClose = indexName1;
}
}
}
i = 1;
if (Bars.Current.BarStartTime.DayOfWeek.Equals(DayOfWeek.Tuesday)
|| Bars.Current.BarStartTime.DayOfWeek.Equals(DayOfWeek.Wednesday))
{
if (positionToClose != null && positionToClose.Equals(Symbol.Name))
{
foreach (Position position in OtherSymbols[positionToClose].OpenPositions)
position.CloseAtMarket();
positionToClose = null;
}
if (positionToOpen != null && positionToOpen.Equals(Symbol.Name))
{
if (OpenPositions.Count == 0) OpenPosition(PositionType.Long, OrderType.Market);
positionToOpen = null;
}
}
}
public override void OrderFilled(Position position, Trade trade)
{
// This method is called when an order is filled
}
public override void OrderCancelled(Position position, Order order, string information)
{
// This method is called when an order is cancelled or rejected
if (!order.CancelPending)
{
OutputWarning("Unexpected order cancel: " + order.ToString() + " " + information);
}
}
}

This is true that the three symbols don't have the same granularity.

Two are daily, one is weekly.

When looking at SMALong.Count or SMAShort.Count, it seems to be always set to 0.

While I initialised my SMA with OtherSymbols[curveName].Close as Inputs, I see OtherSymbols[curveName].Close.Count to 0, is that normal?

Why? Please.


Edited: Monday April 02 2012 by remi
Posted Tuesday April 03 2012
Hello, do you have any idea why SMALong.Count and SMAShort.Long are always equals to 0 ?

How is it supposed to be incremented if we don't call NewBar() on them?

I had seen that call here : http://www.rightedgesystems.com/forums/Attachment16.aspx

 

Looking around on the forum, I found this topic that might be helpful:

http://www.rightedgesystems.com/forums/Topic13819-18-1.aspx?Highlight=SMA

The thing is I cannot call

OtherSymbols[curveName].SMA.Current , that simply does not compile...

Please help me.


Edited: Tuesday April 03 2012 by remi
Posted Tuesday April 03 2012
Simple question: Do I have to build my SMA indicator? Cause I did not in here.....

That may explain why.



Similar Topics


Reading This Topic


2005-2017 © RightEdge Systems