What I want is to make an order at the price when the current bar breaks the highest point of the latest 20 bars.
After that order is closed and that specific bar is complete, I need to know how to search for a new set of 20 bars and to make an order.
If that doesn't work out, the following problem happens.
(orderstotal = 0) can't stop the numerous orders tangled up in the same bar.
Also, I did some research and found the following function:
SetCurrencyAndTimeframe(symbol,SearchTimeFrame);
if time(0) <> PrevTime then
begin
PrevTime := Time(0);
// here it is new bar appears
This also is unable to prevent making multiple orders in the same bar. However, when I set the SearchTimeFrame as PERIOD_M15
begin
imax:=0;
for i:= 1 to 20 do
If imax < iHigh(Symbol,TimeFrame,i) then imax:=iHigh(Symbol,TimeFrame,i);
if (imax < iHigh(Symbol,TimeFrame,0)) and (orderstotal = 0)
then
SendInstantOrder(symbol, op_Buy, LotSize, Ask - StopLoss*Point, Ask + TakeProfit*Point, '', 0, OrderHandle);
AND in the consecutive function set the TimeFrame as PERIOD_M14 the "multiple orders made in the same bar" problem disappeared. (But what I want is NOT M14)
The biggest problem is that "if time(0) <> PrevTime then" shifts all orders by one bar. The order is made in the bar after the original bar that the order was supposed
to be made.
At times, an order isn't made when it has to be made and vice versa.
In the original approach withouth the use of the PrevTime fuction, it was complicated and twisted, but the order signals were always almost correct. But after adding
the PrevTime function, the orders are irregular, and are shifted to the next bar.
The whole function follows:
Code: Select all
library RND_0305;
uses
SysUtils,
Classes,
DateUtils,
StrategyInterfaceUnit,
TechnicalFunctions;
var
Timeframe:integer=15;
Currency: PChar;
Searchtimeframe: integer=15;
StopLoss: integer = 30;
TakeProfit: integer = 30;
LotSize: double = 1;
PrevTime: TDateTime;
iMAX:double=0;
jLOW:double=8000;
procedure InitStrategy; stdcall;
begin
StrategyShortName('4h_15m_test_stv_331');
StrategyDescription('Strategy opens orders at 8 a.m');
RegOption('Currency', ot_Currency, Currency);
RegOption('Timeframe',ot_integer,Timeframe);
RegOption('SearchTimeframe',ot_integer,SearchTimeframe);
RegOption('StopLoss', ot_Integer, StopLoss);
RegOption('TakeProfit', ot_Integer, TakeProfit);
RegOption('LotSize', ot_Double, LotSize);
SetOptionDigits('LotSize', 1);
end;
procedure DoneStrategy; stdcall;
begin
FreeMem(Currency);
end;
procedure ResetStrategy; stdcall;
begin
PrevTime:= 0;
end;
procedure GetSingleTick; stdcall;
var
OrderHandle: integer;
i:Integer;
begin
if Symbol <> Currency then
exit;
SetCurrencyAndTimeframe(symbol,SearchTimeFrame);
if time(0) <> PrevTime then
begin
PrevTime := Time(0);
// here it is new bar appears
// Buy
begin
imax:=0;
for i:= 1 to 20 do
If imax < iHigh(Symbol,TimeFrame,i) then imax:=iHigh(Symbol,TimeFrame,i);
if (imax < iHigh(Symbol,TimeFrame,0)) and (orderstotal = 0)
then
SendInstantOrder(symbol, op_Buy, LotSize, Ask - StopLoss*Point, Ask + TakeProfit*Point, '', 0, OrderHandle);
//Sell
jlow:=8000;
for i := 1 to 20 do
If jlow > iLow(Symbol,TimeFrame,i) then jlow:=iLow(Symbol,TimeFrame,i);
if (jlow > iLow(Symbol,TimeFrame,0)) and (orderstotal = 0)
then SendInstantOrder(symbol, op_Sell, LotSize, Bid + StopLoss*Point, Bid - TakeProfit*Point, '', 0, OrderHandle);
end
end;
end;
exports
InitStrategy,
DoneStrategy,
ResetStrategy,
GetSingleTick;
begin
end.
It's the first time I'm using the programming language, so it would be nice for you to help me out a little.
Thank you so much!