|
- function newDateTimeStr()
- {
- newNowDateTime=Now(0);
- sYear=StrLeft(Now(1),4);
- //_TRACE("sYear: "+sYear);
- sDate=StrRight(Now(1),StrLen(Now(1))-5);
- //_TRACE("sDate: "+sDate);
- iMonth=StrToNum(sDate);
- if(iMonth<10)
- sMonth="0"+NumToStr(iMonth,1.0);
- else
- sMonth=NumToStr(iMonth,1.0);
- //_TRACE("sMonth: "+sMonth);
- if(iMonth<10)
- sDay=StrMid(sDate,2,2);
- else
- sDay=StrMid(sDate,3,2);
- iDay=StrToNum(sDay);
- if(iDay<10)
- sDay="0"+NumToStr(iDay,1.0);
- else
- sDay=NumToStr(iDay,1.0);
- //_TRACE("sDay: "+sDay);
- sTime=StrFormat("%g",Now(4));
- if(StrLen(sTime)<6)
- sTime="0"+sTime;
- newNowDateTime=sYear+"/"+sMonth+"/"+sDay+" "+sTime;
- //_TRACE("newNowDateTime: "+newNowDateTime);
- return newNowDateTime;
- }
- //***********************************************//
- function PersistentVarSet( VarName, Number )
- {
- global PersistentPath;
- String = NumToStr(Number);
- fh = fopen( PersistentPath+VarName+".pva","w" );
- if( fh )
- {
- fputs( String, fh );
- fclose( fh );
- }
- return fh;
- }
- function PersistentVarGet( VarName )
- {
- global PersistentPath;
- fh = fopen( PersistentPath+VarName+".pva","r" );
- if( fh )
- {
- String = fgets( fh );
- fclose( fh );
- Number = StrToNum(String);
- }
- else Number = Null;
- return Number;
- }
- //***********************************************//
- //The Above are Functions
- PersistentPath =ParamStr("Save Path","C:\\AB_PVA\");//must create folder first, or "fopen" will return false.
- IDstr=ParamStr("ID String","ABTW");
- SMA=Param("SMA", 3, 2, 20);
- LMA=Param("LMA", 10, 10, 200);
- if ( Status("action") == actionScan )
- {
- //_TRACE("actionScan");
- OMComAPI = CreateStaticObject("OMSignAPI.OMCOMAPI");
- NowPostion=0;
- NowPosition=OMComAPI.GetNowPosition(IDstr);
- if(NowPosition>99990)
- {
- if(PersistentVarGet( IDstr )==Null)
- {
- NowPosition=0;
- PersistentVarSet(IDstr, NowPosition);
- _TRACE("Can't Get Persistent Var then Create: "+IDstr);
- }
- else
- {
- NowPosition=PersistentVarGet( IDstr );
- //PersistentVarSet(IDstr, NowPosition);
- _TRACE("Get Persistent Var First Call: "+IDstr);
- }
- OMComAPI.IniDllAndPosition(IDstr, NowPosition);
- AlertIf(1,"EMAIL", "OrderMaster Initialized: "+NumToStr(NowPosition,1.0), 0);
- }
- //_TRACE(PersistentPath);
- //_TRACE("NowPosition: "+NumToStr(NowPosition));
- }//if ( Status("action") == actionScan )
- //stradegy here
- up=Cross(MA(C,SMA),MA(C,LMA));
- down=Cross(MA(C,LMA),MA(C,SMA));
- Buy = up;
- Sell = down OR TimeNum()>133900;//force to exit after 13:40:00
- Buy =ExRem(Buy,Sell);
- Sell=ExRem(Sell,Buy);
- if ( Status("action") == actionScan )
- {
- if(NowPosition<1) //avoid repeat buy at every updating chart
- if (LastValue(Ref(Buy,-1)) )
- {
- NowDateTime=newDateTimeStr();
- NowPosition = 1;
- NowClose=LastValue(Ref(Close,-1));//LastValue(Close);
- OMComAPI.GoOrder(IDstr,"",NowDateTime,Nowposition,Nowclose);
- _TRACE("Buy"+NumToStr(NowPosition,1.0)+"@"+NumToStr(Nowclose,1.0)+"@"+NowDateTime);
- AlertIf(1,"EMAIL", "Buy"+NumToStr(NowPosition,1.0)+"@"+NumToStr(Nowclose,1.0)+"@"+NowDateTime, 1);
- PersistentVarSet(IDstr, NowPosition);
- }
- if(NowPosition>0)
- if (LastValue(Ref(Sell,-1)) )
- {
- NowDateTime=newDateTimeStr();
- NowPosition = 0;
- NowClose=LastValue(Ref(Close,-1));//LastValue(Close);
- OMComAPI.GoOrder(IDstr,"",NowDateTime,NowPosition,NowClose);
- _TRACE("Sell"+NumToStr(NowPosition,1.0)+"@"+NumToStr(Nowclose,1.0)+"@"+NowDateTime);
- AlertIf(1,"EMAIL", "Sell"+NumToStr(NowPosition,1.0)+"@"+NumToStr(Nowclose,1.0)+"@"+NowDateTime, 2);
- PersistentVarSet(IDstr, NowPosition);
- }
- }//if ( Status("action") == actionScan )
- Plot(MA(C,SMA),"SMA"+NumToStr(SMA,1.0),colorRed,styleLine);
- Plot(MA(C,LMA),"LMA"+NumToStr(LMA,1.0),colorBlue,styleLine);
- //PlotShapes(IIf(Buy , shapeSmallUpTriangle, shapeNone) ,colorYellow, 0,L,-50);
- //PlotShapes(IIf( Sell, shapeSmallDownTriangle, shapeNone) ,colorBlue, 0,H,-45);
- PlotShapes(Buy * shapeSmallUpTriangle ,colorRed, 0,L,-50);
- PlotShapes(Sell * shapeSmallDownTriangle ,colorGreen, 0,H,-45);
- dist=1.5*ATR(10);
- for( i = 1; i < BarCount; i++ )
- {
- if( Buy[i-1] ) PlotText( "Buy\n@" + C[ i-1 ], i, L[ i ]-dist[i], colorRed );
- if( Sell[i-1] ) PlotText( "Sell\n@" + C[ i-1 ], i, H[ i ]+dist[i], colorGreen );
- }
- _SECTION_BEGIN("Price1");
- SetChartOptions(0,chartShowArrows|chartShowDates);
- _N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
- Plot( C, "Close", ParamColor("Color", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
- _SECTION_END();
複製代碼 |
|