How to code one trade a day or...

Questions about MultiCharts and user contributed studies.
zanna77
Posts: 3
Joined: 26 Jan 2017
Has thanked: 2 times

How to code one trade a day or...

Postby zanna77 » 09 Mar 2023

Hi everyone,

I have a question: I want that the strategy makes only one trade a day or that it can makes more than one but only if the first trade of the day has a positive profit.
I have tried this but nothing changes in the performance report:

if (TradesToday(Date[0])=0)

then condition1 = True ;
if condition1 and ,,,,,,,then buy at market..

any suggestion?

Many thanks

Zanna

ScottishSpeculator
Posts: 47
Joined: 03 Jan 2023
Been thanked: 3 times

Re: How to code one trade a day or...

Postby ScottishSpeculator » 10 Mar 2023

Zanna

Since no one else will have a go, ill give it an attempt. Still new but want to help where possible. As I rely on people replying to my posts hopefully. I think you can use the keywords Dailylosers or numlostrades but I am not sure if they are specific to each strategy or the overall portfolio. Doesn't say in the function description, perhaps an MC staff member can answer? How about doing something like this:

Code: Select all

input: Losslimit (1); vars: Allowedtotrade(False); If date<>date[1] and Dailylosers(Currentdate)<= losslimit then Allowedtotrade = True; If Allowedtotrade then begin { entry conditions} end;

ScottishSpeculator
Posts: 47
Joined: 03 Jan 2023
Been thanked: 3 times

Re: How to code one trade a day or...

Postby ScottishSpeculator » 12 Mar 2023

I was thinking about this and was reading the Turtle strategy. They have a filter in which you don't take the trade if the last trade was a win.
Does anyone know how to get around this logic issue?
You could code something easy enough saying dont trade if the last trade was a win but then how do you track account for the results of the trade you didn't take.. ?

Global variable? I might post in separate post

zanna77
Posts: 3
Joined: 26 Jan 2017
Has thanked: 2 times

Re: How to code one trade a day or...

Postby zanna77 » 13 Mar 2023

Hi and thanks for your help. I have tried also your suggestion but doesn't work. I thinks that the reserved word tradestoday doesn't work .
It seems an easy istruction to code but I can't find the solution

ScottishSpeculator
Posts: 47
Joined: 03 Jan 2023
Been thanked: 3 times

Re: How to code one trade a day or...

Postby ScottishSpeculator » 13 Mar 2023

Did you try daily losers and numlostrades keywords? I don't really do intraday strategies so its not something I have researched yet or am familiar with. I would also like to know this piece of code. It could be a nice function to have saved. If I have time this week I may play around with one of my strategies on the intraday and see if I can limit the trades taken.

Hopefully someone else jumps in with some ideas/input

ScottishSpeculator
Posts: 47
Joined: 03 Jan 2023
Been thanked: 3 times

Re: How to code one trade a day or...

Postby ScottishSpeculator » 20 Mar 2023

Zanna

Was playing around with this function today "limit losing trades" and got the start of something which appears to work. Need to double-check works on diff instruments/timeframes but you can make a function like this below. A counter and then create a "loss_per_day" input value that you can use in the entry condition.

Code: Select all

variables: Closed_netprofit(0), Counter(0), trade_losses(0); Closed_netprofit = i_ClosedEquity; if date<>date[1] then begin counter=0; end; if Closed_netprofit < Closed_netprofit[1] then Counter=Counter+1; trade_losses =counter; limit_losing_trades = trade_losses;
Also if your interested I am trying to set up a telegram group/ maybe discord in which people new to MC can share strategy code or come together to upvote ideas and get them build by professional for super cheap per person. See below seems you new and could be interested.

https://t.me/+h8iTLItDWGAxNGY0

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: How to code one trade a day or...

Postby rrams » 26 Mar 2023

Hello Zanna,

The TradesToday function does work as intended, (although it should more appropriately be called EntriesToday). It may not have worked for you because of any of the following reasons:
1. You forgot to reset the condition1 back to false after every bar.
2. You didn't get enough signals to trigger multiple entries per day.
3. You misunderstood the difference between a trade, an entry and a position.
4. Your symbol has a trading session that spans multiple days.
...or some other reason. It is impossible to tell without all the code.

Here is an example of a complete trading system that only takes one trade per day unless the first position that was entered and closed in the day is profitable. In that case it takes unlimited trades for the day:

Code: Select all

// Multiple trades per day if the first trade closed profitable. vars: TradeProfit(true), N(0), MP(0); MP=MarketPosition; TradeProfit=true; // One or more closed positions held today. if ExitDate(1)=D then begin // Scan all CLOSED entries for the first for N=1 to MaxPositionsAgo-1 // one of the day and check if profitable. begin if EntryDate(N)<>D then break else if PositionProfit(N)>0 then TradeProfit=true else TradeProfit=false; end; end; // Allow open positions to be closed. if MP>0 and C<C[1] then sell next bar market; if MP<0 and C>C[1] then buytocover next bar market; // Enter new positions if allowed. if MP=0 and TradeProfit and C<C[1] then sellshort next bar market; if MP=0 and TradeProfit and C>C[1] then buy next bar market;
You may want something different. You didn't specify what to do with overnight positions that carry a profit into the next day. Or you might want your profit window to encompass session times not calendar times. Consider paying a small fee to me or others to write exactly what you need as the coding can be a little tricky.

zanna77
Posts: 3
Joined: 26 Jan 2017
Has thanked: 2 times

Re: How to code one trade a day or...

Postby zanna77 » 28 Mar 2023

Thanks Rramms,

Maybe the problem is that I haven't written the reset the condition1 back to false after every bar.
How I code it?

For example

Code: Select all

if tradestoday(0)=0 then condition1=true; if condtion1 and c>highd(0) then buy next bar at market; ]if mp=1 and c<lowd(0) then sell next bar at market;
I want to enter a long trade only once a day.
But how I have explained the function tradestoday seems don't work
Last edited by zanna77 on 29 Mar 2023, edited 3 times in total.

User avatar
TJ
Posts: 7742
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2222 times

Re: How to code one trade a day or...

Postby TJ » 28 Mar 2023

See post #1 & #2
viewtopic.php?t=11713

User avatar
rrams
Posts: 128
Joined: 10 Feb 2011
Location: USA
Has thanked: 7 times
Been thanked: 70 times
Contact:

Re: How to code one trade a day or...

Postby rrams » 30 Mar 2023

zanna,
Are you using realtime Synchronous AutoTrading mode? If that is the case then the EntryDate() keyword is going to give you trouble and a different solution is necessary.


Return to “MultiCharts”