'Reset Project' not executing strategy

Bug reports and errors in the program
Message
Author
satori
Posts: 2
Joined: Tue Oct 08, 2019 3:44 am

'Reset Project' not executing strategy

#1 Postby satori » Tue Oct 06, 2020 3:54 am

When I finish testing a date range - and then click Restart/New Project to retest with new parameters - the Test and Quick Test run but don't execute the strategy. It does not take trades or Print to the Journal.

I have to close and re-open FT4 after each test. Once I re-open, I can then Restart Project and it will take trades per usual.

Update: I've just discovered Restart Project is not necessary. I can re-open FT4 and Quick Test without Restart Project. But I do still need to re-open.

Is there a common cause/fix for this? Everything functions great, it's just time-consuming at optimization stage.

I guess there is something that reopening FT4 is doing, that Restart Project does not.
I'm working with Borland Delphi 7 and FT 4.2.0.51 on Win10. I have FreeMem(Currency) in DoneStrategy and OrderHandle:=-1 in ResetStrategy, per the examples.
I have doubles, integers and booleans in the code, along with the Currency PChar.
Is there maybe something I can put in my code to release/reset settings, so that trades are taken after Restart Project?

This is what I'm working with:

Code: Select all

//-----------------------------------------------------------------------//
// HEADER                                                                //
//-----------------------------------------------------------------------//
library WeeklyPivot;

uses
  SysUtils,
  DateUtils,
  StrategyInterfaceUnit;

//-----------------------------------------------------------------------//
// GLOBAL VARIABLES                                                    //
//-----------------------------------------------------------------------//
var
  Currency: PChar;
  Timeframe: integer;

  PivotPointValue: double;
  IsPivotPointSet: boolean = false;

  TakeLongs: boolean;
  LongEntryDistance: integer;
  LongProfitTarget: integer;
  LongStoploss: integer;

  TakeShorts: boolean;
  ShortEntryDistance: integer;
  ShortProfitTarget: integer;
  ShortStoploss: integer;

  NotTouchedOnly: boolean;

  MondayEntry: boolean;
  TuesdayEntry: boolean;
  WednesdayEntry: boolean;
  ThursdayEntry: boolean;
  FridayEntry: boolean;

  IsTradeOpen: boolean = false;
  IsTradeDone: boolean = false;
  OrderHandle: integer;

  BarCount: integer;

//-----------------------------------------------------------------------//
// INIT                                                                  //
//-----------------------------------------------------------------------//
procedure InitStrategy; stdcall;
begin
  StrategyShortName('Weekly Pivot');
  StrategyDescription('Trade towards weekly pivot if price far enough away');

//-----------------------------------------------------------------------//
// REGISTER EXTERNAL VARIABLES                                           //
//-----------------------------------------------------------------------//

RegOption('Currency', ot_Currency, Currency);
RegOption('Timeframe', ot_Timeframe, Timeframe);

AddSeparator('Long Settings');
RegOption('Take Longs?', ot_Boolean, TakeLongs);
RegOption('Long Entry Distance', ot_Integer, LongEntryDistance);
RegOption('Long Profit Target', ot_Integer, LongProfitTarget);
RegOption('Long Stoploss', ot_Integer, LongStoploss);

AddSeparator('Short Settings');
RegOption('Take Shorts?', ot_Boolean, TakeShorts);
RegOption('Short Entry Distance', ot_Integer, ShortEntryDistance);
RegOption('Short Profit Target', ot_Integer, ShortProfitTarget);
RegOption('Short Stoploss', ot_Integer, ShortStoploss);

AddSeparator('Only If Price Not Touched');
RegOption('Only Enter if Not Yet Touched PP?', ot_Boolean, NotTouchedOnly);

AddSeparator('Days to Enter');
RegOption('Enter Mondays?', ot_Boolean, MondayEntry);
RegOption('Enter Tuesdays?', ot_Boolean, TuesdayEntry);
RegOption('Enter Wednesdays?', ot_Boolean, WednesdayEntry);
RegOption('Enter Thursdays?', ot_Boolean, ThursdayEntry);
RegOption('Enter Fridays?', ot_Boolean, FridayEntry);

end;

//-----------------------------------------------------------------------//
// RELEASE REGISTERED EXTERNAL VARIABLES                                 //
//-----------------------------------------------------------------------//
procedure DoneStrategy; stdcall;
begin
  FreeMem(Currency);
end;

//-----------------------------------------------------------------------//
// CLEAR TO BEGIN TESTING                                                //
//-----------------------------------------------------------------------//
procedure ResetStrategy; stdcall;
begin
  OrderHandle := -1;
end;
//-----------------------------------------------------------------------//


//-----------------------------------------------------------------------//
//-----------------------------------------------------------------------//
// WORKSPACE BELOW                                                       //
//-----------------------------------------------------------------------//
//-----------------------------------------------------------------------//


