Hi,
I need help with AFL. I am doing intraday, if my entry and exit are fulfilled. i want to avoid re entering the Trade for the same stock..
Thanks in advance
Hi,
I need help with AFL. I am doing intraday, if my entry and exit are fulfilled. i want to avoid re entering the Trade for the same stock..
Thanks in advance
apologies to be more clear.. if my buy signal gets triggered and few bars later my sell condition also gets fulfilled. I do not want to re-enter the trade again. Below is my code i get many re entrys on the same day. Please Help
Period=BarsSince(DateNum()!=Ref(DateNum(),-1))+1;
PR=IIf(Open-Close<0,Close-Open,Open-Close); //Range of price
MPR=MA(PR,Period); //Moving Avg of Price Range
upCrssDate=ValueWhen(Cross(WMA(C,36),MA(C,50)),DateNum(),1); //Cross between WMA and MA
dwncrossdate=ValueWhen(Cross(MA(C,50),WMA(C,36)),DateNum(),1); // Cross between MA and WMA
dn = DateNum();
isEndOfDay = dn!= Ref(dn ,1);
SignalBuy=PR>(1.5*MPR) AND Volume>(1.5*Ref(Volume,-1)) AND Close>Open AND DateNum()==upCrssDate AND TimeNum()>=092900 AND TimeNum()<=140000; // AND Ref(high,1)>High+1;
SignalShort=PR>(1.5*MPR) AND Volume>(1.5*Ref(Volume,-1)) AND Close<Open AND DateNum()==dwncrossdate AND TimeNum()>=092900 AND TimeNum()<=140000; // AND Ref(Low,1)<Low-1;
FirstSignalBuy=ExRem(SignalBuy, isEndOfDay);
FirstSignalShort=ExRem(SignalShort, isEndOfDay);
BSSignalBuy= BarsSince(FirstSignalBuy);
BSSignalShort= BarsSince(FirstSignalShort);
BuySignalDate=ValueWhen(SignalBuy,DateNum(),1);
SellSignalDate=ValueWhen(SignalShort,DateNum(),1);
Buy= DateNum()==BuySignalDate AND High>Ref(High,-BSSignalBuy)+1 AND TimeNum()>=092900 AND TimeNum()<=143000 ; //SignalBuy<Barsxday //AND DateNum()==upCrssDate
Short= DateNum()==SellSignalDate AND Low<Ref(Low,-BSSignalShort)-1 AND TimeNum()>=092900 AND TimeNum()<=143000; //SignalShort<Barsxday //AND DateNum()==dwncrossdate
Sell= High>Ref(High,-BSSignalBuy)+3 OR Low<Ref(Low,-BSSignalBuy) OR TimeNum()>=151000;
Cover= Low>Ref(Low,-BSSignalShort)-3 OR High>Ref(High,-BSSignalShort) OR TimeNum()>=151000;
BuyPrice=Ref(High,-BSSignalBuy)+1;
ShortPrice=Ref(Low,-BSSignalShort)-1;
SellPrice=IIf(High>Ref(High,-BSSignalBuy)+3,Ref(High,-BSSignalBuy)+3,IIf(Low<Ref(Low,-BSSignalBuy),Ref(Low,-BSSignalBuy),Close));
CoverPrice=IIf(Low<Ref(Low,-BSSignalShort)-3,Ref(Low,-BSSignalShort)-3,IIf(High>Ref(High,-BSSignalShort),Ref(High,-BSSignalShort),Close));
please help. need this for my analysis
You can use newday variable as a flag and then remove excess signals after new day, which will allow any signal to come only once everyday. For ex:
NewDay = Day()!= Ref(Day(), -1) ;
buy=exrem(buy,newday);
This way buy signal comes only once per day.
still gives me new new buy for the remaining day after the buy is closed with Sell.
where is the code? You post something, then some member replies and you conveniently delete your own post.
Then come back and say code suggest is not working.
Bad for your rep.
sorry i am trying to express in simple terms. I want to avoid re entry in a Intra day trade if my trade is squared off (buy offset with sell and vise versa). I do not want to enter the trade for the same symbol again for the day.
Did you even bother to read these?
@travick I restored previous version of the post of @George-The-Trader so the code is shown
@George-The-Trader sorry I don't have time to look at your code as there is no effort in it for debugging (and I'm not a day trader so I won't be looking) but @akshay.gupta55 's suggestion seems to work fine for a simple intraday code.
FastMA = MA( C, 10 );
Condition1 = Cross( Close, FastMA );
NewDay = Day() != Ref( Day(), -1 ) ;
Buy = Cross( Close, FastMA );
Buy = exrem( buy, newday );
Sell = Cross( FastMA, Close );
////////////////////////
// Explore for debugging
////////////////////////
Filter = 1;
Buy_color = IIf( Buy, colorGreen, colorDefault );
Condition1_color = IIf( Condition1, colorYellow, colorDefault );
Sell_color = IIf( Sell, colorRed, colorDefault );
NewDay_color = IIf( NewDay, colorlightBlue, colorDefault );
SetOption( "NoDefaultColumns", True );
AddtextColumn( Name(), "Ticker" );
AddColumn( DateTime(), "Date", formatDateTime, colorDefault, NewDay_color );
AddColumn( Condition1, "Cross(Close, FastMA)", 1.0, colorDefault, Condition1_color );
AddColumn( Buy, "Buy", 1.0, colorDefault, Buy_color );
AddColumn( Sell, "Sell", 1.0, colorDefault, Sell_color );
////////////////////////
// Chart for debugging
////////////////////////
Plot( Condition1, "C", colorGreen, styleHistogram | styleThick );
Plot( IIf( Buy, 2, 0 ), "B", colorWhite, styleHistogram );
//Plot(IIf(Sell, 1.5,0), "S", colorRed, styleHistogram | styleThick);
Looks like only one Buy signal is acted upon per day.
@George-The-Trader The code you post is highly inefficient, you shouldn't be making repeated multiple calls to the same functions or operations.
It just doubles the work.
I stopped half-way, but after optimizing the first part of your code, the Code-check and Profile reports a gain of 50% (a little more actually) so you can imagine how much unnecessary load that is.
You can go through the optimized code and see how to use variables and do that for the whole bit.
// Optimized version of OP's code
dn = DateNum();
isEndOfDay = dn != Ref(dn ,1); // multi use
Period = BarsSince( isEndOfDay ) +1;
PR=IIf( Open-Close < 0, Close-Open,Open-Close);
MPR=MA(PR,Period);
W36 = WMA(C,36); // only one call
M50 = MA(C,50); // one call
upCrssDate=ValueWhen(Cross( W36, M50), dn ,1); //Cross between WMA and MA
dwncrossdate=ValueWhen(Cross( M50, W36 ), dn,1); // Cross between MA and WMA
// same here
CO = Close > Open;
TTM = TimeNum()>=092900 AND TimeNum()<=140000;
VC = Volume > (1.5*Ref(Volume,-1));
PRMPR = PR>(1.5*MPR);
SignalBuy=PRMPR AND VC AND CO AND dn ==upCrssDate AND TTM;
SignalShort=PRMPR AND VC AND CO AND dn ==dwncrossdate AND TTM ;
// The rest of the code still needs to be checked
This is just an example, so I used shorthand for the variables to save time. Big performance difference if the whole code is rewritten.
@portfoliobuilder Larry, your test with Buy signal is correct, an ExRem with NewDay will ensure only one signal per day.
However, the Sell signal hasn't been restricted, so in the test code you can get many repeated Sell. Anyway, maybe you were just testing ExRem with Buy and overlooked that but your point is right with @akshay.gupta55's suggestion as a possible way.
OP however thinks otherwise.
@Tomasz thanks for catching my post and reverting OP's.
Not only did OP come back and say it doesnt work, whereas he should have posted modified code with snapshot to prove that its not working but instead chose to delete his own post.
Anyway, for a working solution, I have to assume, and therefore can make an "ass out of u and me"
Cite: Define Ass: 2. informal•British a foolish or stupid person. source: Google
In this variant the code will take only one Trade per day (intraday) starting with a Buy and subsequently exit (Sell).
/// Author: @travick
FastMA = MA( C, 10 );
NewDay = Day() != Ref( Day(), -1 );
SigBuy = Cross( Close, FastMA );
SigSell = Cross( FastMA, Close );
Buy = SigBuy;
Buy = ExRem( Buy, NewDay );
InTrade = Ref( Flip( Buy, SigSell), -1);
// useful Flip() for Sell doing opposite of ExRem() in Buy
Sell = ( SigSell AND InTrade )
OR ( TimeNum() > 155000 AND InTrade );
// Close trade with Sell signal or End of Day by Time
shape1 = Buy * shapeUpArrow + Sell * shapeDownArrow;
Plot(C, "Close", colorDefault, styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
PlotShapes( shape1, IIf( Buy, colorLime, colorLightOrange ), 0, IIf( Buy, Low, High ), -15 );
RAW Signals are like this:
And if the Entry is ANY (Long or Short) first then the simple thing does it
Buy = SigBuy;
Sell = SigSell;
Buy = ExRem( Buy, NewDay );
Sell = ExRem( Sell, NewDay );
Short = Sell;
Cover = Buy;
Brainstorming has already begun in New Year's hangover mode
@travick yes I had deliberately left the SELL signals untouched but with a commented out line in the chart so the OP could work on that himself. But you have done a nice job completing and cleaning up the code. My suggestion to the OP is to add methods of "debugging" his code like an Exploration and a Chart so that he can find out what his code and variables are creating and how they respond to changes such as ExRem
and Flip
.
Thank you @travick @portfoliobuilder @akshay.gupta55 @Tomasz. really appreciate the effort to optimize my code. i debugged by code and found the repeated signals is a result of
FirstSignalBuy=ExRem(SignalBuy, isEndOfDay)
the buy signal stays active (1) till end of the day even after it is offset by the sell signal . There by generating new buys. I have fixed it by putting an condition to buy which checks if a sell has occurred after the buy. which avoids further buys. ( iam learning quickly, but lot more to go..Please bare with me)
Thank you for the guidance
Hello. I was looking for a topic and this was the closest to it. How would I enter a command to not re-enter a trade for at least 21 days after selling that particular stock?
Thanks.
@Matt_Joinery perhaps this will help
Thanks man. That was it.
you guy doing really a nice job for learners like us....thanks