CreateOrder() function allows you to open a position at the current market rate. It has 8 parameters:
1. | Instrument: TInstrument– the instrument. |
2. | Account: TAccount – the account. |
3. | Amount: Double– the number of lots. |
4. | BuySell: TBuySell– buy or sell. |
5. | StopRate: Double – the predefined stop order rate (enter 'NullRate' if you do not want to place a stop order). |
6. | LimitRate: Double – the predefined limit order rate (enter 'NullRate' if you do not want to place a limit order). |
7. | TraderRange: Integer – Trader Range. |
8. | OperationTag: String – the custom order tag. |
Custom order tags are used to make it easier to identify and access the individual orders and consequent positions. Note that tags should have unique names. You may want to use a counter for tagging the orders and positions.
Note: In the time of high market volatility, Bid and Ask rates can change very frequently. In such condition it is advisable NOT to set predefined stop and limit too close to the order execution rate, as it may result in a conditional order reject causing the termination of a strategy.
Example: Let us create a strategy that will open a buy position if the difference between High and Low of the last finished candle is greater than 50 points. It should also set a Stop 20 points below the Sell rate, and a Limit 60 points above the Sell rate.
We will use the procedure OnNewCandle, as it runs when a new candle opens. Remember, that you must set this function in the procedure OnCreate().
The script will be as follows:
const StrategyName='My Strategy';
var History: TCandleHistory; Account: TAccount; Amount, Point: Double; Stop, Limit, TraderRange: Integer;
procedure OnCreate; begin AddCandleHistorySetting(@History, 'Candle History', '', CI_1_Minute, 100); History.OnNewCandleEvent := @OnNewCandle; AddAccountSetting(@Account, 'Account', ''); AddFloatSetting(@Amount, 'Amount(Lots)', 5); AddIntegerSetting(@Stop, 'Stop in pips', 20); AddIntegerSetting(@Limit, 'Limit in pips', 60); AddIntegerSetting(@TraderRange, 'Trader Range', 2); end;
procedure OnNewCandle; begin Point := History.Instrument.PointSize; if ((History.Last(1).High-History.Last(1).Low)>50*Point) then CreateOrder(History.Instrument, Account, Amount, bsBuy, History.Instrument.Sell -Stop*Point, History.Instrument.Sell +Limit*Point, TraderRange, 'MyTrade'); end; |
Note: Since Stop and Limit are set in pips, in order to convert them to the same measurement as the rates, we need to use the value of the PointSize property.