Hi guys,
which is the right way to wright the code so that it does a maximum of one operation per week?
Thanks!
Max one trade per week
Re: Max one trade per week
Here are some functions that I've written that count the number of filled entry orders each day. You can modify these to work on a weekly basis instead, and then do something in your signal like "If _EntriesThisWeek() = 0 then ...."
The first approach iterates through the open positions and counts any long or short entry orders from today. Note that it doesn't count closed positions. (E.g., if we have an entry order followed by a position close on the same day, it will return "0," not "1.")
The second approach is to use the FilledOrder event. Here's an example (not thoroughly tested) that counts the total number of entry orders today (both open and closed). Note that this will return "1" in the above example.
If anyone else has a more elegant solution, I'd appreciate any other suggestions. Hope this helps!
The first approach iterates through the open positions and counts any long or short entry orders from today. Note that it doesn't count closed positions. (E.g., if we have an entry order followed by a position close on the same day, it will return "0," not "1.")
Code: Select all
// Function name: _EntriesToday
//
// Description: Returns the current number of long or short open entry orders from the current day.
Variables:
EntryDate(0),
EntryName("");
_EntriesToday = 0;
For Value1 = 0 to OpenEntriesCount - 1 begin
For Value2 = 0 to PosTradeCount(Value1) - 1 begin
EntryDate = JulianToDate(PosTradeEntryDateTime(Value1, Value2));
EntryName = PosTradeEntryName(Value1, Value2);
If EntryDate = Date AND (EntryName = "Buy" OR EntryName = "Short") then begin
_EntriesToday += 1;
end;
End;
End;
Code: Select all
If Date <> Date[1] then begin
_EntriesToday = 0;
end;
If FilledOrderAction <> 0 then _EntriesToday += 1;
Re: Max one trade per week
An update: I discovered that the second function won't work during backtesting since the FilledOrderAction event is only raised during live trading. Here's an alternative function that seems to work better:
Code: Select all
Variables:
Positions(0);
If Time = Sess1EndTime then begin
_EntriesToday = 0;
end;
Positions = PosTradeCount(0);
If Positions > Positions[1] then begin
_EntriesToday += 1;
end;