EasyLanguage Daily Profit and Daily Loss limit in strategy - futures io
futures io



EasyLanguage Daily Profit and Daily Loss limit in strategy


Discussion in EasyLanguage Programming

Updated
      Top Posters
    1. looks_one ABCTG with 6 posts (4 thanks)
    2. looks_two 20YRTRADER with 3 posts (0 thanks)
    3. looks_3 AlexBa with 3 posts (0 thanks)
    4. looks_4 djplastic with 2 posts (0 thanks)
      Best Posters
    1. looks_one Big Mike with 16 thanks per post
    2. looks_two Ranger with 6 thanks per post
    3. looks_3 insideday with 3 thanks per post
    4. looks_4 ABCTG with 0.7 thanks per post
    1. trending_up 25,508 views
    2. thumb_up 32 thanks given
    3. group 17 followers
    1. forum 25 posts
    2. attach_file 1 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

EasyLanguage Daily Profit and Daily Loss limit in strategy

(login for full post details)
  #1 (permalink)
 Big Mike 
Site Administrator
Swing Trader
Data Scientist & DevOps
Manta, Ecuador
 
Experience: Advanced
Platform: Custom solution
Trading: Futures & Crypto
 
Big Mike's Avatar
 
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").

 
Code
                            
// for use in signal
inputs:
dailyprofit(500),
dailyloss(250);

vars:
todaynet(0),
yesterdaynet(0);

