Site Administrator Swing Trader Data Scientist & DevOps
Manta, Ecuador
Experience: Advanced
Platform: Custom solution
Trading: Futures & Crypto
Posts: 49,993 since Jun 2009
Thanks: 32,461 given,
98,251
received
I needed this for my own strategies, so I wanted to share it here.
This will allow a dollar limit of daily loss and daily profit targets, whereby if one or the other is hit then the strategy will stop for the day.
If you wanted to make it weekly, it wouldn't be hard to do. The input for daily loss should be a + value (ie: "250" means a loss of 250, don't use "-250").
Condition1 evaluates to true if the profit for today is between the max loss and max profit. It evaluates to false if one or the other is exceeded. So place your entry code (buy/sellshort) inside the if block.
H
I everyone
I am trying to implement a daily profit/ loss limit in my trading strategy. To understand exactly what is going on I implemented Big Mike Code in a simple MACD cross over, prior to implementing it my "production trading code" - .
For some reason the code below doesn't work, and don't understand why it isn't working
What I am trying to achieve is very simple; - I want the trading to stop IF dailyprofit or dailyloss is reached within the specified timeframe.
I am trying to set up an automated strategy involving a daily stop loss/profit target in Easy Language. I was browsing the forum for any answers and discovered Mengelbrecht encountered the same problem that I am having, in 2010, but I don't know whether the issue was resolved as that's where the thread ends.
In a nutshell, I also wanted to somehow incorporate in the code, a mechanism to terminate a current position if its profit/loss makes the daily profit/loss reach the target. I independently wrote an almost identical code as the code posted by Mengelbrecht, but also put [Intrabarordergeneration=true], and declared some variables as "intrabarpersist". Still, it didn't work.
If that issue has been resolved, could someone please post a working code that is taking into account any current position (the code should be probably using "positionprofit") and terminates that position when the daily profit/loss is reached?
post your code and I am sure someone here in the forum can point you in the right direction.
Regards,
ABCTG
volatilius
Hi Mengelbrecht and all:
I am trying to set up an automated strategy involving a daily stop loss/profit target in Easy Language. I was browsing the forum for any answers and discovered Mengelbrecht encountered the same problem that I am having, in 2010, but I don't know whether the issue was resolved as that's where the thread ends.
In a nutshell, I also wanted to somehow incorporate in the code, a mechanism to terminate a current position if its profit/loss makes the daily profit/loss reach the target. I independently wrote an almost identical code as the code posted by Mengelbrecht, but also put [Intrabarordergeneration=true], and declared some variables as "intrabarpersist". Still, it didn't work.
If that issue has been resolved, could someone please post a working code that is taking into account any current position (the code should be probably using "positionprofit") and terminates that position when the daily profit/loss is reached?
Thanks!
The following user says Thank You to ABCTG for this post:
I am trying to set up an automated strategy involving a daily stop loss/profit target in Easy Language. I was browsing the forum for any answers and discovered Mengelbrecht encountered the same problem that I am having, in 2010, but I don't know whether the issue was resolved as that's where the thread ends.
In a nutshell, I also wanted to somehow incorporate in the code, a mechanism to terminate a current position if its profit/loss makes the daily profit/loss reach the target. I independently wrote an almost identical code as the code posted by Mengelbrecht, but also put [Intrabarordergeneration=true], and declared some variables as "intrabarpersist". Still, it didn't work.
If that issue has been resolved, could someone please post a working code that is taking into account any current position (the code should be probably using "positionprofit") and terminates that position when the daily profit/loss is reached?
Thanks!
Hi volatilius
sorry for the late respons, I have been away from my desk all weekend.
This is code that someone send me:
How can this code be modified to set a profit objective for the day that acts as a profit stop? For example the profit breaches a $200 profit level and keeps trading unless the profit falls back below $200 in the same day. Any ideas?
the post from @mengelbrecht two above your post shows how to store the previous NetProfit in a variable on a new day. Which is exactly what you'd have to do, too. Then you can use this variable in your checks.
Regards,
ABCTG
The following user says Thank You to ABCTG for this post:
I needed this for my own strategies, so I wanted to share it here.
This will allow a dollar limit of daily loss and daily profit targets, whereby if one or the other is hit then the strategy will stop for the day.
If you wanted to make it weekly, it wouldn't be hard to do. The input for daily loss should be a + value (ie: "250" means a loss of 250, don't use "-250").
// for use in signal
inputs:
dailyprofit(500),
dailyloss(250);
vars:
todaynet(0),
yesterdaynet(0);
if date <> date[1] then begin
yesterdaynet = NetProfit;
end;
if MarketPosition = 0 then begin
todaynet = NetProfit - yesterdaynet;
end;
condition1 = -dailyloss < todaynet and todaynet < dailyprofit;
// entries
if condition 1 then begin
// put your long and short code here
end;
Condition1 evaluates to true if the profit for today is between the max loss and max profit. It evaluates to false if one or the other is exceeded. So place your entry code (buy/sellshort) inside the if block.
Let me know if it proves useful!
Mike
By any chance, do you have an example of this incorporated into a full coded strategy example? I'm trying to learn how to code some strategies, and not sure where everything should be placed in regards to what you listed here. I assume this is for NT7? Thanks in advance if you can help.
Mike - I was wondering if something similar to this could be implemented for multiple TradeStation (TS) Workspaces? I trade 4 futures markets in TS and my goal for each day is $2K. I sometimes enter 3-4 markets at the same time (via my strategy) as they are all trending in the same direction. Many times they hit the $2k mark and fall off if am not watching every tick. I can get distracted with bathroom trips, snacks, phone calls, etc. I would like to have all my Workspaces shut down if I reach this daily profit or loss target. Is this a possibility? I was thinking you might be able to cross reference all 4 Workspaces some way. Maybe with a 5th chart referencing the other 4 as long as the same account is used? What do you think? Thanks in advance for your help. JK
it depends a bit of what exactly you want to do. If you want a global target across all workspaces, this would require some communication between the charts and maybe a separate chart that monitors all targets and blocks/allows the trading on the individual charts. One way to accomplish this in real time could by using a Global Dictionary.
Regards,
ABCTG
20YRTRADER
Mike - I was wondering if something similar to this could be implemented for multiple TradeStation (TS) Workspaces? I trade 4 futures markets in TS and my goal for each day is $2K. I sometimes enter 3-4 markets at the same time (via my strategy) as they are all trending in the same direction. Many times they hit the $2k mark and fall off if am not watching every tick. I can get distracted with bathroom trips, snacks, phone calls, etc. I would like to have all my Workspaces shut down if I reach this daily profit or loss target. Is this a possibility? I was thinking you might be able to cross reference all 4 Workspaces some way. Maybe with a 5th chart referencing the other 4 as long as the same account is used? What do you think? Thanks in advance for your help. JK
yes, this is defined in EasyLanguage. Global Dictionaries were introduced with Object Oriented EasyLanguage and can for example be used to share data across studies, charts or between applications.
Regards,
ABCTG
20YRTRADER
What is a Global Dictionary? Is this defined in Easy Language?
How can I put this on a Indicator? I want to see it.
Big Mike
I needed this for my own strategies, so I wanted to share it here.
This will allow a dollar limit of daily loss and daily profit targets, whereby if one or the other is hit then the strategy will stop for the day.
If you wanted to make it weekly, it wouldn't be hard to do. The input for daily loss should be a + value (ie: "250" means a loss of 250, don't use "-250").
// for use in signal
inputs:
dailyprofit(500),
dailyloss(250);
vars:
todaynet(0),
yesterdaynet(0);
if date <> date[1] then begin
yesterdaynet = NetProfit;
end;
if MarketPosition = 0 then begin
todaynet = NetProfit - yesterdaynet;
end;
condition1 = -dailyloss < todaynet and todaynet < dailyprofit;
// entries
if condition 1 then begin
// put your long and short code here
end;
Condition1 evaluates to true if the profit for today is between the max loss and max profit. It evaluates to false if one or the other is exceeded. So place your entry code (buy/sellshort) inside the if block.
You can do it reference to the account balance...
I have not the code to hand, but I can give you later..
20YRTRADER
Mike - I was wondering if something similar to this could be implemented for multiple TradeStation (TS) Workspaces? I trade 4 futures markets in TS and my goal for each day is $2K. I sometimes enter 3-4 markets at the same time (via my strategy) as they are all trending in the same direction. Many times they hit the $2k mark and fall off if am not watching every tick. I can get distracted with bathroom trips, snacks, phone calls, etc. I would like to have all my Workspaces shut down if I reach this daily profit or loss target. Is this a possibility? I was thinking you might be able to cross reference all 4 Workspaces some way. Maybe with a 5th chart referencing the other 4 as long as the same account is used? What do you think? Thanks in advance for your help. JK
what exactly do you want the indicator to display? Strategy related reserved words won't be available in indicators, but TS offers reserved words that make some strategy related values available in indicators like I_closedequity
for example and you can use this to replace Netprofit in the indicator code. You will however still need a strategy applied to the same chart in order for I_closedequity to return a value different than 0.
Regards,
ABCTG
AlexBa
How can I put this on a Indicator? I want to see it.
The following user says Thank You to ABCTG for this post:
I want to see a indicator show me by a line the Current day P/L, so when the P/L take the max profit limit or take the max loss limit, this can be with a green line when the P/L is between the max profit and max loss, else with a red line.
Regards
ABCTG
AlexBa,
what exactly do you want the indicator to display? Strategy related reserved words won't be available in indicators, but TS offers reserved words that make some strategy related values available in indicators like I_closedequity
for example and you can use this to replace Netprofit in the indicator code. You will however still need a strategy applied to the same chart in order for I_closedequity to return a value different than 0.
Mike - Is this code applicable to one strategy or all 2+ open at that time? If I have 2 workspaces open and I have a profit in one workspace of $200 and $300 in another, will it close down both charts at market at a $500 target? Thanks.
John
PS - I am not sure if your old post from 2010 is linked.
the information the reserved word's from the code @Big Mike posted provide are chart specific only.
Regards,
ABCTG
20YRTRADER
Mike - Is this code applicable to one strategy or all 2+ open at that time? If I have 2 workspaces open and I have a profit in one workspace of $200 and $300 in another, will it close down both charts at market at a $500 target? Thanks.
John
PS - I am not sure if your old post from 2010 is linked.
The following user says Thank You to ABCTG for this post:
I'm trying to do exactly the same thing you are, and found a clue that maybe someone here might be able to resolve.
I’m trading Russell Futures (RTY) in Tradestation. I wish to build a Stand-Alone strategy for Daily Profit/Loss Limit. Strategy needs to reference my Beginning-of-Day Account Balance while I’m in a trade; and exit the trade when / if my [user defined] Daily Profit or Loss target is achieved. It would also prohibit any further trades that day, after Profit or Loss target has been achieved.
My 2017 EasyLanguage Essentials Programmers Guide states the following:
• “EasyLanguage allows you to access brokerage account information within your strategies so you can more accurately tie your account money management to your real-time strategy trading.”
and
• “TradeManager reserved words allow you to access fields from the TradeManager’s Balance and Positions tabs. There are specific reserved words for both your equity and futures accounts.”
Further down the page I find reserved words and specific discussion of my strategy goals:
• GetBDAccountNetWorth retrieves the beginning Day Account Net Worth amount from the TradeManager’s Balances tab for the given equity/stock account. Zero will be returned for a futures account or an invalid account.
• GetRTAccountNetWorth retrieves the real-time Account Net Worth amount from the TradeManager’s Balances tab for the given equity account. Zero will be returned for a futures account or an invalid account.
Usage Example (TradeManager account balances):
Vars: NetProf(0);
NetProf = GetRTAccountNetWorth – GetBDAccountNetWorth;
In this example, we can calculate the real-time net profit for an account.
The above ‘Usage example’ is exactly what I want to do (I think), but Zero will be returned for a futures account or an invalid account. What am I supposed to make of that?
Any suggestions for this as a Futures Account strategy?
The following user says Thank You to Vientomarsol for this post:
I am in a similar situation with the same questions. I am going by trial and error now and will post if I have good results. Please update the same if you guys learn more.