- ago
I have a daily strategy that can invest multiple positions and runs across a group of stocks.

If I use the strategy monitor, when signals are triggered buy stock A, B and C, how do I then track sell signals for those particular stocks?

Currently I'm creating a chart and dragging the strategy on each day.

I cant use the IBKR live positions sync feature.

Thanks
0
253
3 Replies

Reply

Bookmark

Sort
Cone8
 ( 4.98% )
- ago
#1
QUOTE:
I cant use the IBKR live positions sync feature.
I'll assume that it's because your strategy isn't compatible with "Use Live Positions". Is that it?

If that's the case, you can get the broker position(s) yourself when the strategy initializes. Then if the symbol is "Live", you can trigger the exit logic directly, but you'll need to assign a Transaction.Quantity for the exit signal.

Here's something to get you started. If you're exiting partial shares at different points, then there's more work for you to do.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript5 { public class MyStrategy : UserStrategyBase {       const string _myAccountId = "YOUR_ACCOUNT_ID_HERE";       BrokerPosition _bp;        public override void Initialize(BarHistory bars) {          _bp = GetBrokerPosition(_myAccountId, bars.Symbol, PositionType.Long);           }       public override void Execute(BarHistory bars, int idx)       {          if (_bp is null && !HasOpenPosition(bars, PositionType.Long)) {             // Entry logic }          else          {             // Exit logic             if (bars.Close.CrossesOver(bars.High[idx - 1], idx))             {                Transaction t = PlaceTrade(bars, TransactionType.Sell, OrderType.Market);                t.Quantity = _bp.Quantity;   // sell as much or little as you want here             } } }       //this method looks through all of the installed brokers, for the specified account       //and returns the live broker position for the specified symbol.       private BrokerPosition GetBrokerPosition(string acct, string symbol, PositionType pt)       {          foreach (BrokerBase broker in SignalManager.Brokers)          {             BrokerAccount ba = broker.FindAccount(acct);             if (ba != null)                return ba.FindPosition(symbol, pt);          }          return null;       }       private BrokerAccount GetBrokerAccount(string acct)       {          foreach (BrokerBase broker in SignalManager.Brokers)          {             BrokerAccount ba = broker.FindAccount(acct);             if (ba != null)                return ba;          }          return null;       }    } }
1
- ago
#2
Hi Cone, its because Ive worked in different countries and pensions providers dont provide the ability to integrate with anything.
If I read the code its looking for a broker to get a position - I could get this information from a file which I maintain by hand?
0
Cone8
 ( 4.98% )
- ago
#3
Of course.
0

Reply

Bookmark

Sort