- ago
how i use the "Add" method to create custome bar historyin in Q studio?
because its make me error " execute exeption (symbol,start index) object reference not set to an object"

my code : " BarHistoryCotume.Add(Datetime.today , 9 , 8 , 2, 4, 5);" and it doesnt work..
I intentionally put numbers, to make it simple and work .. but even with "real" values it does not work, where can the problem be?




Not the first time I try to get answers here about q stodio, you guys just ignore me and I have no support for using Q studio!

0
582
Solved
33 Replies

Reply

Bookmark

Sort
Cone8
 ( 28.32% )
- ago
#1
QUOTE:
you guys just ignore me and I have no support for using Q studio!
That's an unfair accusation! You can verify for yourself that every discussion you started was quickly answered.

What purpose do you have to create a custom BarHistory? If we knew what you're trying to do, we could answer the question better.

And, why haven't you switched to Wealth-Lab 7?

0
Glitch8
 ( 12.10% )
- ago
#2
We would need to see a full code example to do any diagnosis.
0
Glitch8
 ( 12.10% )
- ago
#3
Did you actually create the instance of BarHistoryCotume in your code using the new operator?
0
- ago
#4
I say this because when you shut down the Q Studio website I had no idea and I was trying to contact you for two weeks via email, which I did not know was not working. Then I found this site and only then was I able to get back to work after realizing I needed to change version ... and note that in my previous question I asked about q studio and you answered me about wealth lab 7.

I do not use wealth lab 7 because I do not have a life time license there, I bought the software for long use a few months before you upgraded.
And even when I had problems with q STUDIO that you did not solve (before the whole upgrade) and I contacted you by email, the answer was that I would upgrade to wealth lab (which I am not interested in after paying a sum of money for software that will be used by me for a long time).

Your software is excellent and I very much appreciate your work, but it does not change the fact that I feel I have no proper answer from you.
-----------------------------------------------------------------------

this is not my real code because it's more complicated to screenshot, but its the same problem. thanks

