4.6 Strategies

From MultiCharts
Revision as of 19:35, 29 April 2013 by Roman MultiCharts (talk | contribs) (Created page with "==Orders== Order objects can be created and generated only by signals. All orders inherit from the basic interface. <syntaxhighlight> public interface IOrderObject { int I...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Orders

Order objects can be created and generated only by signals. All orders inherit from the basic interface.

public interface IOrderObject
	{
		int ID { get; } //unique order number
		Order Info { get; }//Basic order information (direction, type, category etc.)
	}

There are three types of orders:

1) Market orders executed at current market price.

	public interface IOrderMarket : IOrderObject
	{
		void Send(); 
		void Send(int numLots);
	}

2) Price orders executed when market price touches or crosses order price level.

	
        public interface IOrderPriced : IOrderObject
	{
		void Send(double price);
		void Send(double price, int numLots);
	}

3) Algorithmic Stop-Limit orders executed as price Limit orders, but only after market price touches or crosses the stop price.

        public interface IOrderStopLimit : IOrderObject
	{
		void Send(double stopPrice, double limitPrice);
		void Send(double stopPrice, double limitPrice, int numLots);
	}

Order objects are created only in Create() method using OrderCreator:

1) IOrderMarket OrderCreator.MarketNextBar(SOrderParameters orderParams); - creates a Market order that should be sent at the Open of the next bar, the one after the bar where the order was generated. 2) IOrderMarket OrderCreator.MarketThisBar(SOrderParameters orderParams); - creates a Market order that should be sent on the Close of the bar where it was generated. 3) IOrderPriced OrderCreator.Limit(SOrderParameters orderParams); - creates a price Limit order that should be sent at the Open of the next bar, the one after the bar where the order was generated. A Limit order can be filled at the price set, or better, in Send() method when generating the order where buy equals order price and lower and sell equals order price and higher. 4) IOrderPriced OrderCreator.Stop(SOrderParameters orderParams); - creates a price Stop order that should be sent at the Open of the next bar, the one after the bar where the order was generated. Stop order can be filled at the price set, or worse, in Send () when generating the order buy equals order price and lower and sell equals order price and higher. 5) IOrderStopLimit OrderCreator.StopLimit(SOrderParameters orderParams); - creates algorithmic Limit order with a Stop condition that should be sent at the Open of the next bar, the one after the bar where the order was generated.

The following structure is used to create an order object in all the OrderCreator() methods:

SOrderParameters:
SOrderParameters(EOrderAction action);
SOrderParameters(Contracts lots, EOrderAction action);
SOrderParameters(EOrderAction action, string name);
SOrderParameters(Contracts lots, EOrderAction action, OrderExit exitInfo);
SOrderParameters(Contracts lots, string name, EOrderAction action);
SOrderParameters(Contracts lots, string name, EOrderAction action, OrderExit exitInfo);

Settings:

  • EOrderAction:

Buy – long position entry (buying). If the short position is currently opened, then it will be closed and the long one will be opened.

Sell – long position exit (selling). This order cannot be sent, if the long position is not opened. Also, this position cannot be reversed into short.

SellShort – short position entry (selling). If the long position is currently opened, then it will be closed, and the short one will be opened.

BuyToCover – short position exit (buying). This order cannot be sent, if the short position is not opened.

  • Contracts:

Default – the quantity of contracts is calculated, according to the strategy settings in Strategy Properties window.

CreateUserSpecified(int num) – the default quantity of contracts for the order is set in the method settings.

name – Custom name of the order.

  • OrderExit:

FromAll – closes all entries, which opened the position. If the quantity of contracts is specified, then close each entry partially on the quantity of contracts specified.

Total – closes the position on ONLY the specified quantity of contracts. The entries will be closed accordingly, starting from the first one, up to the last one, until the exit is executed upon the quantity of contracts specified.

FromEntry(IOrderObject entry) – closes the particular entry, in the entry setting, created earlier, in the same signal.

Special Orders

The signal can generate special exit order types, besides price and market orders such as: ProfitTarget, StopLoss, BreakEven, DollarTrailing, PercentTrailing, ExitOnClose.

Special order types can work in one of two modes (if the mode is set in several signals, then the mode that was set last is used for all special orders):

CurSpecOrdersMode = PerContract – special exits are attached to each entry that opened the position. Average entry order fill price is used to calculate the special order.The Amount should be specified in currency per one contract in the special order settings. CurSpecOrdersMode = PerPosition – one exit is generated per position which closes it completely. The average open position price is used for the calculation of special order prices. In the special order settings, the Amount should be specified in currency per entire position.

NOTE: Amount is one of the parameters used for special orders generation. It sets potential profit value after reaching which the special order becomes active.

Commands to generate special order types are:


  • GenerateExitOnClose() – to generate a market order which closes the current position upon the session close. Please note, that during automated trading, this order may not be executed, because at time the order is sent the trading session may already be closed and orders may not be accepted. To use this type of order in automated trading correctly, it is recommended to set the sessions so they end, for example, a minute before the exchange trading session closes.
  • GenerateProfitTarget(double amount) – to generate a Limit order with a price where the profit (excluding commission) from a position (CurSpecOrdersMode = PerPosition) or from a contract for each entry (CurSpecOrdersMode = PerContract), should not be lower than the amount set in the Amount parameter.

Example:

