Hello again
I've been playing around with AFL and want to create small building blocks that I can depend on.
I want to create an intraday range(bound by time) breakout system. It should only go long when the price gets above the range by a certain buffer.
I want to trade it on CFDs where I pay no commission only spread.
Given I'm new to AFL, I appreciate any feedback from you guys!
I did my best to make a code that simulates how the broker would fill my orders. I run this code on 1m
chart.
Expectations:
- Enter at the break of the range by treshold and exit at either TP or SL
- Intention is to simulate a limit buy with a bracket SL/TP as limit orders
- To actually get filled like the broker would a CFD
- To understand how the spread actually works
- To plot what I've ploted accurately(I hope it's self explanatory)
- I specically did not use
ApplyStop
so to control the process. Could this be an issue?
Issues I encountered:
- I feel my code is clunky
- The way I get
slOrTPExecutionPrice
is not ok! I know this because sometimes it comes asNull
. I've also plotted it so I see it in the Data Window. When this happens, the exit price is the candle'sClose
- I don't get filled ok when the price plus the spread gets outside the candle. I think I might have seen some option for this in the forum/docs but it does not come to mind now
- The above issues get obvious when I increase the spread
Overall I I'm kindof accomplishing what I want so for a first run I'm starting to be hopeful I can learn AFL.
Any feedback/review or help with the issues encountered is immensely appreciated!
If things can be improved or you smell something funny please also let me know.
Thanks in advance
SetOption( "InitialEquity", 100000 );
SetOption( "MaxOpenPositions", 1000 );
SetOption( "AccountMargin", 80 );
SetTradeDelays( 0, 0, 0, 0 );
spread = 0.9;
pointsBufferEntry = 2;
//Vars
rangeStart = ParamTime("rangeStart", "11:00");
rangeEnd = ParamTime("rangeEnd", "11:30");
targetMult = 2;
maxTradesPerDay = 1;
//Helping stuff
timeNow = TimeNum();
isNewDay = DateNum() > Ref(DateNum(), -1);
//What to draw
chartShowRange = False;
chartDetails = True;
//Range stuff
rangeBarStart = timeNow == rangeStart;
rangeBarEnd = timeNow == rangeEnd;
rangeHigh = ValueWhen(rangeBarEnd, HighestSince(rangeBarStart, High));
rangeLow = ValueWhen(rangeBarEnd, LowestSince(rangeBarStart, Low));
range = rangeHigh - rangeLow;
entryPriceTheoretical = rangeHigh + pointsBufferEntry; //the breakout trigger price
entryPriceBrokerTreshold = entryPriceTheoretical; //threshold price for broker to execute my order
entryPriceExecution = entryPriceBrokerTreshold + spread; //price I get filled at
targetPoints = range * targetMult;
targetTheoretical = entryPriceTheoretical + targetPoints; //the target I want
targetBrokerTreshold = targetTheoretical + spread; //threshold that price hass to cross for sell to execute at broker
targetExecution = targetTheoretical; //price I get filled at when I sell
lossTheoretical = rangeLow; //the stop loss I want
lossBrokerTreshold = lossTheoretical + spread; //threshold. price needs to dip below this to sell position
lossExecution = lossTheoretical; //price I get filled at when I sell
breakout = H >= entryPriceBrokerTreshold;
isTPHit = H >= targetBrokerTreshold;
isSLHit = L <= lossBrokerTreshold;
Buy = breakout AND timeNow > rangeEnd; //buy the breakout after range time
Buy = Buy AND Sum(Buy, BarsSince(isNewDay) +1) <= maxTradesPerDay; //Allow only maxTradesPerDay
Sell = isTPHit OR isSLHit;
//What I try to do here is get the actual exit price but can't use <if> :(
//THIS IS NOT GOOD as sometimes slOrTPExecutionPrice is NULL on exit and the exit defaults to the bar's close
tpPrice = IIf(isTPHit, targetExecution, Null);
slPrice = IIf(isSLHit, lossExecution, Null);
slOrTPExecutionPrice= IIf(tpPrice, tpPrice, slPrice);
BuyPrice = entryPriceExecution;
SellPrice = slOrTPExecutionPrice;
if(chartShowRange){
Plot(rangeHigh, "rangeHigh", colorBlue, styleDashed);
Plot(rangeLow, "rangeLow", colorBlue, styleDashed);
}
if(chartDetails){
Plot(BuyPrice, "Entry/BuyPrice/fill from Ami", colorRed, styleDots);
Plot(entryPriceBrokerTreshold, "Entry treshold", colorIndigo, styleLine);
Plot(targetBrokerTreshold, "TP treshold", colorIndigo, styleLine);
Plot(targetExecution, "TP fill price ", colorRed, styleDots);
Plot(lossBrokerTreshold, "SL treshold", colorIndigo, styleLine);
Plot(lossExecution, "SL fill price", colorRed, styleDots);
Plot(slOrTPExecutionPrice, "Exit Price", colorOrange, styleLine);
}
Plot( timeNow >= rangeStart AND timeNow <= rangeEnd, "Colored Range", ColorBlend( colorYellow, colorLightOrange, 0.5 ), styleArea | styleOwnScale, 0, 1, 0, -1);