0
- ago
#5
Whether it's NinjaTrader, Wealth-Lab or Quantacula, prior to using an object (assigning properties) it requires initialization. Which is missing in your code. Note the Initialize block and put there a line to instantiate the object e.g.
CODE:
BarHistoryCostume = new BarHistory(...

See proper syntax in QDocs.
0
- ago
#6
thanks!
im still don't succeed to initialize that object, can you write me the full line code for example, or explain me where should i find the correct syntax in the Qdocs?
because i don't understand what should i put in the brackets () for costume bar history
0
Glitch8
 ( 12.10% )
- ago
#7
Full code:

CODE:
BarHistoryCotume = new BarHistory("XYZ", HistoryScale.Daily);
0
- ago
#8
Hi,
i tried several times and don't succeed.. even its does not make me an error message, the Qustodio shut down if i click on chart to see what i get (after running the program).
where i was wrong?



thanks
0
Glitch8
 ( 12.10% )
- ago
#9
Do you think you could paste the full code here or email it to us at support@wealth-lab.com?
0
- ago
#10
It was full code I just narrowed it down to fit in one screen cut.

I think there may be a mistake I miss in creating the object, possibly in the format I insert in parentheses in the object initialization row.



0
Glitch8
 ( 12.10% )
- ago
#11
OK, but it would save us a lot of work having to type the code in by hand. Can't you copy it out of the editor and paste the code into a post so I don't have to meticulously type out all those lines from your screen shot? Thanks for the consideration.
0
- ago
#12
for sure.

CODE:
using Quantacula.Backtest; using System; using Quantacula.Core; using Quantacula.Indicators; using System.Drawing; using System.Collections.Generic; namespace Quantacula { public class MyModel1 : UserModelBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {     BarHistoryCostume = new BarHistory("BarHistoryCostume", HistoryScale.Daily);          LNTb = GetHistory(bars, "LNT");          DTEb = GetHistory(bars, "DTE"); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) {       open1 = DTEb.Open[idx] - LNTb.Open[idx];     high1 = DTEb.High[idx] - LNTb.High[idx];     low1 = DTEb.Low[idx] - LNTb.Low[idx];     close1 = DTEb.Close[idx] - LNTb.Close[idx];     vol1 = DTEb.Volume[idx] + LNTb.Volume[idx];       BarHistoryCostume.Add(DateTime.Today, open1, high1, low1, close1, vol1);       PlotBarHistory(BarHistoryCostume, "costume" , Color.Black);     if (!HasOpenPosition(bars, PositionType.Long)) { //code your buy conditions here } else { //code your sell conditions here } }     //declare private variables below     private BarHistory DTEb;     private BarHistory LNTb;     private BarHistory BarHistoryCostume;     private double open1;     private double high1;     private double low1;     private double close1;     private double vol1;            } }
0
Glitch8
 ( 12.10% )
- ago
#13
There were two issues ...

1. You were adding "Today" to each bar in the custom BarHistory, instead of the correct DateTime (fixed).
2. You were calling the plot method in the Execute method, which causes it to plot each time the method is called. This resulted in an overload. In WL7 we have added some defensive code around this, but I fixed it below in your code for QS.

CODE:
using Quantacula.Backtest; using System; using Quantacula.Core; using Quantacula.Indicators; using System.Drawing; using System.Collections.Generic; namespace Quantacula {    public class MyModel1 : UserModelBase    {       //create indicators and other objects here, this is executed prior to the main trading loop       public override void Initialize(BarHistory bars)       {          BarHistoryCostume = new BarHistory("BarHistoryCostume", HistoryScale.Daily);          PlotBarHistory(BarHistoryCostume, "costume", Color.Black);          LNTb = GetHistory(bars, "LNT");          DTEb = GetHistory(bars, "DTE");       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx)       {          open1 = DTEb.Open[idx] - LNTb.Open[idx];          high1 = DTEb.High[idx] - LNTb.High[idx];          low1 = DTEb.Low[idx] - LNTb.Low[idx];          close1 = DTEb.Close[idx] - LNTb.Close[idx];          vol1 = DTEb.Volume[idx] + LNTb.Volume[idx];          DateTime dt = bars.DateTimes[idx];          BarHistoryCostume.Add(dt, open1, high1, low1, close1, vol1);                if (!HasOpenPosition(bars, PositionType.Long))          {             //code your buy conditions here          }          else          {             //code your sell conditions here          }       }       //declare private variables below       private BarHistory DTEb;       private BarHistory LNTb;       private BarHistory BarHistoryCostume;       private double open1;       private double high1;       private double low1;       private double close1;       private double vol1;    } }
0
Best Answer
- ago
#14
Thank you very much!
0
- ago
#15
Hi ,
after i create the bar history costume i tried to plot an indicator on it, and it raises me an error: "eror population by symbol: index was out of range..".
it doesnt raises me an error when i create the indicator, just when i trying to plot it.
(this how i created the indicator: indicator1 = new sma(BarHistoryCostume.close , 10); )

what should i need to do for plot and use indicators on bar history costume?
0
Glitch8
 ( 12.10% )
- ago
#16
Because you are adding bars one by one to your custom BarHistory in the Execute method, you need to plot it after the Execute methods are completed, the best place is in the Cleanup method, like this:

CODE:
using Quantacula.Backtest; using System; using Quantacula.Core; using Quantacula.Indicators; using System.Drawing; using System.Collections.Generic; namespace Quantacula {    public class MyModel1 : UserModelBase    {       //create indicators and other objects here, this is executed prior to the main trading loop       public override void Initialize(BarHistory bars)       {          BarHistoryCostume = new BarHistory("BarHistoryCostume", HistoryScale.Daily);          PlotBarHistory(BarHistoryCostume, "costume", Color.Black);          LNTb = GetHistory(bars, "LNT");          DTEb = GetHistory(bars, "DTE");       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx)       {          open1 = DTEb.Open[idx] - LNTb.Open[idx];          high1 = DTEb.High[idx] - LNTb.High[idx];          low1 = DTEb.Low[idx] - LNTb.Low[idx];          close1 = DTEb.Close[idx] - LNTb.Close[idx];          vol1 = DTEb.Volume[idx] + LNTb.Volume[idx];          DateTime dt = bars.DateTimes[idx];          BarHistoryCostume.Add(dt, open1, high1, low1, close1, vol1);                if (!HasOpenPosition(bars, PositionType.Long))          {             //code your buy conditions here          }          else          {             //code your sell conditions here          }       } public override void Cleanup(BarHistory bars) {          SMA sma = new SMA(BarHistoryCostume.Close, 10);          PlotTimeSeries(sma, "SMA", "costume", Color.Blue); } //declare private variables below private BarHistory DTEb;       private BarHistory LNTb;       private BarHistory BarHistoryCostume;       private double open1;       private double high1;       private double low1;       private double close1;       private double vol1;    } }
0
- ago
#17
thanks,
what the different between the plot indicator and plot bar history costume?, because yesteraday i was think what you say, but you told me that i need to plot the bar history costume in the prior loop before the execute , so i realized its ok to plot there the indicator... what the different between them?
0
Glitch8
 ( 12.10% )
- ago
#18
The different between PlotIndicator and PlotBarHistory is:

- PlotHistory is intended to plot instances of classes derived from IndicatorBase
- PlotBarHistory is intended to plot instances of BarHistory

What I said before was that you should not plot in the Execute loop.

It's usually OK to plot in Initialize. But you're building up your BarHistory inside the Execute loop, so plotting it Initialize won't work. That's why we need to plot it in CleanUp, which gets called after all of the Execute calls are finished.
0
- ago
#19
thanks !
0
- ago
#20
Why after I created all the indicators in cleanup method it does not let me use their values ?. if I change the indicator6 (in the buy\sel condition) to a created at the prior loop (initialize)indicator, it works.
should i use another method to get the values for the execute loop?

0
Glitch8
 ( 12.10% )
- ago
#21
Because Cleanup gets called in the final stage, after all the Execute calls have been made. You should not use a Cleanup method here, move all that into Initialize.

0
- ago
#22
I have already tried ,but in the initialize it raises me an error , i cant put it there.
and i also tried before and after the initalize... it doesnt work
0
Glitch8
 ( 12.10% )
- ago
#23
OK, copy and paste the code that you have and I will try and fix it.
0
- ago
#25
CODE:
using Quantacula.Backtest; using System; using Quantacula.Core; using Quantacula.Indicators; using System.Drawing; using System.Collections.Generic; namespace Quantacula { public class MyModel1 : UserModelBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars)       {          BarHistoryRatio = new BarHistory("ratio", HistoryScale.Daily);          BarHistorySpread = new BarHistory("spread", HistoryScale.Daily);          OneSymbol = new BarHistory("DTE", HistoryScale.Daily);          TwoSymbol = new BarHistory("LNT", HistoryScale.Daily);          OneSymbol = GetHistory(bars, "DTE");          TwoSymbol = GetHistory(bars, "LNT");          PlotBarHistory(BarHistoryRatio, "ratio", Color.Black);          PlotBarHistory(BarHistorySpread, "spread", Color.Black);              StartIndex = 50;        }     //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) {          //caculation for bar history costume:          openR = OneSymbol.Open[idx] / TwoSymbol.Open[idx];     highR = OneSymbol.High[idx] / TwoSymbol.High[idx];     lowR = OneSymbol.Low[idx] / TwoSymbol.Low[idx];          closeR = OneSymbol.Close[idx] / TwoSymbol.Close[idx];     volR = OneSymbol.Volume[idx] + TwoSymbol.Volume[idx];     openS = OneSymbol.Open[idx] - TwoSymbol.Open[idx];     highS = OneSymbol.High[idx] - TwoSymbol.High[idx];     lowS = OneSymbol.Low[idx] - TwoSymbol.Low[idx];     closeS = OneSymbol.Close[idx] - TwoSymbol.Close[idx];     volS = OneSymbol.Volume[idx] + TwoSymbol.Volume[idx];          DateTimeForCostume = bars.DateTimes[idx];          BarHistoryRatio.Add(DateTimeForCostume, openR, highR, lowR, closeR, volR);          BarHistorySpread.Add(DateTimeForCostume, openS, highS, lowS, closeS, volS);        if (!HasOpenPosition(bars, PositionType.Long)) {             OpenPos = true; } else {             OpenPos = false; }     //buy condotion          if(indicator6[idx] > 2)          {             PlaceTrade(OneSymbol, TransactionType.Buy, OrderType.Market, 0, 0);                PlaceTrade(TwoSymbol, TransactionType.Short, OrderType.Market, 0, 0);             }     //sell condotion          if(indicator6[idx] < 1)          {             PlaceTrade(OneSymbol, TransactionType.Sell, OrderType.Market, 0, 0);             PlaceTrade(TwoSymbol, TransactionType.Cover, OrderType.Market, 0,0);          } }           public override void Cleanup(BarHistory bars)       {          indicator1 = new SMA(BarHistoryRatio.Close, 50);          indicator3 = new ZScore(BarHistoryRatio.Close, 20);          indicator5 = new StdDev(BarHistoryRatio.Close, 30);          //PlotTimeSeries(indicator1,"sma","ratio", Color.Blue);          //PlotTimeSeries(indicator3,"zscore","ratio", Color.Blue);          PlotTimeSeries(indicator3,"std","ratio", Color.Blue);                    indicator2 = new SMA(BarHistorySpread.Close, 50);          indicator4 = new ZScore(BarHistorySpread.Close, 20);          indicator6 = new StdDev(BarHistorySpread.Close, 30);          //PlotTimeSeries(indicator2,"sma","spread", Color.Blue);          //PlotTimeSeries(indicator4,"zcore","spread", Color.Blue);          PlotTimeSeries(indicator3,"std","spread", Color.Blue);       }           //declare private variables below       private IndicatorBase indicator1;       private IndicatorBase indicator2;       private IndicatorBase indicator3;       private IndicatorBase indicator4;     private IndicatorBase indicator5;     private IndicatorBase indicator6;     private IndicatorBase indicator7;     private IndicatorBase indicator8;       private BarHistory BarHistorySpread;       private BarHistory BarHistoryRatio;       private BarHistory OneSymbol;     private BarHistory TwoSymbol;       private DateTime DateTimeForCostume;     private double openR;     private double closeR;     private double highR;     private double lowR;     private double volR;       private double openS;       private double closeS;       private double highS;       private double lowS;       private double volS;       private bool OpenPos;           } }
0
Glitch8
 ( 12.10% )
- ago
#26
I went ahead and refactored it to populate everything in the Initialize method

CODE:
using Quantacula.Backtest; using System; using Quantacula.Core; using Quantacula.Indicators; using System.Drawing; using System.Collections.Generic; namespace Quantacula {    public class MyModel1 : UserModelBase    {       //create indicators and other objects here, this is executed prior to the main trading loop       public override void Initialize(BarHistory bars)       {          BarHistoryRatio = new BarHistory("ratio", HistoryScale.Daily);          BarHistorySpread = new BarHistory("spread", HistoryScale.Daily);          OneSymbol = new BarHistory("DTE", HistoryScale.Daily);          TwoSymbol = new BarHistory("LNT", HistoryScale.Daily);          OneSymbol = GetHistory(bars, "DTE");          TwoSymbol = GetHistory(bars, "LNT");          for (int idx = 0; idx < bars.Count; idx++)          {             //caculation for bar history costume:             openR = OneSymbol.Open[idx] / TwoSymbol.Open[idx];             highR = OneSymbol.High[idx] / TwoSymbol.High[idx];             lowR = OneSymbol.Low[idx] / TwoSymbol.Low[idx];             closeR = OneSymbol.Close[idx] / TwoSymbol.Close[idx];             volR = OneSymbol.Volume[idx] + TwoSymbol.Volume[idx];             openS = OneSymbol.Open[idx] - TwoSymbol.Open[idx];             highS = OneSymbol.High[idx] - TwoSymbol.High[idx];             lowS = OneSymbol.Low[idx] - TwoSymbol.Low[idx];             closeS = OneSymbol.Close[idx] - TwoSymbol.Close[idx];             volS = OneSymbol.Volume[idx] + TwoSymbol.Volume[idx];             DateTimeForCostume = bars.DateTimes[idx];             BarHistoryRatio.Add(DateTimeForCostume, openR, highR, lowR, closeR, volR);             BarHistorySpread.Add(DateTimeForCostume, openS, highS, lowS, closeS, volS);          }          PlotBarHistory(BarHistoryRatio, "ratio", Color.Black);          PlotBarHistory(BarHistorySpread, "spread", Color.Black);          indicator1 = new SMA(BarHistoryRatio.Close, 50);          indicator3 = new ZScore(BarHistoryRatio.Close, 20);          indicator5 = new StdDev(BarHistoryRatio.Close, 30);          //PlotTimeSeries(indicator1,"sma","ratio", Color.Blue);          //PlotTimeSeries(indicator3,"zscore","ratio", Color.Blue);          PlotTimeSeries(indicator3, "std", "ratio", Color.Blue);          indicator2 = new SMA(BarHistorySpread.Close, 50);          indicator4 = new ZScore(BarHistorySpread.Close, 20);          indicator6 = new StdDev(BarHistorySpread.Close, 30);          //PlotTimeSeries(indicator2,"sma","spread", Color.Blue);          //PlotTimeSeries(indicator4,"zcore","spread", Color.Blue);          PlotTimeSeries(indicator3, "std", "spread", Color.Blue);          StartIndex = 50;       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx)       {          if (!HasOpenPosition(bars, PositionType.Long))          {             OpenPos = true;          }          else          {             OpenPos = false;          }          //buy condotion          if(indicator6[idx] > 2)          {             PlaceTrade(OneSymbol, TransactionType.Buy, OrderType.Market, 0, 0);             PlaceTrade(TwoSymbol, TransactionType.Short, OrderType.Market, 0, 0);          }          //sell condotion          if(indicator6[idx] < 1)          {             PlaceTrade(OneSymbol, TransactionType.Sell, OrderType.Market, 0, 0);             PlaceTrade(TwoSymbol, TransactionType.Cover, OrderType.Market, 0, 0);          }       }       public override void Cleanup(BarHistory bars)       {       }       //declare private variables below       private IndicatorBase indicator1;       private IndicatorBase indicator2;       private IndicatorBase indicator3;       private IndicatorBase indicator4;       private IndicatorBase indicator5;       private IndicatorBase indicator6;       private IndicatorBase indicator7;       private IndicatorBase indicator8;       private BarHistory BarHistorySpread;       private BarHistory BarHistoryRatio;       private BarHistory OneSymbol;       private BarHistory TwoSymbol;       private DateTime DateTimeForCostume;       private double openR;       private double closeR;       private double highR;       private double lowR;       private double volR;       private double openS;       private double closeS;       private double highS;       private double lowS;       private double volS;       private bool OpenPos;    } }
0
- ago
#27
Thank you !
the cleanup method isn't necessary anymore, right?
and why the for loop fix the problem ? What does it prevent ?
0
Glitch8
 ( 12.10% )
- ago
#28
Right, the way I changed it, the CleanUp method is not needed any longer.

The loop is required to fully populate the custom TimeSeries you are creating based on external symbol data. Because you are then creating other indicators based off of them. In order to create the other indicators, the source TimeSeries need to be fully populated.
0
- ago
#29
QUOTE:
... you are creating based on external symbol data. Because you are then creating other indicators based off of them. In order to create the other indicators, the source TimeSeries need to be fully populated.
The key point is "fully populated locally". You get the best Principle of Locality (i.e. processor on-chip cache hits) buy minimizing your memory footprint during TimeSeries calculations/creation. That means constructing these TimeSeries in Initialize() whenever possible.

It's about keeping memory requirements as tight and localized as possible for maximizing on-chip processor cache speed. (You can spread these calculations out over more procedures, but you'll take a speed hit, especially in processor concurrency and hardware cache hits.)
0
- ago
#30
thanks you both ! Very helpful to me
0
- ago
#31
I'm tring to create a model combine two history scale (daily and minute), and it raises my an eror: "execute exeption (symbol,2) index was out of range. must be non-negative and less than size of the collction (parameter index)".


this is the code in the prior loop, where i created the daily scale (the model setting is on minutes) :

CODE:
//create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {     DailyBarsT = new BarHistory("day",HistoryScale.Daily);          DailyBarsT = BarHistoryCompressor.ToDaily(bars);          DailyBars1 = new BarHistory("daily", HistoryScale.Daily);          DailyBars1 = BarHistorySynchronizer.Synchronize(DailyBarsT, bars);          indicator1 = new ROC(DailyBars1.Close,1);              StartIndex = 2; }


thanks!
0
- ago
#32
I think this topic has grown too large with different questions appended to "QS: BarHistory.Add method". Let me suggest you start a separate discussion per distinct request.
0
- ago
#33
Yes, I agree with you. I do so because there is no discussion here about the version I am using (QS). So I do not want to add information that is not relevant to the public .. but whatever you say, OK.
0

Reply

Bookmark

Sort