Issues with counting number of trades - futures io
futures io



Issues with counting number of trades


Discussion in EasyLanguage Programming

Updated
    1. trending_up 3,159 views
    2. thumb_up 3 thanks given
    3. group 2 followers
    1. forum 5 posts
    2. attach_file 2 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 150,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
 

Issues with counting number of trades

(login for full post details)
  #1 (permalink)
 Iluvchocmilk 
Portland
 
Experience: Beginner
Platform: TradingView
Broker: Tradestation
Trading: Emini ES
 
Posts: 49 since Jul 2020
Thanks: 32 given, 37 received

Hello,

I am having issues with my code not limiting the number of trades in the short direction even though it appears to be working in the long direction.

My original approach was using the BuysToday and ShortsToday counter which was working but then I noticed it was taking more than one trade in the short direction. I then started trying to debug it and tried the TradeCounter approach but that isn't working either. I am wondering if there is a problem I am just missing to see in the code.

Here is an image showing how it is taking more than one short position after the previous one stopped out. Is it registering that it is in fact flat because it stopped out therefore it is flat?





Inputs: StopAmt(15), ProfitAmt(80), MaxTrades(2);

Vars: timestart(0640), timeend(1300), RangeHigh(0),RangeLow(0), BuysToday(0), ShortsToday(0), TradeCounter(0), MP(0);
MP=Marketposition;

If ( MP <> 0 ) and (MP[1] <> MP ) then TradeCounter = TradeCounter+ 1; //Checks to see change in state
if Marketposition=1 then BuysToday=1; //Increments to 1 if there is an open trade
if Marketposition=-1 then ShortsToday=1; //Increments to 1 if there is an open trade

if time >=630 and time <=640 then
Begin
RangeHigh=Highest(High,2); //Defines high of range
RangeLow=Lowest(Low,2); //Defines low of the range
BuysToday=0; ShortsToday=0; TradeCounter=0; //Resets trade counter to zero on the open
End;

If time>=timestart and time <timeend then
Begin
If TradeCounter<MaxTrades and close<RangeLow then Sell Short 1 contracts next bar at market;

If BuysToday=0 and close>RangeHigh then Buy 1 contracts next bar at market;

SetStopLoss(StopAmt);
SetProfitTarget(ProfitAmt);
end;

//TradeCounter<MaxTrades

Visit my futures io Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on futures io?
Dissect open interest data
Crypto Futures
How do I create Strategy with the Second Entry indicator
NinjaTrader
Trading Futures Now TFN Algo
Trading Reviews and Vendors
help with take profit several targets
TradeStation
NT8 Order Management Strategy
The Elite Circle
 
Best Threads (Most Thanked)
in the last 7 days on futures io
Is anyone actually making money?
119 thanks
ApexTraderFunding.com experience and review
11 thanks
Artificial Intelligence (AI) and Chat GPT
8 thanks
Want your NinjaTrader indicator created, free?
7 thanks
Dax market profile (FDAX, TPO)
7 thanks
 
(login for full post details)
  #2 (permalink)
 kevinkdog   is a Vendor
 
 
Posts: 3,463 since Jul 2012
Thanks: 1,827 given, 6,984 received

It looks like all your short trades (except last one) entered and exited on the same bar. So the MarketPosition keyword would never get updated - things only get updated at end of the bar.

I tried adjusting your code, I did not verify or test this, but I did the buy and sell trade counter differently. Maybe this will work better for you.

 
Code
Inputs: StopAmt(15), ProfitAmt(80), MaxTrades(2);

Vars: timestart(0640), timeend(1300), RangeHigh(0),RangeLow(0), BuysToday(0), ShortsToday(0), TradesAtStart(0);


if time >=630 and time <=640 then
Begin
RangeHigh=Highest(High,2); //Defines high of range
RangeLow=Lowest(Low,2); //Defines low of the range
BuysToday=0; ShortsToday=0; TradesAtStart=TotalTrades; //Resets trade counter to zero on the open
End;

If time>=timestart and time <timeend then
Begin
If TradesAtStart-TotalTrades<MaxTrades and ShortsToday<>1 and close<RangeLow then begin
 Sell Short 1 contracts next bar at market;
 ShortsToday=1;
end;

If TradesAtStart-TotalTrades<MaxTrades and BuysToday<>1  and close>RangeHigh then begin
 Buy 1 contracts next bar at market;
 BuysToday=1;
end;

SetStopLoss(StopAmt);
SetProfitTarget(ProfitAmt);
end;

If it doesn't work, give me some exact chart details of where it is a problem, and I can debug easier...

Follow me on Twitter Reply With Quote
The following user says Thank You to kevinkdog for this post:
 
(login for full post details)
  #3 (permalink)
 Iluvchocmilk 
Portland
 
Experience: Beginner
Platform: TradingView
Broker: Tradestation
Trading: Emini ES
 
Posts: 49 since Jul 2020
Thanks: 32 given, 37 received



kevinkdog View Post
It looks like all your short trades (except last one) entered and exited on the same bar. So the MarketPosition keyword would never get updated - things only get updated at end of the bar.

I tried adjusting your code, I did not verify or test this, but I did the buy and sell trade counter differently. Maybe this will work better for you.

 
Code
Inputs: StopAmt(15), ProfitAmt(80), MaxTrades(2);

Vars: timestart(0640), timeend(1300), RangeHigh(0),RangeLow(0), BuysToday(0), ShortsToday(0), TradesAtStart(0);


if time >=630 and time <=640 then
Begin
RangeHigh=Highest(High,2); //Defines high of range
RangeLow=Lowest(Low,2); //Defines low of the range
BuysToday=0; ShortsToday=0; TradesAtStart=TotalTrades; //Resets trade counter to zero on the open
End;

