
There are 5 procedures that you can use to place, modify or delete orders.
SendInstantOrder - this procedure will instantly buy or sell by market price.
SendPendingOrder - this procedure will place pending order (Buy Limit, Buy Stop, Sell Limit, Sell Stop).
ModifyOrder - with this procedure you can modify existing order, change stop loss/take profit, or price for pending order.
CloseOrder - you can close opened position.
DeleteOrder - you can delete pending order.
Now we will modify our skeleton strategy from Lesson 1 (you can use this strategy as a template for your new strategies). Our new strategy will open a market order at 8 a.m. every day with TakeProfit = 50 and StopLoss = 100.
Let's save our project with different name (Save project as ...) DemoStrategy1. And change its description:
Code: Select all
StrategyShortName('8 hour orders');
StrategyDescription('Strategy opens orders at 8 a.m.');
We will also need one external parameter - Currency, to define on which currency strategy should open orders:
Code: Select all
var
Currency: PChar;
...
RegOption('Currency', ot_Currency, Currency);
We will add following lines to GetSingleTick procedure. Here we receive all the price changes.
Code: Select all
procedure GetSingleTick; stdcall;
var
time: TDateTime;
OrderHandle: integer;
begin
if Symbol <> Currency then
exit;
time := iTime(Symbol, PERIOD_M1, 0);
if (HourOf(time) = 8) and (OrdersTotal = 0) then
SendInstantOrder(Symbol, op_Buy, 0.1, Ask - 100*Point, Ask + 50*Point, '', 0, OrderHandle);
end;
First that we do - check if it is our currency changes. Symbol is a function that returns name of current symbol (currency name) that was changed. If it is not our symbol we just exit from GetSingleTick and do nothing.
Then we obtain current time as a time of the last 1 minute bar for our currency and store it to the variable "time".
Then we check if current hour is 8 a.m. with HourOf(time) function (this function and other functions to work with time are located in DateTimeUtils module, and we added it to "uses" declaration) and if there are no other orders placed with OrdersTotal function.
If we meet both conditions, we open new position for buy with SendInstantOrder.
Complete code:
Code: Select all
library DemoStrategy1;
uses
SysUtils, DateUtils, StrategyInterfaceUnit, TechnicalFunctions;
var
Currency: PChar;
procedure InitStrategy; stdcall;
begin
StrategyShortName('8 hour orders');
StrategyDescription('Strategy opens orders at 8 a.m.');
RegOption('Currency', ot_Currency, Currency);
end;
procedure DoneStrategy; stdcall;
begin
FreeMem(Currency);
end;
procedure ResetStrategy; stdcall;
begin
end;
procedure GetSingleTick; stdcall;
var
time: TDateTime;
OrderHandle: integer;
begin
if Symbol <> Currency then
exit;
time := iTime(Symbol, PERIOD_M1, 0);
if (HourOf(time) = 8) and (OrdersTotal = 0) then
SendInstantOrder(Symbol, op_Buy, 0.1, Ask - 100*Point, Ask + 50*Point, '', 0, OrderHandle);
end;
exports
InitStrategy,
DoneStrategy,
ResetStrategy,
GetSingleTick;
end.
Compile this code with Ctrl+F9, save it to Strategies folder and restart ForexTester.
If you are in Edit Mode now, then generate ticks and switch to Testing Mode.
Now open Strategies List, we can see the new strategy there named "8 hour orders". Check it and uncheck others, that only 1 strategy works at a time. Double click on it to open its parameters:

Because we did not set initial currency in our code, it is empty. Double click on Currency parameter and select currency from list. Press "Apply" button.

Now strategy is ready to work. Don't forget to enable all strategies with menu Testing -> Enable Strategy Execution. Press "Connect" button.

Here we go! You can see that strategy opens orders at 8 a.m. with TakeProfit = 50 and StopLoss = 100.