Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
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 for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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?
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;
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.
If it doesn't work, give me some exact chart details of where it is a problem, and I can debug easier...
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.
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):
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;
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.
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;