Indicator -> Strategy

I’m sick of OnPositionUpdate in my indicator not triggering when a position changes, so I’ve decided to just go ahead and try converting my Indicator(s) to a Strategy.

My first inclination is to just create an effectively blank Strategy, and then cut-and-paste my Indicator code into the Strategy. I’m sure, however, that that won’t work. And I’m also worried about how the new Strategy will interface with Chart Trader and my trade copier.

The reason I have been using an Indicator and not a Strategy is because the Indicator creates a Chart Trader Order, which is then copied by the trade copier and placed for the sub-accounts. And then, whatever changes I make to the order on Chart Trader are magically effected to the sub-account’s orders. I don’t want to break that.

I have seen people recommending to leave the Indicator alone, and just pass data from it to the Strategy, but the Indicator already creates an order.

		ChartControl.Dispatcher.InvokeAsync((Action)(() =>
		{
			myAccount = ChartControl.OwnerChart.ChartTrader.Account;
			int qty = ChartControl.OwnerChart.ChartTrader.Quantity;
			AtmStrategy atm = ChartControl.OwnerChart.ChartTrader.AtmStrategy;
			Instrument instr = ChartControl.OwnerChart.ChartTrader.Instrument;
			o = myAccount.CreateOrder(instr, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Automated, TimeInForce.Day, qty, 0, oldBoxLow - RRR, "", "Entry", Core.Globals.MaxDate, null);
			NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrategy(atm, o);	
		}));

So, it would seem to me that just making it a Strategy, thereby giving it all of the abilities that Strategies natively have to track account events, would solve my immediate problem. And it would eliminate the complexities of having to pass whatever information out of the Indicator because, not being an Indicator that uses AddPlot, there are different variables required and calculations that are done only when the Indicator’s conditions are met.

Are there any recommendations or suggestions on how to avoid the pitfalls that I am likely to encounter during the process? Any help would be greatly appreciated!

Personally, I would consider an addon with an event driven architecture.

That’s exactly what I have. It doesn’t work, because the events aren’t triggering the function calls.

The problem is that I need to set a variable based on when there are no longer any open positions under myAccount. The problem is that OnPositionUpdate is not being run when a position is exited, so the variable that needs to be reset in order for the algorithm to begin looking for the next entry stays in the state of it still being in a position.

The literature states that you cannot force a call to OnPositionUpdate. And there is no other way to check if a position is open without getting position information, which can only be done through an event trigger. So, since the event triggers aren’t triggering, I’m screwed.

Without seeing the full source code, it’s hard to say whether the event itself is failing or whether the subscription/state logic is the issue. A lot of complex systems are event driven, but from your description it sounds like several layers may be tightly coupled: signal detection, chart trader state, ATM order submission, account-position tracking and internal algo state. Rather than converting the indicator to a strategy just to get access to different callbacks, I would look at separating those responsibilities.

Okay, here’s the code that never gets executed:

	private void OnPositionUpdate(object sender, PositionEventArgs e)
	{
		Print("Position Updated.  Current Position = " + p);
		if (p != null && e.MarketPosition == MarketPosition.Flat)
		{ 
			Print("No open positions.");
			p = "Flat";
		}
		else if (p == null && e.MarketPosition != MarketPosition.Flat)
		{	p = e.Position.ToString(); }
		Print("p Updated.  Current Position = " + p);
				
	}

and…

	lock (myAccount.Positions)
   		{
			Print("Position p = " + p);
       		foreach (Position position in myAccount.Positions)
       		{
           		Print("Actual position = " + String.Format("Instrument: {0}, Position: {1}, Quantity: {2}", 
               		position.Instrument, position.MarketPosition, position.Quantity));
       		}
   		}

p is a string variable that holds either the value of e.Position.ToString() or “Flat,” and is null by default. That first Print in the lock loop runs fine, but the value is always null, even if I am currently in a position. And I never see the print from inside the foreach loop.

My belief was that those variables, position.Instrument, position.MarketPosition and position.Quantity, were of the type that, for some stupid reason, only work when they are inside a Strategy. That is why I felt like I had to migrate over.

If I could figure out why OnPositionUpdate isn’t firing when there is a change in the position, that would solve the problem. The reason this is important is because the Indicator needs to be reset to essentially pre-session state after a successful entry and exit, or it won’t return to scanning for new entries. This means resetting p and o back to null.

Unfortunately, the logic for setting o to null is inside a conditional that tests that p is NOT null. Since it’s staying null, o keeps its non-null value, and the reset can never be completed.

If I could get position information from somewhere else, that could work, too, but e is only set inside OnPositionUpdate, and I don’t know any other way to get position info. I tried OnAccountItemUpdate, but that didn’t trigger, either.

I find strategy restrictive because it is unable/not designed to manage positions that were not entered by it.

Yeah, I’ve pretty much abandoned the Strategy idea, and gone back to working with my Indicators.

Thanks guys!