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'm trying to code a simple exit strategy that I can apply to my discressionary trades.
Let's call the bar prior to my entry the signal bar. I'm entering at the High of the signal bar, and I'd like the strategy to place a stop loss order at the low of the signal bar. The target is a function of the high of the signal bar - low of the signal bar times a multiplier.
My problem is that the High and Low of the signal bar keep being recalculated every bar. It's frustrating to watch the orders on the chart keep changing with every bar that passes. Lol.
How can I store these variables in my code so they persist at the original values?
Any help or direction would be great.
Thanks,
Jeff
Can you help answer these questions from other members on NexusFi?
Do you have any code to show? Might be easier if we can see what you're doing.
But, if it was me...
I would have two variables declared: EntTrgt(0) and ExtTrgt(0)
At the completion of the SIGNAL bar, I would set the value of both of those variables as follows:
If Barstatus = 2 then begin
EntTrgt = H;
ExtTrgt = L;
end;
Also, you are most likely missing some sort of switch that turns on or off so that this test only happens when the SIGNAL bar actually becomes the SIGNAL bar. For example, lets say, if x, y and z then SetTargets = True;
Therefore, going back to the previous suggestion:
If Barstatus = 2 and SetTargets then begin
EntTrgt = H;
ExtTrgt = L;
SetTargets = False;
end;
Of course, then you will need to write out your entry code, but hopefully this helps some.
Thanks for the insights so far. Here is a simple long only script I whipped up that demonstrates the problem....
[IntrabarOrderGeneration = True];
//Long Only
Inputs:
RewardMultiplier( 2 );
Vars:
EMA( 0 ),
ClickStop( 0 ),
ClickTarget( 0 );
EMA = XAverage( C, 21 );
If O < C and L > EMA Then Begin
Buy 1 Contract Next bar at Market;
End;
If Marketposition > 0 Then Begin
ClickStop = (High[ 1 ] - Low[ 1 ]) * 4 ;
ClickTarget = ClickStop * RewardMultiplier;
setstoploss_pt(ClickStop);
setprofittarget_pt(ClickTarget);
end;
It doesn't seem to matter where I put the "ClickStop" Calculation, it keeps happening every bar. I'd like it to store the low of the "signal bar" and leave the top and target orders where they start.
So if you are trying to set targets based on the candle that met your criteria (If O < C and L > EMA), then I would do what I suggested earlier and save the values of the Hi and Lo to declared variables that get defined once and stays that way until you need to redefine it at a later time. In this case, you are saying if you are long, then set the stop and targets off the last bar... BUT if that doesn't happen then you do it again the next bar and so on. That's why it's recalculating.
The Sim mode on my Multicharts has decided to not load data at the moment, however when I print the values of "stoploss" and "target" they seam to be holding the same value as new bars prints.
Am I correct in thinking that this code should be doing the trick?
the way you wrote your code you allow the values to get updated whenever the expression "Barstatus=2 and O < C and L > EMA" is fulfilled i.e. they can change while you are in a position. If you want to prevent the values from changing while you are in a long trade you might want to update the values only while you are not yet long.
Thanks for the observation, I see what you mean. Should I move the calculations to calculate after a position has been opened?
So include them after Marketposition > 0 ? How else could I do this?
that would be the opposite of what I wrote in my reply and likely still update the values with every while you are in a position. Allowing the variables to update while you are not yet long will prevent them from changing while you are long and in turn should accomplish what you have in mind.
Trading: Primarily Energy but also a little Equities, Fixed Income, Metals, U308 and Crypto.
Frequency: Many times daily
Duration: Never
Posts: 5,095 since Dec 2013
Thanks Given: 4,445
Thanks Received: 10,288
If (O < C and L > EMA and Position = 0) Then Begin
Buy 1 Contract Next bar at Market;
ClickStop = (High - Low) * 4 ;
ClickTarget = ClickStop * RewardMultiplier;
setstoploss_pt(ClickStop);
setprofittarget_pt(ClickTarget);
End;