NexusFi: Find Your Edge


Home Menu

 





Issues with counting number of trades


Discussion in EasyLanguage Programming

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




 
Search this Thread

Issues with counting number of trades

  #1 (permalink)
 Iluvchocmilk 
Portland
 
Experience: Beginner
Platform: TradingView
Broker: Tradestation
Trading: Emini ES
Posts: 49 since Jul 2020
Thanks Given: 32
Thanks Received: 37

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 NexusFi Trade Journal Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
MC PL editor upgrade
MultiCharts
Better Renko Gaps
The Elite Circle
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
REcommedations for programming help
Sierra Chart
Trade idea based off three indicators.
Traders Hideout
 
  #2 (permalink)
 kevinkdog   is a Vendor
 
Posts: 3,666 since Jul 2012
Thanks Given: 1,892
Thanks Received: 7,360

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
Thanked by:
  #3 (permalink)
 Iluvchocmilk 
Portland
 
Experience: Beginner
Platform: TradingView
Broker: Tradestation
Trading: Emini ES
Posts: 49 since Jul 2020
Thanks Given: 32
Thanks Received: 37



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 NexusFi Trade Journal Started this thread Reply With Quote
  #4 (permalink)
 Iluvchocmilk 
Portland
 
Experience: Beginner
Platform: TradingView
Broker: Tradestation
Trading: Emini ES
Posts: 49 since Jul 2020
Thanks Given: 32
Thanks Received: 37

@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 NexusFi Trade Journal Started this thread Reply With Quote
Thanked by:
  #5 (permalink)
RandyK0615
Grand Junction, CO
 
Posts: 4 since Mar 2022
Thanks Given: 0
Thanks Received: 1

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
  #6 (permalink)
RandyK0615
Grand Junction, CO
 
Posts: 4 since Mar 2022
Thanks Given: 0
Thanks Received: 1

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
Thanked by:




Last Updated on June 12, 2022


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
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.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts