- ago
Result is to be able to store and access any position data. Does anyone know how to solve this error problem about the index ?

CODE:
Transaction[] _transaction3 = new Transaction[idx]; Position[] _foundposition3 = new Position[idx]; _foundposition3[idx] = FindOpenPosition(4);


1
842
Solved
17 Replies

Reply

Bookmark

Sort
- ago
#1
The following code doesn't make sense to me.
CODE:
Position[] _foundposition3 = new Position[idx]; _foundposition3[idx] = FindOpenPosition(4);
The first line defines an array, _foundposition3, with only one element. The second line tries to store another element in this one element array when this array is only allowed to have one element.

What's the goal here? How many elements do you want _foundposition3 to have in the first place? Perhaps you want something like ... instead. Now you have an array with bars.Count elements (assuming that's your original intention).
CODE:
Position[] _foundposition3 = new Position[bars.Count];
Does _foundporition3 even need to be an array (vector) in the first place, or would a simple Position scaler work?
1
- ago
#2
@Crash - my suggestion is to improve your knowledge of C# so that you won't waste time writing code that, for lack of a better way of putting it, makes no sense. Without improving your knowledge, you'll probably burn a lot of time chasing errors and problems. That time would probably be better spent improving your C# knowledge. Do yourself a favor and take the time.
2
- ago
#3
There's really a deeper mystery here. Look at the line
CODE:
_foundposition3[idx] = FindOpenPosition(4);
This suggests a position is taken for every bar of the stock since idx is the index. But in truth, there's probably only one position taken for every 200 or 300 bars of the stock, so this doesn't look right either.

It's my "guess" that _foundposition3 is meant to be a scaler rather than a vector (array), but that's only a guess because we don't have enough information to know for sure. If that's indeed the case, then the code should look like this:
CODE:
Position _foundposition3; _foundposition3 = FindOpenPosition(4);
1
- ago
#4
Still trying to resolve this problem. Feedback/comments is appreciated.

@Paul, yes agree thanks!

@Superticker
Thanks for the comments! Yes you are correct, a position should be taken for every bar. See the full code below.


CODE:
/* Problem: For every new bar formed, the trading bot will open a new position[idx] with new order transactions[idx]. Therefore, The bot will be able to access any trading position and its order transactions such as Limit Entry Orders and Exit Orders. */ using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript3 { public class MyStrategy : UserStrategyBase { Position foundPosition0; Position foundPosition1; private IndicatorBase indicator; private double entryPrice; private double value; private double val; //private Transaction _transaction; //private Transaction _transaction1;     public MyStrategy() : base() { } public override void Initialize(BarHistory bars) { indicator = new MACDHist(bars.Close, 3, 7, 3); PlotIndicator(indicator, new WLColor(0, 0, 0)); indicator.MassageColors = true; PlotStopsAndLimits(3); PlotStopsAndLimits(3); StartIndex = 0; foundPosition0 = null; foundPosition1 = null; } public override void NewWFOInterval(BarHistory bars) { indicator = new MACDHist(bars.Close, 3, 7, 3); } public override void Execute(BarHistory bars, int idx) { int index = idx;     bool condition0; //val = bars.High[idx]; Transaction[] _transaction = new Transaction[50000]; Position[] _foundposition = new Position[50000];    _foundposition[idx] = FindOpenPosition(idx);    _transaction[idx] = _transaction[idx];       if(_foundposition[idx]==null){    _transaction[idx] = PlaceTrade(bars, TransactionType.Short, OrderType.Limit, bars.High[idx], idx, "Buy Limit"); _transaction[idx].AutoProfitTargetPrice = bars.High[idx] - 0.06; _transaction[idx] = PlaceTrade(bars, TransactionType.Cover, OrderType.Limit, bars.High[idx] - 0.06); ClosePosition(_foundposition[idx], OrderType.Limit, bars.High[idx] - 0.06); }                                          int n=idx;    n=n-1;          if(_foundposition[n] != null) { _transaction[n].AutoProfitTargetPrice = _foundposition[n].BasisPrice - 0.06; _transaction[n] = PlaceTrade(bars, TransactionType.Cover, OrderType.Limit, _foundposition[n].BasisPrice - 0.06); ClosePosition(_foundposition[n], OrderType.Limit, _foundposition[n].BasisPrice - 0.06 );                 }        n=n-1;          if(_foundposition[n] != null) { _transaction[n].AutoProfitTargetPrice = _foundposition[n].BasisPrice - 0.06; _transaction[n] = PlaceTrade(bars, TransactionType.Cover, OrderType.Limit, _foundposition[n].BasisPrice - 0.06); ClosePosition(_foundposition[n], OrderType.Limit, _foundposition[n].BasisPrice - 0.06 );                }            //     if (_foundposition[idx] != null) {    ClosePosition(_foundposition[idx], OrderType.Limit, _foundposition[n].BasisPrice - 0.06 ); _transaction[idx] = PlaceTrade(bars, TransactionType.Cover, OrderType.Limit, foundPosition0.BasisPrice - 0.06); _transaction[idx].AutoProfitTargetPrice = foundPosition0.BasisPrice - 0.06; }            _foundposition[idx] = FindOpenPosition(idx);     } } }





1
- ago
#5
QUOTE:
For every new bar formed, the trading bot will open a new position[idx] with new order transactions[idx].... Therefore, The bot will be able to access any trading position

The whole point of the backtest is to *selectively* pick (and Buy) positions that that might be deemed profitable. And most bars will not lead to profit, so this strategy (of buying on every bar) is doomed to failure by design.

I would pick a sample strategy that's operational from the start, then try to improve it via modifications.
1
- ago
#6
@ WL Team
It should be possible to use an array properties to create new position for every bar. The debugging is returning null. Need to use array for position and transaction. How would you solve this problem below ?

CODE:
/**How would you use array for position and transaction such as: Transaction[] _transaction = new Transaction[50000]; Position[] _foundposition = new Position[50000];    WriteToDebugLog(_foundposition[idx]); This is returning an error **/ using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript3 { public class MyStrategy : UserStrategyBase { Position foundPosition0; Position foundPosition1; private IndicatorBase indicator; private double entryPrice; private double value; private double val; //private Transaction _transaction; //private Transaction _transaction1; Transaction[] _transaction = new Transaction[50000]; Position[] _foundposition = new Position[50000];    public MyStrategy() : base() { } public override void Initialize(BarHistory bars) { indicator = new MACDHist(bars.Close, 3, 7, 3); PlotIndicator(indicator, new WLColor(0, 0, 0)); indicator.MassageColors = true; PlotStopsAndLimits(3); PlotStopsAndLimits(3); StartIndex = 0; //foundPosition0 = null; //foundPosition1 = null; _foundposition[0] =null; //_foundposition = null;          } public override void NewWFOInterval(BarHistory bars) { indicator = new MACDHist(bars.Close, 3, 7, 3); } public override void Execute(BarHistory bars, int idx) { int index = idx; //_foundposition[idx] =null; bool condition0; //val = bars.High[idx]; //Transaction[] _transaction = new Transaction[50000]; //Position[] _foundposition = new Position[50000]; _foundposition[idx] = FindOpenPosition(idx); _transaction[idx] = _transaction[idx];       if(_foundposition[idx]==null){    _transaction[idx] = PlaceTrade(bars, TransactionType.Short, OrderType.Limit, bars.High[idx], idx, "Buy Limit"); _transaction[idx].AutoProfitTargetPrice = bars.High[idx] - 0.06; _transaction[idx] = PlaceTrade(bars, TransactionType.Cover, OrderType.Limit, bars.High[idx] - 0.06); ClosePosition(_foundposition[idx], OrderType.Limit, bars.High[idx] - 0.06); } if (_foundposition[idx] != null) { _transaction[idx].AutoProfitTargetPrice = _foundposition[idx].EntryPrice - 0.06; _transaction[idx] = PlaceTrade(bars, TransactionType.Cover, OrderType.Limit, _foundposition[idx].EntryPrice - 0.06); ClosePosition(_foundposition[idx], OrderType.Limit, _foundposition[idx].EntryPrice - 0.06); } _foundposition[idx] = FindOpenPosition(idx); } } }
0
- ago
#7
These 50000 item arrays look pretty questionable as well as the call to ClosePosition on an object that is null and can still be null i.e. _foundposition[idx]. In other words, what problem are you trying to solve? What are the trading system's rules in plain English and why not attempt to build it using Blocks?
2
Glitch8
 ( 10.94% )
- ago
#8
Yes Crash. The “problem” here is really we have no idea what you’re trying to accomplish.
2
- ago
#10
Sorry for confusion. More clarification: I need to use arrays to store each trading positions in the same strategy to allow to control, track, close and manage each position.

Problem: The bot/staretgy will enter a new position in every new bar. The bot will access each position: position 1, position 2, position 3,...

This requires using arrays for position and transaction. How would you use the following to store and access position members or if you know better way to define it:
Transaction[] _transaction = new Transaction[];
Position[] _foundposition = new Position[];

For example: the strategy will store and will be able to access each position data
_foundposition[idx] // idx=0 bar 1
_foundposition[idx] // idx=1 bar 2
_foundposition[idx] // idx=2 bar 3
0
Cone8
 ( 24.99% )
- ago
#11
First, the way you're trying to do that is error prone because you have to give arrays dimensions and every Transaction won't result in a Position. A better way would be to use a Dictionary<int, Position>.

Second, you shouldn't do what you're trying to do because the Backtester already stores all Positions and Transactions in lists that you can retrieve at any time using GetPositions() and TransactionLog, respectively. Look them up in the QuickRef:
- GetPositions() is a method in UserStrategyBase.
- TransactionLog is a Bactester class property.
2
Best Answer
- ago
#12
Question:

The following array of positions is dynamically defined and some positions are not filled and are open. However, it is not able to reference previous positions. WL has the capability to store previous positions, It seems that this type of code is lacking, not storing the previous positions 4,5,6. For example: Array of positions is defined at 11:30 AM, compiler executes again at 11:31 AM, what happened to the previous array positions: deleted/stored ? The array size is re-created for every time the strategy is executing and therefore Index was outside of the array bounds. Is this correct ?


Position[] _foundposition = new Position[idx];
_foundposition[idx], idx =4
_foundposition[idx], idx =5
_foundposition[idx], idx =6

n=idx-1;
if (_foundposition[n] != null ) // Error = Object reference not set to an instance of an object.


CODE:
Question: Position[] _foundposition = new Position[idx];    _foundposition[idx], idx =4 _foundposition[idx], idx =5 _foundposition[idx], idx =6 n=idx-1; if (_foundposition[n] != null ) // Error = Object reference not set to an instance of an object.


1
- ago
#13
Don't waste time with the arrays, it'd be better if you study Cone's response for the right approach.
2
Cone8
 ( 24.99% )
- ago
#14
Right. Every time the strategy runs, everything starts "at zero / null". There is no memory of what occurred before - unless you persist it in global memory or disk file.
0
- ago
#15
I agree with Reply# 11, use GetPositions() to collect the positions you want to look at; therefore, you don't need the _foundposition[] because GetPositions collects all that automatically. Just understand that the Positions object won't be "fully" populated with all the Positions until after the Execute{} block has completed. So you need to be doing a GetPositions() call in Cleanup{} or the procedure following it. If you lookup GetPositions in the QuickRef, there's a button to create an example using it. Check out that example.

Feel free to correct me if any misinformation is posted.
0
Glitch8
 ( 10.94% )
- ago
#16
superticker please stop posting misinformation here. you certainly can use WL to develop trading bots, given our Portfolio Sync features.

As far as ThinkOrSwim, please do your research before posting a claim. This is a direct quote from the ThinkOrSwim user guide;

“here is an important notice about the strategies: all the signals you get are hypothetical, i.e., you cannot send real orders using strategies.”
1
Cone8
 ( 24.99% )
- ago
#17
QUOTE:
a backtesting platform such as Wealth-Lab, which only looks at past/historical data.
If there's a platform that has access to future data, please let me in on it!!!
0

Reply

Bookmark

Sort