- ago
How to use method: foundPosition0.members with condition if - statements
This is not working: if (foundPosition0.Quantity < 2 ) {}

CODE:
   public override void Execute(BarHistory bars, int idx)       {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);              if (foundPosition0.Quantity < 2 ) { }

1
698
Solved
6 Replies

Reply

Bookmark

Sort
Cone8
 ( 4.98% )
- ago
#1
Objects, like the Position object, can be null. When an object is null you can't reference its properties or methods, so you have to guard against that if you suspect that it could be null, like when FindOpenPosition can't find an open position.

Either of these should work fine -
QUOTE:
// this
if (foundPosition0 != null && foundPosition0.Quantity < 2)

// or shorthand
if (foundPosition0?.Quantity < 2)


3
Best Answer
- ago
#2
Thanks Cone!
Compiler is showing the same error for the following code. Do you know a solution ?

CODE:
Position foundPosition0 = FindOpenPosition(0); double entryPrice;          foundPosition0.EntryPrice = entryPrice; double profitTargetPosition1 = 2; _transaction.AutoProfitTargetPrice = foundPosition0.EntryPrice + profitTargetPosition1 ;
0
- ago
#3
CODE:
Position foundPosition0 = FindOpenPosition(0); if(foundPosition0 != null) { double entryPrice; ... }
1
- ago
#4
Crash - my suggestion is for you to increase your knowledge of the C# language and development in general. Then, you may easily recognize solutions to the problems you noted.
1
- ago
#5
// foundPosition0.EntryPrice
These lines are not working. Method foundPosition0.EntryPrice is returning null and can not use it. Is there a solution to fix it



CODE:
//These lines are not working: //transaction.AutoProfitTargetPrice = foundPosition0.EntryPrice + 0.02; //transaction.AutoProfitTargetPrice = (foundPosition0.EntryPrice - 0.03 * (foundPosition0.Bars.SymbolInfo?.PointValue ?? 1)) ; //   _transaction = PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, bars.AveragePriceOHLC[idx] + 0.03, 0, "Place Sell Limit (1)"); using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript62 {    public class MyStrategy : UserStrategyBase    {       public MyStrategy() : base()       {       }       public override void Initialize(BarHistory bars)       {          StartIndex = 0;       }       public override void Execute(BarHistory bars, int idx)       {          int index = idx;          foundPosition0 = FindOpenPosition(0);          bool condition0;          val = bars.AveragePriceOHLC[idx];          if (foundPosition0 == null)          {             _transaction = PlaceTrade(bars, TransactionType.Short, OrderType.Limit, bars.AveragePriceOHLC[idx], "Short At Limit (1)");             //_transaction.AutoProfitTargetPrice = (foundPosition0.EntryPrice - 0.03 * (foundPosition0.Bars.SymbolInfo?.PointValue ?? 1)) ;                     // _transaction = PlaceTrade(bars, TransactionType.Cover, OrderType.Limit, (foundPosition0.EntryPrice - 0.03 * (foundPosition0.Bars.SymbolInfo?.PointValue ?? 1)), "Cover Place At Limit (1)");           // ClosePosition(foundPosition0, OrderType.Limit, bars.AveragePriceOHLC[idx] - 0.04, "Close Pos at 0.03 profit target");                ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice - 0.03 * (foundPosition0.Bars.SymbolInfo?.PointValue ?? 1), "Cover at 0.03 profit target");          }          if (foundPosition0 == null)          {             _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, bars.AveragePriceOHLC[idx], 0, "Long At Limit (1)");          //   _transaction.AutoProfitTargetPrice = foundPosition0.EntryPrice + 0.02;          //   _transaction = PlaceTrade(bars, TransactionType.Sell, OrderType.Limit, bars.AveragePriceOHLC[idx] + 0.03, 0, "Place Sell Limit (1)");          //   ClosePosition(foundPosition0, OrderType.Limit, bars.AveragePriceOHLC[idx] + 0.04, "Close Pos 0.03 profit target");                ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice + 0.03 * (foundPosition0.Bars.SymbolInfo?.PointValue ?? 1), "Cover at 0.03 profit target");          }       }          public override void NewWFOInterval(BarHistory bars)       {       }       Position foundPosition0;       private IndicatorBase indicator;       private double entryPrice;       private double value;       private double val;       private Transaction _transaction;    } }



0
- ago
#6
I believe the solution is to do what I suggested - increase your knowledge of C# and development. The solution to these types of questions are fairly obvious to a developer with just some experience. You do understand that if foundPosition0 is null then that's it - its null. You never requery for its value. Its not going to magically have a value in it if just because you called PlaceTrade. To me, that shows it would be advantageous to you to improve your knowledge of C# and development.
2

Reply

Bookmark

Sort