Max one trade per week

Questions about MultiCharts and user contributed studies.
Cicikov
Posts: 5
Joined: 11 Apr 2017
Has thanked: 2 times

Max one trade per week

Postby Cicikov » 13 Sep 2017

Hi guys,

which is the right way to wright the code so that it does a maximum of one operation per week?

Thanks!

Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Re: Max one trade per week

Postby Xyzzy » 14 Sep 2017

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.")

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;
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.

Code: Select all

If Date <> Date[1] then begin
_EntriesToday = 0;
end;

If FilledOrderAction <> 0 then _EntriesToday += 1;
If anyone else has a more elegant solution, I'd appreciate any other suggestions. Hope this helps!

Xyzzy
Posts: 162
Joined: 19 Mar 2011
Has thanked: 43 times
Been thanked: 79 times

Re: Max one trade per week

Postby Xyzzy » 14 Sep 2017

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;


Return to “MultiCharts”