If time>=timestart and time <timeend then
Begin
If TradesAtStart-TotalTrades<MaxTrades and ShortsToday<>1 and close<RangeLow then begin
 Sell Short 1 contracts next bar at market;
 ShortsToday=1;
end;

If TradesAtStart-TotalTrades<MaxTrades and BuysToday<>1  and close>RangeHigh then begin
 Buy 1 contracts next bar at market;
 BuysToday=1;
end;

SetStopLoss(StopAmt);
SetProfitTarget(ProfitAmt);
end;

If it doesn't work, give me some exact chart details of where it is a problem, and I can debug easier...


@kevinkdog Thanks for the reply.

Do you know if the TotalTrades function counts the total number of trades for all the data you have loaded or just the total number of trades on the current day?

The EL reference PDF doesn't specify.

TotalTrades


Otherwise it does seem like the markeposition not calculating until the end of the bar was issue.

Visit my futures io Trade Journal Started this thread Reply With Quote
 
(login for full post details)
  #4 (permalink)
 Iluvchocmilk 
Portland
 
Experience: Beginner
Platform: TradingView
Broker: Tradestation
Trading: Emini ES
 
Posts: 49 since Jul 2020
Thanks: 32 given, 37 received

@kevinkdog I answered my own question there haha

If it only calculated the trades for the day, you wouldn't need the TradesAtStart-TotalTrades logic in the code.

Visit my futures io Trade Journal Started this thread Reply With Quote
The following user says Thank You to Iluvchocmilk for this post:
 
(login for full post details)
  #5 (permalink)
RandyK0615
Grand Junction, CO
 
 
Posts: 2 since Mar 2022
Thanks: 0 given, 1 received

Here's the way I might code it but I have not tested this code. It will run a print statement that should update on every tick. That way if you exit intra-bar
it should show that you are flat and keep the count right. But like I said, I have not tested this and have made mistakes before. I like to use intrabarpersist
so I can see what is happening on every tick but there are times that is just a waste of CPU. Also, I give the TradeCounter positive and negative values so
you can see the count of each. Try this and see if it works (I emphasize it is not tested):

Inputs: StopAmt(15), ProfitAmt(80), MaxTrades(2);

Vars: timestart(0640), timeend(1300), RangeHigh(0),RangeLow(0), intrabarpersist TradeCounter(0), intrabarpersist MP(0), intrabarpersist TradeCount(0);

MP = Marketposition;

If ( MP > 0 ) and (MP[1] <= 0 ) then TradeCounter = TradeCounter + 1; //Count Long Trades
If ( MP < 0 ) and (MP[1] >= 0 ) then TradeCounter = TradeCounter - 1; //Count Short Trades

TradeCount = Absvalue(TradeCounter);

if (time >= 630 and time <= 640) then Begin
RangeHigh = Highest(High,2); //Defines high of range
RangeLow = Lowest(Low,2); //Defines low of the range
TradeCounter = 0; TradeCount = 0;//Resets trade counter to zero on the open
End;

If time >= timestart and time < timeend then Begin
If TradeCount < MaxTrades and close < RangeLow then Sell Short 1 contracts next bar at market;

If TradeCount <= 0 and close > RangeHigh then Buy 1 contracts next bar at market;

SetStopLoss(StopAmt);
SetProfitTarget(ProfitAmt);

print(" TradeCounter = " + Numtostr(TradeCounter, 0) + "| TradeCount = " + Numtostr(TradeCount, 0) + "| MP[1] = " + Numtostr(MP[1], 0) + "| MP = " + Numtostr(MP, 0) + newline);
end;

Reply With Quote
 
(login for full post details)
  #6 (permalink)
RandyK0615
Grand Junction, CO
 
 
Posts: 2 since Mar 2022
Thanks: 0 given, 1 received

I woke up during the night and realized there is an error in the code I posted yesterday. It needed to have separate counters for long and short. Here's
what I came up with this time- again not tested.



Inputs: StopAmt(15), ProfitAmt(80), MaxTrades(2);

Vars: timestart(0640), timeend(1300), RangeHigh(0),RangeLow(0), intrabarpersist TradeCounterS(0), intrabarpersist TradeCounterL(0), intrabarpersist MP(0), intrabarpersist TradeCount(0);

MP = Marketposition;

If ( MP > 0 ) and (MP[1] <= 0 ) then TradeCounterL = TradeCounterL + 1; //Count Long Trades
If ( MP < 0 ) and (MP[1] >= 0 ) then TradeCounterS = TradeCounterS + 1; //Count Short Trades

TradeCount = TradeCounterL + TradeCounterS ;

if (time >= 630 and time <= 640) then Begin
RangeHigh = Highest(High,2); //Defines high of range
RangeLow = Lowest(Low,2); //Defines low of the range
TradeCounterL = 0; TradeCounterS = 0; TradeCount = 0;//Resets trade counters to zero on the open
End;

If time >= timestart and time < timeend then Begin
If TradeCount < MaxTrades and close < RangeLow then Sell Short 1 contracts next bar at market;

If TradeCount <= 0 and close > RangeHigh then Buy 1 contracts next bar at market;

SetStopLoss(StopAmt);
SetProfitTarget(ProfitAmt);

print(" TradeCounterL " + Numtostr(TradeCounterL, 0) + " TradeCounterS " + Numtostr(TradeCounterS, 0) +" TradeCount " + Numtostr(TradeCount, 0) + newline);
end;

Reply With Quote
The following user says Thank You to RandyK0615 for this post:


futures io Trading Community Platforms and Indicators EasyLanguage Programming > Issues with counting number of trades


Last Updated on June 12, 2022


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
     



Copyright © 2023 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada), 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