How to force placing an order if Strategy has an active position?
Author: fkhalili
Creation Date: 11/20/2016 8:01 PM
profile picture


Assuming that my Strategy allows Placing a Market Order from the moment Fast EMA crosses Slow EMA any time until Stochastic Oscillator is below Over Bought region. This implies that when I Activate the Strategy anytime from the point of EMA crossing until the moment the Stochastic Oscillator is below Over Brought region, I would like the Strategy to Trigger and place an order.

In reality, using WLP strategy monitor, since I use several days of historical data (using Data Range) to make sure my EMA and Stochastic Oscillators are primed, I have discovered that if the strategy has already triggered an order while processing the Historical data, it would not trigger the order when it begins the Streaming data.

Currently I am forced to manually enter into the first position if the Strategy has entered into the position while processing the Historical data.

Is there a mechanism/framework to avoid the strategy to buy into a position while processing the historical data (while it is being used to prime technical indicators), and enable the strategy to trigger the buy as soon as the Streaming Data kicks in?

profile picture


You could condition the system to start processing after some date by modifying the start of the trading loop. Check out ConvertDateToBar in the QuickRef.
profile picture


Eugene probably understood what you're asking and already answered the question, but the expression "as soon as the Streaming Data kicks in" makes me think that you're not understanding something about how alerts are generated. Why? Because streaming data has nothing to do with generating a strategy alert.

Streaming (tick) data is used...

1. In the Quotes tool for triggering stop and limit orders when prices reaches (or approaches) the stop/limit trigger price, and,

2. In the Strategy Window for building bars. With the exception of applying the "Bad Tick Filter" (See Preferences (F12) > Streaming Data), streaming data in a chart will create the same bars as the historical provider - but you must wait until the end of the interval for the bar to "enter" the chart. When that happens, the strategy is executed and alerts can be generated using that bar.

To say it another way, you could turn off streaming and turn on "on-demand data". If you refresh the chart at the moment the chart interval ends, you would get precisely the same result as using a streaming chart. Streaming does this for you automatically, and without delay.

Consequently, if trading an instrument in the Strategy Monitor is out of synch with a Streaming Strategy Window, it's because you're not using the same settings in both of them. Simple as that.
profile picture


Thank you both for responding so quickly.

After reviewing Eugene's recommendation and after realizing that the Execute() method is first called with Bars.Count reflecting all the historical bars up to the point that I Activate the Strategy in the Strategy Monitor, I decided to save this count and skip any trade execution until after this candle bar. I had to make sure that the Bars.Count is saved only and only once when the first Execute() method is called. Apparently, the same Execute() method is called again for each new bar when a new Candle Bar is completed in real time as per selected "Scale".

I was able to successfully test my strategy with this technique using "Open Strategy" followed by enabling the "Streaming". I could see in the Chart that if all the conditions were met as per my strategy, I will enter into a position right when I am processing the first Bar immediately after historical bars. I noticed a discrepancy associated with "LastPosition" class data element, but I would like to ignore it for the time being since the exact same strategy did not work as expected in the "Strategy Monitor" mode.

When I ran the modified strategy that successfully operated in "Open Strategy" and "Streaming" mode in "Strategy Monitor", I discovered that the strategy correctly enters into the position after the first realtime "Streaming" tick. However, it continues entering into another position on every subsequent streaming tick.

I can not explain why the code that operates properly in simulation mode, does not work correctly in Strategy Monitor mode.

Since my strategy is structured as:

Please log in to see this code.

my only hypothesis is that "IsLastPositionActive" for some reason is not set when the subsequent Execute() command is run despite the fact that we executed a trade in the last Tick.

Unfortunately, I am not aware of any Debugging mechanism to Print out the logs while the strategy is running in the "Strategy Monitor" to debug this further. Any help would be greatly appreciated.