The mouse click’s price level and keyboard key used during the click can be retrieved in PowerLanguage. With that a trading strategy can submit orders based on manual mouse clicks, which gives possibilities like calculating the position size or custom trailing stop logic. To start, how can we submit market orders with mouse clicks?

In this article:

Mouse clicks and submitting trading orders in MultiCharts

Mouse click processing requires that the ProcessMouseEvents attribute is enabled (MultiCharts Wiki, 2012a). Each mouse click on a data series’ chart area will then update the mouse click keywords, such as MouseClickPrice for the click’s price level.

Trading strategy orders are submitted by a signal script with four PowerLanguage keywords. Buy and Sell open and close a long position, respectively (MultiCharts Wiki, 2012b, 2012c). And SellShort initiates a short position while BuyToCover closes it (MultiCharts Wiki, 2012d, 2012e).

Note that mouse clicks can only submit trading orders with real-time or Playback data — manually submitting orders during a strategy’s backtest isn’t possible.

Submitting orders with mouse clicks in PowerLanguage

Those keywords can be combined with mouse clicks with the following result:

Example of trading orders in MultiCharts

To have the code example (discussed below) perform similar as displayed here, two options need to be set.

Multiple entries in the same direction is set with ‘Allow up to x entry orders in the same direction as the currently held position’. This setting is found in the ‘Strategy Properties’ window (right-click on the chart, select ‘Format Signals’, and then click on the ‘Properties’ button):

Enable the 'multiple entries' setting in MultiCharts

Executing multiple orders during the same bar is set by the strategy’s intra-bar order generation settings: right-click on the chart and choose ‘Format Signals’. Then select the strategy, click on ‘Format’, and move to the ‘Properties’ tab:

Specifying the 'intra-bar order generation' setting in MultiCharts

By the way, a trading strategy can only generate orders on a 1 tick time frame when the AllowSendOrdersAlways attribute is set to true (MultiCharts Support, personal communication, April 10, 2015). That means [AllowSendOrdersAlways = true]; needs to be added to your strategy when replicating this article’s example on a 1 tick chart. For other time frames, like the 3 minute chart used in this article, we don’t need to add this attribute to the script.

Trading in MultiCharts with PowerLanguage’s mouse clicks

Programmatically sending orders with mouse clicks can be implemented as follows:


[ProcessMouseEvents = true];
[IntrabarOrderGeneration = true];

// Open a position with a mouse click with Control
if (MouseClickCtrlPressed and not MouseClickShiftPressed) then begin

    if (MouseClickPrice > High) then 
        SellShort ("ES click") 1 contract next bar at market

    else if (MouseClickPrice < Low) then 
        Buy ("EL click") 1 contract next bar at market;

end;

// Close an open position with a mouse click + Control + Shift
if (MouseClickCtrlPressed and MouseClickShiftPressed) then begin

    if (MarketPosition(0) = 1) then 
        Sell ("XL click") all contracts next bar at market

    else if (MarketPosition(0) = -1) then 
        BuyToCover ("XS click") all contracts next bar at market;   

end;

We begin with enabling ProcessMouseEvents so that mouse clicks can be processed. Then another attribute, IntrabarOrderGeneration, is also set to true. This enables intra-bar order generation (MultiCharts Wiki, 2012f), which means the script can then also generate orders inside a bar, instead of just on bar close (MultiCharts Wiki, 2015).

Open positions with a mouse click + Control in MultiCharts

Next an if statement checks whether the Control key was pressed during the click (then MouseClickCtrlPressed returns true) but without Shift (not MouseClickShiftPressed). That way the code inside the if statement isn’t executed for clicks that happen with Control + Shift.

Inside that if statement is an if-else statement, which starts with evaluating if the click’s price level (MouseClickPrice) is above the current bar’s high (High). When that’s true, a short order (SellShort) is submitted with a position size of 1 (1 contract). This market order (at market) is submitted on the next tick (next bar). Since we’ve enabled intra-bar order generation, orders which are marked next bar are actually send on the next tick (MultiCharts Wiki, 2012f).

The else part of the if-else statement checks whether the click’s price value is below the current bar’s low (MouseClickPrice < Low). When that is the case, a buy order (Buy) is submitted that has the same features as the SellShort order. So with this if-else statement, any click that’s above the current bar opens a short, while clicks below it open long positions.

Close open positions with click + Control and Shift

The other part of the example is code that closes an open position. For this, an if statement evaluates whether both the Control and Shift key were used with the click.

When those keys were pressed, an if-else statement is executed that evaluates the MarketPosition() keyword. This keyword returns a value that, when used with the 0 parameter, indicates the strategy’s current position: 0 for flat, 1 means long, and -1 indicates short (MultiCharts Wiki, 2012g).

When MarketPosition(0) says there is a long position, the Sell keyword closes the whole position (all contracts) with a market order (at market) on the next tick (next bar). With an open short position, the if-else statement executes the BuyToCover keyword to go flat with an order similar as the Sell keyword used.

Summary

After setting ProcessMouseEvents to true, mouse click keywords like MouseClickPrice and keyboard keys (MouseClickCtrlPressed and MouseClickShiftPressed) can submit trading strategy orders with Buy, Sell, SellShort, and BuyToCover. When IntrabarOrderGeneration is turned on, next bar orders are actually send for the next tick.


References

MultiCharts Wiki (2012a, February 24). ProcessMouseEvents. Retrieved on January 11, 2015, from http://www.multicharts.com/trading-software/index.php/ProcessMouseEvents

MultiCharts Wiki (2012b, February 19). Buy. Retrieved on January 20, 2015, from http://www.multicharts.com/trading-software/index.php/Buy

MultiCharts Wiki (2012c, February 19). Sell. Retrieved on January 20, 2015, from http://www.multicharts.com/trading-software/index.php/Sell

MultiCharts Wiki (2012d, February 19). SellShort. Retrieved on January 20, 2015, from http://www.multicharts.com/trading-software/index.php/SellShort

MultiCharts Wiki (2012e, February 19). BuyToCover. Retrieved on January 20, 2015, from http://www.multicharts.com/trading-software/index.php/BuyToCover

MultiCharts Wiki (2012f, August 31). IntraBarOrderGeneration. Retrieved on January 20, 2015, from http://www.multicharts.com/trading-software/index.php/IntraBarOrderGeneration

MultiCharts Wiki (2012g, February 14). MarketPosition. Retrieved on January 20, 2015, from http://www.multicharts.com/trading-software/index.php/MarketPosition

MultiCharts Wiki (2015, January 8). How Signals are Calculated. Retrieved on January 20, 2015, from http://www.multicharts.com/trading-software/index.php/How_Signals_are_Calculated