The long position was opened at price 12,25 for 100 contracts. BigPointValue=10, MinMove=25, PriceScale=0,01.

Signal code:

CurSpecOrdersMode = ESpecOrdersMode.PerPosition;
GenerateProfitTarget(5);

Sell Limit order will be generated at price = EntryPrice + (Ammount/Contracts)/ BigPointValue = 12,25 + 5/100/10 = 12,255;

Because the price does not correspond to the minimum price increment, it will be rounded to BETTER price, according to the step MinMove* PriceScale = 25*0,01 = 0,25 до 12,50. The final ProfitTarget order: Sell 100 contracts at 12,50 Limit;

  • GenerateStopLoss(double amount) – to generate a Stop order with the price to set the loss (excluding commission) from a position (CurSpecOrdersMode = PerPosition) or a contract for each entry (CurSpecOrdersMode = PerContract) specified in the Amount parameter.

Example:

The symbol has the following parameters: BigPointValue=100, MinMove=2, PriceScale=0,01. Assume the short position is opened at the average price 0,982 for 10000 contracts by the two orders SellShort1 1000 at 1,00 и SellShort2 9000 at 0,98;

Signal code:

CurSpecOrdersMode = ESpecOrdersMode.PerContract;
GenerateStopLoss(5);

GenerateStopLoss(5) in PerContract mode will generate 2 Stop orders (one exit per each entry).

StopLoss order price for the first entry = EntyPrice + Ammount / BigPointValue = 1,00 + 5/100 = 1,00 + 0,05 = 1,05; considering the price step (0,02) is rounded to the WORSE = 1,06. StopLoss order price for the second entry = EntyPrice + Ammount / BigPointValue = 0,98 + 5/100 = 0,98 + 0,05 = 1,03; considering the price step (0,02) is rounded to the WORSE = 1,04.

Final sending orders: BuyToCover 1000 contracts from SellShort1 at 1,06 Stop; BuyToCover 9000 contracts from SellShort1 at 1,04 Stop;

GenerateBreakEven(double amount) – to generate a stop order at the break-even level (calculated excluding commission) after achieving profit per position (in PerPosition mode) or per contract (PerContract), including commission. Break-even level is calculated excluding the commission and is equal to the execution of the entry order in PerContract mode or the average open price of the position in PerPosition mode. In PerPosition mode, the stop order sending condition is: - Positions[0].MaxRunUp >= amount; In PerContract mode, the stop order sending condition for the entry is: - TradeRunUp >= amount;

GenerateDollarTrailing(double amount) – to generate a trailing stop order with the price to close the position (RepPosition/entry(PerContract), when the potential profit decreases from the maximum value by a particular amount.

Stop order(s) price calculation is executed tick by tick.

Example:

The symbol has the following parameters: BigPointValue=1, MinMove=1, PriceScale=0,01. The long position entry was at price 34,14 for 100 contracts. The price after entry has reached maximum MaxProfitPrice = 35,01.

Signal code:

CurSpecOrdersMode = ESpecOrdersMode.PerPosition;
GenerateDollarTrailing(5);

TralingStop price = MaxPositionProfitPrice – amount/contracts/BigPointValue = 35,01 – 5/100/1 = 35,01 – 0,05 = 34,96. After rounding to the minimum price step to the WORST, we get the price 34,96. The final order: Sell 100 contracts at 34,96 Stop.

GeneratePercentTrailing(double amount, double percentage) – to generate a trailing stop order that closes the position (trade) after the position(PerPosition)/trade(PerContract) reaches the profit that is greater or equal to the price set in the amount parameter. The price will close the position/trade when the profit goes down by a percentage% from the maximum value.

Stop order(s) price calculation is executed tick by tick.

Example:

The symbol has the following parameters: BigPointValue=1, MinMove=1, PriceScale=0,01. The long position entries were at price 24,10 for 100 contracts and 24,56 for 50 contracts. Maximum price after the first entry was 24,60, after the second one was 24,58.

Signal code:

CurSpecOrdersMode = ESpecOrdersMode.PerContract;
GeneratePercentTrailing(0.30, 25);

Sending of trailing stop for the first entry possibility is verified:

MaxProfitPerContract = (MaxPrice – EntryPrice)*BigPointValue = (24,60 - 24,10)*1 = 0,50; Yes, the condition for stop sending is executed MaxProfitPerContract >= amount. Let’s count the price:

StopPrice = MaxPrice – (MaxPrice- EntryPrice)*percentage/100 = 24,60 - (24,60 - 24,10)*25/100 = 24,60 – 0,50*0,25 = 24,475. Let’s round the price to the minimum step to the WORST, we get the price = 24,47.

Sending of trailing stop for the first entry possibility is verified: MaxProfitPerContract = (MaxPrice – EntryPrice)*BigPointValue = (24,58 - 24,56)*1 = 0,02; MaxProfitPerContract = (MaxPrice – EntryPrice)*BigPointValue = (24,58 - 24,56)*1 = 0,02; MaxProfitPerContract condition >= amount for this entry was not executed, so the stop order for it will not be sent at this calculation. It will be generated at the moment, when the price is reached = EntryPrice + amount/BigPointValue = 24,56 + 0,30/1 = 24,86.

The final order will be only one: Sell 100 contracts from Entry1 at 24,47 Stop;

CurSpecOrdersMode – affects all special orders in the strategy. The last set value of this marker is always used for price calculations.