//-----------------------------------------------------------------------//
// CUSTOM FUNCTIONS                                                      //
//-----------------------------------------------------------------------//

// CLOSE TRADE  ---------------------------------------------------------//
procedure CloseTrade; stdcall;
begin

  if (IsTradeOpen = true) then
  begin
    if OrderSelect(OrderHandle, SELECT_BY_TICKET, MODE_TRADES) then
    begin

      if
      (DayOfTheWeek(Time(0)) = 6) and (HourOf(Time(0)) = 6) or
      (OrderType = tp_Buy) and (OrderProfitPips > (LongProfitTarget * 10)) or
      (OrderType = tp_Buy) and (OrderProfitPips < (LongStoploss * -10)) or
      (OrderType = tp_Sell) and (OrderProfitPips > (ShortProfitTarget * 10)) or
      (OrderType = tp_Sell) and (OrderProfitPips < (ShortStoploss * -10))
      then
      begin
        CloseOrder(OrderHandle);
        IsTradeOpen := false;
      end;
         
    end;

  end;

end;
//-----------------------------------------------------------------------//

// SET PIVOT POINT ------------------------------------------------------//
procedure SetPivotPoint; stdcall;
begin

  if (IsPivotPointSet = false) and (DayOfTheWeek(Time(0)) = 1) then
  begin
    SetCurrencyAndTimeframe(Symbol,PERIOD_W1);

    PivotPointValue := (High(1) + Low(1) + Close(1)) / 3;
    PivotPointValue := Round(PivotPointValue * 100000) / 100000;

    Print('PivotPoint ' + FloatToStr(PivotPointValue));

    IsPivotPointSet := true;
    IsTradeOpen := false;
    IsTradeDone := false;

    SetCurrencyAndTimeframe(Symbol,Timeframe);
  end;

  if (IsPivotPointSet = true) and (DayOfTheWeek(Time(0)) = 6) then
  begin
    IsPivotPointSet := false;
  end;

end;
//-----------------------------------------------------------------------//

// OPEN TRADE  ----------------------------------------------------------//
procedure OpenTrade; stdcall;
begin

if (IsTradeOpen = false) and (IsTradeDone = false) and (Bars() > 50) then
begin

  if (DayOfTheWeek(Time(0)) = 1) and (MondayEntry = true) or
     (DayOfTheWeek(Time(0)) = 2) and (TuesdayEntry = true) or
     (DayOfTheWeek(Time(0)) = 3) and (WednesdayEntry = true) or
     (DayOfTheWeek(Time(0)) = 4) and (ThursdayEntry = true) or
     (DayOfTheWeek(Time(0)) = 5) and (FridayEntry = true) then
  begin

    if ((PivotPointValue - Ask) > (LongEntryDistance * Point)) then
    begin
      SetCurrencyAndTimeframe(Symbol,PERIOD_W1);
      if (NotTouchedOnly = false) or
         (NotTouchedOnly = true) and (High(0) < PivotPointValue) then
         begin
           if (TakeLongs = true) then
           begin
             SetCurrencyAndTimeframe(Symbol,Timeframe);
             if SendInstantOrder(Symbol(), op_Buy, 0.01, 0, 0, '', 0, OrderHandle) = true then
             begin
               IsTradeOpen := true;
               IsTradeDone := true;
             end;
           end;
         end;
    end;

    if ((Bid - PivotPointValue) > (ShortEntryDistance * Point)) then
    begin
      SetCurrencyAndTimeframe(Symbol,PERIOD_W1);
      if (NotTouchedOnly = false) or
         (NotTouchedOnly = true) and (Low(0) > PivotPointValue) then
         begin
           if (TakeShorts = true) then
           begin
             SetCurrencyAndTimeframe(Symbol,Timeframe);
             if SendInstantOrder(Symbol(), op_Sell, 0.01, 0, 0, '', 0, OrderHandle) = true then
             begin
               IsTradeOpen := true;
               IsTradeDone := true;
             end;
           end;
         end;
    end;

  end;

end;

end;
//-----------------------------------------------------------------------//


//-----------------------------------------------------------------------//
// ONTICK                                                                //
//-----------------------------------------------------------------------//

procedure GetSingleTick; stdcall;
begin

  SetCurrencyAndTimeframe(Symbol,Timeframe);

  CloseTrade;

  if BarCount < Bars() then
  begin

    SetPivotPoint;

    BarCount := Bars();

  end;

  OpenTrade;

end;

//-----------------------------------------------------------------------//
// EXPORTS                                                               //
//-----------------------------------------------------------------------//
exports
  InitStrategy,
  DoneStrategy,
  ResetStrategy,
  GetSingleTick;
end.

Return to “Bug reports”

Who is online

Users browsing this forum: No registered users and 27 guests