Hi,
I created and successfully installed a strategy using lazarus.
The problem I am having in execution is the Journal displays -
"Error in Strategy: SimpleEMA"
How can I get more info on the error?
I have tried using the delphi try except block
except
on E : Exception do
Print (E.ClassName+' error raised, with message : '+E.Message);
with no luck...
Thanks,
Tanmay
Strategy as below
-------------------------------------------------------------------------------
library Conquer_FX512;
uses
SysUtils, Classes, StrategyInterfaceUnit, TechnicalFunctions, Interfaces ;
var
// External parameters
Currency: PChar = nil;
TimeFrame: integer;
LotSize1: double;
LotSize2: double;
LotSize3: double;
UseStopLoss: boolean = True;
StopLoss: integer;
UseATR:boolean;
ATR_Period:integer;
period1: integer;
period2: integer;
// custom variables
OrderHandle: integer;
OrderStyle: TTradePositionType;
OpenTime: TDateTime;
pTime: TDateTime;
buy_price, sell_price: double;
ma_type: TMAType = ma_EMA;
price_type: TPriceType = pt_Close;
// procedure PlacePendingSellOrders;stdcall;
// procedure PlacePendingBuyOrders;stdcall;
{-----Init strategy---------------------------------------------------------}
procedure InitStrategy; stdcall;
begin
StrategyShortName('SimpleEMA');
StrategyDescription('Strategy based on 2 EMA crossover');
// Register external parameters
RegOption('Currency', ot_Currency, Currency);
ReplaceStr(Currency, 'EURUSD');
RegOption('Timeframe', ot_Timeframe, TimeFrame);
TimeFrame := PERIOD_D1;
RegOption('LotSize1', ot_Double, LotSize1);
SetOptionDigits('LotSize1', 1);
lotSize1 := 0.4;
RegOption('LotSize2', ot_Double, LotSize2);
SetOptionDigits('LotSize2', 1);
lotSize2 := 0.6;
RegOption('LotSize3', ot_Double, LotSize3);
SetOptionDigits('LotSize3', 1);
lotSize3 := 0.2;
RegOption('EMA1 period', ot_Integer, period1);
SetOptionRange('EMA1 period', 2, MaxInt);
period1 := 5;
RegOption('EMA2 period', ot_Integer, period2);
SetOptionRange('EMA2 period', 2, MaxInt);
period2 := 12;
end;
{-----Done strategy---------------------------------------------------------}
procedure DoneStrategy; stdcall;
begin
FreeMem(Currency);
end;
{-----Reset strategy--------------------------------------------------------}
procedure ResetStrategy; stdcall;
begin
OrderHandle := -1;
pTime := 0;
end;
{-----Calculate SMA---------------------------------------------------------}
function GetSMA(period: integer): double;
var
i: integer;
sum: double;
begin
sum := 0;
for i:=0 to period - 1 do
sum := sum + Close(i);
result := sum/period;
end;
{-----Sell Orders ------ }
procedure PlacePendingSellOrders;stdcall;
var
ex_price: double = 0.0;
begin
ex_price := Low(1) - 5*Point;
if Bid > ex_price then
begin
//Send pending sell stop orders
SendPendingOrder(Symbol, op_SellStop, LotSize1,ex_price+(StopLoss*Point), ex_price-(2*StopLoss*Point), ex_price, '1', 885, OrderHandle);
SendPendingOrder(Symbol, op_SellStop, LotSize2,ex_price+(StopLoss*Point), ex_price-(4*StopLoss*Point), ex_price, '2', 885, OrderHandle);
SendPendingOrder(Symbol, op_SellStop, LotSize3,ex_price+(StopLoss*Point), ex_price-(8*StopLoss*Point), ex_price, '3', 885, OrderHandle);
end
else if Bid < (ex_price + (5*Point)) then
begin
//Send pending sell limit orders
SendPendingOrder(Symbol, op_SellLimit, LotSize1,ex_price+(StopLoss*Point), ex_price-(2*StopLoss*Point), ex_price, '1', 886, OrderHandle);
SendPendingOrder(Symbol, op_SellLimit, LotSize2,ex_price+(StopLoss*Point), ex_price-(4*StopLoss*Point), ex_price, '2', 886, OrderHandle);
SendPendingOrder(Symbol, op_SellLimit, LotSize3,ex_price+(StopLoss*Point), ex_price-(8*StopLoss*Point), ex_price, '3', 886, OrderHandle);
end
else
begin
//Send Instant Orders
SendInstantOrder(Symbol, op_Sell, LotSize1, Bid+(StopLoss*Point), Bid-(2*StopLoss*Point), '1', 887, OrderHandle);
SendInstantOrder(Symbol, op_Sell, LotSize1, Bid+(StopLoss*Point), Bid-(4*StopLoss*Point), '2', 887, OrderHandle);
SendInstantOrder(Symbol, op_Sell, LotSize1, Bid+(StopLoss*Point), Bid-(8*StopLoss*Point), '3', 887, OrderHandle);
end;
ex_price := 0.0;
end;
{-----Buy Orders ------ }
procedure PlacePendingBuyOrders;stdcall;
var
ex_price: double = 0.0;
begin
ex_price := High(1) + 5*Point;
if Ask < ex_price then
begin
//Send pending buy stop orders
SendPendingOrder(Symbol, op_BuyStop, LotSize1,ex_price-(StopLoss*Point), ex_price+(2*StopLoss*Point), ex_price, '1', 985, OrderHandle);
SendPendingOrder(Symbol, op_BuyStop, LotSize2,ex_price-(StopLoss*Point), ex_price+(4*StopLoss*Point), ex_price, '2', 985, OrderHandle);
SendPendingOrder(Symbol, op_BuyStop, LotSize3,ex_price-(StopLoss*Point), ex_price+(8*StopLoss*Point), ex_price, '3', 985, OrderHandle);
end
else if Ask > (ex_price - (5*Point)) then
begin
//Send pending buy limit orders
SendPendingOrder(Symbol, op_BuyLimit, LotSize1,ex_price-(StopLoss*Point), ex_price+(2*StopLoss*Point), ex_price, '1', 986, OrderHandle);
SendPendingOrder(Symbol, op_BuyLimit, LotSize2,ex_price-(StopLoss*Point), ex_price+(4*StopLoss*Point), ex_price, '2', 986, OrderHandle);
SendPendingOrder(Symbol, op_BuyLimit, LotSize3,ex_price-(StopLoss*Point), ex_price+(8*StopLoss*Point), ex_price, '3', 986, OrderHandle);
end
else
begin
//Send Instant Orders
SendInstantOrder(Symbol, op_Buy, LotSize1, Ask-(StopLoss*Point), Ask+(2*StopLoss*Point), '1', 987, OrderHandle);
SendInstantOrder(Symbol, op_Buy, LotSize2, Ask-(StopLoss*Point), Ask+(4*StopLoss*Point), '2', 987, OrderHandle);
SendInstantOrder(Symbol, op_Buy, LotSize3, Ask-(StopLoss*Point), Ask+(8*StopLoss*Point), '3', 987, OrderHandle);
end;
ex_price := 0.0;
end;
{-----Process single tick---------------------------------------------------}
procedure GetSingleTick; stdcall;
var
ema1, ema2: double;
i, j : integer;
begin
Try
// check our currency
if Symbol <> string(Currency) then exit;
// set currency and timeframe
SetCurrencyAndTimeframe(Symbol, TimeFrame);
// check number of bars and EMA period
if (Bars < period1) or (Bars < period2) then exit;
if OrdersTotal>0 then
begin
for i:=0 to OrdersTotal - 1 do
begin
if OrderSelect(i, SELECT_BY_POS, MODE_TRADES) then
begin
if (OrderStyle = tp_Buy) or (OrderStyle = tp_Sell) then
begin
if (GetTakeProfitPoints(i) > 399) and (Abs(OrderOpenPrice() - Bid) > (200*Point)) then
begin
SetStopLossPoints(i, 100);
end
else if (GetTakeProfitPoints(i) > 199) and (Abs(OrderOpenPrice() - Bid) > (100*Point)) then
begin
SetStopLossPoints(i, 10);
end;
end;
end;
end;
end;
if Time(0) <> pTime then
begin
pTime := Time(0);
// calculate SMA
ema1 := GetMA(0 , 0, period1, ma_type, price_type, 1);
ema2 := GetMA(0 , 0, period2, ma_type, price_type, 1);
Print('Calculating EMAs');
for j:=0 to OrdersTotal - 1 do
begin
// if BUY order exists and fast EMA crosses slow EMA from top
// then close order
if OrderSelect(j, SELECT_BY_POS, MODE_TRADES) then
begin
if (j <> -1) and (OrderStyle = tp_Buy) and
(ema1 < ema2) then
begin
CloseOrder(j);
end
// if SELL order exists and fast SMA crosses slow SMA from bottom
// then close order
else if (j <> -1) and (OrderStyle = tp_Sell) and
(ema1 > ema2) then
begin
CloseOrder(j);
end
else if (j<>-1) then
if (OrderStyle = tp_BuyLimit) or (OrderStyle = tp_SellLimit) or (OrderStyle = tp_BuyStop)
or (OrderStyle = tp_SellStop) then
begin
DeleteOrder(j);
end;
end;
end;
// if there is no order and fast SMA crosses slow SMA from top
// then open SELL order
if (ema1 < ema2) then
begin
PlacePendingSellOrders();
// SendInstantOrder(Symbol, op_Sell, LotSize1, 0, 0, '', 0, OrderHandle);
// OrderStyle := tp_Sell;
// OpenTime := Time(0);
end
// if there is no order and fast SMA crosses slow SMA from bottom
// then open BUY order
else if (ema1 > ema2) then
begin
PlacePendingBuyOrders();
// SendInstantOrder(Symbol, op_Buy, LotSize1, 0, 0, '', 0, OrderHandle);
// OrderStyle := tp_Buy;
// OpenTime := Time(0);
end;
end;
except
on E : Exception do
Print(E.ClassName+' error raised, with message : '+E.Message);
end;
end;
exports
InitStrategy,
DoneStrategy,
ResetStrategy,
GetSingleTick;
end.
How to view error information for an installed strategy
-
- Posts: 10
- Joined: Wed Jan 19, 2011 5:38 pm
- Location: 92808
- Contact:
-
- Posts: 905
- Joined: Sat Jul 11, 2009 10:54 am
Hello,
Please try to compile and install a strategy with the following code:
Does it work?
Please try to compile and install a strategy with the following code:
Code: Select all
procedure GetSingleTick; stdcall;
var
zero : integer;
begin
Try
zero := 0;
zero := 1 div zero;
Print(inttostr(zero))
except
on E : Exception do
Print(E.ClassName+' error raised, with message : '+E.Message);
end;
end;
Does it work?
Check our other product here:
http://www.forexcopier.com
http://www.forexcopier.com
-
- Posts: 10
- Joined: Wed Jan 19, 2011 5:38 pm
- Location: 92808
- Contact:
Still the same error
Tried the code,
It still gives the same error.
Screenshot attached.
p.s. the code needs a ';' after the first Print.
It still gives the same error.
Screenshot attached.
p.s. the code needs a ';' after the first Print.
- Attachments
-
- FXT2_Exception.JPG (53.07 KiB) Viewed 13702 times
-
- Posts: 905
- Joined: Sat Jul 11, 2009 10:54 am
-
- Posts: 905
- Joined: Sat Jul 11, 2009 10:54 am
-
- Posts: 40
- Joined: Wed Sep 15, 2010 6:52 am
Stable Lazarus
The following version of Lazarus seems to be the most stable. I have used it with Forex Tester for a year now without problems:
Version #: 0.9.26.2 beta
Date: 2009-03-13
FPC Version: 2.2.2
SVN Revision: 18980
i386-win32-win32/win64
One important procedure, however:
When you save a project, do "File / Save All"
When you compile, do "Run / Build All"
I'm sorry, but I can't recall the link where I downloaded this. You can Google some of the version numbers and find it.
Version #: 0.9.26.2 beta
Date: 2009-03-13
FPC Version: 2.2.2
SVN Revision: 18980
i386-win32-win32/win64
One important procedure, however:
When you save a project, do "File / Save All"
When you compile, do "Run / Build All"
I'm sorry, but I can't recall the link where I downloaded this. You can Google some of the version numbers and find it.
creativity + willful purpose
Who is online
Users browsing this forum: No registered users and 16 guests