I'm trying to code an strategy for automatic trading in multicharts.net platform, which uses powerlanguage coding language.
I have been able to code the conditions for strategy, but I need help with some questions that I am not able to figure out.
When certain conditions are met, my strategy needs to send 3 orders when current bar is closed: entry (long or short), stoploss, and profit target. I have code this and works ok.
The problem begins after that. When a new bar begins, the orders are sent. I need to achieve the following:
- If the entry order is executed, and when this bar ends and a new bar begins, neither the profit target nor the stoploss have been fullfilled, I need to move the stop loss to another level, and profit target has to keep. From them on, both orders have to keep unchanged until one of them executes.
- After the entry order is executed, if profit target or stop loss is executed at any time, in that same moment the remain order has to be cancelled in this moment: I can't wait until the bar closes.
- If the entry order is not executed during the bar in which has been sent, all 3 orders has to be cancelled upon bar close.
How to code this depends on how you've already coded the profit target and stop-loss levels. Because it sounds to me what you're saying here is 'update the stop-loss level' with every bar. That can be done, of course, but how that depends on your strategy's logic.
Managed orders in MultiCharts .NET remain active until they are cancelled.
If you, for example, only submit the profit target and stop-loss order as long as there's an open position, then when the position is closed, the orders aren't submitted anymore (and thus cancelled).
If they have to be cancelled immediately in this moment, and not till the price bar closes, you need to use intra-bar order generation.
You can do this by adding this logic to your entry conditions, for example by storing the bar number in a variable the first time the entry signal is generated. Then on each calculation you can compare how many bars have passed since that original entry signal, and if that's more than one bar (so the order hasn't been filled on the initial entry bar), then stop resubmitting the entry order.
The following user says Thank You to Jura for this post:
I define 6 kinds of orders: One stop buy. One stop shellshort. These two are for entries (long or short). One limit sell (Profit for longs). One limit buytocover (profit for shorts), one stop sell (stop protection for longs) and one stop buytocover (stop protection for shorts.
After stablishing conditions for entry, stop loss and profit, I send an entry order to the market. Lets say a long position:
When the conditions meet, at the start of the bar, ecery order is sent. The entry order becomes active, and the others don't reach the market because those kinds of orders can't exist without an open position.
If the bar finishes and the entry has not been executed, the order is automatically canceled (default behaviour of Multicharts).
If it has been executed, the two others orders reach the market, because the position is open and the orders were sent. If one of them is executed, the position is closed, and so, the other cannot exists anymore, so it is automatically canceled.
If tha bar ends without none of the orders executed, the code continue. I have added a block of code that ask for the position in market, so if the position is opened, the two 'protection' orders are sent again.
I have only another question that I haven't been able to solve. How can I know the execution price or an order? I mean that the script knows that price and store it into a variable. I know I can llok for execution price in the account page.
depending on the automation mode you are using (Sync or Async) you can use the Strategy Performance related reserved words.
When operating in Sync mode the strategy positions on the chart will be updated when the broker reports them as filled and they have matching fill prices.
When operating in Async mode they are independent of the broker prices and can therefore be different. While the strategy performance related reserved words will still return the values as you see them on the chart, accessing the broker fill prices will likely require your code to subscribe to the TradeManager to monitor and track the position changes. You can find a few example in the official Multicharts forum on how to do that, but it's a bit slim and much more complex than using the performance related reserved words directly.
I use Sync mode. I have tried with StrategyInfo.AvgEntryPrice but it returns 0. Maybe if I wait till next bar, it will give me the real value, but when I send an order type market, it gets executed in that moment and I have to know what is the execution price when is executed.