Home › Forums › Trading System Mentor Course Community › AmiBroker Coding and AFL › Converting a Number To An Array
- This topic is empty.
-
AuthorPosts
-
June 20, 2021 at 2:14 am #102143TerryDunneParticipant
Hi all,
I’m trying to modify some back test code so that I can vary leverage/position size based on some criteria (probably something simple like Volatility).
I’m having difficulty making this work as the following line of code
SetOption(“AccountMargin”,Margin);
won’t accept an array, which is what I get if I use an IIFstatement to attempt to calculate margin.
If anybody has tried to do what I’m attempting or has any suggestions on how I should go about it, I’d appreciate being pointed in the right direction.
Thanks in advance,
Terry
June 20, 2021 at 5:43 pm #113466RobertMontgomeryParticipantI’m no help yet in solving this, but have been curious how this would test out to vary stretch with volatility or something similar. Seems like the solution would apply to testing either.
June 20, 2021 at 11:23 pm #113470TerryDunneParticipantHi Rob,
I think potentially being able to turn down leverage and turn up stretch in certain market conditions could (hopefully) be really worthwhile in both improving return and reducing drawdown.
Hopefully some of the expert coders here might have some insights/siggestions.
Best wishes,
Terry
June 21, 2021 at 12:15 am #113471Craig FisherKeymasterMargin adjustment can’t be done with the SetOption function as far as I am aware.
CBT has a MarginLoan object but my understanding is that it’s read only and therefore could not be adjusted.
Stretch alone is a simpler way:
Stretch = IIF(Conditon, 0.3, 0.5);June 21, 2021 at 4:58 am #113472TerryDunneParticipantHi Craig,
Thanks for your response, I won’t waste any more time on that.
Is there any way to not use the SetOption function?
Regards,
Terry
June 21, 2021 at 5:53 am #113476Craig FisherKeymasterFor margin, SetOption or the analyser setting.
the former overrides the latter.June 21, 2021 at 8:11 am #113478TerryDunneParticipantThanks Craig. It seems like I’m not going to be able to do what I’ve been trying to do.
June 21, 2021 at 10:47 am #113479JulianCohenParticipantTerry if you are trying to change the leverage, then changing the number of positions allowed will do that.
If you are running 40 positions at 10% then you can change to 30 positions at 10% in lower/higher volatility. I think you can do that in AFL…
June 21, 2021 at 8:19 pm #113480TerryDunneParticipantHi Julian,
That’s worth trying, thanks for the suggestion.
It’s not a perfect solution for my setup as I don’t allow many trades to start with. However, I can probably increase trade number and at least be able to see the effect of changing depending on market conditions.
Thanks,
Terry
June 21, 2021 at 11:03 pm #113481JulianCohenParticipantYes exactly, or change the percentage per trade…either contribute to the effective leverage so it’s a bit of a fudge but it should work to give you the answer you need.
June 23, 2021 at 6:31 am #113482TerryDunneParticipantWell that hasn’t gone very well. I have the same issue with variable position size as I do with variable leverage.
It’s a bit frustrating to be honest. We can run optimizations with literally thousands of iterations of moving average variables but can’t do something as fundamental as vary the position sizing…maybe I’ll have to look at Realtrade too.
June 23, 2021 at 7:43 am #113484GlenPeakeParticipantYou can definitely implement it using RealTest Terry….
I posted a question regarding the syntax usage for it back in February. (Not sure if you’re a member or can read the posts… but just to provide an example below).
FYR:
https://realtest.discoursehosting.net/t/adjust-position-sizing-based-on-a-condition/516
E.g.
UseHalf: c < 50 /// Or whatever your condition is.... ++VOLA etc Quantity: if(usehalf, 0.5, 1) * S.Alloc / NumPos / FillPrice
June 23, 2021 at 8:26 am #113485TerryDunneParticipantThanks Glenn!
I really don’t want to have to use/learn another language as my time is limited and I’d prefer to use it in trading research. However, this is a big deal for me…I think it is an exciting and potentially rewarding area of research and it’s bugging me now too
I need to think about this a bit
June 24, 2021 at 12:11 am #113467ScottMcNabParticipantsomething along this line Terry (eg 10 positions at 10%)?
HighVola = HVola>90 OR PercentRank(HVola,200)>95;
PSMult = IIf(HighVola,0.5,1) ;
PS = 10*PSMult;SetPositionSize(Ref(PS,-1),spsPercentOfEquity);
June 24, 2021 at 12:33 am #113486TerryDunneParticipantHi Scott,
That’s exactly the type of thing I’m hoping to do. The problem is that AFL won’t accept an array in the SetPositionSize function, only a number.
-
AuthorPosts
- You must be logged in to reply to this topic.