Upload
arnold-palmer
View
216
Download
0
Embed Size (px)
Citation preview
8/2/2019 Mmamo 2 Coded Strategy
1/4
#regionUsingdeclarationsusingSystem;usingSystem.ComponentModel;usingSystem.Diagnostics;usingSystem.Drawing;usingSystem.Drawing.Drawing2D;usingSystem.Xml.Serialization;usingNinjaTrader.Cbi;usingNinjaTrader.Data;usingNinjaTrader.Indicator;usingNinjaTrader.Gui.Chart;usingNinjaTrader.Strategy;#endregion
//Thisnamespaceholdsallstrategiesandisrequired.Donotchangeit.namespaceNinjaTrader.Strategy{//////Usingraviasthetriggerwearelookingforsignalsfrommmamoandspearmanwithin5barsofaravi/thresholdcross.Optimizedfortheyearwegota
valueof0.326butthiswaswithasmallprofittarget.Thisversiontriestocodeadynamictargetandstop.///[Description("Usingraviasthetriggerwearelookingforsignalsfrommmamoandspearmanwithin5barsofaravi/thresholdcross.Optimizedfortheyearwegotavalueof0.326butthiswaswithasmallprofittarget.Thisversiontriestocodeadynamictargetandstop.")]publicclassMmamo2coded:Strategy{#regionVariablesprivatedoubleraviT_hold=20.000;
privateIOrderentryOrder =null;//Thisvariableholdsa
nobjectrepresentingourentryorderprivateIOrderstopOrder =null;//Thisvariableholdsanobjectrepresentingourstoplossorder
privateIOrdertargetOrder =null;//Thisvariableholdsanobjectrepresentingourprofittargetorder
#endregion
protectedoverridevoidInitialize(){Add(Mmamo(13,27,13,50,-50,5));Add(Spearman(3,10));
Add(RaviDynamic(27,6,3,RaviT_hold,0,Color.Tan));
CalculateOnBarClose=true;}
protectedoverridevoidOnBarUpdate(){
//Submitanentrylimitorderifwecurrentlydon'thaveanentryorderopen
8/2/2019 Mmamo 2 Coded Strategy
2/4
if(entryOrder==null
//Conditionset1&&CrossAbove(Mmamo(13,27,13,50,-50,5).ShortAVG,Mmamo(13,27,13,50,-50,5).LongAVG,5)&&Rising(Mmamo(13,27,13,50,-50,5).LongAVG)==true&&CrossAbove(Spearman(3,10).SpearmanRC,0,5)&&CrossAbove(RaviDynamic(27,6,3,RaviT_hold,0,Color.Tan).RAV_I,RaviT_hold,1)){entryOrder=EnterLong(1,"LEMmamo2");}
if(Position.MarketPosition==MarketPosition.Long&&Close[0]>=Position.AvgPrice+(Math.Truncate(15*(ATR(5)[0])*(TickSize/2))))
{//CheckstoseeifourStopOrderhasbeensubm
ittedalreadyif(stopOrder!=null&&stopOrder.StopPrice
8/2/2019 Mmamo 2 Coded Strategy
3/4
{//Handleentryordershere.TheentryOrderobjectallow
sustoidentifythattheorderthatiscallingtheOnOrderUpdate()methodistheentryorder.
if(entryOrder!=null&&entryOrder==order){
//ResettheentryOrderobjecttonulliforderwascancelledwithoutanyfill
if(order.OrderState==OrderState.Cancelled&&order.Filled==0)
{entryOrder=null;
}}
}
protectedoverridevoidOnExecution(IExecutionexecution){
/*WeadvisemonitoringOnExecutiontotriggersubmissionofstop/targetordersinsteadofOnOrderUpdate()sinceOnExecution()iscalledafterOnOrderUpdate()
whichensuresyourstrategyhasreceivedtheexecutionwhichisusedforinternalsignaltracking.*/
if(entryOrder!=null&&entryOrder==execution.Order&&Position.MarketPosition==MarketPosition.Long){
if(execution.Order.OrderState==OrderState.Filled||execution.Order.OrderState==OrderState.PartFilled||(execution.Order.OrderState==OrderState.Cancelled&&execution.Order.Filled>0))
{//Stop-Lossorder14ticksbelowouren
trypricestopOrder =ExitLongStop(0,true,
execution.Order.Filled,execution.Order.AvgFillPrice-14*TickSize,"LEStop","LEMmamo2");
//Targetorder(15*ATR)ticksaboveourentrypricetargetOrder=ExitLongLimit(0,true,exe
cution.Order.Filled,execution.Order.AvgFillPrice+Math.Truncate(15*(ATR(5)[0])*TickSize),"LETarget","LEMmamo2");
//ResetstheentryOrderobjecttonullaftertheorderhasbeenfilled
if(execution.Order.OrderState!=OrderState.PartFilled)
{entryOrder =null;
}
}}
if(entryOrder!=null&&entryOrder==execution.Order&&Position.MarketPosition==MarketPosition.Short)
{if(execution.Order.OrderState==OrderState.Fil
led||execution.Order.OrderState==OrderState.PartFilled||(execution.Order.OrderState==OrderState.Cancelled&&execution.Order.Filled>0))
{
8/2/2019 Mmamo 2 Coded Strategy
4/4
//Stop-Lossorder14ticksaboveourentryprice
stopOrder =ExitShortStop(0,true,execution.Order.Filled,execution.Order.AvgFillPrice+14*TickSize,"SEStop","SEMmamo2");
//Targetorder(15*ATR)ticksbelowourentryprice
targetOrder=ExitShortLimit(0,true,execution.Order.Filled,execution.Order.AvgFillPrice-Math.Truncate(15*(ATR(5)[0])*TickSize),"SETarget","SEMmamo2");
//ResetstheentryOrderobjecttonullaftertheorderhasbeenfilled
if(execution.Order.OrderState!=OrderState.PartFilled)
{entryOrder =null;
}}
}
//Resetourstoporderandtargetorders'IOrderobjectsafterourpositionisclosed.
if((stopOrder!=null&&stopOrder==execution.Order)||(targetOrder!=null&&targetOrder==execution.Order)){
if(execution.Order.OrderState==OrderState.Filled||execution.Order.OrderState==OrderState.PartFilled)
{stopOrder=null;targetOrder=null;
}}
}
protectedoverridevoidOnPositionUpdate(IPositionposition)
{ //Printourcurrentpositiontothelowerrighthandcornerofthechart
DrawTextFixed("CurrentPosition",position.ToString(),TextPosition.BottomRight);}
#regionProperties
[Description("ThresholdlineforRavi.Configuredasauserdefinedinputforeasyadjustment.")][GridCategory("Parameters")]
publicdoubleRaviT_hold{get{returnraviT_hold;}set{raviT_hold=Math.Max(0.001,value);}}
#endregion}}