im currently working on a code which does the following:
1) buys when the close price is above the parabolic sar and if the CCI is above 200.
2) sells when the close price is below the parabolic sar and if the cci is below -200
then once an order is open
3) closes a buy order if the parabolic sar goes above the price
4) closes a sell order if the parabolic sar goes below the price.
My code is:
Code: Select all
library CCIPSAR2;
uses
SysUtils,
Classes,
StrategyInterfaceUnit,
TechnicalFunctions;
var
Currency: PChar = nil;
TimeFrame: integer;
LotSize: double;
CCICURRENT: Double;
PSARCURRENT: Double;
CCIPREVIOUS: Double;
CCI: integer;
IndPSAR: integer;
CCIUPPER: Integer;
CCILOWER: Integer;
PSARABOVE: Boolean;
PSARBELOW: Boolean;
OrderHandle: integer;
OrderStyle: TTradePositionType;
OpenTime: TDateTime;
procedure InitStrategy; stdcall;
begin
StrategyShortName('CCI PSAR 2');
StrategyDescription('CCI PSAR TREND FOLLOWER');
RegOption('Currency', ot_Currency, Currency);
ReplaceStr(Currency, 'EURUSD');
RegOption('Timeframe', ot_Timeframe, TimeFrame);
TimeFrame := PERIOD_M1;
RegOption('LotSize', ot_Double, LotSize);
SetOptionDigits('LotSize', 1);
lotSize := 0.1;
RegOption('CCI UPPER BUY LEVEL', ot_Integer, CCIUPPER);
CCIUPPER := 200;
RegOption('CCI LOWER SELL LEVEL', ot_Integer, CCILOWER);
CCILOWER := -200;
end;
procedure DoneStrategy; stdcall;
begin
FreeMem(Currency)
end;
procedure ResetStrategy; stdcall;
begin
OrderHandle := -1;
CCI := CreateIndicator('EURUSD', Period_M1, 'CCI', '400');
IndPSAR := CreateIndicator ('EURUSD', Period_M1, 'PARABOLIC', '0.01; 0.01');
PSARABOVE := false;
PSARBELOW := false;
end;
procedure GetSingleTick; stdcall;
begin
if Symbol <> string(Currency) then exit;
SetCurrencyAndTimeframe(Symbol, TimeFrame);
CCICURRENT := GetIndicatorValue(CCI, 0, 1);
PSARCURRENT := GetIndicatorValue(IndPSAR, 0, 1);
CCIPREVIOUS := GetIndicatorValue(CCI, 1, 1);
if (OrderHandle <> -1) then
begin
if PSARCURRENT > Close(0) then
PSARABOVE := True;
if PSARCURRENT < Close(0) then
PSARABOVE := False;
end;
if (OrderHandle <> -1) then
begin
if PSARCURRENT < Close(0) then
PSARBELOW := True;
if PSARCURRENT > Close(0) then
PSARBELOW := False;
end;
if (OrderHandle = -1) and (CCICURRENT > CCIUPPER) and (PSARCURRENT < Close(0)) then
begin
SendInstantOrder(Symbol, op_Buy, LotSize, 0, 0, '', 0, OrderHandle);
OrderStyle := tp_Buy;
OpenTime := Time(0)
end;
if (OrderHandle = -1) and (CCICURRENT < CCILOWER) and (PSARCURRENT > Close(0)) then
begin
SendInstantOrder(Symbol, op_Sell, LotSize, 0, 0, '',0, OrderHandle);
OrderStyle := tp_Sell;
OpenTime := Time(0)
end;
if (OrderHandle <> -1) and (OrderStyle = tp_Buy) and (PSARABOVE = True) then
begin
CloseOrder(OrderHandle);
OrderHandle := -1;
End;
if (OrderHandle <> -1) and (OrderStyle = tp_Sell) and (PSARBELOW = True) then
begin
CloseOrder(OrderHandle);
OrderHandle := -1;
End;
end;
exports
InitStrategy,
DoneStrategy,
ResetStrategy,
GetSingleTick,
ReplaceStr;
end.
I am having the following problems:
1) The exit when the parabolic goes from below to above the price is not working (or reverse for a sell order)
2) The sell order for when the CCI is below -200 is not working.
Any hints or ideas would be appriciated!!! I have changed my code so many times now!!
Thanks in advance!