if 
date <> date[1then 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

We're here to help -- just ask

For the best trading education, watch our webinars
Searching for trading reviews? Review this list

Follow us on Twitter, YouTube, and Facebook

Support our community as an Elite Member:
https://futures.io/elite/

Visit other sites? Please spread the word about your experience with our community!
Follow me on Twitter Visit my futures io Trade Journal Started this thread Reply With Quote

Journal Challenge April 2021 results (now extended!):
Competing for $1800 in prizes from Jigsaw
looks_oneMaking a Living with the Microsby sstheo
(143 thanks from 20 posts)
looks_twoSalao's Journalby Salao
(45 thanks from 9 posts)
looks_3Deetee’s DAX Trading Journal (time based)by Deetee
(35 thanks from 16 posts)
looks_4Learning to Profit - A journey in algorithms and optionsby Syntax
(15 thanks from 13 posts)
looks_5Maybe a little bit different journalby Malykubo
(14 thanks from 14 posts)
 
Best Threads (Most Thanked)
in the last 7 days on futures io
Big Mike in Ecuador
76 thanks
Would You Sell Your System?
60 thanks
The Crude Dude Oil Trading System
57 thanks
The New Micro Contract - MICRO BITCOIN coming May 2021
25 thanks
futures io site changelog and issues/problem reporting
21 thanks
 
(login for full post details)
  #3 (permalink)
Ranger
Port St Lucie, FL
 
 
Posts: 46 since Jan 2010
Thanks: 9 given, 48 received


Here's a variation of your signal BM; perhaps others may find this useful too.


RANGER

Attached Files
Register to download File Type: txt Net Profit and Time Limit Code Snippet(Signal).txt (2.0 KB, 403 views)
Reply With Quote
The following 6 users say Thank You to Ranger for this post:
 
(login for full post details)
  #4 (permalink)
insideday
vienna
 
 
Posts: 22 since Aug 2009
Thanks: 0 given, 38 received

This is the strategy template from the TS wiki, maybe it is useful for someone who wants to add some code lines to their straregy

 
Code
// EasyLanguageTemplate  [ Strategy ] 
 
// Just select features from the Template 
// that you need for your strategy 
// delete the ones you do not need. 
 
 
[ IntraBarOrderGeneration = False ]; 
// True = Enables Entries or Exits  
// on every Tick during the bar 
// Much more complex coding 
// If using IOG then Backtesting  
// must have Look Inside Bar Testing 
// activated to be approximately accurate 
 
[ BaseCurrency = Account ]; 
// Can ONLY be put on Forex Symbol  
// Give an error on non Forex symbols 
// Forces account currency to be  
// the default for reports, etc. 
 
//[ SameTickOpt = True ]; 
// Can ONLY be put on Indicators 
// Forces a change in price by one tick 
// before TS will process code 
 
//[ InfiniteLoopDetect = False ]; 
// Forces Loop Detection OFF 
 
[ LegacyColorValue = True ]; 
// True = Old EL Colors as default 
// False = New RGB Colors as default 
 
 
 
Inputs: 
    SMAFLength(18), 
    SMASLength(34), 
    StartTime(945), 
    EndTime(1145), 
    LTrailingTicks(0), 
    STrailingTicks(0); 
 
 
Variables: 
    //{ Setup Variables For Inputs } 
    vSMAFLength(0), 
    vSMASLength(0), 
    vStartTime(0), 
    vEndTime(0), 
    vLTrailingTicks(0), 
    vSTrailingTicks(0), 
 
    Price    ( "" ), 
    Decimals( 0 ), 
 
    { Daily NetProfits } 
    vNetProfit(0), 
    vDailyNetProfit(0), 
 
 
    { Calculation Variables } 
    IntraBarPersist SMAFast(0), 
    IntraBarPersist SMASlow(0), 
 
    { Trade High & Low } 
    IntraBarPersist TLHigh(0), 
    IntraBarPersist TLLow(0), 
 
    { Daily High, Low, Open & Close when using IntraDay Charts } 
    IntraBarPersist PriorDayHigh(0), 
    IntraBarPersist PriorDayLow(0), 
    IntraBarPersist PriorDayOpen(0), 
    IntraBarPersist PriorDayClose(0), 
    IntraBarPersist TodayOpen(0), 
    IntraBarPersist TodayHigh(0), 
    IntraBarPersist TodayLow(0), 
 
    { Weekly High & Weekly Low when using IntraDay Charts } 
    IntraBarPersist WeekHigh(0), 
    IntraBarPersist WeekLow(0), 
 
    { Monthly High & Monthly Low when using IntraDay Charts } 
    IntraBarPersist MonthHigh(0), 
    IntraBarPersist MonthLow(0), 
 
    { TradeStation Variables } 
    IntraBarPersist MP(0),            // MarketPosition 
    IntraBarPersist MP1(0),             // Prior Tick MarketPosition 
    IntraBarPersist PP(0),            // PositionProfit 
    IntraBarPersist BSE(0),            // BarsSinceEntry 
    IntraBarPersist BSExit(0),          // BarsSinceExit 
    IntraBarPersist BarsLastExit(0), 
    IntraBarPersist CC(0),            // CurrentContracts 
    IntraBarPersist CC1(0),             // Prior Tick CurrentContracts 
    IntraBarPersist PPPC(0),        // PositionProfit Per Contract     
    IntraBarPersist PPPE(0),        // PositionProfit Per Entry     
    IntraBarPersist CE(0),        // CurrentEntries 
    IntraBarPersist EP(0),            // EntryPrice 
    IntraBarPersist AEP(0),            // Average Entry Price 
    IntraBarPersist vBarNumber(0),      // BarNumber 
    IntraBarPersist LastBar(False),     // LastBarOnChart 
    IntraBarPersist vCategory(0),       // Category 
    IntraBarPersist VCurrentBar(0), 
    IntraBarPersist CE(0),            // CurrentEntries 
    IntraBarPersist PPTotal(0),            // PositionProfit Total 
    OneTick(0), 
    vBarType(0), 
    vBarInterval(0); 
 
 
 
//{ ++++++++++++++++++++++++++++++ } 
//{ +++++++ CurrentBar = 1 +++++++ } 
//{ ++++++++++++++++++++++++++++++ } 
If CurrentBar = 1 then 
begin 
    // Place code here you want processed ONLY on the First Bar of the Chart 
  
    //{ Category Setup } 
    vCategory=Category; 
 
    //{ Decimal Place SetUp by Goose } 
    Price = NumToStr( MinMove/PriceScale, 10 ) ; 
    for Value2 = 1 to 12 
    begin 
            if RightStr( Price, 1 ) = "0" then 
        Price = LeftStr( Price, StrLen( Price ) - 1 ) 
    else 
        Value2 = 12 ;       // stop loop when zero not found 
    end ; 
 
    Decimals = StrLen(Price) -2 ;    // subtract 2 for decimals 
 
    { One Tick } 
    OneTick = MinMove / PriceScale; 
 
    //{ Order Entry Routing SetUp For Stocks } 
    If vCategory = 2 then SetRouteName("ARCX"); 
 
 
    //{ BarType - // 0=tick 1=IntraDay 2=Daily 3=Weekly 4=Monthly } 
    vBarType=BarType; 
    vBarInterval=BarInterval; 
 
    //{ Store Inputs into Variables - reduced CPU load } 
    vSMAFLength     = SMAFLength; 
    vSMASLength     = SMASLength; 
    vStartTime         = StartTime; 
    vEndTime         = EndTime; 
    vLTrailingTicks = LTrailingTicks; 
    vSTrailingTicks = STrailingTicks; 
 
end; 
 
 
//{ +++++++++++++++++++++++++++++++ } 
//{ ++++ TradeStation Variables +++ } 
//{ +++++++++++++++++++++++++++++++ } 
//{ Get TradeStation Variables - use commands one time only to reduce CPU load} 
LastBar = LastBarOnChart; 
MP1 = MP;            // Prior Tick MarketPosition 
MP = MarketPosition; 
EP = EntryPrice; 
AEP= AvgEntryPrice; 
PP = PositionProfit; 
CC1 = CC;            // Prior Tick CurrentContracts  
CC = CurrentContracts; 
CE = CurrentEntries; 
If CurrentBar = 1 then 
    vBarNumber = 1 
else 
    vBarNumber = vBarNumber[1] + 1; 
 
{ TICK level BarsSinceEntry WorkAround } 
// TradeStation BarsSinceEntry Command is  
// Inaccurate with Stop & Reverse Entries  
// or Scaling In Trades 
// This workaround is always correct 
 
If (MP <> 0 and MP1 = 0)          // This tick in a Trade & Prior tick was Flat 
        or (MP = 1 and MP1 = -1)  // This tick in a LONG Trade & Prior tick was SHORT Trade 
        or (MP = -1 and MP1 = 1)  // This tick in a SHORT Trade & Prior tick was LONG Trade 
        or (CC > CC1)             // Another Entry in the same direction 
        then 
   BSE = 0 
else 
If BarStatus(1) = 2 and MP <> 0 then        // Increase BarSinceEntry Counter at End of Bar only 
    BSE = BSE + 1 
else 
If MP = 0 and MP1 <> 0 then     // This Tick no trade & last tick was in a trade 
    BSE = -999; 
 
{ or - Bar Level BarsSinceEntry WorkAround } 
If (MP <> 0 and MP[1] = 0)       // Current Bar in a Trade, Prior Bar Flat 
or (MP = 1 and MP[1] = -1)       // Current Bar Long, Prior bar Short   
or (MP = -1 and MP[1] = 1)       // Current Bar Short, Prior bar Long 
or CC > CC[1] then               // Scaling In Trade 
begin 
   BSE = 0; 
end 
else 
If MP <> 0 
begin 
   BSE = BSE[1] + 1; 
end 
else  
If MP = 0 then 
begin 
   BSE = -999; 
end; 
 
 
{ BarsSinceExit WorkAround - Bar Level } 
// TradeStation BarsSinceExit Command is Inaccurate with Stop & Reverse Entries and Scaling-In Multiple Entries 
If MP <> 0 then 
begin 
    BarsLastExit = 0; 
end 
else 
If BarsSinceExit(1) = 0 or (MP = 0 and MP[1] <> 0) then 
begin 
    BarsLastExit = BarsLastExit[1] + 1; 
end; 
 
 
{ BarsSinceExit WorkAround - Tick Level } 
// TradeStation BarsSinceExit Command is Inaccurate with Stop & Reverse Entries and Scaling-In Multiple Entries 
 
MP1 = MP; 
MP  = MarketPosition; 
CC1 = CC; 
CC  = CurrentContracts; 
 
If (MP = 0 and MP[1] <> 0)    // This tick bar Flat and Prior tick was in a Trade 
        or CC < CC1           // this tick less contracts  
begin 
    BSExit = 0;   // New Exit Just Occurred this bar set BSExit Variable 
                   // Place any code here you want executed on same bar as exit occurred 
end 
else 
If BarStatus(1) = 2 then  
begin 
    BSExit = BSExit[1] + 1;   // Increase BSExit bar counter for number of bars since last exit occurred 
end; 
 
 
{ BarStatus(1) = 0 WorkAround } 
// Code donated by Goose 
// To process code on the First Tick of a Bar BarStatus(1) = 0 is problematic 
// This workaround provides a reliable solution 
 
 
[IntrabarOrderGeneration = True] 
 
Variables: 
    IntrabarPersist Bar_Number( 0 ) ; 
 
if Bar_Number <> CurrentBar then 
begin 
    Bar_Number = CurrentBar ; 
    // Put code here you want processed on first tick of a bar 
end ; 
 
 
{ Position Profit Per Contract & Per Entry } 
If CE > 0 and CC > 0 then 
begin 
    { PPTotal = PositionProfit Total } 
    PPTotal = PP; 
    { PPPC = PositionProfit Per Contract } 
    If PPTotal <> 0 then 
        PPPC = PPTotal / CC; 
    { PPPE = PositionProfit Per Entry } 
    If PPTotal <> 0 then 
        PPPE = PPTotal / CE; 
end; 
 
 
{ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ } 
{ ++++ IntraDay Calculation of Daily High, Low, Open & Close ++++++ } 
{ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ } 
If Date <> Date[1] then 
begin 
    PriorDayClose = Close[1]; 
    PriorDayHigh = TodayHigh; 
    PriorDayLow = TodayLow; 
    PriorDayOpen = TodayOpen; 
 
    TodayHigh = High; 
    TodayLow = Low; 
    TodayOpen = Open; 
end 
else 
begin 
    TodayHigh = MaxList( High, TodayHigh ) ; 
    TodayLow  = MinList( Low, TodayLow ) ; 
end; 
 
 
{ +++++++++++++++++++++++++++++++++++++++++++++++ } 
{ +++++ IntraDay Chart - Weekly High & Low ++++++ } 
{ +++++++++++++++++++++++++++++++++++++++++++++++ } 
If DayOfWeek( Date ) < DayOfWeek( Date[1] ) then 
begin 
    WeekHigh = High; 
    WeekLow = Low; 
end 
else 
begin 
    WeekHigh = MaxList( High, WeekHigh ) ; 
    WeekLow  = MinList( Low, WeekLow ) ; 
end; 
 
 
{ +++++++++++++++++++++++++++++++++++++++++++++++ } 
{ +++++ IntraDay Chart - Monthly High & Low ++++++ } 
{ +++++++++++++++++++++++++++++++++++++++++++++++ } 
If Month(Date) <> Month(Date[1]) then 
begin 
    MonthHigh = High; 
    MonthLow = Low; 
end 
else 
begin 
    MonthHigh = MaxList( High, MonthHigh ) ; 
    MonthLow  = MinList( Low, MonthLow ) ; 
end; 
 
 
//{ ++++++++++++++++++++++++++++++++++++++ } 
//{ ++++ First Bar of Each New Day  ++++++ } 
//{ ++++++++++++++++++++++++++++++++++++++ } 
If Date <> Date[1] then 
begin 
    // Place code here you want executed on the first bar of each new day 
end; 
 
 
//{ ++++++++++++++++++++++++++++++++++++++++++ } 
//{ ++++ First Bar of Each New Session  ++++++ } 
//{ ++++++++++++++++++++++++++++++++++++++++++ } 
If CurrentSession(0) <> CurrentSession(0)[1] then 
begin 
    // Place code here you want executed on First Bar of each New Session 
end; 
 
 
//{ ++++++++++++++++++++++++++++++++++++++ } 
//{ ++++ First Bar of Today ONLY  ++++++++ } 
//{ ++++++++++++++++++++++++++++++++++++++ } 
If Date <> Date[1] and Date = CurrentDate then 
begin 
    // Place code here you want executed on First Bar if Today is the Current Date 
end; 
 
 
//{ ++++++++++++++++++++++++++++++++++++++++++ } 
//{ ++++ First Bar of Yesterday ONLY  ++++++++ } 
//{ ++++++++++++++++++++++++++++++++++++++++++ } 
If Date <> Date[1] and Date = CurrentDate - 1 then 
begin 
    // Place code here you want executed on First Bar Of Yesterday ONLY 
end; 
 
 
//{ ++++++++++++++++++++++++++++++++++++++++ } 
//{ ++++ First Bar of Each New Week  +++++++ } 
//{ ++++++++++++++++++++++++++++++++++++++++ } 
If (( vCategory = 2 and Date <> Date[1] ) 
    or ( vCategory <> 2 and CurrentSession(0) <> CurrentSession(0)[1] ) ) 
    and ( ( vCategory = 2 and DayOfWeek(Date) = 1 ) 
    or ( vCategory <> 2 and DayOfWeek(Date) = 0 ) ) then 
begin 
    // Place code here you want executed of First Bar of each New Week 
end; 
 
 
//{ ++++++++++++++++++++++++++++++++++++++++ } 
//{ ++++ First Bar of Each New Month  ++++++ } 
//{ ++++++++++++++++++++++++++++++++++++++++ } 
If Month(Date) <> Month(Date[1]) then 
begin 
    // Place code here you want executed of First Bar of each New Month 
end; 
 
 
{ ++++++++++++++++++++++++++++++++++++++++++++++++++ } 
{ ++++++++++  First Bar of MONDAY Only  ++++++++++++ } 
{ ++++++++++++++++++++++++++++++++++++++++++++++++++ } 
// For this example if you wanted to execute code on the first bar of Monday Opening Bar ONLY 
// 0 = Sunday, 1=Monday, 2=Tuesday, etc. 
 
If DayOfWeek(Date) = 1 and Date <> Date[1] then 
begin 
    // Put code here to execute on First Bar of every Monday 
end; 
 
 
 
//{ +++++++++++++++++++++++++++++++++++ } 
//{ ++++ New Trade High & Low   +++++++ } 
//{ +++++++++++++++++++++++++++++++++++ } 
If (MP <> 0 and MP[1] = 0)                       // In Trade This Bar and Prior Bar Was Flat 
    or (MP = 1 and MP[1] = -1 )     // In SHORT Trade This Bar and Prior Bar was LONG Trade 
    or (MP = -1 and MP[1] = 1 )  then   // In LONG Trade This Bar and Prior Bar was SHORT Trade 
begin 
    { First Bar of a New Trade } 
    TLHigh = High; 
    TLLow = Low; 
end 
else 
{ Still in Trade so Track Trade High & Trade Low } 
If MP <> 0 then 
begin 
    TLHigh = MaxList( High, TLHigh ) ; 
    TLLow =  MinList( Low, TLLow ) ; 
end 
else 
{ Flat - Reset Variables } 
If MP = 0 then 
begin 
    TLHigh = 0; 
    TLLow = 9999; 
end; 
 
 
//{ +++++++++++++++++++++++++++++++++++++++ } 
//{ ++++++ Calculations Section  ++++++++++ } 
//{ +++++++++++++++++++++++++++++++++++++++ } 
If Barstatus(1) = 2 then 
begin 
    // Place calculations here you only want to run once per bar at the end of each bar 
 
    // IntraDay Chart Calculation of Daily NetProfit 
    // Strategy Code 
    // Can use this to calculate Daily NetProfit 
    // One use is to stop trading if hit a given daily Profit Objective or Loss Amount 
    If Date = CurrentDate and Date <> Date[1] then 
    begin 
        vNetProfit = NetProfit; 
    end 
    else 
    If Date = CurrentDate then 
    begin 
        { Calculate Daily NetProfits } 
        vDailyNetProfit = NetProfit - vNetProfit; 
    end; 
 
    { Calculations } 
    SMAFast = Average(Close,vSMAFLength); 
    SMASlow = Average(Close,vSMASLength); 
 
end; 
 
 
//{ +++++++++++++++++++++ } 
//{ +++ Entries +++++++++ } 
//{ +++++++++++++++++++++ } 
If Time >= vStartTime and Time < vEndTime then 
begin 
    //{ Order Routing SetUp For Stocks } 
    If vCategory = 2 then 
        SetRouteName("ARCX"); 
 
    //{ LONG Initial Entry } 
    If SMAFast crosses above SMASlow then 
         Buy ("L-SMA-X") next bar at market; 
 
    //{ SHORT Initial Entry } 
    If SMASlow crosses under SMASlow then 
        SellShort ("S-SMA-X") next bar at market; 
 
end;     // If BarStatus(1) = 2 then 
 
 
//{ +++++++++++++ } 
//{ ++ Exits ++++ } 
//{ +++++++++++++ } 
If MP <> 0 then 
begin 
    //{ Order Routing SetUp for Stocks } 
    If vCategory = 2 then 
        SetRouteName("ARCX"); 
 
    // Exits Here 
 
    If MP = 1 and Close < TLHigh - (vLTrailingTicks * OneTick) then 
        Sell ("LX-Trailing") next bar at market; 
 
    If MP = -1 and Close > TLLow + (vSTrailingTicks * OneTick) then 
        BuyToCover ("SX-Trailing") next bar at market; 
 
 
end;    // If MP <> 0 then

Reply With Quote
 
(login for full post details)
  #5 (permalink)
 mengelbrecht 
copenhagen, denmark
 
Experience: Intermediate
Platform: multicharts, Ninja
Broker: IB & Kinetick
Trading: ES
 
mengelbrecht's Avatar
 
Posts: 47 since Aug 2010
Thanks: 16 given, 22 received


Big Mike View 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").

 
Code
                            
// for use in signal
inputs:
dailyprofit(500),
dailyloss(250);

vars:
todaynet(0),
yesterdaynet(0);

if 
date <> date[1then 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


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.

What am i missing ??

Rgds Mengelbrecht


Multicharts - Code:
inputs:
FastLength( 12 ),
SlowLength( 26 ),
MACDLength( 9 ),
starttrade (0800),
endtrade(1600),
dailyprofit(500),
dailyloss(250);


Variables:
todaynet(0),
yesterdaynet(0),
var0( 0 ),
var1( 0 ),
var2( 0 ) ;

var0 = MACD( Close, FastLength, SlowLength ) ;
var1 = XAverage( var0, MACDLength ) ;
var2 = var0 - var1 ;

if date <> date[1] then begin
yesterdaynet = NetProfit;
end;


todaynet = NetProfit + openpositionprofit - yesterdaynet;

condition1 = var2 crosses under 0 ;
condition2 = var2 crosses over 0 ;

condition98 = (-dailyloss < todaynet and todaynet < dailyprofit) and (Time > starttrade and time < endtrade);


if condition98 then begin
if condition1 then sellshort ( "MacdSE" ) next bar at market;
if condition2 then buy ( "MacdLE" ) next bar at market ;
end;

Reply With Quote
 
(login for full post details)
  #6 (permalink)
volatilius
Toronto, Canada
 
 
Posts: 1 since Jan 2015
Thanks: 0 given, 0 received

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!

Reply With Quote
 
(login for full post details)
  #7 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,238 since Apr 2013
Thanks: 417 given, 1,489 received

volatilius,

post your code and I am sure someone here in the forum can point you in the right direction.

Regards,

ABCTG


volatilius View Post
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!


Follow me on Twitter Reply With Quote
The following user says Thank You to ABCTG for this post:
 
(login for full post details)
  #8 (permalink)
 mengelbrecht 
copenhagen, denmark
 
Experience: Intermediate
Platform: multicharts, Ninja
Broker: IB & Kinetick
Trading: ES
 
mengelbrecht's Avatar
 
Posts: 47 since Aug 2010
Thanks: 16 given, 22 received


volatilius View Post
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!

Hi volatilius
sorry for the late respons, I have been away from my desk all weekend.
This is code that someone send me:

inputs: dailyprofit(300),dailyloss(300);

variables: todaynet(0), yesterdaynet(0), priordate (date) ;


once
begin
priordate = Date - 1;
end;

if date > priordate then begin
priordate = date;
yesterdaynet = NetProfit;
end;

todaynet = NetProfit + openpositionprofit - commission – yesterdaynet;

You need to integrate this into the strategy that you have developed

Have fun
Rgds
Mengelbrecht

Reply With Quote
The following user says Thank You to mengelbrecht for this post:
 
(login for full post details)
  #9 (permalink)
ELong
Chicago
 
 
Posts: 4 since Apr 2013
Thanks: 3 given, 0 received

Hello Mike,

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?

Reply With Quote
 
(login for full post details)
  #10 (permalink)
djplastic
Prague
 
 
Posts: 2 since Nov 2013
Thanks: 1 given, 0 received


Hi,

is there way to get yesterday Netprofit to compare with todays Netprofit please?

Reply With Quote
 
(login for full post details)
  #11 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,238 since Apr 2013
Thanks: 417 given, 1,489 received

djplastic,

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

Follow me on Twitter Reply With Quote
The following user says Thank You to ABCTG for this post:
 
(login for full post details)
  #12 (permalink)
djplastic
Prague
 
 
Posts: 2 since Nov 2013
Thanks: 1 given, 0 received

thank you ABCTG , I tried modify code from Mike post 1 and it wasnt exactly what I wanted, now it is much better

Reply With Quote
 
(login for full post details)
  #13 (permalink)
 Bullywig 
Chicago
 
Experience: Intermediate
Platform: NinjaTrader
Broker: NinjaTrader Brokerage
Trading: FDAX, NQ, HG, SI, CL
 
Bullywig's Avatar
 
Posts: 55 since Oct 2015
Thanks: 32 given, 17 received


Big Mike View 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").

 
Code
                            
// for use in signal

inputs:
dailyprofit(500),
dailyloss(250);

vars:
todaynet(0),
yesterdaynet(0);

if 
date <> date[1then 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.

Cheers!

Reply With Quote
 
(login for full post details)
  #14 (permalink)
 20YRTRADER 
Atlanta, GA/USA
 
Experience: Advanced
Platform: Ninja Trader & TradeStation
Broker: TradeStation
Trading: Crude Oil, Dow
 
Posts: 7 since Sep 2015
Thanks: 2 given, 0 received

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

Reply With Quote
 
(login for full post details)
  #15 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,238 since Apr 2013
Thanks: 417 given, 1,489 received

20YRTRADER,

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 View Post
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


Follow me on Twitter Reply With Quote
 
(login for full post details)
  #16 (permalink)
 20YRTRADER 
Atlanta, GA/USA
 
Experience: Advanced
Platform: Ninja Trader & TradeStation
Broker: TradeStation
Trading: Crude Oil, Dow
 
Posts: 7 since Sep 2015
Thanks: 2 given, 0 received

What is a Global Dictionary? Is this defined in Easy Language?

Reply With Quote
 
(login for full post details)
  #17 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,238 since Apr 2013
Thanks: 417 given, 1,489 received

20YRTRADER,

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 View Post
What is a Global Dictionary? Is this defined in Easy Language?


Follow me on Twitter Reply With Quote
 
(login for full post details)
  #18 (permalink)
AlexBa
Santo domingo/ Dominican Republic
 
 
Posts: 5 since Oct 2019
Thanks: 2 given, 0 received

How can I put this on a Indicator? I want to see it.



Big Mike View 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").

 
Code
                            
// for use in signal

inputs:
dailyprofit(500),
dailyloss(250);

vars:
todaynet(0),
yesterdaynet(0);

if 
date <> date[1then 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


Reply With Quote
 
(login for full post details)
  #19 (permalink)
AlexBa
Santo domingo/ Dominican Republic
 
 
Posts: 5 since Oct 2019
Thanks: 2 given, 0 received

You can do it reference to the account balance...
I have not the code to hand, but I can give you later..





20YRTRADER View Post
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


Reply With Quote
 
(login for full post details)
  #20 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,238 since Apr 2013
Thanks: 417 given, 1,489 received

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.

Regards,

ABCTG


AlexBa View Post
How can I put this on a Indicator? I want to see it.


Follow me on Twitter Reply With Quote
The following user says Thank You to ABCTG for this post:
 
(login for full post details)
  #21 (permalink)
AlexBa
Santo domingo/ Dominican Republic
 
 
Posts: 5 since Oct 2019
Thanks: 2 given, 0 received

Hello ABCTG

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 View Post
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.

Regards,

ABCTG


Reply With Quote
 
(login for full post details)
  #22 (permalink)
 20YRTRADER 
Atlanta, GA/USA
 
Experience: Advanced
Platform: Ninja Trader & TradeStation
Broker: TradeStation
Trading: Crude Oil, Dow
 
Posts: 7 since Sep 2015
Thanks: 2 given, 0 received

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.

Reply With Quote
 
(login for full post details)
  #23 (permalink)
 ABCTG   is a Vendor
 
 
Posts: 2,238 since Apr 2013
Thanks: 417 given, 1,489 received

John,

the information the reserved word's from the code @Big Mike posted provide are chart specific only.

Regards,

ABCTG


20YRTRADER View Post
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.


Follow me on Twitter Reply With Quote
The following user says Thank You to ABCTG for this post:
 
(login for full post details)
  #24 (permalink)
Donovan2580
Port St. Lucie, FL
 
 
Posts: 33 since Jul 2016
Thanks: 21 given, 21 received

Thank you for all the info in this thread. I was wondering if anyone has a "stand alone" strategy that will work with tradestation?

I am running a strategy on renko charts with limits (Which everything works fine) but can get a little hairy.

My stand-alone profit targets and stop limits work perfectly so I was hoping to just tack on a separate strategy for max daily profit loss.

Any ideas would be greatly appreciated.

Thanks in advance

Donovan

Reply With Quote
The following user says Thank You to Donovan2580 for this post:
 
(login for full post details)
  #25 (permalink)
Vientomarsol
Port St. Lucie, FL.
 
 
Posts: 25 since May 2016
Thanks: 17 given, 9 received

Hi Donovan 2580,

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?

Reply With Quote
The following user says Thank You to Vientomarsol for this post:
 
(login for full post details)
  #26 (permalink)
tradeswimmer
Hartford CT
 
 
Posts: 3 since Feb 2021
Thanks: 2 given, 1 received

Vientomarsol & Donovan 2580,

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.

-Tradeswimmer

Reply With Quote


futures io Trading Community Platforms and Indicators EasyLanguage Programming > EasyLanguage Daily Profit and Daily Loss limit in strategy


Last Updated on March 11, 2021


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
 

Journal Challenge w/$1,800 in prizes!

April
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts